-
-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/migrate boxes sense box id locations #650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
108d823
feat: create response utils
76116e6
feat: add response types to api.boxes.deviceId.data.sensorId
7931d26
fix: replace tab with space
2c826e5
feat: implement location endpoint
cb9fa81
feat: only get required locations from database
fd950a4
feat: unit tests
e32dbab
fix: lint
b342aa0
feat: delete unused method
9eb292e
feat: delete outdated comment
d7f60b8
fix: remove duplicated whitespace
e9e6832
fix: delete measurements for all test times
40a2b87
fix: remove eslint ignore comment
aaf1eec
fix: lint
394c1f5
Merge branch 'dev' into feat/migrate-boxes-senseBoxId-locations
timber-they 110f6e1
feat: remove TODO
7584a06
Merge branch 'feat/migrate-boxes-senseBoxId-locations' of github.com:…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,182 @@ | ||
| import { type Params, type LoaderFunction, type LoaderFunctionArgs } from "react-router"; | ||
| import { type TransformedMeasurement } from "~/lib/outlier-transform"; | ||
| import { getLocations } from "~/models/device.server"; | ||
| import { type Measurement } from "~/schema"; | ||
| import { convertToCsv } from "~/utils/csv"; | ||
| import { parseDateParam, parseEnumParam } from "~/utils/param-utils"; | ||
| import { badRequest, internalServerError, notFound } from "~/utils/response-utils"; | ||
|
|
||
| /** | ||
| * @openapi | ||
| * /boxes/{deviceId}/locations: | ||
| * get: | ||
| * tags: | ||
| * - Boxes | ||
| * summary: Get locations of a senseBox | ||
| * description: Get all locations of the specified senseBox ordered by date as an array of GeoJSON Points. | ||
| * If `format=geojson`, a GeoJSON linestring will be returned, with `properties.timestamps` | ||
| * being an array with the timestamp for each coordinate. | ||
| * parameters: | ||
| * - in: path | ||
| * name: deviceId | ||
| * required: true | ||
| * schema: | ||
| * type: string | ||
| * description: the ID of the senseBox you are referring to | ||
| * - in: query | ||
| * name: from-date | ||
| * required: false | ||
| * schema: | ||
| * type: string | ||
| * description: RFC3339Date | ||
| * format: date-time | ||
| * description: "Beginning date of measurement data (default: 48 hours ago from now)" | ||
| * - in: query | ||
| * name: to-date | ||
| * required: false | ||
| * schema: | ||
| * type: string | ||
| * descrption: TFC3339Date | ||
| * format: date-time | ||
| * description: "End date of measurement data (default: now)" | ||
| * - in: query | ||
| * name: format | ||
| * required: false | ||
| * schema: | ||
| * type: string | ||
| * enum: | ||
| * - json | ||
| * - geojson | ||
| * default: json | ||
| * description: "Can be 'json' (default) or 'geojson' (default: json)" | ||
| * responses: | ||
| * 200: | ||
| * description: Success | ||
| * content: | ||
| * application/json: | ||
| * schema: | ||
| * type: array | ||
| * example: '[{ "coordinates": [7.68123, 51.9123], "type": "Point", "timestamp": "2017-07-27T12:00.000Z"},{ "coordinates": [7.68223, 51.9433, 66.6], "type": "Point", "timestamp": "2017-07-27T12:01.000Z"},{ "coordinates": [7.68323, 51.9423], "type": "Point", "timestamp": "2017-07-27T12:02.000Z"}]' | ||
| * application/geojson: | ||
| * example: '' | ||
| * 400: | ||
| * description: Bad Request | ||
| * content: | ||
| * application/json: | ||
| * schema: | ||
| * type: object | ||
| * properties: | ||
| * error: | ||
| * type: string | ||
| * message: | ||
| * type: string | ||
| * 404: | ||
| * description: Not found | ||
| * content: | ||
| * application/json: | ||
| * schema: | ||
| * type: object | ||
| * properties: | ||
| * error: | ||
| * type: string | ||
| * message: | ||
| * type: string | ||
| * 500: | ||
| * description: Internal Server Error | ||
| * content: | ||
| * application/json: | ||
| * schema: | ||
| * type: object | ||
| * properties: | ||
| * error: | ||
| * type: string | ||
| * message: | ||
| * type: string | ||
| */ | ||
|
|
||
| export const loader: LoaderFunction = async ({ | ||
| request, | ||
| params, | ||
| }: LoaderFunctionArgs): Promise<Response> => { | ||
| try { | ||
|
|
||
| const collected = collectParameters(request, params); | ||
| if (collected instanceof Response) | ||
| return collected; | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const {deviceId, fromDate, toDate, format} = collected; | ||
|
|
||
| const locations = await getLocations({ id: deviceId}, fromDate, toDate); | ||
| if (!locations) | ||
| return notFound("Device not found"); | ||
|
|
||
| const jsonLocations = locations.map((location) => { | ||
| return { | ||
| coordinates: [location.x, location.y], | ||
| type: 'Point', | ||
| timestamp: location.time, | ||
| } | ||
| }); | ||
|
|
||
| let headers: HeadersInit = { | ||
| "content-type": format == "json" ? "application/json; charset=utf-8" : "application/geo+json; charset=utf-8", | ||
| }; | ||
|
|
||
| const responseInit: ResponseInit = { | ||
| status: 200, | ||
| headers: headers, | ||
| }; | ||
|
|
||
| if (format == "json") | ||
| return Response.json(jsonLocations, responseInit); | ||
| else { | ||
| const geoJsonLocations = { | ||
| type: 'Feature', | ||
| geometry: { | ||
| type: 'LineString', coordinates: jsonLocations.map(location => location.coordinates) | ||
| }, | ||
| properties: { | ||
| timestamps: jsonLocations.map(location => location.timestamp) | ||
| } | ||
| }; | ||
| return Response.json(geoJsonLocations, responseInit) | ||
| } | ||
|
|
||
| } catch (err) { | ||
| console.warn(err); | ||
| return internalServerError(); | ||
| } | ||
| }; | ||
|
|
||
| function collectParameters(request: Request, params: Params<string>): | ||
| Response | { | ||
| deviceId: string, | ||
| fromDate: Date, | ||
| toDate: Date, | ||
| format: string | null | ||
| } { | ||
| const deviceId = params.deviceId; | ||
| if (deviceId === undefined) | ||
| return badRequest("Invalid device id specified"); | ||
|
|
||
| const url = new URL(request.url); | ||
|
|
||
| const fromDate = parseDateParam(url, "from-date", new Date(new Date().setDate(new Date().getDate() - 2))) | ||
| if (fromDate instanceof Response) | ||
| return fromDate | ||
|
|
||
| const toDate = parseDateParam(url, "to-date", new Date()) | ||
| if (toDate instanceof Response) | ||
| return toDate | ||
|
|
||
| const format = parseEnumParam(url, "format", ["json", "geojson"], "json"); | ||
| if (format instanceof Response) | ||
| return format | ||
|
|
||
| return { | ||
| deviceId, | ||
| fromDate, | ||
| toDate, | ||
| format | ||
| }; | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.