Skip to content

Commit

Permalink
chore: update traffic data
Browse files Browse the repository at this point in the history
  • Loading branch information
toFrankie committed Aug 12, 2024
1 parent 884c55a commit 75f1390
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion docs/templates/traffic-views.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/traffic-views.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 10 additions & 10 deletions docs/traffic/2024.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"count": 1191,
"uniques": 403,
"count": 1169,
"uniques": 408,
"year": "2024",
"list": [
{
"month": "2024-07",
"count": 244,
"uniques": 70,
"count": 216,
"uniques": 69,
"list": [
{
"day": "2024-07-28",
Expand All @@ -15,8 +15,8 @@
},
{
"day": "2024-07-29",
"count": 38,
"uniques": 2
"count": 10,
"uniques": 1
},
{
"day": "2024-07-30",
Expand All @@ -32,8 +32,8 @@
},
{
"month": "2024-08",
"count": 947,
"uniques": 333,
"count": 953,
"uniques": 339,
"list": [
{
"day": "2024-08-01",
Expand Down Expand Up @@ -92,8 +92,8 @@
},
{
"day": "2024-08-12",
"count": 51,
"uniques": 16
"count": 57,
"uniques": 22
}
]
}
Expand Down
8 changes: 4 additions & 4 deletions docs/traffic/all.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"count": 1191,
"uniques": 403,
"count": 1169,
"uniques": 408,
"list": [
{
"year": "2024",
"count": 1191,
"uniques": 403
"count": 1169,
"uniques": 408
}
]
}
24 changes: 12 additions & 12 deletions scripts/update-traffic-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,31 @@ async function updateYearTrafficJson(trafficDir, data) {
const { views } = data

const currentYear = dayjs().year().toString()
let trafficData = { count: 0, uniques: 0, year: currentYear, list: [] }
let yearData = { count: 0, uniques: 0, year: currentYear, list: [] }

try {
const existingData = await fs.readFile(trafficDir, 'utf8')
trafficData = JSON.parse(existingData)
const originalTrafficData = await fs.readFile(trafficDir, 'utf8')
yearData = JSON.parse(originalTrafficData)
} catch (error) {
// Initialize new traffic data if file doesn't exist
}

views.forEach(view => {
const year = dayjs(view.timestamp).format('YYYY')
const month = dayjs(view.timestamp).format('YYYY-MM')
const date = dayjs(view.timestamp).format('YYYY-MM-DD')
const day = dayjs(view.timestamp).format('YYYY-MM-DD')

if (year !== trafficData.year) return
if (year !== yearData.year) return

let monthData = trafficData.list.find(m => m.month === month)
let monthData = yearData.list.find(m => m.month === month)
if (!monthData) {
monthData = { month, count: 0, uniques: 0, list: [] }
trafficData.list.push(monthData)
yearData.list.push(monthData)
}

let dayData = monthData.list.find(d => d.day === date)
let dayData = monthData.list.find(d => d.day === day)
if (!dayData) {
dayData = { day: date, count: 0, uniques: 0 }
dayData = { day, count: 0, uniques: 0 }
monthData.list.push(dayData)
}

Expand All @@ -48,10 +48,10 @@ async function updateYearTrafficJson(trafficDir, data) {
monthData.uniques = monthData.list.reduce((sum, d) => sum + d.uniques, 0)
})

trafficData.count = trafficData.list.reduce((sum, y) => sum + y.count, 0)
trafficData.uniques = trafficData.list.reduce((sum, y) => sum + y.uniques, 0)
yearData.count = yearData.list.reduce((sum, y) => sum + y.count, 0)
yearData.uniques = yearData.list.reduce((sum, y) => sum + y.uniques, 0)

await fs.writeFile(trafficDir, JSON.stringify(trafficData, null, 2))
await fs.writeFile(trafficDir, JSON.stringify(yearData, null, 2))
}

async function updateAllTrafficJson(trafficDir, allJsonPath) {
Expand Down

0 comments on commit 75f1390

Please sign in to comment.