-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.sql
76 lines (66 loc) · 3.47 KB
/
data.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
DROP TABLE TreatmentLog;
DROP TABLE ArrivalLog;
DROP TABLE ResponseLog;
DROP TABLE AlarmLog;
DROP TABLE Users;
-- Table of Users
CREATE TABLE Users (
ID serial PRIMARY KEY,
MongoID varchar UNIQUE,
Username varchar UNIQUE,
LastLogin timestamptz
);
-- Details when Users start an alarm
CREATE TABLE AlarmLog (
ID serial PRIMARY KEY,
UserID varchar NOT NULL,
AlarmStart timestamptz NOT NULL,
AlarmEnd timestamptz,
AlarmSent Boolean,
FOREIGN KEY (UserID) REFERENCES Users(MongoID) ON DELETE CASCADE
);
-- Record responses of Users when they recieve an alert from a friend's alarm timeout
CREATE TABLE ResponseLog (
ID serial PRIMARY KEY,
ResponderID varchar NOT NULL,
AlarmID Integer NOT NULL,
AlertResponse Boolean NOT NULL,
ResponseTime timestamptz NOT NULL,
UNIQUE (ResponderID, AlarmID), -- users respond to an alert only once
FOREIGN KEY (AlarmID) REFERENCES AlarmLog(ID) ON DELETE CASCADE,
FOREIGN KEY (ResponderID) REFERENCES Users(MongoID) ON DELETE CASCADE
);
-- Responders verify when they have arrived to help another user
CREATE TABLE ArrivalLog (
ID serial PRIMARY KEY,
ResponseID Integer NOT NULL UNIQUE, -- users arrive for an alert only once
ArrivalTime timestamptz NOT NULL,
FOREIGN KEY (ResponseID) REFERENCES ResponseLog(ID) ON DELETE CASCADE
);
-- Record if User was saved from an alarm timeout alert
CREATE TABLE TreatmentLog (
ID serial PRIMARY KEY,
ResponseID Integer NOT NULL,
AlertSuccessful Boolean NOT NULL,
TreatmentTime timestamptz NOT NULL,
FOREIGN KEY (ResponseID) REFERENCES ResponseLog(ID) ON DELETE CASCADE
);
/* Uncomment section to fill database with example data
INSERT INTO Users (Username, MongoID, LastLogin) VALUES ('John', 'j', NOW());
INSERT INTO Users (Username, MongoID, LastLogin) VALUES ('Alice', 'a', '2020-01-30 09:45:00-05');
INSERT INTO Users (Username, MongoID, LastLogin) VALUES ('Bob', 'b', '2020-02-01 15:05:00-08');
INSERT INTO Users (Username, MongoID, LastLogin) VALUES ('Charlie', 'c', NOW());
INSERT INTO ALarmLog (UserID, AlarmStart, AlarmEnd, AlarmSent) VALUES ('j', NOW() - INTERVAL '2 minutes', NOW(), TRUE);
INSERT INTO ALarmLog (UserID, AlarmStart, AlarmEnd, AlarmSent) VALUES ('b', '2020-02-01 18:25:00-08', '2020-02-01 18:27:00-08', TRUE);
INSERT INTO ALarmLog (UserID, AlarmStart, AlarmEnd, AlarmSent) VALUES ('a', NOW() - INTERVAL '2 minutes', NOW(), FALSE);
INSERT INTO ResponseLog (ResponderID, AlarmID, AlertResponse, ResponseTime) VALUES ('c', 1, FALSE, NOW());
INSERT INTO ResponseLog (ResponderID, AlarmID, AlertResponse, ResponseTime) VALUES ('b', 1, TRUE, NOW());
INSERT INTO ResponseLog (ResponderID, AlarmID, AlertResponse, ResponseTime) VALUES ('a', 1, TRUE, NOW());
INSERT INTO ResponseLog (ResponderID, AlarmID, AlertResponse, ResponseTime) VALUES ('a', 2, TRUE, '2020-02-01 18:27:36-08');
INSERT INTO ResponseLog (ResponderID, AlarmID, AlertResponse, ResponseTime) VALUES ('c', 2, TRUE, '2020-02-01 18:28:00-08');
INSERT INTO ArrivalLog (ResponseID, ArrivalTIme) VALUES (2, NOW() + INTERVAL '5 minutes');
INSERT INTO ArrivalLog (ResponseID, ArrivalTIme) VALUES (3, NOW() + INTERVAL '17 minutes');
INSERT INTO ArrivalLog (ResponseID, ArrivalTIme) VALUES (4, '2020-02-01 18:39:28-08');
INSERT INTO TreatmentLog (ResponseID, AlertSuccessful, TreatmentTime) VALUES (1, FALSE, NOW());
INSERT INTO TreatmentLog (ResponseID, AlertSuccessful, TreatmentTIme) VALUES (2, FALSE, NOW());
*/