Skip to content

Commit

Permalink
Merge pull request #571 from DIVD-NL/468-dynamicish-stats
Browse files Browse the repository at this point in the history
468 - Added script to automatically update statistics coming from CSIRT
  • Loading branch information
MagicLegend authored Aug 28, 2024
2 parents 4f71dce + c17f55d commit 90790de
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build_pages_native.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build_pages_no_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
49 changes: 49 additions & 0 deletions scripts/update_stats.js
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 90790de

Please sign in to comment.