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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | 69x 69x 69x 69x 69x 87x 87x 87x 11x 11x 4x 42x 4x 4x 4x 3x 77x 12x 12x 12x 84x 84x 84x 6x 6x 5x 1x 3x 3x 52x 184x 47x 769x 47x 36x 12x 155x 155x 191x | import EventEmitter from 'events'; import { Knex } from 'knex'; import { FeatureEnvironmentKey, IFeatureEnvironmentStore, } from '../types/stores/feature-environment-store'; import { Logger, LogProvider } from '../logger'; import metricsHelper from '../util/metrics-helper'; import { DB_TIME } from '../metric-events'; import { IFeatureEnvironment } from '../types/model'; import NotFoundError from '../error/notfound-error'; const T = { featureEnvs: 'feature_environments', featureStrategies: 'feature_strategies', }; interface IFeatureEnvironmentRow { environment: string; feature_name: string; enabled: boolean; } export class FeatureEnvironmentStore implements IFeatureEnvironmentStore { private db: Knex; private logger: Logger; private readonly timer: Function; constructor(db: Knex, eventBus: EventEmitter, getLogger: LogProvider) { this.db = db; this.logger = getLogger('feature-environment-store.ts'); this.timer = (action) => metricsHelper.wrapTimer(eventBus, DB_TIME, { store: 'feature-environments', action, }); } async delete({ featureName, environment, }: FeatureEnvironmentKey): Promise<void> { await this.db(T.featureEnvs) .where('feature_name', featureName) .andWhere('environment', environment) .del(); } async deleteAll(): Promise<void> { await this.db(T.featureEnvs).del(); } destroy(): void {} async exists({ featureName, environment, }: FeatureEnvironmentKey): Promise<boolean> { const result = await this.db.raw( `SELECT EXISTS (SELECT 1 FROM ${T.featureEnvs} WHERE feature_name = ? AND environment = ?) AS present`, [featureName, environment], ); const { present } = result.rows[0]; return present; } async get({ featureName, environment, }: FeatureEnvironmentKey): Promise<IFeatureEnvironment> { const md = await this.db(T.featureEnvs) .where('feature_name', featureName) .andWhere('environment', environment) .first(); Iif (md) { return { enabled: md.enabled, featureName, environment, }; } throw new NotFoundError( `Could not find ${featureName} in ${environment}`, ); } async getAll(query?: Object): Promise<IFeatureEnvironment[]> { let rows = this.db(T.featureEnvs); if (query) { rows = rows.where(query); } return (await rows).map((r) => ({ enabled: r.enabled, featureName: r.feature_name, environment: r.environment, })); } async disableEnvironmentIfNoStrategies( featureName: string, environment: string, ): Promise<void> { const result = await this.db.raw( `SELECT EXISTS (SELECT 1 FROM ${T.featureStrategies} WHERE feature_name = ? AND environment = ?) AS enabled`, [featureName, environment], ); const { enabled } = result.rows[0]; if (!enabled) { await this.db(T.featureEnvs) .update({ enabled: false }) .where({ feature_name: featureName, environment }); } } async addEnvironmentToFeature( featureName: string, environment: string, enabled: boolean = false, ): Promise<void> { await this.db('feature_environments') .insert({ feature_name: featureName, environment, enabled }) .onConflict(['environment', 'feature_name']) .merge('enabled'); } // TODO: move to project store. async disconnectFeatures( environment: string, project: string, ): Promise<void> { const featureSelector = this.db('features') .where({ project }) .select('name'); await this.db(T.featureEnvs) .where({ environment }) .andWhere('feature_name', 'IN', featureSelector) .del(); await this.db('feature_strategies').where({ environment, project_name: project, }); } async featureHasEnvironment( environment: string, featureName: string, ): Promise<boolean> { const result = await this.db.raw( `SELECT EXISTS (SELECT 1 FROM ${T.featureEnvs} WHERE feature_name = ? AND environment = ?) AS present`, [featureName, environment], ); const { present } = result.rows[0]; return present; } async getEnvironmentMetaData( environment: string, featureName: string, ): Promise<IFeatureEnvironment> { const md = await this.db(T.featureEnvs) .where('feature_name', featureName) .andWhere('environment', environment) .first(); if (md) { return { enabled: md.enabled, featureName, environment, }; } throw new NotFoundError( `Could not find ${featureName} in ${environment}`, ); } async isEnvironmentEnabled( featureName: string, environment: string, ): Promise<boolean> { const row = await this.db(T.featureEnvs) .select('enabled') .where({ feature_name: featureName, environment }) .first(); return row.enabled; } async removeEnvironmentForFeature( featureName: string, environment: string, ): Promise<void> { await this.db(T.featureEnvs) .where({ feature_name: featureName, environment }) .del(); } async setEnvironmentEnabledStatus( environment: string, featureName: string, enabled: boolean, ): Promise<number> { return this.db(T.featureEnvs).update({ enabled }).where({ environment, feature_name: featureName, enabled: !enabled, }); } async connectProject( environment: string, projectId: string, ): Promise<void> { await this.db('project_environments').insert({ environment_name: environment, project_id: projectId, }); } async connectFeatures( environment: string, projectId: string, ): Promise<void> { const featuresToEnable = await this.db('features') .select('name') .where({ project: projectId, }); const rows: IFeatureEnvironmentRow[] = featuresToEnable.map((f) => ({ environment, feature_name: f.name, enabled: false, })); if (rows.length > 0) { await this.db<IFeatureEnvironmentRow>('feature_environments') .insert(rows) .onConflict(['environment', 'feature_name']) .ignore(); } } async disconnectProject( environment: string, projectId: string, ): Promise<void> { await this.db('project_environments') .where({ environment_name: environment, project_id: projectId }) .del(); } async connectFeatureToEnvironmentsForProject( featureName: string, projectId: string, ): Promise<void> { const environmentsToEnable = await this.db('project_environments') .select('environment_name') .where({ project_id: projectId }); await Promise.all( environmentsToEnable.map(async (env) => { await this.db('feature_environments') .insert({ environment: env.environment_name, feature_name: featureName, enabled: false, }) .onConflict(['environment', 'feature_name']) .ignore(); }), ); } } |