Remove duplicate data from bill_data and add PK to prevent recurrence

This commit is contained in:
Richard
2016-03-02 21:00:32 +00:00
committed by Richard Lawley
parent dc0f695796
commit 042ddaa6fe
3 changed files with 18 additions and 6 deletions

View File

@ -97,7 +97,7 @@ function getLastPortCounter($port_id) {
function getLastMeasurement($bill_id) {
$return = array();
$row = dbFetchRow("SELECT timestamp,delta,in_delta,out_delta FROM bill_data WHERE bill_id = ? ORDER BY timestamp DESC LIMIT 1", array($port_id));
$row = dbFetchRow("SELECT timestamp,delta,in_delta,out_delta FROM bill_data WHERE bill_id = ? ORDER BY timestamp DESC LIMIT 1", array($bill_id));
if (!is_null($row)) {
$return[delta] = $row['delta'];
$return[delta_in] = $row['delta_in'];
@ -108,9 +108,7 @@ function getLastMeasurement($bill_id) {
else {
$return[state] = 'failed';
}
return ($return);
}//end getLastMeasurement()

View File

@ -27,6 +27,13 @@ rrdtool_pipe_open($rrd_process, $rrd_pipes);
$poller_start = microtime(true);
echo "Starting Polling Session ... \n\n";
// Wait for schema update, as running during update can break update
$dbVersion = dbFetchCell('SELECT version FROM dbSchema');
if ($dbVersion < 107) {
logfile("BILLING: Cannot continue until dbSchema update to >= 107 is complete");
exit(1);
}
foreach (dbFetchRows('SELECT * FROM `bills`') as $bill_data) {
echo 'Bill : '.$bill_data['bill_name']."\n";

View File

@ -1,4 +1,11 @@
CREATE TABLE bill_port_counters(port_id int NOT NULL PRIMARY KEY, `timestamp` timestamp NOT NULL DEFAULT current_timestamp, in_counter bigint, in_delta bigint NOT NULL DEFAULT 0, out_counter bigint, out_delta bigint NOT NULL DEFAULT 0);
INSERT INTO bill_port_counters(port_id, timestamp, in_counter, out_counter) SELECT q.port_id, q.max_timestamp, max(i.counter), max(o.counter) FROM (SELECT port_id, MAX(`timestamp`) AS max_timestamp FROM port_in_measurements GROUP BY port_id) q INNER JOIN port_in_measurements i ON q.port_id = i.port_id AND q.max_timestamp = i.timestamp INNER JOIN port_out_measurements o ON q.port_id = o.port_id AND q.max_timestamp = o.timestamp GROUP BY q.port_id, q.max_timestamp;
UPDATE dbSchema SET version = 107;
CREATE TABLE bill_port_counters_tmp(port_id int NOT NULL PRIMARY KEY, `timestamp` timestamp NOT NULL DEFAULT current_timestamp, in_counter bigint, in_delta bigint NOT NULL DEFAULT 0, out_counter bigint, out_delta bigint NOT NULL DEFAULT 0);
INSERT INTO bill_port_counters_tmp(port_id, timestamp, in_counter, out_counter) SELECT q.port_id, q.max_timestamp, max(i.counter), max(o.counter) FROM (SELECT port_id, MAX(`timestamp`) AS max_timestamp FROM port_in_measurements GROUP BY port_id) q INNER JOIN port_in_measurements i ON q.port_id = i.port_id AND q.max_timestamp = i.timestamp INNER JOIN port_out_measurements o ON q.port_id = o.port_id AND q.max_timestamp = o.timestamp GROUP BY q.port_id, q.max_timestamp;
RENAME TABLE bill_port_counters_tmp TO bill_port_counters;
ALTER TABLE bill_data ADD id int NOT NULL PRIMARY KEY AUTO_INCREMENT FIRST;
DELETE bill_data FROM bill_data INNER JOIN (SELECT bill_id, timestamp, MIN(id) as first_id FROM bill_data GROUP BY bill_id, timestamp HAVING COUNT(id) > 1) q ON bill_data.bill_id = q.bill_id AND bill_data.timestamp = q.timestamp;
ALTER TABLE bill_data DROP id;
ALTER TABLE bill_data ADD PRIMARY KEY (bill_id, timestamp);
ALTER TABLE bill_data DROP INDEX bill_id_2;
DROP TABLE port_in_measurements;
DROP TABLE port_out_measurements;
DROP TABLE port_out_measurements;