Skip to content

Commit

Permalink
Merge pull request #335 from IMLS/334-remove-timezone-field
Browse files Browse the repository at this point in the history
removed timezone field per meeting 12.16.22
  • Loading branch information
Ben Klaas authored Jan 12, 2023
2 parents 7e5567a + cc30786 commit e679b22
Show file tree
Hide file tree
Showing 11 changed files with 449,675 additions and 113 deletions.
11 changes: 2 additions & 9 deletions imls-backend/init/120-tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,15 @@ imlswifi.libraries (
fscs_id character varying(16) PRIMARY KEY NOT NULL
);

CREATE TABLE IF NOT EXISTS
imlswifi.imls_lookup (
id SERIAL PRIMARY KEY,
fscs_id character varying(16) NOT NULL REFERENCES imlswifi.libraries(fscs_id),
timezone time with time zone NOT NULL
);

CREATE TABLE IF NOT EXISTS
imlswifi.presences (
presence_id SERIAL PRIMARY KEY,
start_time timestamp with time zone NOT NULL,
end_time timestamp with time zone NOT NULL,
fscs_id character varying(16) NOT NULL REFERENCES imlswifi.libraries(fscs_id),
timezone character varying(6) NOT NULL,
fscs_id character varying(16) NOT NULL REFERENCES imlswifi.libraries(fscs_id)
-- FIXME: what about our tag provided by the library IT director?
-- We're missing something, but it should not be a UID of some sort.
manufacturer_index integer
);

CREATE TABLE IF NOT EXISTS
Expand Down
13 changes: 0 additions & 13 deletions imls-backend/init/130-views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,6 @@ CREATE VIEW admin.libraries AS
SELECT libraries.fscs_id
FROM imlswifi.libraries;


--
-- Name: imls_lookup; Type: VIEW; Schema: api; Owner: -
--
-- NOTE: What is the difference, really, between these two?
CREATE VIEW api.imls_lookup AS SELECT * FROM imlswifi.imls_lookup;
-- CREATE VIEW api.imls_lookup AS
-- SELECT imls_lookup.id,
-- imls_lookup.fscs_id,
-- imls_lookup.timezone
-- FROM imlswifi.imls_lookup;


--
-- Name: presences; Type: VIEW; Schema: api; Owner: -
--
Expand Down
26 changes: 1 addition & 25 deletions imls-backend/init/300-public_api.sql
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ DECLARE
_min_minutes INT := 5;
_max_minutes INT := 600;
BEGIN
SELECT api.get_timezone_from_fscs_id(_fscs_id) INTO _timezone_offset;
SELECT SUBSTRING(timezone,1,3)::INTEGER FROM api.presences WHERE fscs_id = _fscs_id ORDER BY presence_id LIMIT 1 INTO _timezone_offset;
_hour := _hour - _timezone_offset;
_day_end := _day_end - _timezone_offset;

Expand Down Expand Up @@ -87,30 +87,6 @@ BEGIN
END
$$;


--
-- Name: get_timezone_from_fscs_id(text); Type: FUNCTION; Schema: api; Owner: -
--

CREATE FUNCTION api.get_timezone_from_fscs_id(_fscs_id text) RETURNS integer
LANGUAGE plpgsql IMMUTABLE
AS $$
DECLARE
_timezone TIMETZ;
_timezone_offset INT:=0;
BEGIN
SELECT imls_lookup.timezone::TIMETZ INTO _timezone::TIMETZ
FROM api.imls_lookup
WHERE imls_lookup.fscs_id = _fscs_id;

_timezone_offset := extract(timezone_hour FROM _timezone::TIMETZ);
SELECT extract(timezone_hour FROM _timezone::TIMETZ) INTO _timezone_offset;

RETURN _timezone_offset;
END
$$;


-- migrate:down


Expand Down
10 changes: 6 additions & 4 deletions imls-backend/init/320-authenticated_api.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ $BODY$;

-- TODO: validate fscs based on the api key, instead of relying on senders to send their fscs id
CREATE OR REPLACE FUNCTION api.update_presence(
_start timestamptz,
_end timestamptz)
_start character varying(15),
_end character varying(15))
RETURNS character varying
LANGUAGE 'plpgsql'

AS $BODY$
DECLARE
_claim_fscs_id CHARACTER VARYING := current_setting('request.jwt.claims', true)::json->>'fscs_id';
BEGIN
INSERT INTO imlswifi.presences(start_time, end_time, fscs_id, manufacturer_index)
VALUES(_start, _end, _claim_fscs_id, 0);
INSERT INTO imlswifi.presences(start_time, end_time, timezone, fscs_id)
VALUES(_start::timestamptz, _end::timestamptz, RIGHT(_start, 6), _claim_fscs_id);
RETURN _claim_fscs_id;
END;
$BODY$;


CREATE OR REPLACE FUNCTION api.verify_presence(
_fscs_id character varying(16),
_start timestamptz,
Expand Down
4 changes: 1 addition & 3 deletions imls-backend/init/400-permissions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
GRANT USAGE ON SCHEMA api TO web_anon;
GRANT USAGE ON SCHEMA data TO web_anon;
GRANT SELECT ON TABLE data.imls_data TO web_anon;

GRANT SELECT ON TABLE api.imls_lookup TO web_anon;
GRANT SELECT ON TABLE api.presences TO web_anon;

-- Public
Expand All @@ -22,7 +20,7 @@ GRANT EXECUTE ON FUNCTION api.verify_presence(character varying, timestamptz, ti
-- Private

GRANT EXECUTE ON FUNCTION api.beat_the_heart(character varying, character varying) TO sensor;
GRANT EXECUTE ON FUNCTION api.update_presence(timestamp with time zone, timestamp with time zone) TO sensor;
GRANT EXECUTE ON FUNCTION api.update_presence(character varying, character varying) TO sensor;
GRANT SELECT, INSERT ON imlswifi.heartbeats TO sensor;
GRANT USAGE ON SCHEMA api TO sensor;
GRANT USAGE ON SCHEMA imlswifi TO sensor;
Expand Down
449,629 changes: 449,629 additions & 0 deletions imls-backend/test/durations_v2.csv

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions imls-backend/test/setup-for-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ psql ${DATABASE_URL} -v ON_ERROR_STOP=0 <<-EOSQL
INSERT INTO basic_auth.users VALUES ('KY0069-002', 'hello-goodbye', 'sensor') ON CONFLICT DO NOTHING;
EOSQL

psql ${DATABASE_URL} -v ON_ERROR_STOP=0 -f test-data.sql

psql ${DATABASE_URL} -v ON_ERROR_STOP=0 <<-EOSQL
INSERT INTO imlswifi.imls_lookup (fscs_id, timezone) (
SELECT DISTINCT fscs_id,'00:00:00-04'::TIMETZ FROM imlswifi.libraries
);
EOSQL
psql ${DATABASE_URL} -v ON_ERROR_STOP=0 -f test-data-load.sql

psql ${DATABASE_URL} -v ON_ERROR_STOP=0 <<-EOSQL
NOTIFY pgrst, 'reload schema';
Expand Down
28 changes: 28 additions & 0 deletions imls-backend/test/test-data-load.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
CREATE TABLE IF NOT EXISTS
public.durations_v2 (
id INTEGER PRIMARY KEY,
pi_serial character varying(16),
fcfs_seq_id character varying(16),
device_tag character varying(32),
session_id character varying(255),
patron_index integer,
start text,
"end" text,
timezone character varying(6)
);

\COPY public.durations_v2 (id, pi_serial, fcfs_seq_id, device_tag, session_id, patron_index, start, "end", timezone) FROM 'durations_v2.csv' csv;

INSERT INTO imlswifi.libraries
(SELECT DISTINCT(fcfs_seq_id) FROM durations_v2);

INSERT INTO imlswifi.libraries(fscs_id) values ('KY0069-002');

INSERT INTO imlswifi.sensors(fscs_id)
(SELECT DISTINCT fcfs_seq_id AS fscs_id
FROM durations_v2);

INSERT INTO imlswifi.presences(start_time, end_time, fscs_id, timezone)
(SELECT to_timestamp(start::int) AS start_time, to_timestamp("end"::int) AS end_time, fcfs_seq_id AS fscs_id,
timezone AS timezone
FROM durations_v2 d, imlswifi.sensors s);
14 changes: 0 additions & 14 deletions imls-backend/test/test_public_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ def endpoint(ep_arr):
return test_url + "/" + "/".join(ep_arr)


class IMLSTests(TestCase):
def test_existence_of_libraries_in_imls_lookup_table(self):
url = endpoint(["imls_lookup"])
response = requests.get(url)
# If we don't see a 200 response, that's just plain bad.
if response.status_code != 200:
print(response.json())
self.assertTrue(response.status_code == 200)
items = response.json()
# We should always see multiple libraries here, even in production.
# If we don't that means something is very broken.
self.assertTrue(len(items) > 0)


class PresencesTests(TestCase):
# NOTE: There's a ?limit parameter on these, because otherwise a lot of
# values come back by default, and that makes for slow tests.
Expand Down
36 changes: 0 additions & 36 deletions imls-backend/test/test_timezone_lookup.py

This file was deleted.

9 changes: 7 additions & 2 deletions imls-backend/test/test_update_presences.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
import os
import requests
from unittest import TestCase
Expand All @@ -11,10 +11,13 @@ def endpoint(ep_arr):


class PresencesTests(TestCase):
now = datetime.now()
now = datetime.now(timezone.utc).replace(microsecond=0)
end = now + timedelta(minutes=5)
now = now.astimezone().isoformat()
end = end.astimezone().isoformat()

def test_post_random_presence(self):

token_url = endpoint(["rpc", "login"])
body = {"fscs_id": "KY0069-002", "api_key": "hello-goodbye"}
tr = requests.post(token_url, json=body)
Expand All @@ -32,12 +35,14 @@ def test_post_random_presence(self):
self.assertEqual(r.json(), "KY0069-002")

def test_verify_insertion(self):

verify_insert_url = endpoint(["rpc", "verify_presence"])
params = {
"_fscs_id": "KY0069-002",
"_start": str(self.now),
"_end": str(self.end),
}

headers = {"Prefer": "count=estimated"}
rv = requests.post(verify_insert_url, headers=headers, json=params)
self.assertEqual(rv.status_code, 200)
Expand Down

0 comments on commit e679b22

Please sign in to comment.