Metrics Tracking

This commit is contained in:
ssyyhhrr
2022-12-26 18:12:01 +00:00
parent a9055b8154
commit ae6b6044d0
9 changed files with 86 additions and 7 deletions

3
.gitignore vendored
View File

@@ -1,3 +1,4 @@
node_modules node_modules
driver.txt driver.txt
drivers.json drivers.json
.idea

5
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,5 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/

7
.idea/discord.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="PROJECT_FILES" />
<option name="description" value="" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/stewardle.iml" filepath="$PROJECT_DIR$/.idea/stewardle.iml" />
</modules>
</component>
</project>

12
.idea/stewardle.iml generated Normal file
View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/temp" />
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
<excludeFolder url="file://$MODULE_DIR$/tmp" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

40
app.js
View File

@@ -5,8 +5,10 @@ const schedule = require("node-schedule")
const express = require("express") const express = require("express")
const favicon = require("serve-favicon") const favicon = require("serve-favicon")
const morgan = require("morgan") const morgan = require("morgan")
const dayjs = require("dayjs")
const path = "./assets/drivers.json" const driversPath = "./assets/drivers.json"
const statsPath = "./assets/stats.json"
const flag = { const flag = {
"British": "gb", "British": "gb",
@@ -55,6 +57,11 @@ const team = {
"Ferrari": "ferrari" "Ferrari": "ferrari"
} }
let stats = {
"visits": 0,
"guesses": 0
}
let drivers = {} let drivers = {}
let driver let driver
@@ -68,14 +75,24 @@ schedule.scheduleJob("0 0 * * *", async () => {
dotd() dotd()
}).catch(() => { }).catch(() => {
console.log("API is unreachable! Not updating drivers...") console.log("API is unreachable! Not updating drivers...")
if (fs.existsSync(path)) { if (fs.existsSync(driversPath)) {
let data = fs.readFileSync(path) let data = fs.readFileSync(driversPath)
drivers = JSON.parse(data) drivers = JSON.parse(data)
dotd() dotd()
} else { } else {
throw "Ergast API is unreachable and the drivers.json cache has not been built. Please try again when the Ergast API is online." throw "Ergast API is unreachable and the drivers.json cache has not been built. Please try again when the Ergast API is online."
} }
}) })
let rawStatsFile = fs.readFileSync(statsPath)
let statsFile = JSON.parse(rawStatsFile)
let date = dayjs.format("YYYY-MM-DD")
statsFile[date] = stats
let newStatsFile = JSON.stringify(statsFile)
fs.writeFileSync(statsPath, newStatsFile)
stats = {
"visits": 0,
"guesses": 0
}
}) })
async function main() { async function main() {
@@ -84,9 +101,13 @@ async function main() {
dotd() dotd()
}).catch(() => { }).catch(() => {
console.log("API is unreachable! Not updating drivers...") console.log("API is unreachable! Not updating drivers...")
let data = fs.readFileSync(path) if (fs.existsSync(driversPath)) {
drivers = JSON.parse(data) let data = fs.readFileSync(driversPath)
dotd() drivers = JSON.parse(data)
dotd()
} else {
throw "Ergast API is unreachable and the drivers.json cache has not been built. Please try again when the Ergast API is online."
}
}) })
server() server()
} }
@@ -164,6 +185,12 @@ function server() {
app.get("/", (req, res) => { app.get("/", (req, res) => {
res.render("index") res.render("index")
stats.visits++
})
app.get("/stats", (req, res) => {
let rawStatsFile = fs.readFileSync(statsPath)
res.json(JSON.parse(rawStatsFile))
}) })
app.get("/winner", (req, res) => { app.get("/winner", (req, res) => {
@@ -222,6 +249,7 @@ function server() {
"firstYear": response[4], "firstYear": response[4],
"wins": response[5] "wins": response[5]
}) })
stats.guesses++
}) })
let port = 3000 let port = 3000

11
package-lock.json generated
View File

@@ -10,6 +10,7 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"axios": "^0.27.2", "axios": "^0.27.2",
"dayjs": "^1.11.7",
"ejs": "^3.1.8", "ejs": "^3.1.8",
"express": "^4.18.1", "express": "^4.18.1",
"lodash": "^4.17.21", "lodash": "^4.17.21",
@@ -233,6 +234,11 @@
"node": ">=0.8" "node": ">=0.8"
} }
}, },
"node_modules/dayjs": {
"version": "1.11.7",
"resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.7.tgz",
"integrity": "sha512-+Yw9U6YO5TQohxLcIkrXBeY73WP3ejHWVvx8XCk3gxvQDCTEmS48ZrSZCKciI7Bhl/uCMyxYtE9UqRILmFphkQ=="
},
"node_modules/debug": { "node_modules/debug": {
"version": "2.6.9", "version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
@@ -1177,6 +1183,11 @@
"luxon": "^1.26.0" "luxon": "^1.26.0"
} }
}, },
"dayjs": {
"version": "1.11.7",
"resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.7.tgz",
"integrity": "sha512-+Yw9U6YO5TQohxLcIkrXBeY73WP3ejHWVvx8XCk3gxvQDCTEmS48ZrSZCKciI7Bhl/uCMyxYtE9UqRILmFphkQ=="
},
"debug": { "debug": {
"version": "2.6.9", "version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",

View File

@@ -10,6 +10,7 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"axios": "^0.27.2", "axios": "^0.27.2",
"dayjs": "^1.11.7",
"ejs": "^3.1.8", "ejs": "^3.1.8",
"express": "^4.18.1", "express": "^4.18.1",
"lodash": "^4.17.21", "lodash": "^4.17.21",