-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
200 lines (159 loc) · 5.9 KB
/
schema.sql
File metadata and controls
200 lines (159 loc) · 5.9 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
CREATE TABLE IF NOT EXISTS uploads (
id TEXT PRIMARY KEY,
given_name TEXT,
platform TEXT,
upload_timestamp REAL,
updated_at REAL,
color TEXT
);
CREATE TABLE IF NOT EXISTS uploaded_files ( -- filled during extraction step
id TEXT PRIMARY KEY,
manifest_file_id TEXT,
upload_id TEXT,
opfs_filename TEXT,
manifest_filename TEXT,
file_hash TEXT,
upload_timestamp REAL,
file_size_bytes INTEGER,
parse_status TEXT,
FOREIGN KEY(upload_id) REFERENCES uploads(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS raw_data ( -- filled during extraction step
id TEXT PRIMARY KEY,
upload_id TEXT,
file_id TEXT,
data JSONTEXT,
FOREIGN KEY(upload_id) REFERENCES uploads(id) ON DELETE CASCADE,
FOREIGN KEY(file_id) REFERENCES uploaded_files(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS events ( -- filled during semantic map
id TEXT PRIMARY KEY,
upload_id TEXT,
file_ids JSONTEXT, -- multiple possible after deduplication
raw_data_ids JSONTEXT, -- can be multiple raw data entries that map to the same event, stored as JSON list of raw_data ids
--
timestamp REAL,
event_action TEXT,
event_kind TEXT,
event_category JSONTEXT DEFAULT '[]',
event_type JSONTEXT DEFAULT '[]',
--
message TEXT,
attributes JSONTEXT, --
tags JSONTEXT DEFAULT "[]",
labels JSONTEXT DEFAULT "[]",
--
deduplicated BOOLEAN DEFAULT 0,
extra_timestamps JSONTEXT DEFAULT "[]",
--
FOREIGN KEY(upload_id) REFERENCES uploads(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS event_comments (
id TEXT PRIMARY KEY,
event_id TEXT,
comment TEXT,
created_at REAL,
updated_at REAL,
FOREIGN KEY(event_id) REFERENCES events(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS auth_devices_initial ( -- filled during semantic map
id TEXT PRIMARY KEY,
upload_id TEXT,
file_id TEXT,
raw_data_id TEXT,
--
entity_type TEXT,
event_kind TEXT,
event_category JSONTEXT DEFAULT '[]',
--
attributes JSONTEXT,
--
FOREIGN KEY(upload_id) REFERENCES uploads(id) ON DELETE CASCADE,
FOREIGN KEY(file_id) REFERENCES uploaded_files(id) ON DELETE CASCADE,
FOREIGN KEY(raw_data_id) REFERENCES raw_data(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS auth_devices ( -- hard merge based on static device identifiers. user cannot edit this.
id TEXT PRIMARY KEY,
upload_ids JSONTEXT NOT NULL, -- JSON list of uploads that contributed to this merged device
file_ids JSONTEXT NOT NULL, -- ^^ for uploaded_files.id
auth_devices_initial_ids JSONTEXT NOT NULL, -- ^^ for auth_devices_initial.id
--
attributes JSONTEXT -- merged attributes
);
CREATE TABLE IF NOT EXISTS device_groups (
id TEXT PRIMARY KEY,
auth_devices_ids JSONTEXT NOT NULL, -- JSON list of auth_devices.id that are in this cluster
--
initial_soft_merge BOOLEAN DEFAULT 0,
soft_merge_flag_status TEXT DEFAULT "na", -- "na" | "shown" | "user_confirmed" | "user_rejected"
--
created_at REAL,
updated_at REAL,
--
tags JSONTEXT DEFAULT "[]",
labels JSONTEXT DEFAULT "[]"
);
CREATE TABLE IF NOT EXISTS device_group_comments (
id TEXT PRIMARY KEY,
device_group_id TEXT,
comment TEXT,
created_at REAL,
updated_at REAL,
FOREIGN KEY(device_group_id) REFERENCES device_groups(id) ON DELETE CASCADE
);
-- CREATE TABLE IF NOT EXISTS device_group_history (
-- id TEXT PRIMARY KEY,
-- device_group_id TEXT NOT NULL,
-- timestamp REAL,
-- action TEXT, -- "added" | "removed"
-- auth_device_ids JSONTEXT, -- elements added/removed
-- origin TEXT, -- "initial_system_group" | "user"
-- );
-- CREATE TABLE IF NOT EXISTS device_clusters ( -- filled during device grouping
-- id TEXT PRIMARY KEY,
-- upload_id TEXT NOT NULL,
-- label TEXT, -- user-editable display name
-- source_ids JSONTEXT NOT NULL, -- JSON list of auth_devices_initial.id
-- attributes JSONTEXT, -- merged best-guess attribute set
-- match_type TEXT NOT NULL, -- "hard" | "soft_suggested" | "user_confirmed" | "user_split"
-- merged_into TEXT, -- self-ref: if this cluster was merged into another
-- created_at REAL,
-- FOREIGN KEY(upload_id) REFERENCES uploads(id) ON DELETE CASCADE,
-- FOREIGN KEY(merged_into) REFERENCES device_clusters(id)
-- );
-----------------------------------------
-------- VIEWS --------
-----------------------------------------
-- view for Events Mappings
CREATE VIEW IF NOT EXISTS v_event_field_mappings AS
-- static columns
SELECT 'id' AS field, 'text' AS type
UNION SELECT 'timestamp', 'timestamp'
UNION SELECT 'message', 'text'
UNION SELECT 'event_category', 'category'
UNION SELECT 'event_action', 'text'
UNION SELECT 'event_kind', 'category'
UNION SELECT 'platform', 'text'
UNION
-- dynamic from JSON attributes
SELECT DISTINCT key AS field, 'text' AS type
FROM events, json_each(events.attributes)
WHERE events.attributes IS NOT NULL AND events.attributes != '';
-- view for Auth Devices Mappings
CREATE VIEW IF NOT EXISTS v_device_field_mappings AS
-- static columns
SELECT 'id' AS field, 'text' AS type
UNION SELECT 'entity_type', 'category'
UNION SELECT 'event_kind', 'category'
UNION SELECT 'event_category', 'category'
UNION SELECT 'platform', 'text'
UNION
-- dynamic from JSON attributes
SELECT DISTINCT key AS field, 'text' AS type
FROM auth_devices_initial, json_each(auth_devices_initial.attributes)
WHERE auth_devices_initial.attributes IS NOT NULL AND auth_devices_initial.attributes != '';
-- all event action types
CREATE VIEW IF NOT EXISTS v_event_actions AS
SELECT DISTINCT event_action
FROM events
WHERE event_action IS NOT NULL AND event_action != '';