Skip to content

Commit

Permalink
fix: add some interfaces and types to fix compile errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderMelde committed Mar 3, 2024
1 parent 11d7aff commit 96f8832
Show file tree
Hide file tree
Showing 19 changed files with 746 additions and 640 deletions.
26 changes: 19 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"build": "cross-env NODE_ENV=production next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@observablehq/plot": "^0.6.13",
"@types/d3": "^7.4.3",
"cross-env": "^7.0.3",
"d3": "^7.8.5",
"d3-dsv": "^3.0.1",
"d3-geo": "^3.1.0",
Expand All @@ -20,14 +21,14 @@
"swr": "^2.2.5"
},
"devDependencies": {
"typescript": "^5",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"autoprefixer": "^10.0.1",
"eslint": "^8",
"eslint-config-next": "14.1.1",
"postcss": "^8",
"tailwindcss": "^3.3.0",
"eslint": "^8",
"eslint-config-next": "14.1.1"
"typescript": "^5"
}
}
77 changes: 77 additions & 0 deletions src/app/api/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
export interface PortalResponse {
help: string,
success: boolean,
result: {
license_title: string,
maintainer: string,
resources: PortalResource[],
}
}

export interface PortalResource {
mimetype: string;
url: string;
}

export interface SensorDataEntry {
time: Date;
min: number;
max: number;
mean: number;
}

export interface SensorDataEntryJSON {
time: string;
min: number;
max: number;
mean: number;
}

export interface FeinstaubDataEntryOriginal {
ID: string;
Datum: string;
Zeit: string;
Breitengrad: string;
Laengengrad: string;
"PM2.5": string;
PM10: string;
temp: string;
humi: string;
pres: string;
WSpeed: string;
WAngle: string;
clouds: string;
}

export interface FeinstaubDataEntry {
ID: string;
Datum: string;
Zeit: string;
Breitengrad: string;
Laengengrad: string;
"PM2.5": string;
PM10: string;
temp: string;
humi: string;
pres: string;
WSpeed: string;
WAngle: string;
clouds: string;
combinedTime: number;
}

export interface GreenHouseGasEntry {
year: string;
category: string;
type: string;
co2: string;
note: string;
}

export interface GreenHouseGasEntryImproved {
year: string;
category: string;
type: string;
co2: number;
note: string;
}
156 changes: 72 additions & 84 deletions src/app/api/test-data/route.ts
Original file line number Diff line number Diff line change
@@ -1,92 +1,80 @@
import * as d3 from "d3";
import * as geo from "d3-geo";
import * as dsv from "d3-dsv";
import { NextRequest, NextResponse } from "next/server";
import {NextRequest, NextResponse} from "next/server";
import {PortalResponse, SensorDataEntry, SensorDataEntryJSON} from "@/app/api/models";

const fmt = dsv.dsvFormat(";");

export async function GET(
req: NextRequest,
req: NextRequest,
): Promise<NextResponse> {
let agg: {
time: Date;
min: number;
max: number;
mean: number;
}[] = [];

/*
TODO: unterschiedliche Tiefe, auch bodenfeuchte, lat & lon.
*/

const f = await fetch(
"https://transparenz.karlsruhe.de/api/3/action/package_show?id=sensordaten-karlsruhe",
).then((x) => x.json());

const urls = f["result"]["resources"].filter((x) => x.mimetype == "text/csv")
.map((x) => x.url);

for (
const url of urls
) {
const response = await fetch(url);

const data = fmt.parse(await response.text());

const data2 = d3.map(
data,
(row) => ({
"bodentemperatur": row["bodentemperatur"],
"combinedTime": Date.parse(
row["Datum"].split("-").toReversed().join("-") + "T" +
row["Uhrzeit"] +
":00.000Z",
),
}),
);

const data3 = d3.map(
data2,
(row) => ({
time: new Date(row["combinedTime"]),
bodentemperatur: Number.parseFloat(
row["bodentemperatur"].replace(",", "."),
),
}),
);

const data4 = d3.filter(
data3,
(row) => row.bodentemperatur < 100 && row.bodentemperatur > -100,
);

const groups = d3.group(
data4,
(row) => d3.utcDay.floor(row.time),
);

const groups2 = d3.map(
groups,
([l, r]) => ({ key: l, val: d3.map(r, (x) => x.bodentemperatur) }),
);

const agg2 = d3.map(
groups2,
(r) => ({
time: r.key,
min: d3.min(r.val)!,
max: d3.max(r.val)!,
mean: d3.mean(r.val)!,
}),
);

agg = [...agg, ...agg2];
}

const agg2 = d3.sort(agg, (l, r) => l.time.getTime() - r.time.getTime());
const agg3 = d3.map(agg2, (x) => ({ ...x, time: x.time.toJSON() }));

return NextResponse.json({
data: agg3,
});
let agg: SensorDataEntry[] = [];

/*
TODO: unterschiedliche Tiefe, auch bodenfeuchte, lat & lon.
*/

const fetchResponse = await fetch("https://transparenz.karlsruhe.de/api/3/action/package_show?id=sensordaten-karlsruhe")
const portalResponse: PortalResponse = await fetchResponse.json();

const portalResourceURLs = portalResponse.result.resources.filter(portalResource => portalResource.mimetype == "text/csv").map(({url}) => url);

for (const url of portalResourceURLs) {
const response = await fetch(url);
const data = fmt.parse(await response.text());

const data2 = d3.map(
data,
(row) => ({
"bodentemperatur": row["bodentemperatur"],
"combinedTime": Date.parse(
row["Datum"].split("-").toReversed().join("-") + "T" +
row["Uhrzeit"] +
":00.000Z",
),
}),
);

const data3 = d3.map(
data2,
(row) => ({
time: new Date(row["combinedTime"]),
bodentemperatur: Number.parseFloat(
row["bodentemperatur"].replace(",", "."),
),
}),
);

const data4 = d3.filter(
data3,
(row) => row.bodentemperatur < 100 && row.bodentemperatur > -100,
);

const groups = d3.group(
data4,
(row) => d3.utcDay.floor(row.time),
);

const groups2 = d3.map(
groups,
([l, r]) => ({key: l, val: d3.map(r, (x) => x.bodentemperatur)}),
);

const agg2 = d3.map(
groups2,
(r) => ({
time: r.key,
min: d3.min(r.val)!,
max: d3.max(r.val)!,
mean: d3.mean(r.val)!,
}),
);

agg = [...agg, ...agg2];
}

const agg2 = d3.sort(agg, (l, r) => l.time.getTime() - r.time.getTime());
const agg3: SensorDataEntryJSON[] = d3.map(agg2, (x) => ({...x, time: x.time.toJSON()}));

return NextResponse.json({data: agg3});
}
Loading

0 comments on commit 96f8832

Please sign in to comment.