From 4f01023df730973f2b6e75ae12c434722c87e0eb Mon Sep 17 00:00:00 2001 From: Peder Vatn Austad Date: Sun, 26 May 2024 21:03:20 +0200 Subject: [PATCH] 0.2.4 EstVeJurnah gota get it to return its id tho nut other than that diz good --- data/data.go | 2 +- database/EstimatedVehicleJourney.go | 38 ++++ database/ServiceDeliveryDB.go | 5 +- export/database.go | 262 ++++------------------------ main.go | 2 +- 5 files changed, 74 insertions(+), 235 deletions(-) create mode 100644 database/EstimatedVehicleJourney.go diff --git a/data/data.go b/data/data.go index 5e42599..016c7c5 100644 --- a/data/data.go +++ b/data/data.go @@ -127,7 +127,7 @@ type Data struct { // FetchData function to retrieve data from API func FetchData() (*Data, error) { - resp, err := http.Get("https://api.entur.io/realtime/v1/rest/et?maxSize=0") + resp, err := http.Get("https://api.entur.io/realtime/v1/rest/et?maxSize=10000") if err != nil { return nil, err } diff --git a/database/EstimatedVehicleJourney.go b/database/EstimatedVehicleJourney.go new file mode 100644 index 0000000..aa9eb81 --- /dev/null +++ b/database/EstimatedVehicleJourney.go @@ -0,0 +1,38 @@ +package database + +import ( + "database/sql" + "fmt" +) + +func InsertOrUpdateEstimatedVehicleJourney(db *sql.DB, values []interface{}) error { + query := ` + INSERT INTO estimatedvehiclejourney (servicedelivery, recordedattime, lineref, directionref, datasource, datedvehiclejourneyref, vehiclemode, dataframeref, originref, destinationref, operatorref, vehicleref, cancellation, other) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) + ON CONFLICT (lineref, directionref, datasource, datedvehiclejourneyref) + DO UPDATE SET + servicedelivery = EXCLUDED.servicedelivery, + recordedattime = EXCLUDED.recordedattime, + vehiclemode = COALESCE(EXCLUDED.vehiclemode, estimatedvehiclejourney.vehiclemode), + dataframeref = COALESCE(EXCLUDED.dataframeref, estimatedvehiclejourney.dataframeref), + originref = COALESCE(EXCLUDED.originref, estimatedvehiclejourney.originref), + destinationref = COALESCE(EXCLUDED.destinationref, estimatedvehiclejourney.destinationref), + operatorref = COALESCE(EXCLUDED.operatorref, estimatedvehiclejourney.operatorref), + vehicleref = COALESCE(EXCLUDED.vehicleref, estimatedvehiclejourney.vehicleref), + cancellation = COALESCE(EXCLUDED.cancellation, estimatedvehiclejourney.cancellation), + other = COALESCE(EXCLUDED.other, estimatedvehiclejourney.other); + ` + + stmt, err := db.Prepare(query) + if err != nil { + return fmt.Errorf("error preparing statement: %v", err) + } + defer stmt.Close() + + _, err = stmt.Exec(values...) + if err != nil { + return fmt.Errorf("error executing statement: %v", err) + } + + return nil +} diff --git a/database/ServiceDeliveryDB.go b/database/ServiceDeliveryDB.go index 178fa4b..deba70b 100644 --- a/database/ServiceDeliveryDB.go +++ b/database/ServiceDeliveryDB.go @@ -10,11 +10,10 @@ func InsertServiceDelivery(db *sql.DB, responseTimestamp string, recordedAtTime var id int err := db.QueryRow("INSERT INTO public.ServiceDelivery (ResponseTimestamp, RecordedAtTime) VALUES ($1, $2) RETURNING ID", responseTimestamp, recordedAtTime).Scan(&id) - fmt.Println(err) if err != nil { + fmt.Println(err) return 0, err } - fmt.Println("ID:", id) - fmt.Println("ServiceDelivery inserted successfully!") + fmt.Println("ServiceDelivery inserted successfully! (", id, ")") return id, nil } diff --git a/export/database.go b/export/database.go index 01469b5..860683b 100644 --- a/export/database.go +++ b/export/database.go @@ -12,59 +12,46 @@ import ( func DBData(data *data.Data) { fmt.Println(data.ServiceDelivery.ResponseTimestamp) fmt.Println(data.ServiceDelivery.EstimatedTimetableDelivery[0].EstimatedJourneyVersionFrame.RecordedAtTime) - config.ConnectToPostgreSQL() db, err := config.ConnectToPostgreSQL() if err != nil { log.Fatal(err) } - //log.Printf("DB: %+v", db) + defer db.Close() - database.InsertServiceDelivery(db, data.ServiceDelivery.ResponseTimestamp, data.ServiceDelivery.EstimatedTimetableDelivery[0].EstimatedJourneyVersionFrame.RecordedAtTime) + // Get service id aka sid + sid, err := database.InsertServiceDelivery(db, data.ServiceDelivery.ResponseTimestamp, data.ServiceDelivery.EstimatedTimetableDelivery[0].EstimatedJourneyVersionFrame.RecordedAtTime) + if err != nil { + log.Fatal(err) + } + fmt.Println("SID:", sid) for _, journey := range data.ServiceDelivery.EstimatedTimetableDelivery[0].EstimatedJourneyVersionFrame.EstimatedVehicleJourney { - if journey.RecordedAtTime != "" { - fmt.Println(journey.RecordedAtTime) - } - if journey.LineRef != "" { - fmt.Println(journey.LineRef) - } - if journey.DirectionRef != "" { - fmt.Println(journey.DirectionRef) - } - if journey.DataSource != "" { - fmt.Println(journey.DataSource) - } + var values []interface{} + var datedVehicleJourneyRef, otherJson string + + values = append(values, sid) + values = append(values, journey.RecordedAtTime) + values = append(values, journey.LineRef) + values = append(values, journey.DirectionRef) + values = append(values, journey.DataSource) if journey.FramedVehicleJourneyRef.DatedVehicleJourneyRef != "" { - fmt.Println(journey.FramedVehicleJourneyRef.DatedVehicleJourneyRef) + datedVehicleJourneyRef = journey.FramedVehicleJourneyRef.DatedVehicleJourneyRef } else if journey.DatedVehicleJourneyRef != "" { - fmt.Println(journey.DatedVehicleJourneyRef) + datedVehicleJourneyRef = journey.DatedVehicleJourneyRef } else { - fmt.Println("evj." + journey.EstimatedVehicleJourneyCode) + datedVehicleJourneyRef = "evj." + journey.EstimatedVehicleJourneyCode } + values = append(values, datedVehicleJourneyRef) - if journey.VehicleMode != "" { - fmt.Println(journey.VehicleMode) - } - if journey.FramedVehicleJourneyRef.DataFrameRef != "" { - fmt.Println(journey.FramedVehicleJourneyRef.DataFrameRef) - } - if journey.OriginRef != "" { - fmt.Println(journey.OriginRef) - } - if journey.DestinationRef != "" { - fmt.Println(journey.DestinationRef) - } - if journey.OperatorRef != "" { - fmt.Println(journey.OperatorRef) - } - if journey.VehicleRef != "" { - fmt.Println(journey.VehicleRef) - } - if journey.Cancellation != "" { - fmt.Println(journey.Cancellation) - } + values = append(values, journey.VehicleMode) + values = append(values, journey.FramedVehicleJourneyRef.DataFrameRef) + values = append(values, journey.OriginRef) + values = append(values, journey.DestinationRef) + values = append(values, journey.OperatorRef) + values = append(values, journey.VehicleRef) + values = append(values, journey.Cancellation) // Create a map to hold the JSON object for the current journey jsonObject := make(map[string]interface{}) @@ -73,91 +60,69 @@ func DBData(data *data.Data) { if journey.OriginName != "" { jsonObject["OriginName"] = journey.OriginName } - if journey.DestinationName != "" { jsonObject["DestinationName"] = journey.DestinationName } - if journey.ProductCategoryRef != "" { jsonObject["ProductCategoryRef"] = journey.ProductCategoryRef } - if journey.ServiceFeatureRef != "" { jsonObject["ServiceFeatureRef"] = journey.ServiceFeatureRef } - if journey.Monitored != "" { jsonObject["Monitored"] = journey.Monitored } - if journey.JourneyPatternRef != "" { jsonObject["JourneyPatternRef"] = journey.JourneyPatternRef } - if journey.JourneyPatternName != "" { jsonObject["JourneyPatternName"] = journey.JourneyPatternName } - if journey.PublishedLineName != "" { jsonObject["PublishedLineName"] = journey.PublishedLineName } - if journey.DirectionName != "" { jsonObject["DirectionName"] = journey.DirectionName } - if journey.OriginAimedDepartureTime != "" { jsonObject["OriginAimedDepartureTime"] = journey.OriginAimedDepartureTime } - if journey.DestinationAimedArrivalTime != "" { jsonObject["DestinationAimedArrivalTime"] = journey.DestinationAimedArrivalTime } - if journey.BlockRef != "" { jsonObject["BlockRef"] = journey.BlockRef } - if journey.VehicleJourneyRef != "" { jsonObject["VehicleJourneyRef"] = journey.VehicleJourneyRef } - if journey.Occupancy != "" { jsonObject["Occupancy"] = journey.Occupancy } - if journey.DestinationDisplayAtOrigin != "" { jsonObject["DestinationDisplayAtOrigin"] = journey.DestinationDisplayAtOrigin } - if journey.ExtraJourney != "" { jsonObject["ExtraJourney"] = journey.ExtraJourney } - if journey.RouteRef != "" { jsonObject["RouteRef"] = journey.RouteRef } - if journey.GroupOfLinesRef != "" { jsonObject["GroupOfLinesRef"] = journey.GroupOfLinesRef } - if journey.ExternalLineRef != "" { jsonObject["ExternalLineRef"] = journey.ExternalLineRef } - if journey.InCongestion != "" { jsonObject["InCongestion"] = journey.InCongestion } - if journey.PredictionInaccurate != "" { jsonObject["PredictionInaccurate"] = journey.PredictionInaccurate } - if journey.JourneyNote != "" { jsonObject["JourneyNote"] = journey.JourneyNote } - if journey.Via.PlaceName != "" { jsonObject["Via"] = journey.Via.PlaceName } @@ -167,176 +132,13 @@ func DBData(data *data.Data) { if err != nil { log.Fatal(err) } + otherJson = string(jsonString) + values = append(values, otherJson) - // Print the JSON string for the current journey - fmt.Println(string(jsonString)) - - fmt.Println("Calls:") - for _, recordedCall := range journey.RecordedCalls { - for _, call := range recordedCall.RecordedCall { - if call.StopPointRef != "" { - fmt.Println("RecordedCall StopPointRef:", call.StopPointRef) - } - if call.Order != "" { - fmt.Println("RecordedCall Order:", call.Order) - } - if call.Cancellation != "" { - fmt.Println("RecordedCall Cancellation:", call.Cancellation) - } - if call.AimedDepartureTime != "" { - fmt.Println("RecordedCall AimedDepartureTime:", call.AimedDepartureTime) - } - if call.ActualDepartureTime != "" { - fmt.Println("RecordedCall ActualDepartureTime:", call.ActualDepartureTime) - } - if call.AimedArrivalTime != "" { - fmt.Println("RecordedCall AimedArrivalTime:", call.AimedArrivalTime) - } - if call.ActualArrivalTime != "" { - fmt.Println("RecordedCall ActualArrivalTime:", call.ActualArrivalTime) - } - if call.ExpectedArrivalTime != "" { - fmt.Println("RecordedCall ExpectedArrivalTime:", call.ExpectedArrivalTime) - } - if call.ExpectedDepartureTime != "" { - fmt.Println("RecordedCall ExpectedDepartureTime:", call.ExpectedDepartureTime) - } - - jsonObjectRC := make(map[string]interface{}) - - if call.StopPointName != "" { - jsonObjectRC["StopPointName"] = call.StopPointName - } - if call.ArrivalPlatformName != "" { - jsonObjectRC["ArrivalPlatformName"] = call.ArrivalPlatformName - } - if call.DeparturePlatformName != "" { - jsonObjectRC["DeparturePlatformName"] = call.DeparturePlatformName - } - if call.PredictionInaccurate != "" { - jsonObjectRC["PredictionInaccurate"] = call.PredictionInaccurate - } - if call.Occupancy != "" { - jsonObjectRC["Occupancy"] = call.Occupancy - } - - // Convert the JSON object to a JSON string - jsonString, err := json.Marshal(jsonObjectRC) - if err != nil { - log.Fatal(err) - } - - // Print the JSON string for the current journey - fmt.Println(string(jsonString)) - } + // Insert or update the record + err = database.InsertOrUpdateEstimatedVehicleJourney(db, values) + if err != nil { + fmt.Printf("Error inserting/updating estimated vehicle journey: %v\n", err) } - for _, estimatedCall := range journey.EstimatedCalls { - for _, call := range estimatedCall.EstimatedCall { - if call.StopPointRef != "" { - fmt.Println("EstimatedCall StopPointRef:", call.StopPointRef) - } - if call.Order != "" { - fmt.Println("EstimatedCall Order:", call.Order) - } - if call.Cancellation != "" { - fmt.Println("EstimatedCall Cancellation:", call.Cancellation) - } - if call.AimedDepartureTime != "" { - fmt.Println("EstimatedCall AimedDepartureTime:", call.AimedDepartureTime) - } - if call.AimedArrivalTime != "" { - fmt.Println("EstimatedCall AimedArrivalTime:", call.AimedArrivalTime) - } - if call.ExpectedArrivalTime != "" { - fmt.Println("EstimatedCall ExpectedArrivalTime:", call.ExpectedArrivalTime) - } - if call.ExpectedDepartureTime != "" { - fmt.Println("EstimatedCall ExpectedDepartureTime:", call.ExpectedDepartureTime) - } - - jsonObjectEC := make(map[string]interface{}) - - if call.StopPointName != "" { - jsonObjectEC["StopPointName"] = call.StopPointName - } - if call.RequestStop != "" { - jsonObjectEC["RequestStop"] = call.RequestStop - } - if call.DepartureStatus != "" { - jsonObjectEC["DepartureStatus"] = call.DepartureStatus - } - if call.DeparturePlatformName != "" { - jsonObjectEC["DeparturePlatformName"] = call.DeparturePlatformName - } - if call.DepartureBoardingActivity != "" { - jsonObjectEC["DepartureBoardingActivity"] = call.DepartureBoardingActivity - } - if call.ArrivalStatus != "" { - jsonObjectEC["ArrivalStatus"] = call.ArrivalStatus - } - if call.ArrivalPlatformName != "" { - jsonObjectEC["ArrivalPlatformName"] = call.ArrivalPlatformName - } - if call.ArrivalBoardingActivity != "" { - jsonObjectEC["ArrivalBoardingActivity"] = call.ArrivalBoardingActivity - } - if call.CallNote != "" { - jsonObjectEC["CallNote"] = call.CallNote - } - if call.DestinationDisplay != "" { - jsonObjectEC["DestinationDisplay"] = call.DestinationDisplay - } - if call.TimingPoint != "" { - jsonObjectEC["TimingPoint"] = call.TimingPoint - } - if call.SituationRef != "" { - jsonObjectEC["SituationRef"] = call.SituationRef - } - if call.PredictionInaccurate != "" { - jsonObjectEC["PredictionInaccurate"] = call.PredictionInaccurate - } - if call.Occupancy != "" { - jsonObjectEC["Occupancy"] = call.Occupancy - } - if call.DepartureStopAssignment.AimedQuayRef != "" { - jsonObjectEC["DepartureAimedQuayRef"] = call.DepartureStopAssignment.AimedQuayRef - } - if call.DepartureStopAssignment.ExpectedQuayRef != "" { - jsonObjectEC["DepartureExpectedQuayRef"] = call.DepartureStopAssignment.ExpectedQuayRef - } - if call.DepartureStopAssignment.ActualQuayRef != "" { - jsonObjectEC["DepartureActualQuayRef"] = call.DepartureStopAssignment.ActualQuayRef - } - if call.Extensions.StopsAtAirport != "" { - jsonObjectEC["StopsAtAirport"] = call.Extensions.StopsAtAirport - } - if call.ArrivalStopAssignment.AimedQuayRef != "" { - jsonObjectEC["ArrivalAimedQuayRef"] = call.ArrivalStopAssignment.AimedQuayRef - } - if call.ArrivalStopAssignment.ExpectedQuayRef != "" { - jsonObjectEC["ArrivalExpectedQuayRef"] = call.ArrivalStopAssignment.ExpectedQuayRef - } - if call.ArrivalStopAssignment.ActualQuayRef != "" { - jsonObjectEC["ArrivalActualQuayRef"] = call.ArrivalStopAssignment.ActualQuayRef - } - if call.ExpectedDeparturePredictionQuality.PredictionLevel != "" { - jsonObjectEC["ExpectedDeparturePredictionLevel"] = call.ExpectedDeparturePredictionQuality.PredictionLevel - } - - if call.ExpectedArrivalPredictionQuality.PredictionLevel != "" { - jsonObjectEC["ExpectedArrivalPredictionLevel"] = call.ExpectedArrivalPredictionQuality.PredictionLevel - } - - jsonString, err := json.Marshal(jsonObjectEC) - - if err != nil { - log.Fatal(err) - } - - fmt.Println(string(jsonString)) - - } - } - } } diff --git a/main.go b/main.go index 57b4e9f..98fafa3 100644 --- a/main.go +++ b/main.go @@ -19,5 +19,5 @@ func main() { //log.Printf("Data fetched successfully: %+v", data) - export.PrintData(data) + //export.PrintData(data) }