You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* DB and libpqxx updates
* add lifetime field to RootPlots and PlotlyPlots tables. Default retention to last 5
* make postgres install directory general, add ordering to indices, add devices table, although referential integrity of device configs is not yet enabled
---------
Co-authored-by: Marcus O'Flaherty <moflaherty1@sheffield.ac.uk>
Co-authored-by: Marcus O'Flaherty <marcus.o-flaherty@warwick.ac.uk>
Co-authored-by: marcus <root@dbServermoflaher@warwick.ac.uk>
psql -ddaq -c "ALTER DATABASE daq SET TIME ZONE 'UTC';"
61
81
62
-
echo"creating monitoring table"
63
-
psql -ddaq -c "create table monitoring (time timestamp with time zone NOT NULL, device text NOT NULL, subject text NOT NULL, data JSONB NOT NULL);"
82
+
# TODO FIXME To optimize storage and minimize wasted space due to alignment padding,
83
+
# it's advisable to arrange columns in the table definition from largest to smallest data type.
64
84
65
-
echo"creating indices on device name and subject"
66
-
psql -ddaq -c "CREATE INDEX ON monitoring (device) WITH (deduplicate_items = on);"
67
-
# this may be more efficient as we're unlikely to search by subject alone...
68
-
psql -ddaq -c "CREATE INDEX ON monitoring (device, subject) WITH (deduplicate_items = on);"
85
+
echo"creating users table"
86
+
psql -ddaq -c "create table users (user_id serial NOT NULL, username text NOT NULL, password_hash text NOT NULL, permissions jsonb, UNIQUE (user_id));"
87
+
88
+
echo"creating run_info table"
89
+
psql -ddaq -c "create table run_info (run int NOT NULL, subrun int NOT NULL, start_time timestamp with time zone NOT NULL, stop_time timestamp with time zone, config_id int NOT NULL, comments text NOT NULL, UNIQUE (run, subrun));"
90
+
91
+
echo"creating index on run_info table"
92
+
psql -ddaq -c "CREATE INDEX ON run_info (run DESC NULLS LAST)"
93
+
94
+
echo"creating run_config table"
95
+
psql -ddaq -c "create table run_config (config_id serial NOT NULL primary key, time timestamp with time zone NOT NULL DEFAULT now(), name text NOT NULL, version int NOT NULL, description text NOT NULL, author text NOT NULL, data jsonb NOT NULL, UNIQUE (name, version) );"
96
+
97
+
echo"creating autoincrement function for run_config version"
98
+
psql -ddaq -c 'create or replace function "fn_config_ver"() returns "pg_catalog"."trigger" as $BODY$ begin new.version = (select COALESCE(MAX(version)+1,0) from run_config where name=new.name); return NEW; end; $BODY$ LANGUAGE plpgsql VOLATILE COST 100;'
99
+
psql -ddaq -c 'CREATE TRIGGER trig_config_ver BEFORE insert ON run_config FOR EACH ROW EXECUTE PROCEDURE fn_config_ver();'
100
+
101
+
echo"creating devices table"
102
+
psql -ddaq -c "CREATE TABLE devices (name text not null);"
103
+
psql -ddaq -c "CREATE UNIQUE INDEX dev_name_idx ON devices(LOWER(name));"
104
+
105
+
echo"creating device_config table"
106
+
# TODO enable below to require device_configs reference a device in the devices table
107
+
#psql -ddaq -c "create table device_config (time timestamp with time zone NOT NULL DEFAULT now(), device text references devices(name), version int NOT NULL, author text NOT NULL, description text NOT NULL, data jsonb NOT NULL, UNIQUE (device, version) );"
108
+
psql -ddaq -c "create table device_config (time timestamp with time zone NOT NULL DEFAULT now(), device text NOT NULL, version int NOT NULL, author text NOT NULL, description text NOT NULL, data jsonb NOT NULL, UNIQUE (device, version) );"
109
+
110
+
echo"creating index on device_config table"
111
+
psql -ddaq -c "CREATE INDEX ON device_config (device, version DESC NULLS LAST)"
112
+
113
+
echo"creating autoincrement function for device_config version"
114
+
psql -ddaq -c 'create or replace function "fn_devconfig_ver"() returns "pg_catalog"."trigger" as $BODY$ begin new.version = (select COALESCE(MAX(version)+1,0) from device_config where device=new.device); return NEW; end; $BODY$ LANGUAGE plpgsql VOLATILE COST 100;'
115
+
psql -ddaq -c 'CREATE TRIGGER trig_devconfig_ver BEFORE insert ON device_config FOR EACH ROW EXECUTE PROCEDURE fn_devconfig_ver();'
116
+
117
+
echo"creating calibration table"
118
+
psql -ddaq -c "create table calibration (time timestamp with time zone NOT NULL DEFAULT now(), name text NOT NULL, version int NOT NULL, description text NOT NULL, data jsonb NOT NULL, UNIQUE (name, version) );"
119
+
120
+
echo"creating index on calibration table"
121
+
psql -ddaq -c "CREATE INDEX ON calibration (name, version DESC NULLS LAST)"
122
+
123
+
echo"creating autoincrement function for calibration version"
124
+
psql -ddaq -c 'create or replace function "fn_calibration_ver"() returns "pg_catalog"."trigger" as $BODY$ begin new.version = (select COALESCE(MAX(version)+1,0) from calibration where name=new.name); return NEW; end; $BODY$ LANGUAGE plpgsql VOLATILE COST 100;'
125
+
psql -ddaq -c 'CREATE TRIGGER trig_calibration_ver BEFORE insert ON calibration FOR EACH ROW EXECUTE PROCEDURE fn_calibration_ver();'
69
126
70
127
echo"creating logging table"
71
-
psql -ddaq -c "create table logging (time timestamp with time zone NOT NULL, device text NOT NULL, severity integer NOT NULL, message text NOT NULL);"
128
+
psql -ddaq -c "create table logging (time timestamp with time zone NOT NULL DEFAULT now(), device text NOT NULL, severity integer NOT NULL, message text NOT NULL);"
72
129
73
-
echo"creating indices on device name and message severity"
130
+
echo"creating indices on logging device name and message severity"
74
131
psql -ddaq -c "CREATE INDEX ON logging (device) WITH (deduplicate_items = on);"
75
-
# as above. is anyone really going to want 'high priority logs from any device'? i doubt it
76
132
psql -ddaq -c "CREATE INDEX ON logging (device,severity) WITH (deduplicate_items = on);"
77
133
134
+
echo"creating monitoring table"
135
+
psql -ddaq -c "create table monitoring (time timestamp with time zone NOT NULL DEFAULT now(), device text NOT NULL, subject text NOT NULL, data jsonb NOT NULL);"
136
+
137
+
echo"creating indices on monitoring device name and subject"
138
+
psql -ddaq -c "CREATE INDEX ON monitoring (device) WITH (deduplicate_items = on);"
139
+
psql -ddaq -c "CREATE INDEX ON monitoring (device, subject) WITH (deduplicate_items = on);"
140
+
78
141
echo"creating alarms table"
79
-
psql -ddaq -c "create table alarms (time timestamp with time zone NOT NULL, device text NOT NULL, level integer NOT NULL, alarm text NOT NULL, silenced integer DEFAULT 0 );"
142
+
psql -ddaq -c "create table alarms (time timestamp with time zone NOT NULL DEFAULT now(), device text NOT NULL, level integer NOT NULL, alarm text NOT NULL, silenced integer DEFAULT 0 );"
80
143
81
-
echo"creating device_config table"
82
-
psql -ddaq -c "create table device_config (time timestamp with time zone NOT NULL, device text NOT NULL, version int NOT NULL, author text NOT NULL, description text NOT NULL, data JSONB NOT NULL, UNIQUE (device, version));"
144
+
echo"creating rootplots table"
145
+
psql -ddaq -c "create table rootplots (time timestamp with time zone NOT NULL DEFAULT now(), name text NOT NULL, version int NOT NULL, data jsonb NOT NULL, draw_options text NOT NULL DEFAULT '', lifetime int NOT NULL DEFAULT 5, UNIQUE (name, version));"
83
146
84
-
echo"creating calibration table"
85
-
psql -ddaq -c "create table calibration (time timestamp with time zone NOT NULL, device text NOT NULL, version int NOT NULL, description text NOT NULL, data JSONB NOT NULL, UNIQUE (device, version) );"
147
+
echo"creating autoincrement function for rootplots version"
148
+
psql -ddaq -c 'create or replace function "fn_rootplot_ver"() returns "pg_catalog"."trigger" as $BODY$ begin new.version = (select COALESCE(MAX(version)+1,0) from rootplots where name=new.name); return NEW; end; $BODY$ LANGUAGE plpgsql VOLATILE COST 100;'
149
+
psql -ddaq -c 'CREATE TRIGGER trig_rootplot_ver BEFORE insert ON rootplots FOR EACH ROW EXECUTE PROCEDURE fn_rootplot_ver();'
86
150
87
-
echo"creating configurations table"
88
-
psql -ddaq -c "create table configurations (config_id serial NOT NULL primary key, time timestamp with time zone NOT NULL, name text NOT NULL, version int NOT NULL, description text NOT NULL, author text NOT NULL, data JSONB NOT NULL, UNIQUE (name, version));"
151
+
echo"creating plotlyplots table"
152
+
psql -ddaq -c "create table plotlyplots (time timestamp with time zone NOT NULL DEFAULT now(), name text NOT NULL, version int NOT NULL, data jsonb NOT NULL, layout jsonb NOT NULL DEFAULT '{}', lifetime int NOT NULL DEFAULT 5, UNIQUE (name, version));"
89
153
90
-
echo"creating run_info table"
91
-
psql -ddaq -c "create table run_info (run int NOT NULL, subrun int NOT NULL, start_time timestamp with time zone NOT NULL, stop_time timestamp with time zone, config_id int NOT NULL, comments text NOT NULL, UNIQUE (run, subrun));"
154
+
echo"creating autoincrement function for plotlyplots version"
155
+
psql -ddaq -c 'create or replace function "fn_plotlyplot_ver"() returns "pg_catalog"."trigger" as $BODY$ begin new.version = (select COALESCE(MAX(version)+1,0) from plotlyplots where name=new.name); return NEW; end; $BODY$ LANGUAGE plpgsql VOLATILE COST 100;'
156
+
psql -ddaq -c 'CREATE TRIGGER trig_plotlyplot_ver BEFORE insert ON plotlyplots FOR EACH ROW EXECUTE PROCEDURE fn_plotlyplot_ver();'
92
157
93
-
echo"creating rootplots table"
94
-
psql -ddaq -c "create table rootplots (name text NOT NULL, draw_options text NOT NULL, time timestamp with time zone NOT NULL, data jsonb NOT NULL, version int NOT NULL, UNIQUE (name, version));"
158
+
echo"creating event_display table"
159
+
psql -ddaq -c "create table event_display (time timestamp with time zone NOT NULL DEFAULT now(), evnt bigint NOT NULL, data jsonb NOT NULL, UNIQUE (evnt));"
95
160
96
-
echo"creating users table"
97
-
psql -ddaq -c "create table users (user_id serial NOT NULL, username text NOT NULL, password_hash text NOT NULL, permissions JSONB, UNIQUE (user_id));"
161
+
psql -ddaq -c "CREATE INDEX ON event_display (evnt DESC NULLS LAST)"
98
162
99
163
echo"creating pmt table"
100
164
psql -ddaq -c "create type pmt_location as enum ('bottom', 'barrel', 'top');"
101
165
psql -ddaq -c "create table pmt (id int NOT NULL, x real NOT NULL, y real NOT NULL, z real, type text NOT NULL, size real NOT NULL, location pmt_location NOT NULL, UNIQUE (id));"
102
166
103
-
echo"creating event_display table"
104
-
psql -ddaq -c "create table event_display (evnt bigint NOT NULL, time timestamp with time zone NOT NULL, data JSONB NOT NULL, UNIQUE (evnt));"
105
-
106
-
echo"creating plotlyplots table"
107
-
psql -ddaq -c "create table plotlyplots (name text NOT NULL, time timestamp with time zone NOT NULL DEFAULT now(), version int NOT NULL, traces jsonb NOT NULL, layout jsonb NOT NULL DEFAULT '{}', UNIQUE (name, version));"
@@ -117,6 +175,6 @@ echo "Inserting example monitoring data"
117
175
psql -ddaq -c "INSERT INTO monitoring (time, device, subject, data) SELECT now() - (i * INTERVAL '1 minute') AS time, 'test_device' AS device, 'general' AS subject, jsonb_build_object( 'temperature', round((random() * 50 + 10)::numeric, 2), 'humidity', round((random() * 100)::numeric, 2)) AS data FROM generate_series(1, 100) i;"
0 commit comments