From 855743e24c6f2260e0101dc16ac9092473956a5c Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Mon, 6 Feb 2023 08:49:46 -0400 Subject: [PATCH] chore: truncate data, create indexes and rename column --- telemetry/bindata.go | 26 +++++++++++++++++++++- telemetry/protocolstats.go | 10 ++++----- telemetry/sql/000003_index_truncate.up.sql | 13 +++++++++++ 3 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 telemetry/sql/000003_index_truncate.up.sql diff --git a/telemetry/bindata.go b/telemetry/bindata.go index 71a892d..ad90cef 100644 --- a/telemetry/bindata.go +++ b/telemetry/bindata.go @@ -2,6 +2,7 @@ // sources: // 000001_message_type.up.sql (66B) // 000002_bandwidth_protocol.up.sql (719B) +// 000003_index_truncate.up.sql (598B) // doc.go (73B) package telemetry @@ -106,11 +107,31 @@ func _000002_bandwidth_protocolUpSql() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "000002_bandwidth_protocol.up.sql", size: 719, mode: os.FileMode(0664), modTime: time.Unix(1675262994, 0)} + info := bindataFileInfo{name: "000002_bandwidth_protocol.up.sql", size: 719, mode: os.FileMode(0664), modTime: time.Unix(1675687684, 0)} a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xfe, 0x83, 0x69, 0xab, 0x3e, 0xf5, 0x8d, 0x44, 0xb2, 0x6e, 0x52, 0x8d, 0x27, 0xe8, 0x95, 0x28, 0x3c, 0xea, 0x29, 0x93, 0x6d, 0xa3, 0x10, 0xde, 0x9b, 0xc8, 0xa6, 0xb9, 0x80, 0xa1, 0x3, 0x6f}} return a, nil } +var __000003_index_truncateUpSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x84\x91\xb1\x6a\x80\x30\x10\x86\xf7\x3c\xc5\x8d\x0a\x5d\x3a\x67\x4a\x35\x83\xa0\x11\xd2\x08\xdd\x24\xe8\x91\x0a\xb6\x91\xe4\x5a\xfa\xf8\xc5\x4a\x2b\x18\xb5\x6b\xfe\xef\xbe\xfb\xb9\x18\xdd\xa9\x42\x18\x09\x46\x3c\xd5\x12\x02\x0e\x38\x7d\xe2\xd8\x60\x8c\xd6\x61\xe4\xec\x00\x2c\xc1\x93\x1f\xfc\xfc\x4c\x96\xa2\xb6\x84\xf7\x84\xf1\x64\xe7\xd4\x72\x58\x23\x9c\x0b\xe8\x2c\xe1\xc8\x19\x2b\xb4\x5c\xc1\x4a\x95\xf2\x25\xe9\xd3\x0f\x01\x57\x4e\x10\xb4\x2a\x49\xb3\xbf\x34\xe7\xb7\x9e\x7d\x61\x1f\x3e\xde\x4f\x65\x3b\x92\xfd\x20\xf9\xb1\x5a\x72\x89\x7e\x1a\xbf\x1e\x57\x53\x92\x64\xbf\x2f\xca\xbe\xe1\x03\x5c\xb6\x3c\x39\xdd\xb9\x74\xcb\x6e\xb4\x4c\xd4\x46\xea\xab\x3f\x03\x2d\x95\x68\x24\x14\x6d\xdd\x35\x0a\x5e\x7d\xa4\xaa\x04\xd3\xc2\x82\x18\xaa\x92\x5f\x4f\x6f\x8b\xff\x9d\xff\x0e\x00\x00\xff\xff\x11\x42\xcb\x4c\x56\x02\x00\x00") + +func _000003_index_truncateUpSqlBytes() ([]byte, error) { + return bindataRead( + __000003_index_truncateUpSql, + "000003_index_truncate.up.sql", + ) +} + +func _000003_index_truncateUpSql() (*asset, error) { + bytes, err := _000003_index_truncateUpSqlBytes() + if err != nil { + return nil, err + } + + info := bindataFileInfo{name: "000003_index_truncate.up.sql", size: 598, mode: os.FileMode(0664), modTime: time.Unix(1675687705, 0)} + a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xcf, 0x8, 0x4, 0x47, 0xc8, 0x65, 0x38, 0x79, 0x3e, 0x37, 0xec, 0x4e, 0x1a, 0x24, 0x50, 0x3c, 0x1c, 0x75, 0xe8, 0x3b, 0x2, 0x62, 0x2, 0x52, 0x50, 0xff, 0x4a, 0x8f, 0x9d, 0x71, 0x79, 0xf6}} + return a, nil +} + var _docGo = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x2c\xc9\x31\x12\x84\x20\x0c\x05\xd0\x9e\x53\xfc\x0b\x90\xf4\x7b\x9b\xac\xfe\xc9\x38\x20\x41\x4c\xe3\xed\x6d\xac\xdf\xb4\xad\x99\x13\xf7\xd5\x4b\x51\xf5\xf8\x39\x07\x97\x25\xe1\x51\xff\xc7\xd8\x2d\x0d\x75\x36\x47\xb2\xf3\x64\xae\x07\x35\x20\xa2\x1f\x8a\x07\x44\xcb\x1b\x00\x00\xff\xff\xb6\x03\x50\xe0\x49\x00\x00\x00") func docGoBytes() ([]byte, error) { @@ -226,6 +247,8 @@ var _bindata = map[string]func() (*asset, error){ "000002_bandwidth_protocol.up.sql": _000002_bandwidth_protocolUpSql, + "000003_index_truncate.up.sql": _000003_index_truncateUpSql, + "doc.go": docGo, } @@ -272,6 +295,7 @@ type bintree struct { var _bintree = &bintree{nil, map[string]*bintree{ "000001_message_type.up.sql": &bintree{_000001_message_typeUpSql, map[string]*bintree{}}, "000002_bandwidth_protocol.up.sql": &bintree{_000002_bandwidth_protocolUpSql, map[string]*bintree{}}, + "000003_index_truncate.up.sql": &bintree{_000003_index_truncateUpSql, map[string]*bintree{}}, "doc.go": &bintree{docGo, map[string]*bintree{}}, }} diff --git a/telemetry/protocolstats.go b/telemetry/protocolstats.go index 52bc217..e1832b9 100644 --- a/telemetry/protocolstats.go +++ b/telemetry/protocolstats.go @@ -13,27 +13,27 @@ type Metric struct { } type ProtocolStats struct { - HostID string `json:"hostID"` + PeerID string `json:"hostID"` Relay Metric `json:"relay"` Store Metric `json:"store"` } func (r *ProtocolStats) insertRate(db *sql.DB, protocolName string, metric Metric) error { - stmt, err := db.Prepare("INSERT INTO protocolStatsRate (hostId, protocolName, rateIn, rateOut, createdAt) VALUES ($1, $2, $3, $4, $5);") + stmt, err := db.Prepare("INSERT INTO protocolStatsRate (peerID, protocolName, rateIn, rateOut, createdAt) VALUES ($1, $2, $3, $4, $5);") if err != nil { return err } - _, err = stmt.Exec(r.HostID, protocolName, metric.RateIn, metric.RateOut, time.Now().Unix()) + _, err = stmt.Exec(r.PeerID, protocolName, metric.RateIn, metric.RateOut, time.Now().Unix()) if err != nil { return err } - stmt, err = db.Prepare("INSERT INTO protocolStatsTotals (hostId, protocolName, totalIn, totalOut, createdAt) VALUES ($1, $2, $3, $4, $5) ON CONFLICT ON CONSTRAINT protocolStatsTotals_unique DO UPDATE SET totalIn = $3, totalOut = $4;") + stmt, err = db.Prepare("INSERT INTO protocolStatsTotals (peerID, protocolName, totalIn, totalOut, createdAt) VALUES ($1, $2, $3, $4, $5) ON CONFLICT ON CONSTRAINT protocolStatsTotals_unique DO UPDATE SET totalIn = $3, totalOut = $4;") if err != nil { return err } - _, err = stmt.Exec(r.HostID, protocolName, metric.TotalIn, metric.TotalOut, time.Now().Format("2006-01-02")) + _, err = stmt.Exec(r.PeerID, protocolName, metric.TotalIn, metric.TotalOut, time.Now().Format("2006-01-02")) if err != nil { return err } diff --git a/telemetry/sql/000003_index_truncate.up.sql b/telemetry/sql/000003_index_truncate.up.sql new file mode 100644 index 0000000..b225ea5 --- /dev/null +++ b/telemetry/sql/000003_index_truncate.up.sql @@ -0,0 +1,13 @@ +TRUNCATE TABLE receivedMessages; +TRUNCATE TABLE protocolStatsRate; +TRUNCATE TABLE protocolStatsTotals; +TRUNCATE TABLE receivedMessageAggregated; + +CREATE INDEX receivedMessages_createdAt ON receivedMessages(createdAt); +CREATE INDEX receivedMessageAggregated_runAt ON receivedMessageAggregated(runAt); + +CREATE INDEX protocolStatsRate_idx1 ON protocolStatsRate(protocolName, createdAt); +CREATE INDEX protocolStatsTotals_idx1 ON protocolStatsTotals(protocolName, createdAt); + +ALTER TABLE protocolStatsRate RENAME COLUMN hostID TO peerID; +ALTER TABLE protocolStatsTotals RENAME COLUMN hostID TO peerID;