From 83e8b7106ea39170ef0eb9c1f291c83cc3a39ee3 Mon Sep 17 00:00:00 2001 From: Peder Vatn Austad Date: Thu, 26 Dec 2024 17:46:05 +0100 Subject: [PATCH] trobleshooting and changeing formating --- data/data.go | 2 +- database/EstimatedCall.go | 45 +++++++++++++++++++++++++++++++++------ export/database.go | 29 +++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 10 deletions(-) diff --git a/data/data.go b/data/data.go index adea9d1..1d162d6 100644 --- a/data/data.go +++ b/data/data.go @@ -169,7 +169,7 @@ func FetchData() (*Data, error) { }, } - resp, err := client.Get("https://api.entur.io/realtime/v1/rest/et?useOriginalId=true&maxSize=10000") + resp, err := client.Get("https://api.entur.io/realtime/v1/rest/et?useOriginalId=true&maxSize=10") if err != nil { return nil, err } diff --git a/database/EstimatedCall.go b/database/EstimatedCall.go index 772c65d..ddb6f88 100644 --- a/database/EstimatedCall.go +++ b/database/EstimatedCall.go @@ -2,16 +2,47 @@ package database import ( "database/sql" - "errors" - "math/rand" + "fmt" ) -func InsertOrUpdateEstimatedCall(db *sql.DB, estimatedValues []string) (int, string, error) { - if len(estimatedValues) == 0 { - return 0, "", errors.New("no estimated values provided") +func InsertOrUpdateEstimatedCall(db *sql.DB, values []interface{}) (int, string, error) { + query := ` + INSERT INTO calls ( + estimatedvehiclejourney, "order", stoppointref, + aimeddeparturetime, expecteddeparturetime, + aimedarrivaltime, expectedarrivaltime, + cancellation, estimated_data + ) + VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9) + ON CONFLICT (estimatedvehiclejourney, "order") + DO UPDATE SET + stoppointref = EXCLUDED.stoppointref, + aimeddeparturetime = EXCLUDED.aimeddeparturetime, + expecteddeparturetime = EXCLUDED.expecteddeparturetime, + aimedarrivaltime = EXCLUDED.aimedarrivaltime, + expectedarrivaltime = EXCLUDED.expectedarrivaltime, + cancellation = EXCLUDED.cancellation, + estimated_data = EXCLUDED.estimated_data + RETURNING CASE WHEN xmax = 0 THEN 'insert' ELSE 'update' END, id; + ` + stmt, err := db.Prepare(query) + if err != nil { + return 0, "", fmt.Errorf("error preparing statement: %v", err) } + defer stmt.Close() - id := rand.Intn(100) - action := "tmp" + var action string + var id int + err = stmt.QueryRow(values...).Scan(&action, &id) + if err != nil { + if 1 == 0 { + fmt.Println("Executing query:", query) + for i, v := range values { + fmt.Printf("Value %d: (%v)\n", i+1, v) + } + + } + return 0, "", fmt.Errorf("error executing statement: %v", err) + } return id, action, nil } diff --git a/export/database.go b/export/database.go index 79a3dc2..735c779 100644 --- a/export/database.go +++ b/export/database.go @@ -142,19 +142,40 @@ func DBData(data *data.Data) { } else { fmt.Printf("Action: %s, ID: %d\n", action, id) } - var estimatedValues []interface{} for _, estimatedCall := range journey.EstimatedCalls { for _, call := range estimatedCall.EstimatedCall { + var estimatedValues []interface{} + + //1 estimatedvehiclejourney estimatedValues = append(estimatedValues, id) + //2 order estimatedValues = append(estimatedValues, call.Order) + //3 stoppointref estimatedValues = append(estimatedValues, call.StopPointRef) + //4 aimeddeparturetime estimatedValues = append(estimatedValues, call.AimedDepartureTime) + //5 expecteddeparturetime estimatedValues = append(estimatedValues, call.ExpectedDepartureTime) + //6 aimedarrivaltime estimatedValues = append(estimatedValues, call.AimedArrivalTime) + //7 expectedarrivaltime estimatedValues = append(estimatedValues, call.ExpectedArrivalTime) + //8 cancellation estimatedValues = append(estimatedValues, call.Cancellation) + //9 estimated_data (JSON) estimatedJsonObject := make(map[string]interface{}) + // data allrady loged + if call.ExpectedDepartureTime != "" { + estimatedJsonObject["ExpectedDepartureTime"] = call.ExpectedDepartureTime + } + if call.ExpectedArrivalTime != "" { + estimatedJsonObject["ExpectedArrivalTime"] = call.ExpectedArrivalTime + } + if call.Cancellation != "" { + estimatedJsonObject["Cancellation"] = call.Cancellation + } + // The rest if call.StopPointName != "" { estimatedJsonObject["StopPointName"] = call.StopPointName } @@ -237,7 +258,11 @@ func DBData(data *data.Data) { for i, v := range estimatedValues { stringValues[i] = fmt.Sprintf("%v", v) } - id, action, err := database.InsertOrUpdateEstimatedCall(db, stringValues) + interfaceValues := make([]interface{}, len(stringValues)) + for i, v := range stringValues { + interfaceValues[i] = v + } + id, action, err := database.InsertOrUpdateEstimatedCall(db, interfaceValues) if err != nil { fmt.Printf("Error inserting/updating estimated call: %v\n", err) } else {