2020-06-02 00:47:37 +03:00
|
|
|
import moment from "moment-timezone";
|
2023-07-01 12:17:45 +00:00
|
|
|
import { In } from "typeorm";
|
2024-04-09 20:57:18 +03:00
|
|
|
import { DBDateFormat } from "../../utils.js";
|
|
|
|
import { dataSource } from "../dataSource.js";
|
|
|
|
import { Config } from "../entities/Config.js";
|
2020-06-02 00:47:37 +03:00
|
|
|
|
|
|
|
const CLEAN_PER_LOOP = 50;
|
|
|
|
|
|
|
|
export async function cleanupConfigs() {
|
2023-07-01 12:17:45 +00:00
|
|
|
const configRepository = dataSource.getRepository(Config);
|
2020-06-02 00:47:37 +03:00
|
|
|
|
2022-06-26 15:23:17 +03:00
|
|
|
// FIXME: The query below doesn't work on MySQL 8.0. Pending an update.
|
|
|
|
return;
|
|
|
|
|
2020-06-02 00:47:37 +03:00
|
|
|
let cleaned = 0;
|
|
|
|
let rows;
|
|
|
|
|
|
|
|
// >1 month old: 1 config retained per month
|
2021-09-11 19:06:51 +03:00
|
|
|
const oneMonthCutoff = moment.utc().subtract(30, "days").format(DBDateFormat);
|
2020-06-02 00:47:37 +03:00
|
|
|
do {
|
2023-07-01 12:17:45 +00:00
|
|
|
rows = await dataSource.query(
|
2020-06-02 00:47:37 +03:00
|
|
|
`
|
|
|
|
WITH _configs
|
|
|
|
AS (
|
|
|
|
SELECT
|
|
|
|
id,
|
|
|
|
\`key\`,
|
|
|
|
YEAR(edited_at) AS \`year\`,
|
|
|
|
MONTH(edited_at) AS \`month\`,
|
|
|
|
ROW_NUMBER() OVER (
|
|
|
|
PARTITION BY \`key\`, \`year\`, \`month\`
|
|
|
|
ORDER BY edited_at
|
|
|
|
) AS row_num
|
|
|
|
FROM
|
|
|
|
configs
|
|
|
|
WHERE
|
|
|
|
is_active = 0
|
|
|
|
AND edited_at < ?
|
|
|
|
)
|
|
|
|
SELECT *
|
|
|
|
FROM _configs
|
|
|
|
WHERE row_num > 1
|
|
|
|
`,
|
|
|
|
[oneMonthCutoff],
|
|
|
|
);
|
|
|
|
|
|
|
|
if (rows.length > 0) {
|
|
|
|
await configRepository.delete({
|
2021-09-11 19:06:51 +03:00
|
|
|
id: In(rows.map((r) => r.id)),
|
2020-06-02 00:47:37 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
cleaned += rows.length;
|
|
|
|
} while (rows.length === CLEAN_PER_LOOP);
|
|
|
|
|
|
|
|
// >2 weeks old: 1 config retained per day
|
2021-09-11 19:06:51 +03:00
|
|
|
const twoWeekCutoff = moment.utc().subtract(2, "weeks").format(DBDateFormat);
|
2020-06-02 00:47:37 +03:00
|
|
|
do {
|
2023-07-01 12:17:45 +00:00
|
|
|
rows = await dataSource.query(
|
2020-06-02 00:47:37 +03:00
|
|
|
`
|
|
|
|
WITH _configs
|
|
|
|
AS (
|
|
|
|
SELECT
|
|
|
|
id,
|
|
|
|
\`key\`,
|
|
|
|
DATE(edited_at) AS \`date\`,
|
|
|
|
ROW_NUMBER() OVER (
|
|
|
|
PARTITION BY \`key\`, \`date\`
|
|
|
|
ORDER BY edited_at
|
|
|
|
) AS row_num
|
|
|
|
FROM
|
|
|
|
configs
|
|
|
|
WHERE
|
|
|
|
is_active = 0
|
|
|
|
AND edited_at < ?
|
|
|
|
AND edited_at >= ?
|
|
|
|
)
|
|
|
|
SELECT *
|
|
|
|
FROM _configs
|
|
|
|
WHERE row_num > 1
|
|
|
|
`,
|
|
|
|
[twoWeekCutoff, oneMonthCutoff],
|
|
|
|
);
|
|
|
|
|
|
|
|
if (rows.length > 0) {
|
|
|
|
await configRepository.delete({
|
2021-09-11 19:06:51 +03:00
|
|
|
id: In(rows.map((r) => r.id)),
|
2020-06-02 00:47:37 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
cleaned += rows.length;
|
|
|
|
} while (rows.length === CLEAN_PER_LOOP);
|
|
|
|
|
|
|
|
return cleaned;
|
|
|
|
}
|