Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | 69x 69x 69x 69x 87x 87x 87x 87x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 16x 6x 6x 16x 3x 16x 8x 30x 30x 30x 79x 79x 57x 79x 6x 79x 79x 79x 79x 79x 79x 79x 79x 79x 79x 38x 38x 79x 79x 30x 30x 18x 30x 57x 18x 38x 20x 79x 3x 6x 6x 18x 12x 69x | import { Knex } from 'knex'; 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'; import { DEFAULT_ENV } from '../util/constants'; import { PartialDeep } from '../types/partial'; import { IExperimentalOptions } from '../experimental'; import EventEmitter from 'events'; 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 experimental: IExperimentalOptions; private timer: Function; constructor( db: Knex, eventBus: EventEmitter, getLogger: LogProvider, experimental: IExperimentalOptions, ) { this.db = db; this.logger = getLogger('feature-toggle-client-store.ts'); this.experimental = experimental; this.timer = (action) => metricsHelper.wrapTimer(eventBus, DB_TIME, { store: 'feature-toggle', action, }); } private async getAll( featureQuery?: IFeatureToggleQuery, archived: boolean = false, isAdmin: boolean = true, ): Promise<IFeatureToggleClient[]> { const environment = featureQuery?.environment || DEFAULT_ENV; const stopTimer = this.timer('getFeatureAdmin'); const { inlineSegmentConstraints = false } = this.experimental?.segments ?? {}; let selectColumns = [ 'features.name as name', 'features.description as description', 'features.type as type', 'features.project as project', 'features.stale as stale', 'features.impression_data as impression_data', 'features.variants as variants', 'features.created_at as created_at', 'features.last_seen_at as last_seen_at', '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', ]; if (inlineSegmentConstraints) { selectColumns = [ ...selectColumns, 'segments.id as segment_id', 'segments.constraints as segment_constraints', ]; } let query = this.db('features') .select(selectColumns) .fullOuterJoin( 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', 'features.name', ); if (inlineSegmentConstraints) { query = query .fullOuterJoin( 'feature_strategy_segment as fss', `fss.feature_strategy_id`, `fs.id`, ) .fullOuterJoin('segments', `segments.id`, `fss.segment_id`); } query = query.where({ archived, }); 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('features.name', tagQuery); } if (featureQuery.project) { query = query.whereIn('project', featureQuery.project); } if (featureQuery.namePrefix) { query = query.where( 'features.name', 'like', `${featureQuery.namePrefix}%`, ); } } const rows = await query; stopTimer(); const featureToggles = rows.reduce((acc, r) => { let feature: PartialDeep<IFeatureToggleClient> = acc[r.name] ?? { strategies: [], }; if (this.isUnseenStrategyRow(feature, r)) { feature.strategies.push( FeatureToggleClientStore.rowToStrategy(r), ); } if (inlineSegmentConstraints && r.segment_id) { this.addSegmentToStrategy(feature, r); } feature.impressionData = r.impression_data; feature.enabled = !!r.enabled; 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; }, {}); const features: IFeatureToggleClient[] = Object.values(featureToggles); if (!isAdmin) { // We should not send strategy IDs from the client API, // as this breaks old versions of the Go SDK (at least). FeatureToggleClientStore.removeIdsFromStrategies(features); } return features; } private static rowToStrategy(row: Record<string, any>): IStrategyConfig { return { id: row.strategy_id, name: row.strategy_name, constraints: row.constraints || [], parameters: row.parameters, }; } private static removeIdsFromStrategies(features: IFeatureToggleClient[]) { features.forEach((feature) => { feature.strategies.forEach((strategy) => { delete strategy.id; }); }); } private isUnseenStrategyRow( feature: PartialDeep<IFeatureToggleClient>, row: Record<string, any>, ): boolean { return ( row.strategy_id && !feature.strategies.find((s) => s.id === row.strategy_id) ); } private addSegmentToStrategy( feature: PartialDeep<IFeatureToggleClient>, row: Record<string, any>, ) { feature.strategies .find((s) => s.id === row.strategy_id) ?.constraints.push(...row.segment_constraints); } 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; |