From 991a4a863422ddc8b4e9a4dece393e56ebebb7d3 Mon Sep 17 00:00:00 2001 From: Youssef Date: Tue, 30 Nov 2021 10:40:58 +0100 Subject: [PATCH 1/7] add last-update migration to projects table --- .../20211130093333-add-last-update-to-projects.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/migrations/20211130093333-add-last-update-to-projects.js diff --git a/src/migrations/20211130093333-add-last-update-to-projects.js b/src/migrations/20211130093333-add-last-update-to-projects.js new file mode 100644 index 0000000000..dd696a6d20 --- /dev/null +++ b/src/migrations/20211130093333-add-last-update-to-projects.js @@ -0,0 +1,12 @@ +'use strict'; + +exports.up = function (db, callback) { + db.runSql( + 'ALTER TABLE projects ADD COLUMN "last_update" TIMESTAMP WITH TIME ZONE', + callback, + ); +}; + +exports.down = function (db, callback) { + db.runSql('ALTER TABLE projects DROP COLUMN "last_update";', callback); +}; From 10c1b56e38c95081fa4ac869847d18786cdfebe9 Mon Sep 17 00:00:00 2001 From: Youssef Date: Tue, 30 Nov 2021 14:05:44 +0100 Subject: [PATCH 2/7] add lastupdate to health report --- src/lib/db/project-store.ts | 13 +++++++++++-- src/lib/services/project-health-service.ts | 1 + src/lib/types/model.ts | 2 ++ src/lib/types/stores/project-store.ts | 2 ++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/lib/db/project-store.ts b/src/lib/db/project-store.ts index 8d6efc3b43..2dbe02564b 100644 --- a/src/lib/db/project-store.ts +++ b/src/lib/db/project-store.ts @@ -11,7 +11,14 @@ import { } from '../types/stores/project-store'; import { DEFAULT_ENV } from '../util/constants'; -const COLUMNS = ['id', 'name', 'description', 'created_at', 'health']; +const COLUMNS = [ + 'id', + 'name', + 'description', + 'created_at', + 'health', + 'last_update', +]; const TABLE = 'projects'; class ProjectStore implements IProjectStore { @@ -30,6 +37,7 @@ class ProjectStore implements IProjectStore { id: data.id, name: data.name, description: data.description, + lastUpdate: data.lastUpdate, }; } @@ -74,7 +82,7 @@ class ProjectStore implements IProjectStore { async updateHealth(healthUpdate: IProjectHealthUpdate): Promise { await this.db(TABLE) .where({ id: healthUpdate.id }) - .update({ health: healthUpdate.health }); + .update({ health: healthUpdate.health, last_update: new Date() }); } async create(project: IProjectInsert): Promise { @@ -197,6 +205,7 @@ class ProjectStore implements IProjectStore { description: row.description, createdAt: row.created_at, health: row.health || 100, + lastUpdate: row.last_update || new Date(), }; } } diff --git a/src/lib/services/project-health-service.ts b/src/lib/services/project-health-service.ts index 24651affb5..a2b1cf379c 100644 --- a/src/lib/services/project-health-service.ts +++ b/src/lib/services/project-health-service.ts @@ -72,6 +72,7 @@ export default class ProjectHealthService { name: project.name, description: project.description, health: project.health, + lastUpdate: project.lastUpdate, environments, features, members, diff --git a/src/lib/types/model.ts b/src/lib/types/model.ts index 495beb746d..50c8fe5d3e 100644 --- a/src/lib/types/model.ts +++ b/src/lib/types/model.ts @@ -143,6 +143,7 @@ export interface IProjectOverview { members: number; version: number; health: number; + lastUpdate?: Date; } export interface IProjectHealthReport extends IProjectOverview { @@ -309,6 +310,7 @@ export interface IProject { description: string; health?: number; createdAt?: Date; + lastUpdate?: Date; } export interface IProjectWithCount extends IProject { diff --git a/src/lib/types/stores/project-store.ts b/src/lib/types/stores/project-store.ts index adcff6168e..6ca53d4a97 100644 --- a/src/lib/types/stores/project-store.ts +++ b/src/lib/types/stores/project-store.ts @@ -1,3 +1,4 @@ +import { DateFileAppender } from 'log4js'; import { IProject } from '../model'; import { Store } from './store'; @@ -5,6 +6,7 @@ export interface IProjectInsert { id: string; name: string; description: string; + lastUpdate?: Date; } export interface IProjectArchived { From 71c6b05bc4d0bb6b991f90edf7127af8304b86fe Mon Sep 17 00:00:00 2001 From: Youssef Date: Tue, 30 Nov 2021 14:53:31 +0100 Subject: [PATCH 3/7] fix: remove unused dep --- src/lib/types/stores/project-store.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/types/stores/project-store.ts b/src/lib/types/stores/project-store.ts index 6ca53d4a97..cff7a92c8b 100644 --- a/src/lib/types/stores/project-store.ts +++ b/src/lib/types/stores/project-store.ts @@ -1,4 +1,3 @@ -import { DateFileAppender } from 'log4js'; import { IProject } from '../model'; import { Store } from './store'; From c3273ce1ab378ad9f7a58b6384ef335f78b23d87 Mon Sep 17 00:00:00 2001 From: Youssef Date: Tue, 30 Nov 2021 15:14:50 +0100 Subject: [PATCH 4/7] fix: remove lastUpdate from fieldToRow --- src/lib/db/project-store.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/db/project-store.ts b/src/lib/db/project-store.ts index 2dbe02564b..a34485baea 100644 --- a/src/lib/db/project-store.ts +++ b/src/lib/db/project-store.ts @@ -37,7 +37,6 @@ class ProjectStore implements IProjectStore { id: data.id, name: data.name, description: data.description, - lastUpdate: data.lastUpdate, }; } From 00f5740d1800d0ff5985b165f66afc856a03956f Mon Sep 17 00:00:00 2001 From: Youssef Date: Tue, 30 Nov 2021 15:25:52 +0100 Subject: [PATCH 5/7] fix: rename last_update to updated_at --- src/lib/db/project-store.ts | 6 +++--- src/lib/services/project-health-service.ts | 2 +- src/lib/types/model.ts | 4 ++-- src/lib/types/stores/project-store.ts | 2 +- .../20211130093333-add-last-update-to-projects.js | 12 ------------ .../20211130142314-add-updated-at-to-projects.js | 12 ++++++++++++ 6 files changed, 19 insertions(+), 19 deletions(-) delete mode 100644 src/migrations/20211130093333-add-last-update-to-projects.js create mode 100644 src/migrations/20211130142314-add-updated-at-to-projects.js diff --git a/src/lib/db/project-store.ts b/src/lib/db/project-store.ts index a34485baea..4d19552c44 100644 --- a/src/lib/db/project-store.ts +++ b/src/lib/db/project-store.ts @@ -17,7 +17,7 @@ const COLUMNS = [ 'description', 'created_at', 'health', - 'last_update', + 'updated_at', ]; const TABLE = 'projects'; @@ -81,7 +81,7 @@ class ProjectStore implements IProjectStore { async updateHealth(healthUpdate: IProjectHealthUpdate): Promise { await this.db(TABLE) .where({ id: healthUpdate.id }) - .update({ health: healthUpdate.health, last_update: new Date() }); + .update({ health: healthUpdate.health, updated_at: new Date() }); } async create(project: IProjectInsert): Promise { @@ -204,7 +204,7 @@ class ProjectStore implements IProjectStore { description: row.description, createdAt: row.created_at, health: row.health || 100, - lastUpdate: row.last_update || new Date(), + updatedAt: row.updated_at || new Date(), }; } } diff --git a/src/lib/services/project-health-service.ts b/src/lib/services/project-health-service.ts index a2b1cf379c..0eabecb2d9 100644 --- a/src/lib/services/project-health-service.ts +++ b/src/lib/services/project-health-service.ts @@ -72,7 +72,7 @@ export default class ProjectHealthService { name: project.name, description: project.description, health: project.health, - lastUpdate: project.lastUpdate, + updatedAt: project.updatedAt, environments, features, members, diff --git a/src/lib/types/model.ts b/src/lib/types/model.ts index 50c8fe5d3e..f83f82866a 100644 --- a/src/lib/types/model.ts +++ b/src/lib/types/model.ts @@ -143,7 +143,7 @@ export interface IProjectOverview { members: number; version: number; health: number; - lastUpdate?: Date; + updatedAt?: Date; } export interface IProjectHealthReport extends IProjectOverview { @@ -310,7 +310,7 @@ export interface IProject { description: string; health?: number; createdAt?: Date; - lastUpdate?: Date; + updatedAt?: Date; } export interface IProjectWithCount extends IProject { diff --git a/src/lib/types/stores/project-store.ts b/src/lib/types/stores/project-store.ts index cff7a92c8b..9d0448e066 100644 --- a/src/lib/types/stores/project-store.ts +++ b/src/lib/types/stores/project-store.ts @@ -5,7 +5,7 @@ export interface IProjectInsert { id: string; name: string; description: string; - lastUpdate?: Date; + updatedAt?: Date; } export interface IProjectArchived { diff --git a/src/migrations/20211130093333-add-last-update-to-projects.js b/src/migrations/20211130093333-add-last-update-to-projects.js deleted file mode 100644 index dd696a6d20..0000000000 --- a/src/migrations/20211130093333-add-last-update-to-projects.js +++ /dev/null @@ -1,12 +0,0 @@ -'use strict'; - -exports.up = function (db, callback) { - db.runSql( - 'ALTER TABLE projects ADD COLUMN "last_update" TIMESTAMP WITH TIME ZONE', - callback, - ); -}; - -exports.down = function (db, callback) { - db.runSql('ALTER TABLE projects DROP COLUMN "last_update";', callback); -}; diff --git a/src/migrations/20211130142314-add-updated-at-to-projects.js b/src/migrations/20211130142314-add-updated-at-to-projects.js new file mode 100644 index 0000000000..016d43ab0e --- /dev/null +++ b/src/migrations/20211130142314-add-updated-at-to-projects.js @@ -0,0 +1,12 @@ +'use strict'; + +exports.up = function (db, callback) { + db.runSql( + 'ALTER TABLE projects ADD COLUMN "updated_at" TIMESTAMP WITH TIME ZONE', + callback, + ); +}; + +exports.down = function (db, callback) { + db.runSql('ALTER TABLE projects DROP COLUMN "updated_at";', callback); +}; From 26c9bfa3c8fbb682cde3ef1bc7fe8eafd047c751 Mon Sep 17 00:00:00 2001 From: Youssef Date: Wed, 1 Dec 2021 09:59:34 +0100 Subject: [PATCH 6/7] fix: add timestamp on project creation for update_at column --- src/migrations/20211130142314-add-updated-at-to-projects.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/migrations/20211130142314-add-updated-at-to-projects.js b/src/migrations/20211130142314-add-updated-at-to-projects.js index 016d43ab0e..358c3c9d3e 100644 --- a/src/migrations/20211130142314-add-updated-at-to-projects.js +++ b/src/migrations/20211130142314-add-updated-at-to-projects.js @@ -2,7 +2,7 @@ exports.up = function (db, callback) { db.runSql( - 'ALTER TABLE projects ADD COLUMN "updated_at" TIMESTAMP WITH TIME ZONE', + 'ALTER TABLE projects ADD COLUMN "updated_at" TIMESTAMP WITH TIME ZONE DEFAULT now();', callback, ); }; From 5e6e7e1aac2835d7a7a52708855f5dd2d3f1acdb Mon Sep 17 00:00:00 2001 From: Youssef Date: Wed, 1 Dec 2021 10:41:41 +0100 Subject: [PATCH 7/7] adding tests --- .../api/admin/project/project.health.e2e.test.ts | 13 +++++++++++++ src/test/e2e/stores/project-store.e2e.test.ts | 1 + 2 files changed, 14 insertions(+) diff --git a/src/test/e2e/api/admin/project/project.health.e2e.test.ts b/src/test/e2e/api/admin/project/project.health.e2e.test.ts index a10daed317..f303329e12 100644 --- a/src/test/e2e/api/admin/project/project.health.e2e.test.ts +++ b/src/test/e2e/api/admin/project/project.health.e2e.test.ts @@ -302,3 +302,16 @@ test('Sorts environments correctly if sort order is equal', async () => { expect(feature.environments[1].name).toBe(envTwo); }); }); + +test('Update update_at when setHealth runs', async () => { + await app.services.projectHealthService.setHealthRating(); + await app.request + .get('/api/admin/projects/default/health-report') + .expect(200) + .expect('Content-Type', /json/) + .expect((res) => { + let now = new Date().getTime(); + let updatedAt = new Date(res.body.updatedAt).getTime(); + expect(now - updatedAt).toBeLessThan(5000); + }); +}); diff --git a/src/test/e2e/stores/project-store.e2e.test.ts b/src/test/e2e/stores/project-store.e2e.test.ts index 48d2a8f7d9..fa665769e4 100644 --- a/src/test/e2e/stores/project-store.e2e.test.ts +++ b/src/test/e2e/stores/project-store.e2e.test.ts @@ -39,6 +39,7 @@ test('should create new project', async () => { expect(project.name).toEqual(ret.name); expect(project.description).toEqual(ret.description); expect(ret.createdAt).toBeTruthy(); + expect(ret.updatedAt).toBeTruthy(); expect(exists).toBe(true); });