2021-09-13 10:23:57 +02:00
|
|
|
import { Knex } from 'knex';
|
|
|
|
import EventEmitter from 'events';
|
|
|
|
import metricsHelper from '../util/metrics-helper';
|
|
|
|
import { DB_TIME } from '../metric-events';
|
|
|
|
import { Logger, LogProvider } from '../logger';
|
|
|
|
import {
|
|
|
|
IFeatureToggleClient,
|
|
|
|
IFeatureToggleQuery,
|
|
|
|
IStrategyConfig,
|
|
|
|
} from '../types/model';
|
|
|
|
import { IFeatureToggleClientStore } from '../types/stores/feature-toggle-client-store';
|
2021-09-24 13:55:00 +02:00
|
|
|
import { DEFAULT_ENV } from '../util/constants';
|
2021-09-13 10:23:57 +02:00
|
|
|
|
|
|
|
export interface FeaturesTable {
|
|
|
|
name: string;
|
|
|
|
description: string;
|
|
|
|
type: string;
|
|
|
|
stale: boolean;
|
|
|
|
variants: string;
|
|
|
|
project: string;
|
|
|
|
last_seen_at?: Date;
|
|
|
|
created_at?: Date;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class FeatureToggleClientStore
|
|
|
|
implements IFeatureToggleClientStore
|
|
|
|
{
|
|
|
|
private db: Knex;
|
|
|
|
|
|
|
|
private logger: Logger;
|
|
|
|
|
|
|
|
private timer: Function;
|
|
|
|
|
|
|
|
constructor(db: Knex, eventBus: EventEmitter, getLogger: LogProvider) {
|
|
|
|
this.db = db;
|
|
|
|
this.logger = getLogger('feature-toggle-client-store.ts');
|
|
|
|
this.timer = (action) =>
|
|
|
|
metricsHelper.wrapTimer(eventBus, DB_TIME, {
|
|
|
|
store: 'feature-toggle',
|
|
|
|
action,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private getAdminStrategy(
|
|
|
|
r: any,
|
|
|
|
includeId: boolean = true,
|
|
|
|
): IStrategyConfig {
|
2021-10-01 12:27:05 +02:00
|
|
|
if (includeId) {
|
|
|
|
return {
|
|
|
|
name: r.strategy_name,
|
|
|
|
constraints: r.constraints || [],
|
|
|
|
parameters: r.parameters,
|
|
|
|
id: r.strategy_id,
|
|
|
|
};
|
2021-09-13 10:23:57 +02:00
|
|
|
}
|
2021-10-07 10:22:20 +02:00
|
|
|
return {
|
|
|
|
name: r.strategy_name,
|
|
|
|
constraints: r.constraints || [],
|
|
|
|
parameters: r.parameters,
|
|
|
|
};
|
2021-09-13 10:23:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async getAll(
|
|
|
|
featureQuery?: IFeatureToggleQuery,
|
|
|
|
archived: boolean = false,
|
|
|
|
isAdmin: boolean = true,
|
|
|
|
): Promise<IFeatureToggleClient[]> {
|
2021-09-24 13:55:00 +02:00
|
|
|
const environment = featureQuery?.environment || DEFAULT_ENV;
|
2021-09-13 10:23:57 +02:00
|
|
|
const stopTimer = this.timer('getFeatureAdmin');
|
|
|
|
let query = this.db('features')
|
|
|
|
.select(
|
|
|
|
'features.name as name',
|
|
|
|
'features.description as description',
|
|
|
|
'features.type as type',
|
|
|
|
'features.project as project',
|
|
|
|
'features.stale as stale',
|
2022-02-03 11:06:51 +01:00
|
|
|
'features.impression_data as impression_data',
|
2021-09-13 10:23:57 +02:00
|
|
|
'features.variants as variants',
|
|
|
|
'features.created_at as created_at',
|
|
|
|
'features.last_seen_at as last_seen_at',
|
2021-10-01 12:27:05 +02:00
|
|
|
'fe.enabled as enabled',
|
|
|
|
'fe.environment as environment',
|
|
|
|
'fs.id as strategy_id',
|
|
|
|
'fs.strategy_name as strategy_name',
|
|
|
|
'fs.parameters as parameters',
|
|
|
|
'fs.constraints as constraints',
|
2021-09-13 10:23:57 +02:00
|
|
|
)
|
|
|
|
.fullOuterJoin(
|
2021-10-01 12:27:05 +02:00
|
|
|
this.db('feature_strategies')
|
|
|
|
.select('*')
|
|
|
|
.where({ environment })
|
|
|
|
.as('fs'),
|
|
|
|
'fs.feature_name',
|
|
|
|
'features.name',
|
|
|
|
)
|
|
|
|
.fullOuterJoin(
|
|
|
|
this.db('feature_environments')
|
|
|
|
.select('feature_name', 'enabled', 'environment')
|
|
|
|
.where({ environment })
|
|
|
|
.as('fe'),
|
|
|
|
'fe.feature_name',
|
2021-09-13 10:23:57 +02:00
|
|
|
'features.name',
|
|
|
|
)
|
|
|
|
.where({ archived });
|
2021-09-24 13:55:00 +02:00
|
|
|
|
2021-09-13 10:23:57 +02:00
|
|
|
if (featureQuery) {
|
|
|
|
if (featureQuery.tag) {
|
|
|
|
const tagQuery = this.db
|
|
|
|
.from('feature_tag')
|
|
|
|
.select('feature_name')
|
|
|
|
.whereIn(['tag_type', 'tag_value'], featureQuery.tag);
|
|
|
|
query = query.whereIn('name', tagQuery);
|
|
|
|
}
|
|
|
|
if (featureQuery.project) {
|
|
|
|
query = query.whereIn('project', featureQuery.project);
|
|
|
|
}
|
|
|
|
if (featureQuery.namePrefix) {
|
|
|
|
query = query.where(
|
|
|
|
'name',
|
|
|
|
'like',
|
|
|
|
`${featureQuery.namePrefix}%`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-10-01 12:27:05 +02:00
|
|
|
|
2021-09-13 10:23:57 +02:00
|
|
|
const rows = await query;
|
|
|
|
stopTimer();
|
|
|
|
const featureToggles = rows.reduce((acc, r) => {
|
|
|
|
let feature;
|
|
|
|
if (acc[r.name]) {
|
|
|
|
feature = acc[r.name];
|
|
|
|
} else {
|
|
|
|
feature = {};
|
|
|
|
}
|
|
|
|
if (!feature.strategies) {
|
|
|
|
feature.strategies = [];
|
|
|
|
}
|
|
|
|
if (r.strategy_name) {
|
|
|
|
feature.strategies.push(this.getAdminStrategy(r, isAdmin));
|
|
|
|
}
|
2022-02-03 11:06:51 +01:00
|
|
|
feature.impressionData = r.impression_data;
|
2021-10-01 12:27:05 +02:00
|
|
|
feature.enabled = !!r.enabled;
|
2021-09-13 10:23:57 +02:00
|
|
|
feature.name = r.name;
|
|
|
|
feature.description = r.description;
|
|
|
|
feature.project = r.project;
|
|
|
|
feature.stale = r.stale;
|
|
|
|
feature.type = r.type;
|
|
|
|
feature.variants = r.variants;
|
|
|
|
feature.project = r.project;
|
|
|
|
if (isAdmin) {
|
|
|
|
feature.lastSeenAt = r.last_seen_at;
|
|
|
|
feature.createdAt = r.created_at;
|
|
|
|
}
|
|
|
|
acc[r.name] = feature;
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
return Object.values(featureToggles);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getClient(
|
|
|
|
featureQuery?: IFeatureToggleQuery,
|
|
|
|
): Promise<IFeatureToggleClient[]> {
|
|
|
|
return this.getAll(featureQuery, false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAdmin(
|
|
|
|
featureQuery?: IFeatureToggleQuery,
|
|
|
|
archived: boolean = false,
|
|
|
|
): Promise<IFeatureToggleClient[]> {
|
|
|
|
return this.getAll(featureQuery, archived, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = FeatureToggleClientStore;
|