<?xml version="1.0" encoding="UTF-8"?>

<!-- Differential patch for M/Monit database schema version 22 to 23 -->

<patch>

<!-- sqlite -->

<statements type="sqlite">

<comment>Add analytics table with 2m precision</comment>
<statement>
CREATE TABLE statistics_aggregate_2m (
  statisticsid INTEGER,
  collectedsec INTEGER NOT NULL,
  value_avg REAL,
  count INTEGER,
  CONSTRAINT statistics_aggregate_2m_statisticsid_fk FOREIGN KEY(statisticsid) REFERENCES statistics(id) ON DELETE CASCADE
);
</statement>

<comment>Add analytics table with 30m precision</comment>
<statement>
CREATE TABLE statistics_aggregate_30m (
  statisticsid INTEGER,
  collectedsec INTEGER NOT NULL,
  value_avg REAL,
  count INTEGER,
  CONSTRAINT statistics_aggregate_30m_statisticsid_fk FOREIGN KEY(statisticsid) REFERENCES statistics(id) ON DELETE CASCADE
);
</statement>

<comment>Add analytics table with 1h precision</comment>
<statement>
CREATE TABLE statistics_aggregate_1h (
  statisticsid INTEGER,
  collectedsec INTEGER NOT NULL,
  value_avg REAL,
  count INTEGER,
  CONSTRAINT statistics_aggregate_1h_statisticsid_fk FOREIGN KEY(statisticsid) REFERENCES statistics(id) ON DELETE CASCADE
);
</statement>

<comment>Add analytics table with 4h precision</comment>
<statement>
CREATE TABLE statistics_aggregate_4h (
  statisticsid INTEGER,
  collectedsec INTEGER NOT NULL,
  value_avg REAL,
  count INTEGER,
  CONSTRAINT statistics_aggregate_4h_statisticsid_fk FOREIGN KEY(statisticsid) REFERENCES statistics(id) ON DELETE CASCADE
);
</statement>

<comment>Add analytics table with 3d precision</comment>
<statement>
CREATE TABLE statistics_aggregate_3d (
  statisticsid INTEGER,
  collectedsec INTEGER NOT NULL,
  value_avg REAL,
  count INTEGER,
  CONSTRAINT statistics_aggregate_3d_statisticsid_fk FOREIGN KEY(statisticsid) REFERENCES statistics(id) ON DELETE CASCADE
);
</statement>

<comment>Add analytics table with 4w precision</comment>
<statement>
CREATE TABLE statistics_aggregate_4w (
  statisticsid INTEGER,
  collectedsec INTEGER NOT NULL,
  value_avg REAL,
  count INTEGER,
  CONSTRAINT statistics_aggregate_4w_statisticsid_fk FOREIGN KEY(statisticsid) REFERENCES statistics(id) ON DELETE CASCADE
);
</statement>

<comment>Aggregating data from statistics_aggregate_1m to statistics_aggregate_2m. This can take a while. Please be patient</comment>
<statement>
INSERT INTO statistics_aggregate_2m (statisticsid, collectedsec, value_avg, count) SELECT DISTINCT statisticsid, collectedsec - collectedsec % 120 AS collectedsecnew, AVG(value_avg), SUM(count) / 2 FROM statistics_aggregate_1m GROUP BY statisticsid, collectedsecnew;
</statement>

<comment>Aggregating data from statistics_aggregate_15m to statistics_aggregate_30m. This can take a while. Please be patient</comment>
<statement>
INSERT INTO statistics_aggregate_30m (statisticsid, collectedsec, value_avg, count) SELECT DISTINCT statisticsid, collectedsec - collectedsec % 1800 AS collectedsecnew, AVG(value_avg), SUM(count) / 2 FROM statistics_aggregate_15m GROUP BY statisticsid, collectedsecnew;
</statement>

<comment>Aggregating data from statistics_aggregate_30m to statistics_aggregate_1h. This can take a while. Please be patient</comment>
<statement>
INSERT INTO statistics_aggregate_1h (statisticsid, collectedsec, value_avg, count) SELECT DISTINCT statisticsid, collectedsec - collectedsec % 3600 AS collectedsecnew, AVG(value_avg), SUM(count) / 2 FROM statistics_aggregate_30m GROUP BY statisticsid, collectedsecnew;
</statement>

<comment>Aggregating data from statistics_aggregate_2h to statistics_aggregate_4h. This can take a while. Please be patient</comment>
<statement>
INSERT INTO statistics_aggregate_4h (statisticsid, collectedsec, value_avg, count) SELECT DISTINCT statisticsid, collectedsec - collectedsec % 14400 AS collectedsecnew, AVG(value_avg), SUM(count) / 2 FROM statistics_aggregate_2h GROUP BY statisticsid, collectedsecnew;
</statement>

<comment>Aggregating data from statistics_aggregate_1d to statistics_aggregate_3d. This can take a while. Please be patient</comment>
<statement>
INSERT INTO statistics_aggregate_3d (statisticsid, collectedsec, value_avg, count) SELECT DISTINCT statisticsid, collectedsec - collectedsec % 259200 AS collectedsecnew, AVG(value_avg), SUM(count) / 3 FROM statistics_aggregate_1d GROUP BY statisticsid, collectedsecnew;
</statement>

<comment>Aggregating data from statistics_aggregate_14d to statistics_aggregate_4w. This can take a while. Please be patient</comment>
<statement>
INSERT INTO statistics_aggregate_4w (statisticsid, collectedsec, value_avg, count) SELECT DISTINCT statisticsid, collectedsec - collectedsec % 2419200 AS collectedsecnew, AVG(value_avg), SUM(count) / 2 FROM statistics_aggregate_14d GROUP BY statisticsid, collectedsecnew;
</statement>

<comment>Create indexes for statistics_aggregate_2m</comment>
<statement>
CREATE INDEX statistics_aggregate_2m_statisticsid_index ON statistics_aggregate_2m(statisticsid);
</statement>
<statement>
CREATE INDEX statistics_aggregate_2m_collectedsec_index ON statistics_aggregate_2m(collectedsec);
</statement>

<comment>Create indexes for statistics_aggregate_30m</comment>
<statement>
CREATE INDEX statistics_aggregate_30m_statisticsid_index ON statistics_aggregate_30m(statisticsid);
</statement>
<statement>
CREATE INDEX statistics_aggregate_30m_collectedsec_index ON statistics_aggregate_30m(collectedsec);
</statement>

<comment>Create indexes for statistics_aggregate_1h</comment>
<statement>
CREATE INDEX statistics_aggregate_1h_statisticsid_index ON statistics_aggregate_1h(statisticsid);
</statement>
<statement>
CREATE INDEX statistics_aggregate_1h_collectedsec_index ON statistics_aggregate_1h(collectedsec);
</statement>

<comment>Create indexes for statistics_aggregate_4h</comment>
<statement>
CREATE INDEX statistics_aggregate_4h_statisticsid_index ON statistics_aggregate_4h(statisticsid);
</statement>
<statement>
CREATE INDEX statistics_aggregate_4h_collectedsec_index ON statistics_aggregate_4h(collectedsec);
</statement>

<comment>Create indexes for statistics_aggregate_3d</comment>
<statement>
CREATE INDEX statistics_aggregate_3d_statisticsid_index ON statistics_aggregate_3d(statisticsid);
</statement>
<statement>
CREATE INDEX statistics_aggregate_3d_collectedsec_index ON statistics_aggregate_3d(collectedsec);
</statement>

<comment>Create indexes for statistics_aggregate_4w</comment>
<statement>
CREATE INDEX statistics_aggregate_4w_statisticsid_index ON statistics_aggregate_4w(statisticsid);
</statement>
<statement>
CREATE INDEX statistics_aggregate_4w_collectedsec_index ON statistics_aggregate_4w(collectedsec);
</statement>

</statements>


<!-- mysql -->

<statements type="mysql">

<comment>Add analytics table with 2m precision</comment>
<statement>
CREATE TABLE statistics_aggregate_2m (
  statisticsid BIGINT,
  collectedsec BIGINT NOT NULL,
  value_avg DOUBLE PRECISION,
  count BIGINT,
  CONSTRAINT statistics_aggregate_2m_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
</statement>

<comment>Add analytics table with 30m precision</comment>
<statement>
CREATE TABLE statistics_aggregate_30m (
  statisticsid BIGINT,
  collectedsec BIGINT NOT NULL,
  value_avg DOUBLE PRECISION,
  count BIGINT,
  CONSTRAINT statistics_aggregate_30m_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
</statement>

<comment>Add analytics table with 1h precision</comment>
<statement>
CREATE TABLE statistics_aggregate_1h (
  statisticsid BIGINT,
  collectedsec BIGINT NOT NULL,
  value_avg DOUBLE PRECISION,
  count BIGINT,
  CONSTRAINT statistics_aggregate_1h_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
</statement>

<comment>Add analytics table with 4h precision</comment>
<statement>
CREATE TABLE statistics_aggregate_4h (
  statisticsid BIGINT,
  collectedsec BIGINT NOT NULL,
  value_avg DOUBLE PRECISION,
  count BIGINT,
  CONSTRAINT statistics_aggregate_4h_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
</statement>

<comment>Add analytics table with 3d precision</comment>
<statement>
CREATE TABLE statistics_aggregate_3d (
  statisticsid BIGINT,
  collectedsec BIGINT NOT NULL,
  value_avg DOUBLE PRECISION,
  count BIGINT,
  CONSTRAINT statistics_aggregate_3d_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
</statement>

<comment>Add analytics table with 4w precision</comment>
<statement>
CREATE TABLE statistics_aggregate_4w (
  statisticsid BIGINT,
  collectedsec BIGINT NOT NULL,
  value_avg DOUBLE PRECISION,
  count BIGINT,
  CONSTRAINT statistics_aggregate_4w_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
</statement>

<comment>Aggregating data from statistics_aggregate_1m to statistics_aggregate_2m. This can take a while. Please be patient</comment>
<statement>
INSERT INTO statistics_aggregate_2m (statisticsid, collectedsec, value_avg, count) SELECT DISTINCT statisticsid, collectedsec - collectedsec % 120 AS collectedsecnew, AVG(value_avg), SUM(count) / 2 FROM statistics_aggregate_1m GROUP BY statisticsid, collectedsecnew;
</statement>

<comment>Aggregating data from statistics_aggregate_15m to statistics_aggregate_30m. This can take a while. Please be patient</comment>
<statement>
INSERT INTO statistics_aggregate_30m (statisticsid, collectedsec, value_avg, count) SELECT DISTINCT statisticsid, collectedsec - collectedsec % 1800 AS collectedsecnew, AVG(value_avg), SUM(count) / 2 FROM statistics_aggregate_15m GROUP BY statisticsid, collectedsecnew;
</statement>

<comment>Aggregating data from statistics_aggregate_30m to statistics_aggregate_1h. This can take a while. Please be patient</comment>
<statement>
INSERT INTO statistics_aggregate_1h (statisticsid, collectedsec, value_avg, count) SELECT DISTINCT statisticsid, collectedsec - collectedsec % 3600 AS collectedsecnew, AVG(value_avg), SUM(count) / 2 FROM statistics_aggregate_30m GROUP BY statisticsid, collectedsecnew;
</statement>

<comment>Aggregating data from statistics_aggregate_2h to statistics_aggregate_4h. This can take a while. Please be patient</comment>
<statement>
INSERT INTO statistics_aggregate_4h (statisticsid, collectedsec, value_avg, count) SELECT DISTINCT statisticsid, collectedsec - collectedsec % 14400 AS collectedsecnew, AVG(value_avg), SUM(count) / 2 FROM statistics_aggregate_2h GROUP BY statisticsid, collectedsecnew;
</statement>

<comment>Aggregating data from statistics_aggregate_1d to statistics_aggregate_3d. This can take a while. Please be patient</comment>
<statement>
INSERT INTO statistics_aggregate_3d (statisticsid, collectedsec, value_avg, count) SELECT DISTINCT statisticsid, collectedsec - collectedsec % 259200 AS collectedsecnew, AVG(value_avg), SUM(count) / 3 FROM statistics_aggregate_1d GROUP BY statisticsid, collectedsecnew;
</statement>

<comment>Aggregating data from statistics_aggregate_14d to statistics_aggregate_4w. This can take a while. Please be patient</comment>
<statement>
INSERT INTO statistics_aggregate_4w (statisticsid, collectedsec, value_avg, count) SELECT DISTINCT statisticsid, collectedsec - collectedsec % 2419200 AS collectedsecnew, AVG(value_avg), SUM(count) / 2 FROM statistics_aggregate_14d GROUP BY statisticsid, collectedsecnew;
</statement>

<comment>Create indexes for statistics_aggregate_2m</comment>
<statement>
CREATE INDEX statistics_aggregate_2m_statisticsid_index ON statistics_aggregate_2m(statisticsid);
</statement>
<statement>
CREATE INDEX statistics_aggregate_2m_collectedsec_index ON statistics_aggregate_2m(collectedsec);
</statement>

<comment>Create indexes for statistics_aggregate_30m</comment>
<statement>
CREATE INDEX statistics_aggregate_30m_statisticsid_index ON statistics_aggregate_30m(statisticsid);
</statement>
<statement>
CREATE INDEX statistics_aggregate_30m_collectedsec_index ON statistics_aggregate_30m(collectedsec);
</statement>

<comment>Create indexes for statistics_aggregate_1h</comment>
<statement>
CREATE INDEX statistics_aggregate_1h_statisticsid_index ON statistics_aggregate_1h(statisticsid);
</statement>
<statement>
CREATE INDEX statistics_aggregate_1h_collectedsec_index ON statistics_aggregate_1h(collectedsec);
</statement>

<comment>Create indexes for statistics_aggregate_4h</comment>
<statement>
CREATE INDEX statistics_aggregate_4h_statisticsid_index ON statistics_aggregate_4h(statisticsid);
</statement>
<statement>
CREATE INDEX statistics_aggregate_4h_collectedsec_index ON statistics_aggregate_4h(collectedsec);
</statement>

<comment>Create indexes for statistics_aggregate_3d</comment>
<statement>
CREATE INDEX statistics_aggregate_3d_statisticsid_index ON statistics_aggregate_3d(statisticsid);
</statement>
<statement>
CREATE INDEX statistics_aggregate_3d_collectedsec_index ON statistics_aggregate_3d(collectedsec);
</statement>

<comment>Create indexes for statistics_aggregate_4w</comment>
<statement>
CREATE INDEX statistics_aggregate_4w_statisticsid_index ON statistics_aggregate_4w(statisticsid);
</statement>
<statement>
CREATE INDEX statistics_aggregate_4w_collectedsec_index ON statistics_aggregate_4w(collectedsec);
</statement>

</statements>


<!-- postgresql -->

<statements type="postgresql">

<comment>Add analytics table with 2m precision</comment>
<statement>
CREATE TABLE statistics_aggregate_2m (
  statisticsid BIGINT,
  collectedsec BIGINT NOT NULL,
  value_avg DOUBLE PRECISION,
  count BIGINT,
  CONSTRAINT statistics_aggregate_2m_statisticsid_fk FOREIGN KEY(statisticsid) REFERENCES statistics(id) MATCH FULL ON DELETE CASCADE
);
</statement>

<comment>Add analytics table with 30m precision</comment>
<statement>
CREATE TABLE statistics_aggregate_30m (
  statisticsid BIGINT,
  collectedsec BIGINT NOT NULL,
  value_avg DOUBLE PRECISION,
  count BIGINT,
  CONSTRAINT statistics_aggregate_30m_statisticsid_fk FOREIGN KEY(statisticsid) REFERENCES statistics(id) MATCH FULL ON DELETE CASCADE
);
</statement>

<comment>Add analytics table with 1h precision</comment>
<statement>
CREATE TABLE statistics_aggregate_1h (
  statisticsid BIGINT,
  collectedsec BIGINT NOT NULL,
  value_avg DOUBLE PRECISION,
  count BIGINT,
  CONSTRAINT statistics_aggregate_1h_statisticsid_fk FOREIGN KEY(statisticsid) REFERENCES statistics(id) MATCH FULL ON DELETE CASCADE
);
</statement>

<comment>Add analytics table with 4h precision</comment>
<statement>
CREATE TABLE statistics_aggregate_4h (
  statisticsid BIGINT,
  collectedsec BIGINT NOT NULL,
  value_avg DOUBLE PRECISION,
  count BIGINT,
  CONSTRAINT statistics_aggregate_4h_statisticsid_fk FOREIGN KEY(statisticsid) REFERENCES statistics(id) MATCH FULL ON DELETE CASCADE
);
</statement>

<comment>Add analytics table with 3d precision</comment>
<statement>
CREATE TABLE statistics_aggregate_3d (
  statisticsid BIGINT,
  collectedsec BIGINT NOT NULL,
  value_avg DOUBLE PRECISION,
  count BIGINT,
  CONSTRAINT statistics_aggregate_3d_statisticsid_fk FOREIGN KEY(statisticsid) REFERENCES statistics(id) MATCH FULL ON DELETE CASCADE
);
</statement>

<comment>Add analytics table with 4w precision</comment>
<statement>
CREATE TABLE statistics_aggregate_4w (
  statisticsid BIGINT,
  collectedsec BIGINT NOT NULL,
  value_avg DOUBLE PRECISION,
  count BIGINT,
  CONSTRAINT statistics_aggregate_4w_statisticsid_fk FOREIGN KEY(statisticsid) REFERENCES statistics(id) MATCH FULL ON DELETE CASCADE
);
</statement>

<comment>Aggregating data from statistics_aggregate_1m to statistics_aggregate_2m. This can take a while. Please be patient</comment>
<statement>
INSERT INTO statistics_aggregate_2m (statisticsid, collectedsec, value_avg, count) SELECT DISTINCT statisticsid, collectedsec - collectedsec % 120 AS collectedsecnew, AVG(value_avg), SUM(count) / 2 FROM statistics_aggregate_1m GROUP BY statisticsid, collectedsecnew;
</statement>

<comment>Aggregating data from statistics_aggregate_15m to statistics_aggregate_30m. This can take a while. Please be patient</comment>
<statement>
INSERT INTO statistics_aggregate_30m (statisticsid, collectedsec, value_avg, count) SELECT DISTINCT statisticsid, collectedsec - collectedsec % 1800 AS collectedsecnew, AVG(value_avg), SUM(count) / 2 FROM statistics_aggregate_15m GROUP BY statisticsid, collectedsecnew;
</statement>

<comment>Aggregating data from statistics_aggregate_30m to statistics_aggregate_1h. This can take a while. Please be patient</comment>
<statement>
INSERT INTO statistics_aggregate_1h (statisticsid, collectedsec, value_avg, count) SELECT DISTINCT statisticsid, collectedsec - collectedsec % 3600 AS collectedsecnew, AVG(value_avg), SUM(count) / 2 FROM statistics_aggregate_30m GROUP BY statisticsid, collectedsecnew;
</statement>

<comment>Aggregating data from statistics_aggregate_2h to statistics_aggregate_4h. This can take a while. Please be patient</comment>
<statement>
INSERT INTO statistics_aggregate_4h (statisticsid, collectedsec, value_avg, count) SELECT DISTINCT statisticsid, collectedsec - collectedsec % 14400 AS collectedsecnew, AVG(value_avg), SUM(count) / 2 FROM statistics_aggregate_2h GROUP BY statisticsid, collectedsecnew;
</statement>

<comment>Aggregating data from statistics_aggregate_1d to statistics_aggregate_3d. This can take a while. Please be patient</comment>
<statement>
INSERT INTO statistics_aggregate_3d (statisticsid, collectedsec, value_avg, count) SELECT DISTINCT statisticsid, collectedsec - collectedsec % 259200 AS collectedsecnew, AVG(value_avg), SUM(count) / 3 FROM statistics_aggregate_1d GROUP BY statisticsid, collectedsecnew;
</statement>

<comment>Aggregating data from statistics_aggregate_14d to statistics_aggregate_4w. This can take a while. Please be patient</comment>
<statement>
INSERT INTO statistics_aggregate_4w (statisticsid, collectedsec, value_avg, count) SELECT DISTINCT statisticsid, collectedsec - collectedsec % 2419200 AS collectedsecnew, AVG(value_avg), SUM(count) / 2 FROM statistics_aggregate_14d GROUP BY statisticsid, collectedsecnew;
</statement>

<comment>Create indexes for statistics_aggregate_2m</comment>
<statement>
CREATE INDEX statistics_aggregate_2m_statisticsid_index ON statistics_aggregate_2m(statisticsid);
</statement>
<statement>
CREATE INDEX statistics_aggregate_2m_collectedsec_index ON statistics_aggregate_2m(collectedsec);
</statement>

<comment>Create indexes for statistics_aggregate_30m</comment>
<statement>
CREATE INDEX statistics_aggregate_30m_statisticsid_index ON statistics_aggregate_30m(statisticsid);
</statement>
<statement>
CREATE INDEX statistics_aggregate_30m_collectedsec_index ON statistics_aggregate_30m(collectedsec);
</statement>

<comment>Create indexes for statistics_aggregate_1h</comment>
<statement>
CREATE INDEX statistics_aggregate_1h_statisticsid_index ON statistics_aggregate_1h(statisticsid);
</statement>
<statement>
CREATE INDEX statistics_aggregate_1h_collectedsec_index ON statistics_aggregate_1h(collectedsec);
</statement>

<comment>Create indexes for statistics_aggregate_4h</comment>
<statement>
CREATE INDEX statistics_aggregate_4h_statisticsid_index ON statistics_aggregate_4h(statisticsid);
</statement>
<statement>
CREATE INDEX statistics_aggregate_4h_collectedsec_index ON statistics_aggregate_4h(collectedsec);
</statement>

<comment>Create indexes for statistics_aggregate_3d</comment>
<statement>
CREATE INDEX statistics_aggregate_3d_statisticsid_index ON statistics_aggregate_3d(statisticsid);
</statement>
<statement>
CREATE INDEX statistics_aggregate_3d_collectedsec_index ON statistics_aggregate_3d(collectedsec);
</statement>

<comment>Create indexes for statistics_aggregate_4w</comment>
<statement>
CREATE INDEX statistics_aggregate_4w_statisticsid_index ON statistics_aggregate_4w(statisticsid);
</statement>
<statement>
CREATE INDEX statistics_aggregate_4w_collectedsec_index ON statistics_aggregate_4w(collectedsec);
</statement>

</statements>

</patch>

