-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.js
54 lines (47 loc) · 1.48 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
const { blue, green, yellow, cyan, white, magenta, red } = require('colorette');
const fs = require('fs');
const path = require('path');
function printReport(pages) {
console.log('')
console.log('')
console.log(blue('========================'))
console.log(green('WEBCRAWLER REPORT'))
console.log(blue('========================'))
console.log(magenta(`Report generated on: ${new Date().toLocaleString()}`))
console.log(blue('========================'))
const sortedPages = sortPages(pages)
sortedPages.forEach((sortedPage, index) => {
const url = sortedPage[0]
const hits = sortedPage[1]
let color = yellow
if (index < 3) {
color = red
}
console.log(`${color('Webpage:')} ${cyan(url)}, ${color('Number of hits:')} ${white(hits)}`)
})
console.log(blue('========================'))
console.log(green('END OF REPORT'))
console.log(blue('========================'))
console.log('')
console.log('')
exportReportToCSV(sortedPages);
}
function sortPages(pages) {
const pagesArr = Object.entries(pages)
pagesArr.sort((a, b) => {
const aHits = a[1]
const bHits = b[1]
return bHits - aHits
})
return pagesArr
}
function exportReportToCSV(sortedPages) {
const csvContent = sortedPages.map(page => `${page[0]},${page[1]}`).join('\n');
const filePath = path.join(__dirname, 'report.csv');
fs.writeFileSync(filePath, csvContent);
console.log(green(`Report exported to ${filePath}`));
}
module.exports = {
sortPages,
printReport
}