diff --git a/.github/workflows/build_pages_native.yml b/.github/workflows/build_pages_native.yml index 40046495..ba4a0e9c 100644 --- a/.github/workflows/build_pages_native.yml +++ b/.github/workflows/build_pages_native.yml @@ -31,6 +31,7 @@ jobs: npm install npm run gen-people npm run gen-teams + npm run update-stats hugo --minify npm run postbuild # - name: Test HTML code diff --git a/.github/workflows/build_pages_no_push.yml b/.github/workflows/build_pages_no_push.yml index 42d2adef..fd98a3f5 100644 --- a/.github/workflows/build_pages_no_push.yml +++ b/.github/workflows/build_pages_no_push.yml @@ -24,6 +24,7 @@ jobs: npm install npm run gen-people npm run gen-teams + npm run update-stats hugo --minify npm run postbuild # - name: Test HTML code diff --git a/package.json b/package.json index c501ebb6..16e75f12 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "postbuild": "node ./scripts/postbuild.js", "gen-people": "node ./scripts/generate_people.js", "gen-teams": "node ./scripts/generate_teams.js", + "update-stats": "node ./scripts/update_stats.js", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Les Kleuver", diff --git a/scripts/update_stats.js b/scripts/update_stats.js new file mode 100644 index 00000000..cfaeea4b --- /dev/null +++ b/scripts/update_stats.js @@ -0,0 +1,49 @@ +const axios = require('axios'); +const fs = require('fs'); +const matter = require('gray-matter'); + +async function updateMarkdown() { + try { + // Fetch the data from the endpoint + const response = await axios.get('https://csirt.divd.nl/csv/stats.json'); + const { data } = response; + + // Initialize totals + let totalMembers = 0; + let totalCases = 0; + let totalIps = 0; + + // Aggregate data from each year using Object.entries + Object.entries(data).forEach(([, stats]) => { + totalMembers += stats.cases; + totalCases += stats.cases; + totalIps += stats.ips; + }); + + // Read the existing markdown file + const filePath = 'content/block/_index.en.md'; + const fileContent = fs.readFileSync(filePath, 'utf8'); + + // Parse the front matter + const parsed = matter(fileContent); + const frontMatter = parsed.data; + + // Update the front matter with the aggregated data + frontMatter.mission.members = totalMembers.toString(); + frontMatter.mission.totalcases = totalCases.toString(); + frontMatter.mission.ips = totalIps; + + // Convert back to markdown with updated front matter + const newContent = matter.stringify(parsed.content, frontMatter); + + // Write the updated content back to the file + fs.writeFileSync(filePath, newContent, 'utf8'); + + console.log('Markdown file updated successfully!'); + } catch (error) { + console.error('Error fetching data:', error); + } +} + +// Run the function +updateMarkdown();