UNPKG

unleash-server

Version:

Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.

49 lines 1.6 kB
import metricsHelper from '../../util/metrics-helper.js'; import { DB_TIME } from '../../metric-events.js'; import memoizee from 'memoizee'; import { hoursToMilliseconds } from 'date-fns'; export class FeatureLinksReadModel { constructor(db, eventBus) { this.db = db; this.timer = (action) => metricsHelper.wrapTimer(eventBus, DB_TIME, { store: 'feature_links', action, }); this._getTopDomainsMemoized = memoizee(this._getTopDomains.bind(this), { promise: true, maxAge: hoursToMilliseconds(1), }); } getTopDomains() { return this._getTopDomainsMemoized(); } async _getTopDomains() { 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(3); stopTimer(); return topDomains.map(({ domain, count }) => ({ domain, count: Number.parseInt(count, 10), })); } async getLinks(...features) { const links = await this.db .from('feature_link') .whereIn('feature_name', features) .orderBy('id', 'asc'); return links.map((link) => ({ id: link.id, url: link.url, title: link.title, feature: link.feature_name, })); } } //# sourceMappingURL=feature-links-read-model.js.map