Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gsanchietti committed Aug 21, 2024
1 parent 66b790c commit fed3fb4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
1 change: 1 addition & 0 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func main() {

// init storage
storage.Init()
storage.InitReportDb()

// init socket connection
socket.Init()
Expand Down
20 changes: 16 additions & 4 deletions api/storage/report_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@
*
* author: Giacomo Sanchietti <[email protected]>
*/

CREATE TABLE IF NOT EXISTS units (
CREATE TABLE IF NOT EXISTS units (
id SERIAL PRIMARY KEY,
uuid TEXT,
uuid TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
);

CREATE TABLE IF NOT EXISTS mwan_events (
time TIMESTAMPTZ NOT NULL,
unit_id INTEGER NOT NULL references units(id),
wan TEXT NOT NULL,
event TEXT NOT NULL,
interface TEXT,
UNIQUE (time, unit_id),
CONSTRAINT fk_unit FOREIGN KEY(unit_id) REFERENCES units(id) ON DELETE CASCADE
);

SELECT
create_hypertable('mwan_events', by_range('time'), if_not_exists => TRUE);
17 changes: 3 additions & 14 deletions api/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,24 +284,13 @@ func InitReportDb() *pgxpool.Pool {
}

// execute create tables
_, errExecute := db.Exec(reportSchemaSQL)
_, errExecute := dbpool.Exec(ctx, reportSchemaSQL)
if errExecute != nil {
logs.Logs.Println("[ERR][STORAGE] error in storage file schema init:" + errExecute.Error())
} else {
logs.Logs.Println("[INFO] Report schema initialized")
}

// check if units table exists, create if not
_, err = dbpool.Exec(ctx, "CREATE TABLE IF NOT EXISTS units (id SERIAL PRIMARY KEY, uuid TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)")

// mwan time series table
// {
// "timestamp": 1724221238,
// "wan": "wan",
// "interface": "eth1",
// "event": "offline"
// },

_, err = dbpool.Exec(ctx, "CREATE TABLE IF NOT EXISTS mwan_series (id SERIAL PRIMARY KEY, timestamp BIGINT, wan TEXT, interface TEXT, event TEXT)")

return dbpool
}

Expand Down

0 comments on commit fed3fb4

Please sign in to comment.