diff --git a/workers/tiltify-cache/src/index.ts b/workers/tiltify-cache/src/index.ts index dbbbc9d..5c6b9cd 100644 --- a/workers/tiltify-cache/src/index.ts +++ b/workers/tiltify-cache/src/index.ts @@ -1,6 +1,8 @@ import { getLatestData } from "./api"; import { Env } from "./env"; import { roundAmount } from "./utils"; +import { CurrentGraphPoint } from "./types/CurrentGraphPoint"; +import { ApiResponse } from "./types/ApiResponse"; const UPDATE_TIME_GRAPH = 60 * 1000; // Refresh cache every 60 seconds const UPDATE_TIME_FREQ = 10; // Refresh graph every 10 minutes @@ -88,6 +90,7 @@ export class GraphData { const url = new URL(request.url); console.log('Called ' + url.pathname); + // Get the current cached graph list if (request.method === 'GET' && url.pathname === GRAPH_API_PATH) { let data = await this.storage.get(DO_CACHE_KEY) || null; @@ -102,7 +105,9 @@ export class GraphData { } return new Response(JSON.stringify(data)); - } else if (request.method === 'POST' && url.pathname === GRAPH_API_PATH) { + } + // Update the current cached graph list + else if (request.method === 'POST' && url.pathname === GRAPH_API_PATH) { const data = await request.json(); await this.storage.put(DO_CACHE_KEY, data); return new Response("Manual Update Success", { status: 200 }); @@ -129,7 +134,7 @@ export class GraphData { } } - async getLatestGraphData(): Promise { + async getLatestGraphData(): Promise { const tiltifyData = await this.getLatestData(); if (!tiltifyData || new Date(tiltifyData.date).getMinutes() % UPDATE_TIME_FREQ !== 0) { @@ -144,7 +149,7 @@ export class GraphData { return null; } - let graphData: any[] = []; + let graphData: CurrentGraphPoint[] = []; try { graphData = (await this.storage.get(DO_CACHE_KEY)) || []; } catch (e) { } @@ -163,17 +168,13 @@ export class GraphData { return graphData; } - async defaultObject(data?: any): Promise { + async defaultObject(data?: ApiResponse): Promise { if (!data) { data = await this.getLatestData(); } if (!data) { - return [{ - "date": Date.now(), - "p": 0, - "d": 0 - }]; + return []; } return [{ @@ -183,7 +184,7 @@ export class GraphData { }]; } - async getLatestData(): Promise { + async getLatestData(): Promise { const id = this.env.TILTIFY_DATA.idFromName(CACHE_NAME); const obj = this.env.TILTIFY_DATA.get(id); const resp = await obj.fetch("http://127.0.0.1" + TILTIFY_API_PATH); @@ -191,8 +192,4 @@ export class GraphData { } } -export default { - fetch: async (request: Request, env: Env) => { - return new Response("Not found", { status: 404 }); - } -}; \ No newline at end of file +export default {}; \ No newline at end of file diff --git a/workers/tiltify-cache/src/types/CurrentGraphPoint.ts b/workers/tiltify-cache/src/types/CurrentGraphPoint.ts new file mode 100644 index 0000000..3196caa --- /dev/null +++ b/workers/tiltify-cache/src/types/CurrentGraphPoint.ts @@ -0,0 +1,5 @@ +export interface CurrentGraphPoint { + date: number; + p: number; + d: number; +} \ No newline at end of file diff --git a/workers/tiltify-cache/wrangler.toml b/workers/tiltify-cache/wrangler.toml index 7dcfc8b..9420236 100644 --- a/workers/tiltify-cache/wrangler.toml +++ b/workers/tiltify-cache/wrangler.toml @@ -3,6 +3,7 @@ main = "./src/index.ts" account_id = "b5ef135c0a2feed17e7ba1c44698c5f4" compatibility_date = "2024-09-02" workers_dev = true +logpush = true kv_namespaces = [ { binding = "JINGLE_JAM_DATA", id = "9d285e05684c4887acb7731de7a41a4e", preview_id = "6a08d74af525475baefa19a93f0ba136" } ]