Skip to content

Commit 1a9c572

Browse files
committed
v0.0.1-beta.4
1 parent 0dc1cc2 commit 1a9c572

File tree

4 files changed

+147
-1
lines changed

4 files changed

+147
-1
lines changed

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.0.1-beta.3
1+
v0.0.1-beta.4

ex.sql

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.load dist/http0
2+
.mode box
3+
.timer on
4+
.bail on
5+
6+
create table if not exists snapshots as
7+
select * from http_get() limit 0;
8+
9+
insert into snapshots
10+
select *
11+
from http_get('http://www.ridembl.com:8080/MBLBusTracker/BusTracker/getRouteDetailByRouteID?RouteId=10');
12+
13+
14+
create table vehicle_readings as
15+
select
16+
snapshots.rowid as snapshot,
17+
snapshots.timings ->> '$.body_end' as response_time,
18+
vehicles.value ->> '$.BlockFareboxId' as block_farebox_id,
19+
vehicles.value ->> '$.CommStatus' as comm_status,
20+
vehicles.value ->> '$.Destination' as destination,
21+
vehicles.value ->> '$.Deviation' as deviation,
22+
vehicles.value ->> '$.Direction' as direction,
23+
vehicles.value ->> '$.DirectionLong' as direction_long,
24+
vehicles.value ->> '$.DisplayStatus' as display_status,
25+
vehicles.value ->> '$.StopId' as stop_id,
26+
vehicles.value ->> '$.CurrentStatus' as current_status,
27+
vehicles.value ->> '$.DriverName' as driver_name,
28+
vehicles.value ->> '$.GPSStatus' as gps_status,
29+
vehicles.value ->> '$.Heading' as heading,
30+
vehicles.value ->> '$.LastStop' as last_stop,
31+
vehicles.value ->> '$.LastUpdated' as last_updated,
32+
vehicles.value ->> '$.Latitude' as latitude,
33+
vehicles.value ->> '$.Longitude' as longitude,
34+
vehicles.value ->> '$.Name' as name,
35+
vehicles.value ->> '$.OccupancyStatus' as occupancy_status,
36+
vehicles.value ->> '$.OnBoard' as on_board,
37+
vehicles.value ->> '$.OpStatus' as op_status,
38+
vehicles.value ->> '$.RouteId' as route_id,
39+
vehicles.value ->> '$.RunId' as run_id,
40+
vehicles.value ->> '$.Speed' as speed,
41+
vehicles.value ->> '$.TripId' as trip_id,
42+
vehicles.value ->> '$.VehicleId' as vehicle_id,
43+
vehicles.value ->> '$.SeatingCapacity' as seating_capacity,
44+
vehicles.value ->> '$.TotalCapacity' as total_capacity,
45+
vehicles.value ->> '$.PropertyName' as property_name,
46+
vehicles.value ->> '$.OccupancyStatusReportLabel' as occupancy_status_report_label
47+
from snapshots
48+
join json_each(snapshots.response_body, '$.Vehicles') as vehicles

suncalc.sql

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
.bail on
2+
.mode box
3+
.header on
4+
.load dist/http0
5+
6+
create table cities as
7+
select
8+
value ->> '$.city' as name,
9+
value ->> '$.latitude' as latitude,
10+
value ->> '$.longitude' as longitude,
11+
value ->> '$.population' as population,
12+
value ->> '$.state' as state
13+
from json_each(
14+
http_get_body('https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json')
15+
);
16+
17+
create table christmas_suntimes as
18+
with suntimes as (
19+
select rowid as city,
20+
http_post_body(
21+
'http://localhost:3001/suncalc',
22+
null,
23+
json_object(
24+
'longitude', longitude,
25+
'latitude', latitude,
26+
-- ho ho ho
27+
'date', '2022-12-25'
28+
)
29+
) as suncalc_times
30+
from cities
31+
),
32+
final as (
33+
select
34+
cities.rowid as city,
35+
suncalc_times ->> '$.nadir' as nadir,
36+
suncalc_times ->> '$.nightEnd' as night_end,
37+
suncalc_times ->> '$.nauticalDawn' as nautical_dawn,
38+
suncalc_times ->> '$.dawn' as dawn,
39+
suncalc_times ->> '$.sunrise' as sunrise,
40+
suncalc_times ->> '$.sunriseEnd' as sunrise_end,
41+
suncalc_times ->> '$.goldenHourEnd' as golden_hour_end,
42+
suncalc_times ->> '$.solarNoon' as solar_noon,
43+
suncalc_times ->> '$.goldenHour' as golden_hour,
44+
suncalc_times ->> '$.sunsetStart' as sunset_start,
45+
suncalc_times ->> '$.sunset' as sunset,
46+
suncalc_times ->> '$.dusk' as dusk,
47+
suncalc_times ->> '$.nauticalDusk' as nautical_dusk,
48+
suncalc_times ->> '$.night' as night
49+
from suntimes
50+
left join cities on cities.rowid = suntimes.city
51+
)
52+
select * from final;
53+
54+
select
55+
cities.name,
56+
cities.state,
57+
(unixepoch(sunset) - unixepoch(sunrise)) as daylight_seconds
58+
from christmas_suntimes
59+
left join cities on cities.rowid = christmas_suntimes.city
60+
order by 3 desc
61+
limit 10;
62+
63+
select
64+
cities.name,
65+
cities.state,
66+
(unixepoch(sunset) - unixepoch(sunrise)) as daylight_seconds
67+
from christmas_suntimes
68+
left join cities on cities.rowid = christmas_suntimes.city
69+
order by 3 asc
70+
limit 10;

suncalc.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import suncalc from "https://cdn.skypack.dev/suncalc";
2+
import { serve } from "https://deno.land/[email protected]/http/server.ts";
3+
4+
const port = 3001;
5+
6+
const suntimesRoute = new URLPattern({ pathname: "/suncalc" });
7+
8+
interface SuntimePostParams {
9+
latitude: number;
10+
longitude: number;
11+
date: string;
12+
}
13+
14+
// user POSTs JSON object SuntimePostParams to "/suntimes", return suncalc.getTimes response
15+
async function handleSuntimes(request: Request): Promise<Response> {
16+
const { latitude, longitude, date } =
17+
(await request.json()) as SuntimePostParams;
18+
const times = suncalc.getTimes(new Date(date), latitude, longitude);
19+
return Promise.resolve(new Response(JSON.stringify(times), { status: 200 }));
20+
}
21+
22+
await serve(
23+
(request: Request): Promise<Response> => {
24+
if (suntimesRoute.test(request.url)) return handleSuntimes(request);
25+
return Promise.resolve(new Response("not found :(", { status: 404 }));
26+
},
27+
{ port }
28+
);

0 commit comments

Comments
 (0)