Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instance stats service composition root #6029

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './dependent-features/createDependentFeaturesService';
export * from './tag-type/createTagTypeService';
export * from './project-environments/createEnvironmentService';
export * from './events/createEventsService';
export * from './instance-stats/createInstanceStatsService';
188 changes: 188 additions & 0 deletions src/lib/features/instance-stats/createInstanceStatsService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import { InstanceStatsService } from './instance-stats-service';
import {
createFakeGetActiveUsers,
createGetActiveUsers,
} from './getActiveUsers';
import {
createFakeGetProductionChanges,
createGetProductionChanges,
} from './getProductionChanges';
import { IUnleashConfig } from '../../types';
import { Db } from '../../db/db';
import FeatureToggleStore from '../feature-toggle/feature-toggle-store';
import UserStore from '../../db/user-store';
import ProjectStore from '../../db/project-store';
import EnvironmentStore from '../project-environments/environment-store';
import StrategyStore from '../../db/strategy-store';
import ContextFieldStore from '../../db/context-field-store';
import GroupStore from '../../db/group-store';
import SegmentStore from '../../db/segment-store';
import RoleStore from '../../db/role-store';
import SettingStore from '../../db/setting-store';
import ClientInstanceStore from '../../db/client-instance-store';
import EventStore from '../events/event-store';
import { ApiTokenStore } from '../../db/api-token-store';
import { ClientMetricsStoreV2 } from '../metrics/client-metrics/client-metrics-store-v2';
import VersionService from '../../services/version-service';
import FeatureStrategyStore from '../feature-toggle/feature-toggle-strategies-store';
import FakeUserStore from '../../../test/fixtures/fake-user-store';
import FakeFeatureToggleStore from '../feature-toggle/fakes/fake-feature-toggle-store';
import FakeProjectStore from '../../../test/fixtures/fake-project-store';
import FakeEnvironmentStore from '../project-environments/fake-environment-store';
import FakeGroupStore from '../../../test/fixtures/fake-group-store';
import FakeContextFieldStore from '../../../test/fixtures/fake-context-field-store';
import FakeRoleStore from '../../../test/fixtures/fake-role-store';
import FakeClientInstanceStore from '../../../test/fixtures/fake-client-instance-store';
import FakeClientMetricsStoreV2 from '../metrics/client-metrics/fake-client-metrics-store-v2';
import FakeApiTokenStore from '../../../test/fixtures/fake-api-token-store';
import FakeEventStore from '../../../test/fixtures/fake-event-store';
import FakeSettingStore from '../../../test/fixtures/fake-setting-store';
import FakeSegmentStore from '../../../test/fixtures/fake-segment-store';
import FakeStrategiesStore from '../../../test/fixtures/fake-strategies-store';
import FakeFeatureStrategiesStore from '../feature-toggle/fakes/fake-feature-strategies-store';

export const createInstanceStatsService = (db: Db, config: IUnleashConfig) => {
const { eventBus, getLogger, flagResolver } = config;
const featureToggleStore = new FeatureToggleStore(
db,
eventBus,
getLogger,
flagResolver,
);
const userStore = new UserStore(db, getLogger);
const projectStore = new ProjectStore(
db,
eventBus,
getLogger,
flagResolver,
);
const environmentStore = new EnvironmentStore(db, eventBus, getLogger);
const strategyStore = new StrategyStore(db, getLogger);
const contextFieldStore = new ContextFieldStore(
db,
getLogger,
flagResolver,
);
const groupStore = new GroupStore(db);
const segmentStore = new SegmentStore(
db,
eventBus,
getLogger,
flagResolver,
);
const roleStore = new RoleStore(db, eventBus, getLogger);
const settingStore = new SettingStore(db, getLogger);
const clientInstanceStore = new ClientInstanceStore(
db,
eventBus,
getLogger,
);
const eventStore = new EventStore(db, getLogger);
const apiTokenStore = new ApiTokenStore(db, eventBus, getLogger);
const clientMetricsStoreV2 = new ClientMetricsStoreV2(
db,
getLogger,
flagResolver,
);
const instanceStatsServiceStores = {
featureToggleStore,
userStore,
projectStore,
environmentStore,
strategyStore,
contextFieldStore,
groupStore,
segmentStore,
roleStore,
settingStore,
clientInstanceStore,
eventStore,
apiTokenStore,
clientMetricsStoreV2,
};
const featureStrategiesStore = new FeatureStrategyStore(
db,
eventBus,
getLogger,
flagResolver,
);
const versionServiceStores = {
...instanceStatsServiceStores,
featureStrategiesStore,
};
const getActiveUsers = createGetActiveUsers(db);
const getProductionChanges = createGetProductionChanges(db);
const versionService = new VersionService(
versionServiceStores,
config,
getActiveUsers,
getProductionChanges,
);

const instanceStatsService = new InstanceStatsService(
instanceStatsServiceStores,
config,
versionService,
getActiveUsers,
getProductionChanges,
);

return instanceStatsService;
};

export const createFakeInstanceStatsService = (config: IUnleashConfig) => {
const { eventBus, getLogger, flagResolver } = config;
const featureToggleStore = new FakeFeatureToggleStore();
const userStore = new FakeUserStore();
const projectStore = new FakeProjectStore();
const environmentStore = new FakeEnvironmentStore();
const strategyStore = new FakeStrategiesStore();
const contextFieldStore = new FakeContextFieldStore();
const groupStore = new FakeGroupStore();
const segmentStore = new FakeSegmentStore();
const roleStore = new FakeRoleStore();
const settingStore = new FakeSettingStore();
const clientInstanceStore = new FakeClientInstanceStore();
const eventStore = new FakeEventStore();
const apiTokenStore = new FakeApiTokenStore();
const clientMetricsStoreV2 = new FakeClientMetricsStoreV2();
const instanceStatsServiceStores = {
featureToggleStore,
userStore,
projectStore,
environmentStore,
strategyStore,
contextFieldStore,
groupStore,
segmentStore,
roleStore,
settingStore,
clientInstanceStore,
eventStore,
apiTokenStore,
clientMetricsStoreV2,
};
const featureStrategiesStore = new FakeFeatureStrategiesStore();
const versionServiceStores = {
...instanceStatsServiceStores,
featureStrategiesStore,
};
const getActiveUsers = createFakeGetActiveUsers();
const getProductionChanges = createFakeGetProductionChanges();
const versionService = new VersionService(
versionServiceStores,
config,
getActiveUsers,
getProductionChanges,
);

const instanceStatsService = new InstanceStatsService(
instanceStatsServiceStores,
config,
versionService,
getActiveUsers,
getProductionChanges,
);

return instanceStatsService;
};
14 changes: 7 additions & 7 deletions src/lib/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ import {
createFakeTagTypeService,
createTagTypeService,
} from '../features/tag-type/createTagTypeService';
import {
createFakeInstanceStatsService,
createInstanceStatsService,
} from '../features/instance-stats/createInstanceStatsService';

export const createServices = (
stores: IUnleashStores,
Expand Down Expand Up @@ -299,13 +303,9 @@ export const createServices = (
eventService,
);

const instanceStatsService = new InstanceStatsService(
stores,
config,
versionService,
db ? createGetActiveUsers(db) : createFakeGetActiveUsers(),
db ? createGetProductionChanges(db) : createFakeGetProductionChanges(),
);
const instanceStatsService = db
? createInstanceStatsService(db, config)
: createFakeInstanceStatsService(config);
Comment on lines +306 to +308
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!


const maintenanceService = new MaintenanceService(config, settingService);

Expand Down
23 changes: 18 additions & 5 deletions src/test/e2e/api/admin/instance-admin.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import dbInit, { ITestDb } from '../../helpers/database-init';
import { IUnleashTest, setupApp } from '../../helpers/test-helper';
import {
IUnleashTest,
setupAppWithCustomConfig,
} from '../../helpers/test-helper';
import getLogger from '../../../fixtures/no-logger';
import { IUnleashStores } from '../../../../lib/types';

Expand All @@ -11,7 +14,17 @@ beforeAll(async () => {
db = await dbInit('instance_admin_api_serial', getLogger);
stores = db.stores;
await stores.settingStore.insert('instanceInfo', { id: 'test-static' });
app = await setupApp(stores);
app = await setupAppWithCustomConfig(
stores,
{
experimental: {
flags: {
strictSchemaValidation: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing schema validation

},
},
},
db.rawDatabase,
);
});

afterAll(async () => {
Expand All @@ -20,7 +33,7 @@ afterAll(async () => {
});

test('should return instance statistics', async () => {
stores.featureToggleStore.create('default', {
await stores.featureToggleStore.create('default', {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing await

name: 'TestStats1',
createdByUserId: 9999,
});
Expand Down Expand Up @@ -65,11 +78,11 @@ test('should return signed instance statistics', async () => {
});

test('should return instance statistics as CVS', async () => {
stores.featureToggleStore.create('default', {
await stores.featureToggleStore.create('default', {
name: 'TestStats2',
createdByUserId: 9999,
});
stores.featureToggleStore.create('default', {
await stores.featureToggleStore.create('default', {
name: 'TestStats3',
createdByUserId: 9999,
});
Expand Down
2 changes: 2 additions & 0 deletions src/test/e2e/helpers/database-init.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { log } from 'db-migrate-shared';
import { migrateDb } from '../../../migrator';
import { createStores } from '../../../lib/db';
import { createDb } from '../../../lib/db/db-pool';
Expand Down Expand Up @@ -97,6 +98,7 @@ export default async function init(
getLogger,
});

log.setLogLevel('error');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stop logging db migration messages in all tests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! This was something I was planning to do yesterday 🥇

const db = createDb(config);

await db.raw(`DROP SCHEMA IF EXISTS ${config.db.schema} CASCADE`);
Expand Down
Loading