mirror of
https://github.com/Unleash/unleash.git
synced 2025-07-12 13:48:35 +02:00
feat: report top used domains (#9934)
This commit is contained in:
parent
499ee1e099
commit
a3ac624deb
@ -4,6 +4,10 @@ import type {
|
|||||||
} from './feature-links-read-model-type';
|
} from './feature-links-read-model-type';
|
||||||
|
|
||||||
export class FakeFeatureLinksReadModel implements IFeatureLinksReadModel {
|
export class FakeFeatureLinksReadModel implements IFeatureLinksReadModel {
|
||||||
|
async getTopDomains(): Promise<{ domain: string; count: number }[]> {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
async getLinks(feature: string): Promise<IFeatureLink[]> {
|
async getLinks(feature: string): Promise<IFeatureLink[]> {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@ export class FeatureLinkStore
|
|||||||
feature_name: item.featureName,
|
feature_name: item.featureName,
|
||||||
url: item.url,
|
url: item.url,
|
||||||
title: item.title,
|
title: item.title,
|
||||||
|
domain: item.domain,
|
||||||
};
|
};
|
||||||
await this.db('feature_link').insert(featureLink);
|
await this.db('feature_link').insert(featureLink);
|
||||||
return { ...item, id };
|
return { ...item, id };
|
||||||
|
@ -6,11 +6,14 @@ import dbInit, { type ITestDb } from '../../../test/e2e/helpers/database-init';
|
|||||||
import type { IEventStore, IFeatureLinkStore } from '../../types';
|
import type { IEventStore, IFeatureLinkStore } from '../../types';
|
||||||
import getLogger from '../../../test/fixtures/no-logger';
|
import getLogger from '../../../test/fixtures/no-logger';
|
||||||
import type { FeatureLinkSchema } from '../../openapi/spec/feature-link-schema';
|
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 app: IUnleashTest;
|
||||||
let db: ITestDb;
|
let db: ITestDb;
|
||||||
let featureLinkStore: IFeatureLinkStore;
|
let featureLinkStore: IFeatureLinkStore;
|
||||||
let eventStore: IEventStore;
|
let eventStore: IEventStore;
|
||||||
|
let featureLinkReadModel: IFeatureLinksReadModel;
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
db = await dbInit('feature_link', getLogger, {
|
db = await dbInit('feature_link', getLogger, {
|
||||||
@ -29,6 +32,10 @@ beforeAll(async () => {
|
|||||||
);
|
);
|
||||||
eventStore = db.stores.eventStore;
|
eventStore = db.stores.eventStore;
|
||||||
featureLinkStore = db.stores.featureLinkStore;
|
featureLinkStore = db.stores.featureLinkStore;
|
||||||
|
featureLinkReadModel = new FeatureLinksReadModel(
|
||||||
|
db.rawDatabase,
|
||||||
|
app.config.eventBus,
|
||||||
|
);
|
||||||
|
|
||||||
await app.request
|
await app.request
|
||||||
.post(`/auth/demo/login`)
|
.post(`/auth/demo/login`)
|
||||||
@ -99,13 +106,20 @@ test('should manage feature links', async () => {
|
|||||||
url: 'https://example.com',
|
url: 'https://example.com',
|
||||||
title: 'feature link',
|
title: 'feature link',
|
||||||
featureName: 'my_feature',
|
featureName: 'my_feature',
|
||||||
|
domain: 'example',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
url: 'https://example_another.com',
|
url: 'https://example_another.com',
|
||||||
title: 'another feature link',
|
title: 'another feature link',
|
||||||
featureName: 'my_feature',
|
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');
|
const { body } = await app.getProjectFeatures('default', 'my_feature');
|
||||||
expect(body.links).toMatchObject([
|
expect(body.links).toMatchObject([
|
||||||
{ id: links[0].id, title: 'feature link', url: 'https://example.com' },
|
{ 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',
|
url: 'https://example_updated.com',
|
||||||
title: 'feature link updated',
|
title: 'feature link updated',
|
||||||
featureName: 'my_feature',
|
featureName: 'my_feature',
|
||||||
|
domain: 'example_updated',
|
||||||
});
|
});
|
||||||
|
|
||||||
await deleteLink('my_feature', links[0].id);
|
await deleteLink('my_feature', links[0].id);
|
||||||
@ -136,6 +151,7 @@ test('should manage feature links', async () => {
|
|||||||
id: links[1].id,
|
id: links[1].id,
|
||||||
title: 'another feature link',
|
title: 'another feature link',
|
||||||
url: 'https://example_another.com',
|
url: 'https://example_another.com',
|
||||||
|
domain: 'example_another',
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -6,4 +6,5 @@ export interface IFeatureLink {
|
|||||||
|
|
||||||
export interface IFeatureLinksReadModel {
|
export interface IFeatureLinksReadModel {
|
||||||
getLinks(feature: string): Promise<IFeatureLink[]>;
|
getLinks(feature: string): Promise<IFeatureLink[]>;
|
||||||
|
getTopDomains(): Promise<{ domain: string; count: number }[]>;
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,39 @@ import type {
|
|||||||
IFeatureLink,
|
IFeatureLink,
|
||||||
IFeatureLinksReadModel,
|
IFeatureLinksReadModel,
|
||||||
} from './feature-links-read-model-type';
|
} 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 {
|
export class FeatureLinksReadModel implements IFeatureLinksReadModel {
|
||||||
private db: Db;
|
private db: Db;
|
||||||
|
private timer: Function;
|
||||||
|
|
||||||
constructor(db: Db) {
|
constructor(db: Db, eventBus: EventEmitter) {
|
||||||
this.db = db;
|
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[]> {
|
async getLinks(feature: string): Promise<IFeatureLink[]> {
|
||||||
|
@ -130,7 +130,7 @@ export const createFeatureToggleService = (
|
|||||||
|
|
||||||
const featureCollaboratorsReadModel = new FeatureCollaboratorsReadModel(db);
|
const featureCollaboratorsReadModel = new FeatureCollaboratorsReadModel(db);
|
||||||
|
|
||||||
const featureLinksReadModel = new FeatureLinksReadModel(db);
|
const featureLinksReadModel = new FeatureLinksReadModel(db, eventBus);
|
||||||
|
|
||||||
const featureToggleService = new FeatureToggleService(
|
const featureToggleService = new FeatureToggleService(
|
||||||
{
|
{
|
||||||
|
@ -292,7 +292,7 @@ export const createServices = (
|
|||||||
: new FakeFeatureCollaboratorsReadModel();
|
: new FakeFeatureCollaboratorsReadModel();
|
||||||
|
|
||||||
const featureLinkReadModel = db
|
const featureLinkReadModel = db
|
||||||
? new FeatureLinksReadModel(db)
|
? new FeatureLinksReadModel(db, config.eventBus)
|
||||||
: new FakeFeatureLinksReadModel();
|
: new FakeFeatureLinksReadModel();
|
||||||
|
|
||||||
const featureToggleService = new FeatureToggleService(
|
const featureToggleService = new FeatureToggleService(
|
||||||
|
Loading…
Reference in New Issue
Block a user