Skip to content

Commit cb8b1eb

Browse files
committed
(cleanup) move clear api traces into own file
1 parent 919efc2 commit cb8b1eb

File tree

3 files changed

+50
-43
lines changed

3 files changed

+50
-43
lines changed

backend/src/jobs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { AppDataSource } from "data-source"
44
import { JobsService } from "services/jobs"
55
import runAllTests from "services/testing/runAllTests"
66
import { logAggregatedStats } from "services/logging"
7+
import clearApiTraces from "services/jobs/clearApiTraces"
78

89
const main = async () => {
910
const datasource = await AppDataSource.initialize()
@@ -70,7 +71,7 @@ const main = async () => {
7071
schedule.scheduleJob("0 * * * *", () => {
7172
clearApiTracesSem.take(async () => {
7273
console.log("\nClearing Api Trace data...")
73-
await JobsService.clearApiTraces()
74+
await clearApiTraces()
7475
console.log("Finished clearing Api Trace data.")
7576
clearApiTracesSem.leave()
7677
})
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { DateTime } from "luxon"
2+
import { ApiTrace } from "models"
3+
import { AppDataSource } from "data-source"
4+
import {
5+
aggregateTracesDataHourlyQuery,
6+
aggregateTracesDataMinutelyQuery,
7+
} from "./queries"
8+
9+
const clearApiTraces = async (): Promise<void> => {
10+
const queryRunner = AppDataSource.createQueryRunner()
11+
await queryRunner.connect()
12+
try {
13+
const now = DateTime.now().startOf("hour")
14+
const oneHourAgo = now.minus({ hours: 1 }).toJSDate()
15+
16+
const maxTimeRes = await queryRunner.manager
17+
.createQueryBuilder()
18+
.select([`MAX("createdAt") as "maxTime"`])
19+
.from(ApiTrace, "traces")
20+
.where('"apiEndpointUuid" IS NOT NULL')
21+
.andWhere("analyzed = TRUE")
22+
.andWhere('"createdAt" < :oneHourAgo', { oneHourAgo })
23+
.getRawOne()
24+
const maxTime: Date = maxTimeRes?.maxTime ?? null
25+
26+
if (maxTime) {
27+
await queryRunner.startTransaction()
28+
await queryRunner.query(aggregateTracesDataMinutelyQuery, [maxTime])
29+
await queryRunner.query(aggregateTracesDataHourlyQuery, [maxTime])
30+
await queryRunner.manager
31+
.createQueryBuilder()
32+
.delete()
33+
.from(ApiTrace)
34+
.where('"apiEndpointUuid" IS NOT NULL')
35+
.andWhere("analyzed = TRUE")
36+
.andWhere('"createdAt" <= :maxTime', { maxTime })
37+
.execute()
38+
await queryRunner.commitTransaction()
39+
}
40+
} catch (err) {
41+
console.error(`Encountered error while clearing trace data: ${err}`)
42+
await queryRunner.rollbackTransaction()
43+
} finally {
44+
await queryRunner?.release()
45+
}
46+
}
47+
48+
export default clearApiTraces

backend/src/services/jobs/index.ts

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { FindManyOptions, IsNull, LessThanOrEqual, Raw } from "typeorm"
22
import { v4 as uuidv4 } from "uuid"
3-
import { DateTime } from "luxon"
43
import {
54
getDataType,
65
isParameter,
@@ -27,8 +26,6 @@ import { DatabaseService } from "services/database"
2726
import axios from "axios"
2827
import { SpecService } from "services/spec"
2928
import {
30-
aggregateTracesDataHourlyQuery,
31-
aggregateTracesDataMinutelyQuery,
3229
getUnauthenticatedEndpointsSensitiveData,
3330
updateUnauthenticatedEndpoints,
3431
} from "./queries"
@@ -289,45 +286,6 @@ export class JobsService {
289286
}
290287
}
291288

292-
static async clearApiTraces(): Promise<void> {
293-
const queryRunner = AppDataSource.createQueryRunner()
294-
await queryRunner.connect()
295-
try {
296-
const now = DateTime.now().startOf("hour")
297-
const oneHourAgo = now.minus({ hours: 1 }).toJSDate()
298-
299-
const maxTimeRes = await queryRunner.manager
300-
.createQueryBuilder()
301-
.select([`MAX("createdAt") as "maxTime"`])
302-
.from(ApiTrace, "traces")
303-
.where('"apiEndpointUuid" IS NOT NULL')
304-
.andWhere("analyzed = TRUE")
305-
.andWhere('"createdAt" < :oneHourAgo', { oneHourAgo })
306-
.getRawOne()
307-
const maxTime: Date = maxTimeRes?.maxTime ?? null
308-
309-
if (maxTime) {
310-
await queryRunner.startTransaction()
311-
await queryRunner.query(aggregateTracesDataMinutelyQuery, [maxTime])
312-
await queryRunner.query(aggregateTracesDataHourlyQuery, [maxTime])
313-
await queryRunner.manager
314-
.createQueryBuilder()
315-
.delete()
316-
.from(ApiTrace)
317-
.where('"apiEndpointUuid" IS NOT NULL')
318-
.andWhere("analyzed = TRUE")
319-
.andWhere('"createdAt" <= :maxTime', { maxTime })
320-
.execute()
321-
await queryRunner.commitTransaction()
322-
}
323-
} catch (err) {
324-
console.error(`Encountered error while clearing trace data: ${err}`)
325-
await queryRunner.rollbackTransaction()
326-
} finally {
327-
await queryRunner?.release()
328-
}
329-
}
330-
331289
static async generateEndpointsFromTraces(): Promise<void> {
332290
const queryRunner = AppDataSource.createQueryRunner()
333291
await queryRunner.connect()

0 commit comments

Comments
 (0)