diff --git a/options.json.example b/options.json.example index fa6d516..6bbd27f 100644 --- a/options.json.example +++ b/options.json.example @@ -2,4 +2,5 @@ "uberPhoneNumber": "1234567891", "startDateIso": "2023-01-01", "endDateIso": "2023-12-31" + "uberPath": "./uberResults.json" } diff --git a/src/generateCSV.js b/src/generateCSV.js new file mode 100644 index 0000000..13e1dca --- /dev/null +++ b/src/generateCSV.js @@ -0,0 +1,41 @@ +import fs from "fs" +import * as utils from "./utils.js" + +function extractValuesAndLabels(data) { + const result = [] + + function traverse(obj) { + if (obj?.value) { + result.push({ value: obj.value / 100000, label: obj.label }) + } else if (typeof obj === "object" && obj !== null) { + for (const key in obj) { + if (obj.hasOwnProperty(key)) { + traverse(obj[key]) + } + } + } + } + + traverse(data) + return result +} + +async function main() { + let options = JSON.parse(fs.readFileSync("./options.json", "utf8")) + let uberResults = JSON.parse( + fs.readFileSync(options.uberPath ?? "./uberResults.json", "utf8"), + ) + let processedData = uberResults + .filter((res) => res) + .map((result) => { + result.values = extractValuesAndLabels(result.unparsedData) + delete result.unparsedData + if (!result.values.length) { + delete result.values + } + return result + }) + fs.writeFileSync("./resultsProcessed.json", JSON.stringify(processedData)) +} + +main()