-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.ts
46 lines (35 loc) · 1.12 KB
/
output.ts
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
import fs from 'node:fs'
import papaparse from 'papaparse'
import type { Results } from './types.js'
function resultsToArray(results: Results): Array<Record<string, string>> {
const array: Array<Record<string, string>> = []
for (const [ip, oids] of Object.entries(results)) {
const ipResults: Record<string, string> = {
ip
}
for (const [oid, oidValue] of Object.entries(oids)) {
// eslint-disable-next-line security/detect-object-injection
ipResults[oid] = oidValue
}
array.push(ipResults)
}
return array
}
/**
* Outputs the polling results to the console.
* @param {Results} results - The polling results.
*/
export function outputToConsole(results: Results): void {
console.log(`Data collected: ${new Date().toLocaleString()}`)
console.table(results)
}
/**
* Outputs the polling results to a CSV file.
* @param {Results} results - The polling results.
*/
export function outputToCSV(results: Results): void {
const fileName = 'output.csv'
const csv = papaparse.unparse(resultsToArray(results))
fs.writeFileSync(fileName, csv)
console.log(`Data written to ${fileName}`)
}