Skip to content

Commit

Permalink
chore: use memoized stats for version (#8776)
Browse files Browse the repository at this point in the history
## About the changes
Builds on top of #8766 to use memoized results from stats-service.
Because stats service depends on version service, and to avoid making
the version service depend on stats service creating a cyclic
dependency. I've introduced a telemetry data provider. It's not clean
code, but it does the job.

After validating this works as expected I'll clean up

Added an e2e test validating that the replacement is correct:
[8475492](8475492)
and it did:
https://github.com/Unleash/unleash/actions/runs/11861854341/job/33060032638?pr=8776#step:9:294

Finally, cleaning up version service
  • Loading branch information
gastonfournier authored Nov 18, 2024
1 parent 39d227c commit f297d86
Show file tree
Hide file tree
Showing 8 changed files with 288 additions and 470 deletions.
42 changes: 14 additions & 28 deletions src/lib/features/instance-stats/createInstanceStatsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ export const createInstanceStatsService = (db: Db, config: IUnleashConfig) => {

const trafficDataUsageStore = new TrafficDataUsageStore(db, getLogger);

const featureStrategiesStore = new FeatureStrategyStore(
db,
eventBus,
getLogger,
flagResolver,
);
const instanceStatsServiceStores = {
featureToggleStore,
userStore,
Expand All @@ -118,27 +124,14 @@ export const createInstanceStatsService = (db: Db, config: IUnleashConfig) => {
apiTokenStore,
clientMetricsStoreV2,
featureStrategiesReadModel,
trafficDataUsageStore,
};
const featureStrategiesStore = new FeatureStrategyStore(
db,
eventBus,
getLogger,
flagResolver,
);
const versionServiceStores = {
...instanceStatsServiceStores,
featureStrategiesStore,
trafficDataUsageStore,
};
const versionServiceStores = { settingStore };
const getActiveUsers = createGetActiveUsers(db);
const getProductionChanges = createGetProductionChanges(db);
const getLicencedUsers = createGetLicensedUsers(db);
const versionService = new VersionService(
versionServiceStores,
config,
getActiveUsers,
getProductionChanges,
);
const versionService = new VersionService(versionServiceStores, config);

const instanceStatsService = new InstanceStatsService(
instanceStatsServiceStores,
Expand Down Expand Up @@ -170,7 +163,7 @@ export const createFakeInstanceStatsService = (config: IUnleashConfig) => {
const clientMetricsStoreV2 = new FakeClientMetricsStoreV2();
const featureStrategiesReadModel = new FakeFeatureStrategiesReadModel();
const trafficDataUsageStore = new FakeTrafficDataUsageStore();

const featureStrategiesStore = new FakeFeatureStrategiesStore();
const instanceStatsServiceStores = {
featureToggleStore,
userStore,
Expand All @@ -187,22 +180,15 @@ export const createFakeInstanceStatsService = (config: IUnleashConfig) => {
apiTokenStore,
clientMetricsStoreV2,
featureStrategiesReadModel,
trafficDataUsageStore,
};
const featureStrategiesStore = new FakeFeatureStrategiesStore();
const versionServiceStores = {
...instanceStatsServiceStores,
featureStrategiesStore,
trafficDataUsageStore,
};

const versionServiceStores = { settingStore };
const getActiveUsers = createFakeGetActiveUsers();
const getLicensedUsers = createFakeGetLicensedUsers();
const getProductionChanges = createFakeGetProductionChanges();
const versionService = new VersionService(
versionServiceStores,
config,
getActiveUsers,
getProductionChanges,
);
const versionService = new VersionService(versionServiceStores, config);

const instanceStatsService = new InstanceStatsService(
instanceStatsServiceStores,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ beforeEach(() => {
const config = createTestConfig();
flagResolver = config.flagResolver;
stores = createStores();
versionService = new VersionService(
stores,
config,
createFakeGetActiveUsers(),
createFakeGetProductionChanges(),
);
versionService = new VersionService(stores, config);
clientInstanceStore = stores.clientInstanceStore;
instanceStatsService = new InstanceStatsService(
stores,
Expand Down
158 changes: 139 additions & 19 deletions src/lib/features/instance-stats/instance-stats-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
IClientMetricsStoreV2,
IEventStore,
IFeatureStrategiesReadModel,
IFeatureStrategiesStore,
ITrafficDataUsageStore,
IUnleashStores,
} from '../../types/stores';
Expand Down Expand Up @@ -33,6 +34,7 @@ import type { GetProductionChanges } from './getProductionChanges';
import { format, minutesToMilliseconds } from 'date-fns';
import memoizee from 'memoizee';
import type { GetLicensedUsers } from './getLicensedUsers';
import type { IFeatureUsageInfo } from '../../services/version-service';

export type TimeRange = 'allTime' | '30d' | '7d';

Expand Down Expand Up @@ -124,6 +126,8 @@ export class InstanceStatsService {

private featureStrategiesReadModel: IFeatureStrategiesReadModel;

private featureStrategiesStore: IFeatureStrategiesStore;

private trafficDataUsageStore: ITrafficDataUsageStore;

constructor(
Expand All @@ -143,6 +147,7 @@ export class InstanceStatsService {
apiTokenStore,
clientMetricsStoreV2,
featureStrategiesReadModel,
featureStrategiesStore,
trafficDataUsageStore,
}: Pick<
IUnleashStores,
Expand All @@ -161,6 +166,7 @@ export class InstanceStatsService {
| 'apiTokenStore'
| 'clientMetricsStoreV2'
| 'featureStrategiesReadModel'
| 'featureStrategiesStore'
| 'trafficDataUsageStore'
>,
{
Expand All @@ -186,13 +192,20 @@ export class InstanceStatsService {
this.eventStore = eventStore;
this.clientInstanceStore = clientInstanceStore;
this.logger = getLogger('services/stats-service.js');
this.getActiveUsers = getActiveUsers;
this.getLicencedUsers = getLicencedUsers;
this.getProductionChanges = getProductionChanges;
this.getActiveUsers = () =>
this.memorize('getActiveUsers', getActiveUsers.bind(this));
this.getLicencedUsers = () =>
this.memorize('getLicencedUsers', getLicencedUsers.bind(this));
this.getProductionChanges = () =>
this.memorize(
'getProductionChanges',
getProductionChanges.bind(this),
);
this.apiTokenStore = apiTokenStore;
this.clientMetricsStore = clientMetricsStoreV2;
this.flagResolver = flagResolver;
this.featureStrategiesReadModel = featureStrategiesReadModel;
this.featureStrategiesStore = featureStrategiesStore;
this.trafficDataUsageStore = trafficDataUsageStore;
}

Expand Down Expand Up @@ -323,8 +336,8 @@ export class InstanceStatsService {
this.getRegisteredUsers(),
this.countServiceAccounts(),
this.countApiTokensByType(),
this.memorize('getActiveUsers', this.getActiveUsers.bind(this)),
this.memorize('getLicencedUsers', this.getLicencedUsers.bind(this)),
this.getActiveUsers(),
this.getLicencedUsers(),
this.getProjectModeCount(),
this.contextFieldCount(),
this.groupCount(),
Expand All @@ -339,20 +352,9 @@ export class InstanceStatsService {
this.hasPasswordAuth(),
this.hasSCIM(),
this.appCount ? this.appCount : this.getLabeledAppCounts(),
this.memorize('deprecatedFilteredCountFeaturesExported', () =>
this.eventStore.deprecatedFilteredCount({
type: FEATURES_EXPORTED,
}),
),
this.memorize('deprecatedFilteredCountFeaturesImported', () =>
this.eventStore.deprecatedFilteredCount({
type: FEATURES_IMPORTED,
}),
),
this.memorize(
'getProductionChanges',
this.getProductionChanges.bind(this),
),
this.featuresExported(),
this.featuresImported(),
this.getProductionChanges(),
this.countPreviousDayHourlyMetricsBuckets(),
this.memorize(
'maxFeatureEnvironmentStrategies',
Expand Down Expand Up @@ -413,6 +415,124 @@ export class InstanceStatsService {
};
}

async getFeatureUsageInfo(): Promise<IFeatureUsageInfo> {
const [
featureToggles,
users,
projectModeCount,
contextFields,
groups,
roles,
customRootRoles,
customRootRolesInUse,
environments,
segments,
strategies,
SAMLenabled,
OIDCenabled,
featureExports,
featureImports,
customStrategies,
customStrategiesInUse,
userActive,
productionChanges,
postgresVersion,
] = await Promise.all([
this.getToggleCount(),
this.getRegisteredUsers(),
this.getProjectModeCount(),
this.contextFieldCount(),
this.groupCount(),
this.roleCount(),
this.customRolesCount(),
this.customRolesCountInUse(),
this.environmentCount(),
this.segmentCount(),
this.strategiesCount(),
this.hasSAML(),
this.hasOIDC(),
this.featuresExported(),
this.featuresImported(),
this.customStrategiesCount(),
this.customStrategiesInUseCount(),
this.getActiveUsers(),
this.getProductionChanges(),
this.postgresVersion(),
]);
const versionInfo = await this.versionService.getVersionInfo();

const featureInfo = {
featureToggles,
users,
projects: projectModeCount
.map((p) => p.count)
.reduce((a, b) => a + b, 0),
contextFields,
groups,
roles,
customRootRoles,
customRootRolesInUse,
environments,
segments,
strategies,
SAMLenabled,
OIDCenabled,
featureExports,
featureImports,
customStrategies,
customStrategiesInUse,
instanceId: versionInfo.instanceId,
versionOSS: versionInfo.current.oss,
versionEnterprise: versionInfo.current.enterprise,
activeUsers30: userActive.last30,
activeUsers60: userActive.last60,
activeUsers90: userActive.last90,
productionChanges30: productionChanges.last30,
productionChanges60: productionChanges.last60,
productionChanges90: productionChanges.last90,
postgresVersion,
};
return featureInfo;
}

featuresExported(): Promise<number> {
return this.memorize('deprecatedFilteredCountFeaturesExported', () =>
this.eventStore.deprecatedFilteredCount({
type: FEATURES_EXPORTED,
}),
);
}

featuresImported(): Promise<number> {
return this.memorize('deprecatedFilteredCountFeaturesImported', () =>
this.eventStore.deprecatedFilteredCount({
type: FEATURES_IMPORTED,
}),
);
}

customStrategiesCount(): Promise<number> {
return this.memorize(
'customStrategiesCount',
async () =>
(await this.strategyStore.getEditableStrategies()).length,
);
}

customStrategiesInUseCount(): Promise<number> {
return this.memorize(
'customStrategiesInUseCount',
async () =>
await this.featureStrategiesStore.getCustomStrategiesInUseCount(),
);
}

postgresVersion(): Promise<string> {
return this.memorize('postgresVersion', () =>
this.settingStore.postgresVersion(),
);
}

groupCount(): Promise<number> {
return this.memorize('groupCount', () => this.groupStore.count());
}
Expand Down
5 changes: 4 additions & 1 deletion src/lib/features/scheduler/schedule-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ export const scheduleServices = async (
);

schedulerService.schedule(
versionService.checkLatestVersion.bind(versionService),
() =>
versionService.checkLatestVersion(() =>
instanceStatsService.getFeatureUsageInfo(),
),
hoursToMilliseconds(48),
'checkLatestVersion',
);
Expand Down
7 changes: 1 addition & 6 deletions src/lib/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@ beforeAll(async () => {
eventStore = stores.eventStore;
environmentStore = new FakeEnvironmentStore();
stores.environmentStore = environmentStore;
const versionService = new VersionService(
stores,
config,
createFakeGetActiveUsers(),
createFakeGetProductionChanges(),
);
const versionService = new VersionService(stores, config);
db = await dbInit('metrics_test', getLogger);

featureLifeCycleReadModel = new FeatureLifecycleReadModel(
Expand Down
23 changes: 2 additions & 21 deletions src/lib/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ import {
createFakePrivateProjectChecker,
createPrivateProjectChecker,
} from '../features/private-project/createPrivateProjectChecker';
import {
createFakeGetActiveUsers,
createGetActiveUsers,
} from '../features/instance-stats/getActiveUsers';
import { DependentFeaturesService } from '../features/dependent-features/dependent-features-service';
import {
createDependentFeaturesService,
Expand All @@ -97,10 +93,6 @@ import {
createFakeLastSeenService,
createLastSeenService,
} from '../features/metrics/last-seen/createLastSeenService';
import {
createFakeGetProductionChanges,
createGetProductionChanges,
} from '../features/instance-stats/getProductionChanges';
import {
createClientFeatureToggleService,
createFakeClientFeatureToggleService,
Expand Down Expand Up @@ -247,19 +239,8 @@ export const createServices = (
const accountService = new AccountService(stores, config, {
accessService,
});
const getActiveUsers = db
? createGetActiveUsers(db)
: createFakeGetActiveUsers();
const getProductionChanges = db
? createGetProductionChanges(db)
: createFakeGetProductionChanges();

const versionService = new VersionService(
stores,
config,
getActiveUsers,
getProductionChanges,
);

const versionService = new VersionService(stores, config);
const healthService = new HealthService(stores, config);
const userFeedbackService = new UserFeedbackService(stores, config);
const changeRequestAccessReadModel = db
Expand Down
Loading

0 comments on commit f297d86

Please sign in to comment.