Skip to content

Commit

Permalink
add support for table cleanUp
Browse files Browse the repository at this point in the history
  • Loading branch information
haithembelhaj committed Jan 31, 2024
1 parent 3d53033 commit 766681f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
26 changes: 26 additions & 0 deletions src/history.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect, test } from "bun:test";

import { cleanUp, getCount, insertJobHistory, init, truncate } from "./history";

init("test.sqlite");
truncate();

test("cleanUp", () => {
let now = Date.now();

for (let i = 0; i < 1002; i++) {
insertJobHistory({
job_name: "test",
response_ok: 1,
response_status: 200,
response_text: "",
triggered_at: now++,
});
}

expect(getCount()).toEqual(1002);
cleanUp(now);
expect(getCount()).toEqual(1002);
cleanUp(now + 1000 * 60 * 60 + 1);
expect(getCount()).toEqual(1000);
});
51 changes: 33 additions & 18 deletions src/history.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
import { Database, Statement } from "bun:sqlite";

export let db: Database;

let insertQuery: Statement<any, any>;
let getQuery: Statement<any, any>;
let countQuery: Statement<any, any>;

let initialized = false;

export let db: Database;
let deleteOldQuery: Statement<any, any>;

export function init(path = "db/history.sqlite") {
if (initialized) return;
if (db) return;

const d = new Database(path);
d.run("PRAGMA journal_mode = WAL;");
d.run(`
db = new Database(path);
db.run("PRAGMA journal_mode = WAL;");
db.run(`
create table if not exists jobs_history (
job_name text not null,
triggered_at number not null,
response_ok boolean not null,
response_status number not null,
response_text text not null
);`);
d.run(
db.run(
`create index if not exists jobs_history_index on jobs_history (job_name);`
);

insertQuery = d.query(
insertQuery = db.query(
`insert into jobs_history (job_name, triggered_at, response_ok, response_status, response_text) values ($job_name, $triggered_at, $response_ok, $response_status, $response_text);`
);
getQuery = d.query(
getQuery = db.query(
"select * from jobs_history where job_name = ? ORDER BY triggered_at DESC LIMIT ? OFFSET ?;"
);
countQuery = d.query("select COUNT(*) from jobs_history");

initialized = true;
countQuery = db.query("select COUNT(*) from jobs_history;");

db = d;
deleteOldQuery = db.query(
"delete from jobs_history where triggered_at in (select triggered_at from jobs_history order by triggered_at asc limit (select count(*) - 1000 from jobs_history));"
);
}

export function truncate() {
Expand All @@ -52,20 +51,36 @@ export type JobHistory = {

export function insertJobHistory(h: JobHistory) {
init();
return insertQuery.run({

insertQuery.run({
$job_name: h.job_name,
$triggered_at: h.triggered_at,
$response_ok: h.response_ok,
$response_status: h.response_status,
$response_text: h.response_text,
});
cleanUp(Date.now());
}

export function getJobHistoryByName(name: string, limit = 1000, offset = 0) {
const oneHour = 1000 * 60 * 60;
let lastCleanup = Date.now();
export function cleanUp(now: number) {
init();
if (now - lastCleanup < oneHour) return;
if (getCount() <= 1000) return;
lastCleanup = now;
deleteOldQuery.run();
}

export function getCount() {
init();
const countRow = countQuery.get();
const count = countRow["COUNT(*)"];
return countRow["COUNT(*)"];
}

export function getJobHistoryByName(name: string, limit = 1000, offset = 0) {
init();
const count = getCount();
const history = getQuery.all(name, limit, offset) as JobHistory[];

return { history, count };
Expand Down

0 comments on commit 766681f

Please sign in to comment.