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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | 69x 69x 69x 69x 69x 69x 87x 87x 87x 6x 169x 1x 1x 1x 3x 3x 3x 1x 3x 3x 3x 3x 3x 3x 3x 2x 3x 4x 4x 18x 18x 30x 199x 199x 199x 17x 152x 152x 2x 2x 5x 5x 5x 5x 15x 5x 3x 3x 3x 3x 3x 26x 6x 98x 10x 10x 10x 10x 10x 5x 249x 7x 242x 69x | import { Knex } from 'knex'; import { Logger, LogProvider } from '../logger'; import NotFoundError from '../error/notfound-error'; import { IProject, IProjectWithCount } from '../types/model'; import { IProjectHealthUpdate, IProjectInsert, IProjectQuery, IProjectStore, } from '../types/stores/project-store'; import { DEFAULT_ENV } from '../util/constants'; import metricsHelper from '../util/metrics-helper'; import { DB_TIME } from '../metric-events'; import EventEmitter from 'events'; const COLUMNS = [ 'id', 'name', 'description', 'created_at', 'health', 'updated_at', ]; const TABLE = 'projects'; export interface IEnvironmentProjectLink { environmentName: string; projectId: string; } class ProjectStore implements IProjectStore { private db: Knex; private logger: Logger; private timer: Function; constructor(db: Knex, eventBus: EventEmitter, getLogger: LogProvider) { this.db = db; this.logger = getLogger('project-store.ts'); this.timer = (action) => metricsHelper.wrapTimer(eventBus, DB_TIME, { store: 'project', action, }); } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types fieldToRow(data): IProjectInsert { return { id: data.id, name: data.name, description: data.description, }; } destroy(): void {} async exists(id: string): Promise<boolean> { const result = await this.db.raw( `SELECT EXISTS(SELECT 1 FROM ${TABLE} WHERE id = ?) AS present`, [id], ); const { present } = result.rows[0]; return present; } async getProjectsWithCounts( query?: IProjectQuery, ): Promise<IProjectWithCount[]> { const projectTimer = this.timer('getProjectsWithCount'); let projects = this.db(TABLE) .select( this.db.raw( 'projects.id, projects.name, projects.description, projects.health, projects.updated_at, count(features.name) AS number_of_features', ), ) .leftJoin('features', 'features.project', 'projects.id') .groupBy('projects.id') .orderBy('projects.name', 'asc'); if (query) { projects = projects.where(query); } const projectAndFeatureCount = await projects; // @ts-ignore const projectsWithFeatureCount = projectAndFeatureCount.map( this.mapProjectWithCountRow, ); projectTimer(); const memberTimer = this.timer('getMemberCount'); const memberCount = await this.db.raw( `SELECT count(role_id) as member_count, project FROM role_user GROUP BY project`, ); memberTimer(); const memberMap = new Map<string, number>( memberCount.rows.map((c) => [c.project, Number(c.member_count)]), ); return projectsWithFeatureCount.map((r) => { return { ...r, memberCount: memberMap.get(r.id) }; }); } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types mapProjectWithCountRow(row): IProjectWithCount { return { name: row.name, id: row.id, description: row.description, health: row.health, featureCount: row.number_of_features, memberCount: row.number_of_users || 0, updatedAt: row.updated_at, }; } async getAll(query: IProjectQuery = {}): Promise<IProject[]> { const rows = await this.db .select(COLUMNS) .from(TABLE) .where(query) .orderBy('name', 'asc'); return rows.map(this.mapRow); } async get(id: string): Promise<IProject> { return this.db .first(COLUMNS) .from(TABLE) .where({ id }) .then(this.mapRow); } async hasProject(id: string): Promise<boolean> { const result = await this.db.raw( `SELECT EXISTS(SELECT 1 FROM ${TABLE} WHERE id = ?) AS present`, [id], ); const { present } = result.rows[0]; return present; } async updateHealth(healthUpdate: IProjectHealthUpdate): Promise<void> { await this.db(TABLE) .where({ id: healthUpdate.id }) .update({ health: healthUpdate.health, updated_at: new Date() }); } async create(project: IProjectInsert): Promise<IProject> { const row = await this.db(TABLE) .insert(this.fieldToRow(project)) .returning('*'); return this.mapRow(row[0]); } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types async update(data): Promise<void> { try { await this.db(TABLE) .where({ id: data.id }) .update(this.fieldToRow(data)); } catch (err) { this.logger.error('Could not update project, error: ', err); } } async importProjects(projects: IProjectInsert[]): Promise<IProject[]> { const rows = await this.db(TABLE) .insert(projects.map(this.fieldToRow)) .returning(COLUMNS) .onConflict('id') .ignore(); if (rows.length > 0) { await this.addDefaultEnvironment(rows); return rows.map(this.mapRow); } return []; } async addDefaultEnvironment(projects: any[]): Promise<void> { const environments = projects.map((p) => ({ project_id: p.id, environment_name: DEFAULT_ENV, })); await this.db('project_environments') .insert(environments) .onConflict(['project_id', 'environment_name']) .ignore(); } async deleteAll(): Promise<void> { await this.db(TABLE).del(); } async delete(id: string): Promise<void> { try { await this.db(TABLE).where({ id }).del(); } catch (err) { this.logger.error('Could not delete project, error: ', err); } } async getProjectLinksForEnvironments( environments: string[], ): Promise<IEnvironmentProjectLink[]> { let rows = await this.db('project_environments') .select(['project_id', 'environment_name']) .whereIn('environment_name', environments); return rows.map(this.mapLinkRow); } async deleteEnvironmentForProject( id: string, environment: string, ): Promise<void> { await this.db('project_environments') .where({ project_id: id, environment_name: environment, }) .del(); } async addEnvironmentToProject( id: string, environment: string, ): Promise<void> { await this.db('project_environments') .insert({ project_id: id, environment_name: environment }) .onConflict(['project_id', 'environment_name']) .ignore(); } async getEnvironmentsForProject(id: string): Promise<string[]> { return this.db('project_environments') .where({ project_id: id, }) .pluck('environment_name'); } async getMembers(projectId: string): Promise<number> { const rolesFromProject = this.db('role_permission') .select('role_id') .distinct() .where({ project: projectId }); const numbers = await this.db('role_user') .countDistinct('user_id as members') .whereIn('role_id', rolesFromProject) .first(); const { members } = numbers; if (typeof members === 'string') { return parseInt(members, 10); } return members; } async count(): Promise<number> { return this.db .from(TABLE) .count('*') .then((res) => Number(res[0].count)); } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types mapLinkRow(row): IEnvironmentProjectLink { return { environmentName: row.environment_name, projectId: row.project_id, }; } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types mapRow(row): IProject { if (!row) { throw new NotFoundError('No project found'); } return { id: row.id, name: row.name, description: row.description, createdAt: row.created_at, health: row.health || 100, updatedAt: row.updated_at || new Date(), }; } } export default ProjectStore; |