-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f5388e
commit efea5df
Showing
7 changed files
with
78 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
export * as Car from "./car"; | ||
import fetch from "node-fetch"; | ||
import fs from "fs"; | ||
import AdmZip from "adm-zip"; | ||
import * as d3 from "d3"; | ||
import { Car } from "./types"; | ||
import { FUEL_TYPE } from "./config"; | ||
import { sortByMake } from "./lib/sortByMake"; | ||
|
||
export const list = async () => { | ||
const tempDir: string = "/tmp"; | ||
const zipFileName: string = `Monthly New Registration of Cars by Make.zip`; | ||
const zipFilePath: string = `${tempDir}/${zipFileName}`; | ||
const csvFileName: string = `M03-Car_Regn_by_make.csv`; | ||
const csvFilePath: string = `${tempDir}/${csvFileName}`; | ||
const zipUrl: string = `https://datamall.lta.gov.sg/content/dam/datamall/datasets/Facts_Figures/Vehicle Registration/${zipFileName}`; | ||
|
||
const response = await fetch(zipUrl); | ||
if (!response.ok) { | ||
throw new Error(`Failed to download the ZIP file: ${response.statusText}`); | ||
} | ||
const data = await response.buffer(); | ||
fs.writeFileSync(zipFilePath, data); | ||
|
||
const zip = new AdmZip(zipFilePath); | ||
zip.extractAllTo(`${tempDir}`, true); | ||
|
||
const csvData = fs.readFileSync(csvFilePath, "utf-8"); | ||
const parsedData = d3.csvParse(csvData); | ||
|
||
const electricCars: Car[] = parsedData | ||
.filter( | ||
({ fuel_type, number }) => | ||
fuel_type === FUEL_TYPE.ELECTRIC && +number !== 0, | ||
) | ||
.reduce((result: Car[], { month, make, fuel_type, number }) => { | ||
const existingCar = result.find( | ||
(car) => car.month === month && car.make === make, | ||
); | ||
|
||
if (existingCar) { | ||
existingCar.number += Number(number); | ||
} else { | ||
result.push({ | ||
month, | ||
make, | ||
fuel_type, | ||
number: Number(number), | ||
}); | ||
} | ||
|
||
return result; | ||
}, []) | ||
.map((car) => ({ ...car, number: +car.number })) | ||
.sort(sortByMake); | ||
|
||
return electricCars; | ||
}; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,7 @@ | ||
import { ApiHandler } from "sst/node/api"; | ||
import { Car } from "@lta-datasets-updater/core/car"; | ||
|
||
export const list = ApiHandler(async (_evt) => ({ | ||
statusCode: 200, | ||
body: JSON.stringify(await Car.list()), | ||
})); |
This file was deleted.
Oops, something went wrong.
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