Skip to content

Commit

Permalink
version usage counters route types
Browse files Browse the repository at this point in the history
  • Loading branch information
TinaHeiligers committed Mar 7, 2023
1 parent 069d8f7 commit 01e2d9e
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 33 deletions.
12 changes: 12 additions & 0 deletions src/plugins/usage_collection/common/types/usage_counters/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@ export interface CounterMetric {
counterType: string;
incrementBy: number;
}

/**
* Details about the counter to be incremented
*/
export interface IncrementCounterParams {
/** The name of the counter **/
counterName: string;
/** The counter type ("count" by default) **/
counterType?: string;
/** Increment the counter by this number (1 if not specified) **/
incrementBy?: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { Reporter, Storage } from '@kbn/analytics';
import { HttpSetup } from '@kbn/core/public';
import { UiCounters } from '../../common/types';

interface AnalyicsReporterConfig {
localStorage: Storage;
Expand All @@ -27,8 +28,8 @@ export function createReporter(config: AnalyicsReporterConfig): Reporter {
body: JSON.stringify({ report }),
asSystemRequest: true,
});

if (response.status !== 'ok') {
const okStatus: UiCounters.v1.UiCountersResponseOk = response.status;
if (response.status !== okStatus) {
throw Error('Unable to store report.');
}
return response;
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/usage_collection/server/usage_counters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { UsageCounters } from '../../common/types';
export type IncrementCounterParams = UsageCounters.v1.IncrementCounterParams;

export type { UsageCountersServiceSetup } from './usage_counters_service';
export type { UsageCountersSavedObjectAttributes, UsageCountersSavedObject } from './saved_objects';
export type { IUsageCounter as UsageCounter, IncrementCounterParams } from './usage_counter';
export type { IUsageCounter as UsageCounter } from './usage_counter';

export { UsageCountersService } from './usage_counters_service';
export type { SerializeCounterParams } from './saved_objects';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import { serializeCounterKey, storeCounter } from './saved_objects';
import { savedObjectsRepositoryMock } from '@kbn/core/server/mocks';
import { CounterMetric } from './usage_counter';

import { UsageCounters } from '../../common/types';

import moment from 'moment';

describe('counterKey', () => {
Expand Down Expand Up @@ -38,7 +40,7 @@ describe('storeCounter', () => {
});

it('stores counter in a saved object', async () => {
const counterMetric: CounterMetric = {
const counterMetric: UsageCounters.v1.CounterMetric = {
domainId: 'a',
counterName: 'b',
counterType: 'c',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
SavedObjectsServiceSetup,
} from '@kbn/core/server';
import moment from 'moment';
import type { CounterMetric } from './usage_counter';
import { UsageCounters } from '../../common/types';

/**
* The attributes stored in the UsageCounters' SavedObjects
Expand Down Expand Up @@ -83,7 +83,7 @@ export const serializeCounterKey = ({
};

export const storeCounter = async (
counterMetric: CounterMetric,
counterMetric: UsageCounters.v1.CounterMetric,
internalRepository: Pick<SavedObjectsRepository, 'incrementCounter'>
) => {
const { counterName, counterType, domainId, incrementBy } = counterMetric;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,12 @@

import * as Rx from 'rxjs';
import { UsageCounters } from '../../common/types';
// export interface CounterMetric {
// domainId: string;
// counterName: string;
// counterType: string;
// incrementBy: number;
// }

export interface UsageCounterDeps {
domainId: string;
counter$: Rx.Subject<UsageCounters.v1.CounterMetric>;
}

/**
* Details about the counter to be incremented
*/
export interface IncrementCounterParams {
/** The name of the counter **/
counterName: string;
/** The counter type ("count" by default) **/
counterType?: string;
/** Increment the counter by this number (1 if not specified) **/
incrementBy?: number;
}

/**
* Usage Counter allows to keep track of any events that occur.
* By calling {@link IUsageCounter.incrementCounter} devs can notify this
Expand All @@ -42,7 +24,7 @@ export interface IUsageCounter {
* Notifies the counter about a new event happening so it can increase the count internally.
* @param params {@link IncrementCounterParams}
*/
incrementCounter: (params: IncrementCounterParams) => void;
incrementCounter: (params: UsageCounters.v1.IncrementCounterParams) => void;
}

export class UsageCounter implements IUsageCounter {
Expand All @@ -54,7 +36,7 @@ export class UsageCounter implements IUsageCounter {
this.counter$ = counter$;
}

public incrementCounter = (params: IncrementCounterParams) => {
public incrementCounter = (params: UsageCounters.v1.IncrementCounterParams) => {
const { counterName, counterType = 'count', incrementBy = 1 } = params;

this.counter$.next({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
import type { Logger, LogMeta } from '@kbn/core/server';

import moment from 'moment';
import { CounterMetric, UsageCounter } from './usage_counter';
import { UsageCounter } from './usage_counter';
import { UsageCounters } from '../../common/types';
import {
registerUsageCountersSavedObjectType,
storeCounter,
Expand Down Expand Up @@ -54,7 +55,7 @@ export class UsageCountersService {
private readonly bufferDurationMs: number;

private readonly counterSets = new Map<string, UsageCounter>();
private readonly source$ = new Rx.Subject<CounterMetric>();
private readonly source$ = new Rx.Subject<UsageCounters.v1.CounterMetric>();
private readonly counter$ = this.source$.pipe(rxOp.multicast(new Rx.Subject()), rxOp.refCount());
private readonly flushCache$ = new Rx.Subject<void>();

Expand All @@ -69,7 +70,7 @@ export class UsageCountersService {
}

public setup = (core: UsageCountersServiceSetupDeps): UsageCountersServiceSetup => {
const cache$ = new Rx.ReplaySubject<CounterMetric>();
const cache$ = new Rx.ReplaySubject<UsageCounters.v1.CounterMetric>();
const storingCache$ = new Rx.BehaviorSubject<boolean>(false);
// flush cache data from cache -> source
this.flushCache$
Expand Down Expand Up @@ -135,7 +136,7 @@ export class UsageCountersService {
};

private storeDate$(
counters: CounterMetric[],
counters: UsageCounters.v1.CounterMetric[],
internalRepository: Pick<SavedObjectsRepository, 'incrementCounter'>
) {
return Rx.forkJoin(
Expand Down Expand Up @@ -170,7 +171,9 @@ export class UsageCountersService {
return this.counterSets.get(type);
};

private mergeCounters = (counters: CounterMetric[]): Record<string, CounterMetric> => {
private mergeCounters = (
counters: UsageCounters.v1.CounterMetric[]
): Record<string, UsageCounters.v1.CounterMetric> => {
const date = moment.now();
return counters.reduce((acc, counter) => {
const { counterName, domainId, counterType } = counter;
Expand All @@ -188,6 +191,6 @@ export class UsageCountersService {
incrementBy: existingCounter.incrementBy + counter.incrementBy,
},
};
}, {} as Record<string, CounterMetric>);
}, {} as Record<string, UsageCounters.v1.CounterMetric>);
};
}

0 comments on commit 01e2d9e

Please sign in to comment.