-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #571 from DIVD-NL/468-dynamicish-stats
468 - Added script to automatically update statistics coming from CSIRT
- Loading branch information
Showing
4 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |