From ed99536d9147df15f0644d000c816b453c6f4f24 Mon Sep 17 00:00:00 2001 From: Niceygy Date: Thu, 13 Jun 2024 11:53:30 +0100 Subject: [PATCH] Now with data archiving! for even more charts! --- src/scheduled/db.js | 11 +++++++++++ src/scheduled/db.sql | 6 ++++++ src/scheduled/weeklyMessage.js | 16 +++++++++++++--- wrangler.toml | 9 ++++----- 4 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 src/scheduled/db.js create mode 100644 src/scheduled/db.sql diff --git a/src/scheduled/db.js b/src/scheduled/db.js new file mode 100644 index 0000000..7c9f918 --- /dev/null +++ b/src/scheduled/db.js @@ -0,0 +1,11 @@ +export default async function archiveData(env, repos, counts) { + const DB = env.DB; + // SQL DB, repo: string, count: string + let date = new Date(); + for (var i = 0; i < repos.length; i++) { + console.log(`Inserting ${repos[i]}: ${counts[i]} into the database`); + await DB.exec( + `INSERT INTO download (date, repo, count) VALUES ('${date}', '${repos[i]}', '${counts[i]}')` + ); + } +} diff --git a/src/scheduled/db.sql b/src/scheduled/db.sql new file mode 100644 index 0000000..accc995 --- /dev/null +++ b/src/scheduled/db.sql @@ -0,0 +1,6 @@ +CREATE TABLE IF NOT EXISTS download ( + date NUMBER, --UNIX time + repo VARCHAR(50), + count INTEGER +); + diff --git a/src/scheduled/weeklyMessage.js b/src/scheduled/weeklyMessage.js index 5ae848b..f581512 100644 --- a/src/scheduled/weeklyMessage.js +++ b/src/scheduled/weeklyMessage.js @@ -1,4 +1,5 @@ import { chart } from "./chart"; +import archiveData from "./db"; /** * Sends the weekly totals to a discord channel. @@ -43,6 +44,7 @@ export async function cron(env) { Dtotal += parseInt(value); } } + repos.sort((a, b) => b.count - a.count); const Plist = await KV2.list(); console.log('Key list aquired for pages'); @@ -63,19 +65,27 @@ export async function cron(env) { pages.sort((a, b) => b.count - a.count); let currentDate = /* @__PURE__ */ new Date(); - var downloadMessageRaw = repos.map((repo) => `Repo: ${repo.name}, Count: ${repo.count}`).join(''); + //create repo and count arrays + var repoArr = []; + var countArr = []; + for (var i = 0; i < repos.length; i++) { + repoArr.push(repos[i].name); + countArr.push(repos[i].count); + } + await archiveData(env, repoArr, countArr); + var downloadMessageRaw = repos.map((repo) => `Repo: ${repo.name}, Count: ${repo.count}\n`).join(''); var downloadMessage = '# Downloads as of ' + currentDate.toUTCString() + '\n\n' + downloadMessageRaw + '\n\nTotal downloads: ' + Dtotal; - var pageViewMessage = '# Page visits as of ' + currentDate.toUTCString() + '\n\n' + pages.map((page) => `Page: ${page.name}, Count: ${page.count}`).join(''); + var pageViewMessage = '# Page visits as of ' + currentDate.toUTCString() + '\n\n' + pages.map((page) => `Page: ${page.name}, Count: ${page.count}\n`).join(''); let pageViewMessage_1 = pageViewMessage.substring(0, 1999); let pageViewMessage_2 = pageViewMessage.substring(2e3); console.log('Message generated. Sending to Discord...'); console.log('Sending data to ' + env.url); await sendMessageToDiscord(downloadMessage, env); await sendMessageToDiscord(pageViewMessage_1, env); - await chart(downloadMessageRaw); if (pageViewMessage_2.length > 0) { await sendMessageToDiscord(pageViewMessage_2, env); } + await chart(downloadMessageRaw); console.log('Message sent to Discord'); return 'Complete! Message sent.'; } diff --git a/wrangler.toml b/wrangler.toml index 1a7f907..8118db0 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -19,11 +19,10 @@ mode = "smart" [triggers] crons = [ "0 12 * * sat" ] -# [unsafe] -# [[unsafe.bindings]] -# database_id = "eafc6a3b-97a7-4c08-94d9-7a10d4c28cb0" -# name = "DB" -# type = "d1" +[[d1_databases]] +binding = "DB" # i.e. available in your Worker on env.DB +database_name = "DownloadArchive" +database_id = "34a375cc-9ec7-4244-9368-b63c71fa9218" [vars] url = "https://discord.com/api/webhooks/1156259064595087521/fLfxylc8D6oe15-ZsnTxhpjwUkmjUIXM08ncj0GYV2pX3yykMM3hMZgbRn36qsvhd9FG"