Skip to content

Commit

Permalink
sql: Move meter_event LUTs to iris schema
Browse files Browse the repository at this point in the history
  • Loading branch information
DougLau committed Nov 7, 2024
1 parent 9437b0b commit 9e05f32
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 45 deletions.
86 changes: 86 additions & 0 deletions sql/migrate-5.62.sql
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,90 @@ CREATE VIEW r_node_view AS
JOIN iris.r_node_transition tr ON n.transition = tr.id;
GRANT SELECT ON r_node_view TO PUBLIC;

-- Move meter_event LUTs to iris schema
DROP VIEW meter_event_view;

CREATE TABLE iris.metering_phase (
id INTEGER PRIMARY KEY,
description VARCHAR(16) NOT NULL
);

INSERT INTO iris.metering_phase (id, description)
VALUES
(0, 'not started'),
(1, 'metering'),
(2, 'flushing'),
(3, 'stopped');

CREATE TABLE iris.meter_queue_state (
id INTEGER PRIMARY KEY,
description VARCHAR(16) NOT NULL
);

INSERT INTO iris.meter_queue_state (id, description)
VALUES
(0, 'unknown'),
(1, 'empty'),
(2, 'exists'),
(3, 'full');

CREATE TABLE iris.meter_limit_control (
id INTEGER PRIMARY KEY,
description VARCHAR(16) NOT NULL
);

INSERT INTO iris.meter_limit_control (id, description)
VALUES
(0, 'passage fail'),
(1, 'storage limit'),
(2, 'wait limit'),
(3, 'target minimum'),
(4, 'backup limit');

ALTER TABLE event.meter_event RENAME TO old_meter_event;

CREATE TABLE event.meter_event (
id SERIAL PRIMARY KEY,
event_date TIMESTAMP WITH time zone DEFAULT NOW() NOT NULL,
event_desc INTEGER NOT NULL REFERENCES event.event_description,
ramp_meter VARCHAR(20) NOT NULL REFERENCES iris._ramp_meter
ON DELETE CASCADE,
phase INTEGER NOT NULL REFERENCES iris.metering_phase,
q_state INTEGER NOT NULL REFERENCES iris.meter_queue_state,
q_len REAL NOT NULL,
dem_adj REAL NOT NULL,
wait_secs INTEGER NOT NULL,
limit_ctrl INTEGER NOT NULL REFERENCES iris.meter_limit_control,
min_rate INTEGER NOT NULL,
rel_rate INTEGER NOT NULL,
max_rate INTEGER NOT NULL,
d_node VARCHAR(10),
seg_density REAL NOT NULL
);

INSERT INTO event.meter_event (
event_date, event_desc, ramp_meter, phase, q_state, q_len, dem_adj,
wait_secs, limit_ctrl, min_rate, rel_rate, max_rate, d_node, seg_density
)
SELECT event_date, event_desc_id, ramp_meter, phase, q_state, q_len, dem_adj,
wait_secs, limit_ctrl, min_rate, rel_rate, max_rate, d_node, seg_density
FROM event.old_meter_event;

DROP TABLE event.old_meter_event;
DROP TABLE event.meter_phase;
DROP TABLE event.meter_queue_state;
DROP TABLE event.meter_limit_control;

CREATE VIEW meter_event_view AS
SELECT me.id, event_date, ed.description, ramp_meter,
mp.description AS phase, qs.description AS q_state, q_len, dem_adj,
wait_secs, lc.description AS limit_ctrl, min_rate, rel_rate,
max_rate, d_node, seg_density
FROM event.meter_event me
JOIN event.event_description ed ON me.event_desc = ed.event_desc_id
JOIN iris.metering_phase mp ON phase = mp.id
JOIN iris.meter_queue_state qs ON q_state = qs.id
JOIN iris.meter_limit_control lc ON limit_ctrl = lc.id;
GRANT SELECT ON meter_event_view TO PUBLIC;

COMMIT;
85 changes: 42 additions & 43 deletions sql/tms-template.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1872,10 +1872,12 @@ CREATE VIEW detector_event_view AS
GRANT SELECT ON detector_event_view TO PUBLIC;

CREATE VIEW detector_auto_fail_view AS
WITH af AS (SELECT device_id, event_desc_id, count(*) AS event_count,
max(event_date) AS last_fail
FROM event.detector_event
GROUP BY device_id, event_desc_id)
WITH af AS (
SELECT device_id, event_desc_id, count(*) AS event_count,
max(event_date) AS last_fail
FROM event.detector_event
GROUP BY device_id, event_desc_id
)
SELECT device_id, label, ed.description, event_count, last_fail
FROM af
JOIN event.event_description ed ON af.event_desc_id = ed.event_desc_id
Expand Down Expand Up @@ -4460,56 +4462,55 @@ CREATE VIEW meter_action_view AS
ORDER BY ramp_meter, time_of_day;
GRANT SELECT ON meter_action_view TO PUBLIC;

CREATE TABLE event.meter_phase (
CREATE TABLE iris.metering_phase (
id INTEGER PRIMARY KEY,
description VARCHAR(16) NOT NULL
);

COPY event.meter_phase (id, description) FROM stdin;
0 not started
1 metering
2 flushing
3 stopped
\.
INSERT INTO iris.metering_phase (id, description)
VALUES
(0, 'not started'),
(1, 'metering'),
(2, 'flushing'),
(3, 'stopped');

CREATE TABLE event.meter_queue_state (
CREATE TABLE iris.meter_queue_state (
id INTEGER PRIMARY KEY,
description VARCHAR(16) NOT NULL
);

COPY event.meter_queue_state (id, description) FROM stdin;
0 unknown
1 empty
2 exists
3 full
\.
INSERT INTO iris.meter_queue_state (id, description)
VALUES
(0, 'unknown'),
(1, 'empty'),
(2, 'exists'),
(3, 'full');

CREATE TABLE event.meter_limit_control (
CREATE TABLE iris.meter_limit_control (
id INTEGER PRIMARY KEY,
description VARCHAR(16) NOT NULL
);

COPY event.meter_limit_control (id, description) FROM stdin;
0 passage fail
1 storage limit
2 wait limit
3 target minimum
4 backup limit
\.
INSERT INTO iris.meter_limit_control (id, description)
VALUES
(0, 'passage fail'),
(1, 'storage limit'),
(2, 'wait limit'),
(3, 'target minimum'),
(4, 'backup limit');

CREATE TABLE event.meter_event (
event_id SERIAL PRIMARY KEY,
id SERIAL PRIMARY KEY,
event_date TIMESTAMP WITH time zone DEFAULT NOW() NOT NULL,
event_desc_id INTEGER NOT NULL
REFERENCES event.event_description(event_desc_id),
event_desc INTEGER NOT NULL REFERENCES event.event_description,
ramp_meter VARCHAR(20) NOT NULL REFERENCES iris._ramp_meter
ON DELETE CASCADE,
phase INTEGER NOT NULL REFERENCES event.meter_phase,
q_state INTEGER NOT NULL REFERENCES event.meter_queue_state,
phase INTEGER NOT NULL REFERENCES iris.metering_phase,
q_state INTEGER NOT NULL REFERENCES iris.meter_queue_state,
q_len REAL NOT NULL,
dem_adj REAL NOT NULL,
wait_secs INTEGER NOT NULL,
limit_ctrl INTEGER NOT NULL REFERENCES event.meter_limit_control,
limit_ctrl INTEGER NOT NULL REFERENCES iris.meter_limit_control,
min_rate INTEGER NOT NULL,
rel_rate INTEGER NOT NULL,
max_rate INTEGER NOT NULL,
Expand All @@ -4518,17 +4519,15 @@ CREATE TABLE event.meter_event (
);

CREATE VIEW meter_event_view AS
SELECT event_id, event_date, event_description.description,
ramp_meter, meter_phase.description AS phase,
meter_queue_state.description AS q_state, q_len, dem_adj,
wait_secs, meter_limit_control.description AS limit_ctrl,
min_rate, rel_rate, max_rate, d_node, seg_density
FROM event.meter_event
JOIN event.event_description
ON meter_event.event_desc_id = event_description.event_desc_id
JOIN event.meter_phase ON phase = meter_phase.id
JOIN event.meter_queue_state ON q_state = meter_queue_state.id
JOIN event.meter_limit_control ON limit_ctrl = meter_limit_control.id;
SELECT me.id, event_date, ed.description, ramp_meter,
mp.description AS phase, qs.description AS q_state, q_len, dem_adj,
wait_secs, lc.description AS limit_ctrl, min_rate, rel_rate,
max_rate, d_node, seg_density
FROM event.meter_event me
JOIN event.event_description ed ON me.event_desc = ed.event_desc_id
JOIN iris.metering_phase mp ON phase = mp.id
JOIN iris.meter_queue_state qs ON q_state = qs.id
JOIN iris.meter_limit_control lc ON limit_ctrl = lc.id;
GRANT SELECT ON meter_event_view TO PUBLIC;

CREATE TABLE event.meter_lock_event (
Expand Down
4 changes: 2 additions & 2 deletions src/us/mn/state/dot/tms/server/KAdaptiveAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -1178,14 +1178,14 @@ private void calculateMeteringRate() {
StationNode dn = s_node.segmentStationNode();
double k = s_node.calculateSegmentDensity(dn);
segment_k_hist.push(k);
phase = checkMeterPhase();
phase = checkMeteringPhase();
if (isMetering())
setRate(calculateRate(k));
}

/** Check metering phase transitions.
* @return New metering phase. */
private MeteringPhase checkMeterPhase() {
private MeteringPhase checkMeteringPhase() {
switch (phase) {
case not_started:
return checkStart();
Expand Down

0 comments on commit 9e05f32

Please sign in to comment.