Skip to content

Commit

Permalink
Refactor data into scope
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchernchong committed Nov 14, 2023
1 parent 3f5388e commit efea5df
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 78 deletions.
58 changes: 58 additions & 0 deletions packages/core/src/car.ts
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.
7 changes: 7 additions & 0 deletions packages/functions/src/car.ts
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()),
}));
73 changes: 0 additions & 73 deletions packages/functions/src/lambda.ts

This file was deleted.

18 changes: 13 additions & 5 deletions stacks/MyStack.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { StackContext, Api, EventBus } from "sst/constructs";

const CUSTOM_DOMAINS: Record<string, any> = {
dev: {
domainName: "dev.api.singapore-ev-trends.ruchern.xyz",
hostedZone: "ruchern.xyz",
},
prod: {
domainName: "api.singapore-ev-trends.ruchern.xyz",
hostedZone: "ruchern.xyz",
},
};

export const api = ({ stack }: StackContext) => {
const bus = new EventBus(stack, "bus", {
defaults: {
Expand All @@ -14,15 +25,12 @@ export const api = ({ stack }: StackContext) => {
bind: [bus],
},
},
customDomain: {
domainName: "api.singapore-ev-trends.ruchern.xyz",
hostedZone: "ruchern.xyz",
},
customDomain: CUSTOM_DOMAINS[stack.stage],
cors: {
allowOrigins: ["https://singapore-ev-trends.ruchern.xyz"],
},
routes: {
"GET /": "packages/functions/src/lambda.handler",
"GET /": "packages/functions/src/car.list",
"GET /todo": "packages/functions/src/todo.list",
"POST /todo": "packages/functions/src/todo.create",
},
Expand Down

0 comments on commit efea5df

Please sign in to comment.