Skip to content

Commit

Permalink
feat(async-jobs): add event date
Browse files Browse the repository at this point in the history
  • Loading branch information
kgajowy committed Sep 30, 2021
1 parent 1ae1296 commit 479fa69
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddTimestampToJobStatusView1632999358910
implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
DROP VIEW "scenario_job_status";
`);
await queryRunner.query(`
CREATE VIEW "scenario_job_status" AS
SELECT
DISTINCT ON (job_type, topic) job_type,
api_events.topic AS scenario_id,
projects.id AS project_id,
api_events.kind,
api_events.data,
api_events.timestamp
FROM
api_events
INNER JOIN scenarios ON api_events.topic = scenarios.id
INNER JOIN projects ON projects.id = scenarios.project_id
CROSS JOIN LATERAL SUBSTRING(
api_events.kind
FROM
'scenario.#"[^.]*#"%' FOR '#'
) AS job_type
ORDER BY
job_type,
api_events.topic,
api_events.timestamp DESC;
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
DROP VIEW "scenario_job_status";
`);
await queryRunner.query(`
CREATE VIEW "scenario_job_status" AS
SELECT
DISTINCT ON (job_type, topic) job_type,
api_events.topic AS scenario_id,
projects.id AS project_id,
api_events.kind,
api_events.data
FROM
api_events
INNER JOIN scenarios ON api_events.topic = scenarios.id
INNER JOIN projects ON projects.id = scenarios.project_id
CROSS JOIN LATERAL SUBSTRING(
api_events.kind
FROM
'scenario.#"[^.]*#"%' FOR '#'
) AS job_type
ORDER BY
job_type,
api_events.topic,
api_events.timestamp DESC;
`);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { QueryRunner } from 'typeorm';

export class AddTimestampToProjectJobsStatus1632999473857 {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
DROP VIEW "project_job_status";
`);

await queryRunner.query(`
CREATE VIEW "project_job_status" as
SELECT DISTINCT ON (job_type, topic) job_type,
api_events.topic AS project_id,
api_events.kind,
api_events.data,
api_events.timestamp
FROM api_events
CROSS JOIN LATERAL SUBSTRING(
api_events.kind
FROM
'project.#"[^.]*#"%' FOR '#'
) AS job_type
ORDER BY job_type,
api_events.topic,
api_events.timestamp DESC;
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
DROP VIEW "project_job_status";
`);

await queryRunner.query(`
CREATE VIEW "project_job_status" as
SELECT DISTINCT ON (job_type, topic) job_type,
api_events.topic AS project_id,
api_events.kind,
api_events.data
FROM api_events
CROSS JOIN LATERAL SUBSTRING(
api_events.kind
FROM
'project.#"[^.]*#"%' FOR '#'
) AS job_type
ORDER BY job_type,
api_events.topic,
api_events.timestamp DESC;
`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export class ScenarioJobStatus {
oneOf: [{ $ref: getSchemaPath(ProgressJobDTO) }],
})
data?: ProgressJobDTO;

@ApiPropertyOptional()
isoDate?: string;
}

export class ScenarioStatus {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export { Status };
export interface Job {
kind: JobType;
status: Status;
isoDate?: string;
}

export type ProgressJob = Job & {
Expand Down Expand Up @@ -68,6 +69,7 @@ export class JobStatusService {
kind: status.jobType,
status: jobStatus,
data: status.publicEventData,
isoDate: new Date(status.timestamp).toISOString(),
});
}

Expand All @@ -79,6 +81,7 @@ export class JobStatusService {
status: job.jobStatus!, // guard, or even types, do not play well
// with getters
data: job.data,
isoDate: new Date(job.timestamp).toISOString(),
})),
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,23 @@ import { ValuesType } from 'utility-types';

@ViewEntity({
expression: `
SELECT
DISTINCT ON (job_type, topic) job_type,
api_events.topic AS scenario_id,
projects.id AS project_id,
api_events.kind,
api_events.data
FROM
api_events
INNER JOIN scenarios ON api_events.topic = scenarios.id
INNER JOIN projects ON projects.id = scenarios.project_id
CROSS JOIN LATERAL SUBSTRING(
api_events.kind
FROM
'scenario.#"[^.]*#"%' FOR '#'
SELECT DISTINCT ON (job_type, topic) job_type,
api_events.topic AS scenario_id,
projects.id AS project_id,
api_events.kind,
api_events.data,
api_events.timestamp
FROM api_events
INNER JOIN scenarios ON api_events.topic = scenarios.id
INNER JOIN projects ON projects.id = scenarios.project_id
CROSS JOIN LATERAL SUBSTRING(
api_events.kind
FROM
'scenario.#"[^.]*#"%' FOR '#'
) AS job_type
ORDER BY
job_type,
api_events.topic,
api_events.timestamp DESC;
ORDER BY job_type,
api_events.topic,
api_events.timestamp DESC;
`,
})
export class ScenarioJobStatus {
Expand Down Expand Up @@ -60,6 +58,9 @@ export class ScenarioJobStatus {
@ViewColumn()
data!: ApiEvent['data'];

@ViewColumn()
timestamp!: Date;

get publicEventData(): { fractionalProgress: number } | undefined {
const data: KnownEventsData | undefined = this.data;
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { ValuesType } from 'utility-types';
SELECT DISTINCT ON (job_type, topic) job_type,
api_events.topic AS project_id,
api_events.kind,
api_events.data
api_events.data,
api_events.timestamp
FROM api_events
CROSS JOIN LATERAL SUBSTRING(
api_events.kind
Expand Down Expand Up @@ -42,6 +43,9 @@ export class ProjectJobStatus {

@ViewColumn()
data!: ApiEvent['data'];

@ViewColumn()
timestamp!: Date;
}

const eventToJobStatusMapping: Record<ValuesType<ProjectEvents>, JobStatus> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ describe(`when has two projects with scenarios and events`, () => {
{
kind: 'costSurface',
status: 'done',
isoDate: expect.any(String),
},
{
kind: 'planningUnitsInclusion',
status: 'failure',
isoDate: expect.any(String),
},
],
scenarioId: scenarioIds[0],
Expand All @@ -60,6 +62,7 @@ describe(`when has two projects with scenarios and events`, () => {
{
kind: 'costSurface',
status: 'running',
isoDate: expect.any(String),
},
],
scenarioId: scenarioIds[1],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { INestApplication } from '@nestjs/common';
import { PromiseType } from 'utility-types';
import * as request from 'supertest';

import { createWorld } from './world';
import { bootstrapApplication } from '../utils/api-application';
Expand Down Expand Up @@ -30,6 +29,7 @@ test(`job statuses for project`, async () => {
data: null,
kind: 'grid',
status: 'running',
isoDate: expect.any(String),
},
]);
expect(result.body.data.attributes.scenarios).toEqual([
Expand All @@ -39,6 +39,7 @@ test(`job statuses for project`, async () => {
{
kind: 'costSurface',
status: 'done',
isoDate: expect.any(String),
},
],
},
Expand All @@ -48,6 +49,7 @@ test(`job statuses for project`, async () => {
{
kind: 'planningUnitsInclusion',
status: 'running',
isoDate: expect.any(String),
},
],
},
Expand Down

0 comments on commit 479fa69

Please sign in to comment.