-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.js
75 lines (64 loc) · 2.03 KB
/
report.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const fs = require('fs');
// The report is output to a CSV file using the 'fs' module to allow for easy analysis in a spreadsheet.
// a built-in module in Node.js that provides functions for interacting with the file system.
/**
* Write report data to a CSV file.
*
* @function writeReportToFile
* @param {Object} report - The report data to write to file.
* @param {string} filename - The name of the output CSV file.
* @returns {void}
*/
function writeReportToFile(report, filename) {
const rows = [];
// Add header row
rows.push(['Page URL', 'Number of Visits']);
// Add data rows
for (const [pageURL, numVisits] of Object.entries(report)) {
rows.push([pageURL, numVisits]);
}
// Write CSV file
const csv = rows.map(row => row.join(',')).join('\n');
fs.writeFileSync(filename, csv);
}
/**
* Prints a report of pages sorted by descending hit count.
*
* @param {Object.<string, number>} pages - An object where the keys are URLs and the values are the hit counts.
* @returns {void}
*/
function printReport(pages) {
console.log("==========")
console.log("START REPORT")
console.log("==========")
const sortedPages = sortPages(pages)
for(const sortedPage of sortedPages) {
const url = sortedPage[0];
const hits = sortedPage[1];
console.log(`Found ${hits} links to page: ${url}`)
}
console.log("==========")
console.log("END REPORT")
console.log("==========")
}
/**
* Sorts pages by descending hit count.
*
* @param {Object.<string, number>} pages - An object where the keys are URLs and the values are the hit counts.
* @returns {Array.<Array.<string|number>>} - An array of arrays where each inner array represents a page and its hit count.
*/
function sortPages(pages) {
const pagesArr = Object.entries(pages);
pagesArr.sort((a, b) => {
const aHits = a[1];
const bHits = b[1];
// return b[1] - a[1]
return bHits - aHits
})
return pagesArr;
}
module.exports = {
sortPages,
printReport,
writeReportToFile
}