-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Detector creation] UI workflow metrics (#865)
* implemented metrics for detector creation Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com> * updated tests Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com> * added config based flag; interval of 2 min to emit browser metrics Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com> * removed unused metrics counters Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com> * added null check Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com> * updated code to check for window unload Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com> --------- Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>
- Loading branch information
Showing
36 changed files
with
632 additions
and
883 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import _ from 'lodash'; | ||
import { DEFAULT_METRICS_COUNTER } from '../server/utils/constants'; | ||
import { MetricsCounter, PartialMetricsCounter } from '../types'; | ||
import { SecurityAnalyticsPluginConfigType } from '../config'; | ||
|
||
export function aggregateMetrics( | ||
metrics: PartialMetricsCounter, | ||
currentMetricsCounter: PartialMetricsCounter | ||
): MetricsCounter { | ||
const partialMetrics: PartialMetricsCounter = { | ||
...currentMetricsCounter, | ||
}; | ||
Object.keys(metrics).forEach((w) => { | ||
const workflow = w as keyof MetricsCounter; | ||
const workFlowMetrics = metrics[workflow]; | ||
|
||
if (workFlowMetrics) { | ||
const counterToUpdate: any = | ||
partialMetrics[workflow] || _.cloneDeep(DEFAULT_METRICS_COUNTER[workflow]); | ||
Object.entries(workFlowMetrics).forEach(([metric, count]) => { | ||
if (!counterToUpdate[metric]) { | ||
counterToUpdate[metric] = 0; | ||
} | ||
counterToUpdate[metric] += count; | ||
}); | ||
|
||
partialMetrics[workflow] = counterToUpdate; | ||
} | ||
}); | ||
|
||
return partialMetrics as MetricsCounter; | ||
} | ||
|
||
let securityAnalyticsPluginConfig: SecurityAnalyticsPluginConfigType; | ||
export const setSecurityAnalyticsPluginConfig = (config: SecurityAnalyticsPluginConfigType) => { | ||
securityAnalyticsPluginConfig = config; | ||
}; | ||
|
||
export const getSecurityAnalyticsPluginConfig = (): SecurityAnalyticsPluginConfigType | undefined => | ||
securityAnalyticsPluginConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { schema, TypeOf } from '@osd/config-schema'; | ||
|
||
export const configSchema = schema.object({ | ||
enabled: schema.boolean({ defaultValue: true }), | ||
// Interval in minutes at which the browser should emit the metrics to the Kibana server | ||
// Setting this to "0" will disable the metrics | ||
uxTelemetryInterval: schema.number({ defaultValue: 2 }), | ||
}); | ||
|
||
export type SecurityAnalyticsPluginConfigType = TypeOf<typeof configSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { CreateDetectorSteps, CreateDetectorStepValue } from '../../types'; | ||
import MetricsService from '../services/MetricsService'; | ||
|
||
export class DetectorMetricsManager { | ||
private static initialCheckpoint = CreateDetectorStepValue[CreateDetectorSteps.notStarted]; | ||
private stepsLogged: number = DetectorMetricsManager.initialCheckpoint; | ||
private creationStartedStepValue = CreateDetectorStepValue[CreateDetectorSteps.started]; | ||
|
||
constructor(private readonly metricsService: MetricsService) {} | ||
|
||
public sendMetrics(step: CreateDetectorSteps, stepNameForCounter?: string) { | ||
const stepValue = CreateDetectorStepValue[step]; | ||
|
||
// If we are not in detection creation flow, we should not emit any metric | ||
if ( | ||
stepValue !== this.creationStartedStepValue && | ||
!this.metricEmittedForStep(this.creationStartedStepValue) | ||
) { | ||
return; | ||
} | ||
|
||
// Checks if we have already emitted metrics for this step | ||
if (!this.metricEmittedForStep(stepValue)) { | ||
this.metricsService.updateMetrics({ | ||
CreateDetector: { | ||
[stepNameForCounter || step]: 1, | ||
}, | ||
}); | ||
this.stepsLogged |= stepValue; | ||
} | ||
} | ||
|
||
public resetMetrics() { | ||
this.stepsLogged = DetectorMetricsManager.initialCheckpoint; | ||
} | ||
|
||
private metricEmittedForStep(stepValue: number): boolean { | ||
return (this.stepsLogged & stepValue) === stepValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import MetricsService from '../services/MetricsService'; | ||
import { DetectorMetricsManager } from './DetectorMetricsManager'; | ||
|
||
export class MetricsContext { | ||
public detectorMetricsManager: DetectorMetricsManager; | ||
|
||
constructor(metricsService: MetricsService) { | ||
this.detectorMetricsManager = new DetectorMetricsManager(metricsService); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.