trobleshooting and changeing formating

This commit is contained in:
Peder Vatn Austad
2024-12-26 17:46:05 +01:00
parent aecf92afbd
commit 83e8b7106e
3 changed files with 66 additions and 10 deletions

View File

@@ -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 { if err != nil {
return nil, err return nil, err
} }

View File

@@ -2,16 +2,47 @@ package database
import ( import (
"database/sql" "database/sql"
"errors" "fmt"
"math/rand"
) )
func InsertOrUpdateEstimatedCall(db *sql.DB, estimatedValues []string) (int, string, error) { func InsertOrUpdateEstimatedCall(db *sql.DB, values []interface{}) (int, string, error) {
if len(estimatedValues) == 0 { query := `
return 0, "", errors.New("no estimated values provided") 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) var action string
action := "tmp" 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 return id, action, nil
} }

View File

@@ -142,19 +142,40 @@ func DBData(data *data.Data) {
} else { } else {
fmt.Printf("Action: %s, ID: %d\n", action, id) fmt.Printf("Action: %s, ID: %d\n", action, id)
} }
var estimatedValues []interface{}
for _, estimatedCall := range journey.EstimatedCalls { for _, estimatedCall := range journey.EstimatedCalls {
for _, call := range estimatedCall.EstimatedCall { for _, call := range estimatedCall.EstimatedCall {
var estimatedValues []interface{}
//1 estimatedvehiclejourney
estimatedValues = append(estimatedValues, id) estimatedValues = append(estimatedValues, id)
//2 order
estimatedValues = append(estimatedValues, call.Order) estimatedValues = append(estimatedValues, call.Order)
//3 stoppointref
estimatedValues = append(estimatedValues, call.StopPointRef) estimatedValues = append(estimatedValues, call.StopPointRef)
//4 aimeddeparturetime
estimatedValues = append(estimatedValues, call.AimedDepartureTime) estimatedValues = append(estimatedValues, call.AimedDepartureTime)
//5 expecteddeparturetime
estimatedValues = append(estimatedValues, call.ExpectedDepartureTime) estimatedValues = append(estimatedValues, call.ExpectedDepartureTime)
//6 aimedarrivaltime
estimatedValues = append(estimatedValues, call.AimedArrivalTime) estimatedValues = append(estimatedValues, call.AimedArrivalTime)
//7 expectedarrivaltime
estimatedValues = append(estimatedValues, call.ExpectedArrivalTime) estimatedValues = append(estimatedValues, call.ExpectedArrivalTime)
//8 cancellation
estimatedValues = append(estimatedValues, call.Cancellation) estimatedValues = append(estimatedValues, call.Cancellation)
//9 estimated_data (JSON)
estimatedJsonObject := make(map[string]interface{}) 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 != "" { if call.StopPointName != "" {
estimatedJsonObject["StopPointName"] = call.StopPointName estimatedJsonObject["StopPointName"] = call.StopPointName
} }
@@ -237,7 +258,11 @@ func DBData(data *data.Data) {
for i, v := range estimatedValues { for i, v := range estimatedValues {
stringValues[i] = fmt.Sprintf("%v", v) 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 { if err != nil {
fmt.Printf("Error inserting/updating estimated call: %v\n", err) fmt.Printf("Error inserting/updating estimated call: %v\n", err)
} else { } else {