Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | 60x 60x 60x 60x 148x 148x 148x 148x 148x 148x 148x 12x 12x 12x 9x 12x 12x 12x 12x 12x 12x 2x 2x 2x 6x 2x 2x 4x 6x 2x 7x 6x 6x | import { Logger } from '../../logger'; import { IUnleashConfig } from '../../server-impl'; import { IUnleashStores } from '../../types'; import { IClientApp } from '../../types/model'; import { ToggleMetricsSummary } from '../../types/models/metrics'; import { IClientMetricsEnv, IClientMetricsStoreV2, } from '../../types/stores/client-metrics-store-v2'; import { clientMetricsSchema } from './schema'; import { hoursToMilliseconds, minutesToMilliseconds } from 'date-fns'; import { IFeatureToggleStore } from '../../types/stores/feature-toggle-store'; import EventEmitter from 'events'; import { CLIENT_METRICS } from '../../types/events'; export default class ClientMetricsServiceV2 { private timer: NodeJS.Timeout; private clientMetricsStoreV2: IClientMetricsStoreV2; private featureToggleStore: IFeatureToggleStore; private eventBus: EventEmitter; private logger: Logger; private bulkInterval: number; constructor( { featureToggleStore, clientMetricsStoreV2, }: Pick<IUnleashStores, 'featureToggleStore' | 'clientMetricsStoreV2'>, { eventBus, getLogger }: Pick<IUnleashConfig, 'eventBus' | 'getLogger'>, bulkInterval = minutesToMilliseconds(5), ) { this.featureToggleStore = featureToggleStore; this.clientMetricsStoreV2 = clientMetricsStoreV2; this.eventBus = eventBus; this.logger = getLogger( '/services/client-metrics/client-metrics-service-v2.ts', ); this.bulkInterval = bulkInterval; this.timer = setInterval(async () => { await this.clientMetricsStoreV2.clearMetrics(48); }, hoursToMilliseconds(12)); this.timer.unref(); } async registerClientMetrics( data: IClientApp, clientIp: string, ): Promise<void> { const value = await clientMetricsSchema.validateAsync(data); const toggleNames = Object.keys(value.bucket.toggles); if (toggleNames.length > 0) { await this.featureToggleStore.setLastSeen(toggleNames); } this.logger.debug(`got metrics from ${clientIp}`); const clientMetrics: IClientMetricsEnv[] = toggleNames .map((name) => ({ featureName: name, appName: value.appName, environment: value.environment, timestamp: value.bucket.start, //we might need to approximate between start/stop... yes: value.bucket.toggles[name].yes, no: value.bucket.toggles[name].no, })) .filter((item) => !(item.yes === 0 && item.no === 0)); // TODO: should we aggregate for a few minutes (bulkInterval) before pushing to DB? await this.clientMetricsStoreV2.batchInsertMetrics(clientMetrics); this.eventBus.emit(CLIENT_METRICS, value); } // Overview over usage last "hour" bucket and all applications using the toggle async getFeatureToggleMetricsSummary( featureName: string, ): Promise<ToggleMetricsSummary> { const metrics = await this.clientMetricsStoreV2.getMetricsForFeatureToggle( featureName, 1, ); const seenApplications = await this.clientMetricsStoreV2.getSeenAppsForFeatureToggle( featureName, ); const groupedMetrics = metrics.reduce((prev, curr) => { if (prev[curr.environment]) { prev[curr.environment].yes += curr.yes; prev[curr.environment].no += curr.no; } else { prev[curr.environment] = { environment: curr.environment, timestamp: curr.timestamp, yes: curr.yes, no: curr.no, }; } return prev; }, {}); return { featureName, lastHourUsage: Object.values(groupedMetrics), seenApplications, }; } async getClientMetricsForToggle( toggleName: string, hoursBack?: number, ): Promise<IClientMetricsEnv[]> { return this.clientMetricsStoreV2.getMetricsForFeatureToggle( toggleName, hoursBack, ); } destroy(): void { clearInterval(this.timer); this.timer = null; } } |