Skip to content

Commit

Permalink
Dont remember lol...
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmaurice committed Feb 4, 2024
1 parent 710041d commit fd4dca7
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions satisfactory-metadata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,35 +63,41 @@ func main() {
pullMetrics(db, "storageInv", "/getStorageInv", false)
pullMetrics(db, "worldInv", "/getWorldInv", false)
pullMetrics(db, "droneStation", "/getDroneStation", false)
pullMetrics(db, "generators", "/getGenerators", false)

// Realtime metrics
pullMetrics(db, "drone", "/getDrone", true)
pullMetrics(db, "train", "/getTrains", true)
pullMetrics(db, "truck", "/getVehicles", true)
pullMetrics(db, "trainStation", "/getTrainStation", true)
pullMetrics(db, "truckStation", "/getTruckStation", true)

}

// flush the metric history cache
// initialize the database
func initDB(db *sql.DB) error {
req := `CREATE TABLE IF NOT EXISTS cache(
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS cache(
id serial primary key,
metric text NOT NULL,
frm_data jsonb
);
CREATE INDEX cache_metric ON cache(metric);
CREATE TABLE IF NOT EXISTS cache_with_history(
id serial primary key,
metric text NOT NULL,
frm_data jsonb,
time timestamp
);`
);
_, err := db.Exec(req)
CREATE INDEX IF NOT EXISTS cache_metric_idx ON cache(metric);
CREATE INDEX IF NOT EXISTS cache_with_history_metric_idx ON cache_with_history(metric);
`)
if err != nil {
fmt.Println("flush metrics history db error: ", err)
fmt.Println("Error while creating DB Tables : ", err)
return err
}
fmt.Println("DB Tables created successfully")
return err
}

Expand All @@ -110,15 +116,19 @@ func pullMetrics(db *sql.DB, metric string, route string, keepHistory bool) {
resp, err := http.Get(*frmApiAddress + route)

if err != nil {
fmt.Println("error when parsing json: %s", err)
fmt.Println("Error while querying Ficsit Remote Monitoring API: ", err)
return
}

var content []json.RawMessage
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&content)
if err != nil {
fmt.Println("error when parsing json: %s", err)
// Try to found if it is an empty object
if _, ok := err.(*json.UnmarshalTypeError); ok {
return
}
fmt.Println("Error while decoding Ficsit Remote Monitoring API response: ", err)
return
}
defer resp.Body.Close()
Expand All @@ -142,6 +152,7 @@ func pullMetrics(db *sql.DB, metric string, route string, keepHistory bool) {
return
}
}
fmt.Println("Successfully cached metrics for " + metric)
}

// cache metrics in the database
Expand Down

0 comments on commit fd4dca7

Please sign in to comment.