1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-07 01:16:28 +02:00

feat: report top used domains (#9934)

This commit is contained in:
Mateusz Kwasniewski 2025-05-08 14:06:10 +02:00 committed by GitHub
parent 499ee1e099
commit a3ac624deb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 52 additions and 3 deletions

View File

@ -4,6 +4,10 @@ import type {
} from './feature-links-read-model-type';
export class FakeFeatureLinksReadModel implements IFeatureLinksReadModel {
async getTopDomains(): Promise<{ domain: string; count: number }[]> {
return [];
}
async getLinks(feature: string): Promise<IFeatureLink[]> {
return [];
}

View File

@ -26,6 +26,7 @@ export class FeatureLinkStore
feature_name: item.featureName,
url: item.url,
title: item.title,
domain: item.domain,
};
await this.db('feature_link').insert(featureLink);
return { ...item, id };

View File

@ -6,11 +6,14 @@ import dbInit, { type ITestDb } from '../../../test/e2e/helpers/database-init';
import type { IEventStore, IFeatureLinkStore } from '../../types';
import getLogger from '../../../test/fixtures/no-logger';
import type { FeatureLinkSchema } from '../../openapi/spec/feature-link-schema';
import type { IFeatureLinksReadModel } from './feature-links-read-model-type';
import { FeatureLinksReadModel } from './feature-links-read-model';
let app: IUnleashTest;
let db: ITestDb;
let featureLinkStore: IFeatureLinkStore;
let eventStore: IEventStore;
let featureLinkReadModel: IFeatureLinksReadModel;
beforeAll(async () => {
db = await dbInit('feature_link', getLogger, {
@ -29,6 +32,10 @@ beforeAll(async () => {
);
eventStore = db.stores.eventStore;
featureLinkStore = db.stores.featureLinkStore;
featureLinkReadModel = new FeatureLinksReadModel(
db.rawDatabase,
app.config.eventBus,
);
await app.request
.post(`/auth/demo/login`)
@ -99,13 +106,20 @@ test('should manage feature links', async () => {
url: 'https://example.com',
title: 'feature link',
featureName: 'my_feature',
domain: 'example',
},
{
url: 'https://example_another.com',
title: 'another feature link',
featureName: 'my_feature',
domain: 'example_another',
},
]);
const topDomains = await featureLinkReadModel.getTopDomains();
expect(topDomains).toMatchObject([
{ domain: 'example_another', count: 1 },
{ domain: 'example', count: 1 },
]);
const { body } = await app.getProjectFeatures('default', 'my_feature');
expect(body.links).toMatchObject([
{ id: links[0].id, title: 'feature link', url: 'https://example.com' },
@ -126,6 +140,7 @@ test('should manage feature links', async () => {
url: 'https://example_updated.com',
title: 'feature link updated',
featureName: 'my_feature',
domain: 'example_updated',
});
await deleteLink('my_feature', links[0].id);
@ -136,6 +151,7 @@ test('should manage feature links', async () => {
id: links[1].id,
title: 'another feature link',
url: 'https://example_another.com',
domain: 'example_another',
},
]);

View File

@ -6,4 +6,5 @@ export interface IFeatureLink {
export interface IFeatureLinksReadModel {
getLinks(feature: string): Promise<IFeatureLink[]>;
getTopDomains(): Promise<{ domain: string; count: number }[]>;
}

View File

@ -3,12 +3,39 @@ import type {
IFeatureLink,
IFeatureLinksReadModel,
} from './feature-links-read-model-type';
import metricsHelper from '../../util/metrics-helper';
import { DB_TIME } from '../../metric-events';
import type EventEmitter from 'events';
export class FeatureLinksReadModel implements IFeatureLinksReadModel {
private db: Db;
private timer: Function;
constructor(db: Db) {
constructor(db: Db, eventBus: EventEmitter) {
this.db = db;
this.timer = (action) =>
metricsHelper.wrapTimer(eventBus, DB_TIME, {
store: 'feature_links',
action,
});
}
async getTopDomains(): Promise<{ domain: string; count: number }[]> {
const stopTimer = this.timer('getTopDomains');
const topDomains = await this.db
.from('feature_link')
.select('domain')
.count('* as count')
.whereNotNull('domain')
.groupBy('domain')
.orderBy('count', 'desc')
.limit(20);
stopTimer();
return topDomains.map(({ domain, count }) => ({
domain,
count: Number.parseInt(count, 10),
}));
}
async getLinks(feature: string): Promise<IFeatureLink[]> {

View File

@ -130,7 +130,7 @@ export const createFeatureToggleService = (
const featureCollaboratorsReadModel = new FeatureCollaboratorsReadModel(db);
const featureLinksReadModel = new FeatureLinksReadModel(db);
const featureLinksReadModel = new FeatureLinksReadModel(db, eventBus);
const featureToggleService = new FeatureToggleService(
{

View File

@ -292,7 +292,7 @@ export const createServices = (
: new FakeFeatureCollaboratorsReadModel();
const featureLinkReadModel = db
? new FeatureLinksReadModel(db)
? new FeatureLinksReadModel(db, config.eventBus)
: new FakeFeatureLinksReadModel();
const featureToggleService = new FeatureToggleService(