Skip to content

Commit

Permalink
More configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
no1mann committed Oct 1, 2024
1 parent 3a30922 commit 58f31f0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
27 changes: 12 additions & 15 deletions workers/tiltify-cache/src/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;

Expand All @@ -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 });
Expand All @@ -129,7 +134,7 @@ export class GraphData {
}
}

async getLatestGraphData(): Promise<any[] | null> {
async getLatestGraphData(): Promise<CurrentGraphPoint[] | null> {
const tiltifyData = await this.getLatestData();

if (!tiltifyData || new Date(tiltifyData.date).getMinutes() % UPDATE_TIME_FREQ !== 0) {
Expand All @@ -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) { }
Expand All @@ -163,17 +168,13 @@ export class GraphData {
return graphData;
}

async defaultObject(data?: any): Promise<any[]> {
async defaultObject(data?: ApiResponse): Promise<CurrentGraphPoint[]> {
if (!data) {
data = await this.getLatestData();
}

if (!data) {
return [{
"date": Date.now(),
"p": 0,
"d": 0
}];
return [];
}

return [{
Expand All @@ -183,16 +184,12 @@ export class GraphData {
}];
}

async getLatestData(): Promise<any> {
async getLatestData(): Promise<ApiResponse> {
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);
return await resp.json();
}
}

export default {
fetch: async (request: Request, env: Env) => {
return new Response("Not found", { status: 404 });
}
};
export default {};
5 changes: 5 additions & 0 deletions workers/tiltify-cache/src/types/CurrentGraphPoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface CurrentGraphPoint {
date: number;
p: number;
d: number;
}
1 change: 1 addition & 0 deletions workers/tiltify-cache/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
]
Expand Down

0 comments on commit 58f31f0

Please sign in to comment.