Skip to content

Commit d673994

Browse files
committed
Prepare KV store for OMM data
1 parent bcd6379 commit d673994

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ new Elysia()
2020
.get("/", async () => {
2121
const activeGroups = [];
2222
for (const group of config.allowedGroups) {
23-
const timestamp = await kv.get(`${group}_timestamp`);
23+
const timestamp = await kv.get(`${group}_timestamp_tle`);
2424
const lastUpdate = timestamp ? new Date(timestamp).toISOString() : "Never";
2525
activeGroups.push({ name: group, lastUpdate });
2626
}

src/routes/tle.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const tleRoute = new Elysia({ prefix: "/tle" })
1717
return new Response(`Group "${group}" is not allowed.`, { status: 403 });
1818
}
1919

20-
let timestamp = await kv.get(`${group}_timestamp`);
20+
let timestamp = await kv.get(`${group}_timestamp_tle`);
2121
const now = Date.now();
2222
const staleDuration = group === "active" ? config.cacheActiveDuration : config.cacheDuration;
2323
const isStale = timestamp ? now - timestamp > staleDuration : true;
@@ -28,20 +28,20 @@ const tleRoute = new Elysia({ prefix: "/tle" })
2828
return new Response(null, { status: 304, headers: { "Last-Modified": new Date(timestamp).toUTCString(), "Cache-Control": `max-age=${group === "active" ? Math.ceil((config.cacheActiveDuration - (now - timestamp)) / 1000) : Math.ceil((config.cacheDuration - (now - timestamp)) / 1000)}` } });
2929
}
3030

31-
let tle = await kv.get(group);
31+
let tle = await kv.get(`${group}_tle`);
3232

3333
if (!tle) {
3434
log.debug(`No cached TLEs for group "${group}". Fetching from Celestrak...`);
3535
tle = await tleFetcher(group);
3636
timestamp = now;
37-
kv.set(group, tle);
38-
kv.set(`${group}_timestamp`, timestamp);
37+
kv.set(`${group}_tle`, tle);
38+
kv.set(`${group}_timestamp_tle`, timestamp);
3939
} else if (isStale) {
4040
log.debug(`TLEs for group "${group}" are stale. Fetching fresh TLEs...`);
4141
tle = await tleFetcher(group);
4242
timestamp = now;
43-
kv.set(group, tle);
44-
kv.set(`${group}_timestamp`, timestamp);
43+
kv.set(`${group}_tle`, tle);
44+
kv.set(`${group}_timestamp_tle`, timestamp);
4545
} else {
4646
log.debug(`Serving cached TLEs for group "${group}".`);
4747
}
@@ -56,7 +56,7 @@ const tleRoute = new Elysia({ prefix: "/tle" })
5656
return new Response(`Group "${group}" is not allowed.`, { status: 403 });
5757
}
5858

59-
const timestamp = await kv.get(`${group}_timestamp`);
59+
const timestamp = await kv.get(`${group}_timestamp_tle`);
6060
if (!timestamp) {
6161
return new Response(`No cached TLEs for group "${group}".`, { status: 404 });
6262
}

src/utils/tleFetcher.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ async function fetchTle(group: string) {
77
const url = `https://celestrak.org/NORAD/elements/gp.php?GROUP=${group}&FORMAT=tle`;
88
log.debug(`Fetching TLEs for group "${group}" from Celestrak...`);
99
try {
10-
const lastFetch = await kv.get(`${group}_timestamp`);
10+
const lastFetch = await kv.get(`${group}_timestamp_tle`);
1111
const response = await fetch(url, {
1212
headers: {
1313
"If-Modified-Since": lastFetch ? new Date(lastFetch).toUTCString() : "",
@@ -18,8 +18,8 @@ async function fetchTle(group: string) {
1818
throw new Error(`Failed to fetch TLEs: ${response.status} ${response.statusText}`);
1919
}
2020
const tleData = await response.text();
21-
await kv.set(group, tleData);
22-
await kv.set(`${group}_timestamp`, Date.now());
21+
await kv.set(`${group}_tle`, tleData);
22+
await kv.set(`${group}_timestamp_tle`, Date.now());
2323
log.debug(`Successfully cached TLEs for group "${group}".`);
2424
return tleData;
2525
} catch (error) {

src/utils/tleGetter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ async function getObjectsTle(noradId: number) {
1313
const isStale = timestamp ? now - timestamp > config.cacheNoradDuration : true;
1414

1515
if (!tleData || isStale) {
16-
let allTles = (await kv.get("active")) as string | null;
16+
let allTles = (await kv.get("active_tle")) as string | null;
1717
if (!allTles) {
1818
await fetchTle("active");
1919
}
20-
allTles = (await kv.get("active")) as string | null;
20+
allTles = (await kv.get("active_tle")) as string | null;
2121

2222
// If we still don't have the active TLEs then something went wrong with fetching, so we throw an error
2323
if (!allTles) {

0 commit comments

Comments
 (0)