Add Old Database

This commit is contained in:
Darius
2026-03-05 01:17:11 +01:00
parent 4a3a994359
commit ba74eb0deb
3 changed files with 19 additions and 8 deletions

View File

@@ -9,6 +9,8 @@ export const Config = {
gadgetbridge: { gadgetbridge: {
db_path: db_path:
process.env.GADGETBRIDGE_DB_PATH || "src/gadgetbridge/db/Gadgetbridge.db", process.env.GADGETBRIDGE_DB_PATH || "src/gadgetbridge/db/Gadgetbridge.db",
old_db_path:
process.env.GADGETBRIDGE_OLD_DB_PATH || "src/gadgetbridge/db/OldSteps.db",
}, },
grist: { grist: {

View File

@@ -8,20 +8,29 @@ export type StepRow = {
export class GadgetbridgeClient { export class GadgetbridgeClient {
private db: DatabaseSync; private db: DatabaseSync;
constructor(dbPath: string) { constructor(dbPath: string, oldDbPath: string) {
this.db = new DatabaseSync(dbPath, { open: true }); this.db = new DatabaseSync(dbPath, { open: true });
this.db.exec(`ATTACH DATABASE '${oldDbPath}' AS old`);
} }
getStepsPerDay(fromTimestamp: number, toTimestamp: number): StepRow[] { getStepsPerDay(fromTimestamp: number, toTimestamp: number): StepRow[] {
const stmt = this.db.prepare(` const stmt = this.db.prepare(`
SELECT date, SUM(steps) AS steps
FROM (
SELECT SELECT
DATE(TIMESTAMP, 'unixepoch', 'localtime') AS date, DATE(TIMESTAMP, 'unixepoch', 'localtime') AS date,
SUM(STEPS) AS steps STEPS AS steps
FROM HUAMI_EXTENDED_ACTIVITY_SAMPLE FROM HUAMI_EXTENDED_ACTIVITY_SAMPLE
WHERE TIMESTAMP >= ? AND TIMESTAMP < ? WHERE TIMESTAMP >= ? AND TIMESTAMP < ?
UNION ALL
SELECT date, steps
FROM old.steps
WHERE date >= DATE(?, 'unixepoch', 'localtime')
AND date < DATE(?, 'unixepoch', 'localtime')
)
GROUP BY date GROUP BY date
ORDER BY date ORDER BY date
`); `);
return stmt.all(fromTimestamp, toTimestamp) as StepRow[]; return stmt.all(fromTimestamp, toTimestamp, fromTimestamp, toTimestamp) as StepRow[];
} }
} }

View File

@@ -140,7 +140,7 @@ await privateServer.register(fastifyAxios, getAxiosConfig());
await publicServer.register(fastifyWebsocket); await publicServer.register(fastifyWebsocket);
// Clients and Services // Clients and Services
const gadgetbridgeClient = new GadgetbridgeClient(Config.gadgetbridge.db_path); const gadgetbridgeClient = new GadgetbridgeClient(Config.gadgetbridge.db_path, Config.gadgetbridge.old_db_path);
const gadgetbridgeService = new GadgetbridgeService(gadgetbridgeClient); const gadgetbridgeService = new GadgetbridgeService(gadgetbridgeClient);
const gristClient = new GristClient(privateServer.axios.grist); const gristClient = new GristClient(privateServer.axios.grist);