-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add some interfaces and types to fix compile errors
- Loading branch information
1 parent
11d7aff
commit 96f8832
Showing
19 changed files
with
746 additions
and
640 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,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; | ||
} |
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 |
---|---|---|
@@ -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}); | ||
} |
Oops, something went wrong.