didnt work well

This commit is contained in:
pigwin
2025-01-04 21:26:09 +00:00
parent 3c1b84197a
commit 51fb986710
2 changed files with 385 additions and 437 deletions

View File

@@ -4,7 +4,6 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"log" "log"
"time"
_ "github.com/lib/pq" _ "github.com/lib/pq"
) )
@@ -29,15 +28,6 @@ func ConnectToPostgreSQL() (*sql.DB, error) {
fmt.Println("Connection to PostgreSQL opened successfully :D") fmt.Println("Connection to PostgreSQL opened successfully :D")
// Set the maximum number of open connections to 20
db.SetMaxOpenConns(20)
// Set the maximum number of idle connections to 10
db.SetMaxIdleConns(10)
// Set the maximum connection lifetime to 1 hour
db.SetConnMaxLifetime(1 * time.Hour)
// Ping database to verify connection // Ping database to verify connection
err = db.Ping() err = db.Ping()

View File

@@ -1,463 +1,421 @@
package export package export
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"strings" "strings"
"sync" "ti1/config"
"ti1/config" "ti1/data"
"ti1/data" "ti1/database"
"ti1/database"
) )
func DBData(data *data.Data) { func DBData(data *data.Data) {
fmt.Println(data.ServiceDelivery.ResponseTimestamp) fmt.Println(data.ServiceDelivery.ResponseTimestamp)
fmt.Println(data.ServiceDelivery.EstimatedTimetableDelivery[0].EstimatedJourneyVersionFrame.RecordedAtTime) fmt.Println(data.ServiceDelivery.EstimatedTimetableDelivery[0].EstimatedJourneyVersionFrame.RecordedAtTime)
db, err := config.ConnectToPostgreSQL() db, err := config.ConnectToPostgreSQL()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer db.Close() defer db.Close()
// Get service id aka sid // Get service id aka sid
sid, err := database.InsertServiceDelivery(db, data.ServiceDelivery.ResponseTimestamp, data.ServiceDelivery.EstimatedTimetableDelivery[0].EstimatedJourneyVersionFrame.RecordedAtTime) sid, err := database.InsertServiceDelivery(db, data.ServiceDelivery.ResponseTimestamp, data.ServiceDelivery.EstimatedTimetableDelivery[0].EstimatedJourneyVersionFrame.RecordedAtTime)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
fmt.Println("SID:", sid) fmt.Println("SID:", sid)
// counters // counters
var insertCount, updateCount, totalCount, estimatedCallInsertCount, estimatedCallUpdateCount, recordedCallInsertCount, recordedCallUpdateCount int var insertCount, updateCount, totalCount, estimatedCallInsertCount, estimatedCallUpdateCount, recordedCallInsertCount, recordedCallUpdateCount int
var mu sync.Mutex
// Create a worker pool for _, journey := range data.ServiceDelivery.EstimatedTimetableDelivery[0].EstimatedJourneyVersionFrame.EstimatedVehicleJourney {
numWorkers := 10 var values []interface{}
jobs := make(chan []interface{}, numWorkers) var datedVehicleJourneyRef, otherJson string
results := make(chan struct {
action string
id int
err error
}, numWorkers)
var wg sync.WaitGroup values = append(values, sid)
values = append(values, journey.RecordedAtTime)
values = append(values, journey.LineRef)
//had to add to lowercase cus some values vary in case and it was causing duplicates
values = append(values, strings.ToLower(journey.DirectionRef))
values = append(values, journey.DataSource)
// Worker function if journey.FramedVehicleJourneyRef.DatedVehicleJourneyRef != "" {
worker := func() { datedVehicleJourneyRef = journey.FramedVehicleJourneyRef.DatedVehicleJourneyRef
defer wg.Done() } else if journey.DatedVehicleJourneyRef != "" {
for values := range jobs { datedVehicleJourneyRef = journey.DatedVehicleJourneyRef
id, action, err := database.InsertOrUpdateEstimatedVehicleJourney(db, values) } else {
results <- struct { datedVehicleJourneyRef = "evj." + journey.EstimatedVehicleJourneyCode
action string }
id int values = append(values, datedVehicleJourneyRef)
err error
}{action, id, err}
}
}
// Start workers values = append(values, journey.VehicleMode)
for i := 0; i < numWorkers; i++ { values = append(values, journey.FramedVehicleJourneyRef.DataFrameRef)
wg.Add(1) values = append(values, journey.OriginRef)
go worker() values = append(values, journey.DestinationRef)
} values = append(values, journey.OperatorRef)
values = append(values, journey.VehicleRef)
values = append(values, journey.Cancellation)
// Send jobs to workers // Create a map to hold the JSON object for the current journey
go func() { jsonObject := make(map[string]interface{})
for _, journey := range data.ServiceDelivery.EstimatedTimetableDelivery[0].EstimatedJourneyVersionFrame.EstimatedVehicleJourney {
var values []interface{}
var datedVehicleJourneyRef, otherJson string
values = append(values, sid) // Add relevant fields to the JSON object
values = append(values, journey.RecordedAtTime) if journey.OriginName != "" {
values = append(values, journey.LineRef) jsonObject["OriginName"] = journey.OriginName
values = append(values, strings.ToLower(journey.DirectionRef)) }
values = append(values, journey.DataSource) 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
}
if journey.FramedVehicleJourneyRef.DatedVehicleJourneyRef != "" { // Convert the JSON object to a JSON string
datedVehicleJourneyRef = journey.FramedVehicleJourneyRef.DatedVehicleJourneyRef jsonString, err := json.Marshal(jsonObject)
} else if journey.DatedVehicleJourneyRef != "" { if err != nil {
datedVehicleJourneyRef = journey.DatedVehicleJourneyRef log.Fatal(err)
} else { }
datedVehicleJourneyRef = "evj." + journey.EstimatedVehicleJourneyCode otherJson = string(jsonString)
} values = append(values, otherJson)
values = append(values, datedVehicleJourneyRef)
values = append(values, journey.VehicleMode) // Insert or update the record
values = append(values, journey.FramedVehicleJourneyRef.DataFrameRef) id, action, err := database.InsertOrUpdateEstimatedVehicleJourney(db, values)
values = append(values, journey.OriginRef) if err != nil {
values = append(values, journey.DestinationRef) fmt.Printf("Error inserting/updating estimated vehicle journey: %v\n", err)
values = append(values, journey.OperatorRef) } else {
values = append(values, journey.VehicleRef) if 1 == 0 {
values = append(values, journey.Cancellation) fmt.Printf("Action: %s, ID: %d\n", action, id)
jsonObject := make(map[string]interface{})
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
}
jsonString, err := json.Marshal(jsonObject)
if err != nil {
log.Fatal(err)
}
otherJson = string(jsonString)
values = append(values, otherJson)
// Insert or update the record and get the id
id, action, err := database.InsertOrUpdateEstimatedVehicleJourney(db, values)
if err != nil {
fmt.Printf("Error inserting/updating estimated vehicle journey: %v\n", err)
continue
} else {
if 1 == 0 {
fmt.Printf("Action: %s, ID: %d\n", action, id)
}
} }
// Add the missing code here if action == "insert" {
for _, estimatedCall := range journey.EstimatedCalls { insertCount++
for _, call := range estimatedCall.EstimatedCall { } else if action == "update" {
var estimatedValues []interface{} updateCount++
}
totalCount = insertCount + updateCount
//1 estimatedvehiclejourney //fmt.Printf("Inserts: %d, Updates: %d, Total: %d\n", insertCount, updateCount, totalCount)
estimatedValues = append(estimatedValues, id) if totalCount%1000 == 0 {
//2 order fmt.Printf(
estimatedValues = append(estimatedValues, call.Order) "Inserts: %d, Updates: %d, Total: %d; estimatedCalls = I: %d U: %d; recordedCalls = I: %d U: %d\n",
//3 stoppointref insertCount,
estimatedValues = append(estimatedValues, call.StopPointRef) updateCount,
//4 aimeddeparturetime totalCount,
estimatedValues = append(estimatedValues, call.AimedDepartureTime) estimatedCallInsertCount,
//5 expecteddeparturetime estimatedCallUpdateCount,
estimatedValues = append(estimatedValues, call.ExpectedDepartureTime) recordedCallInsertCount,
//6 aimedarrivaltime recordedCallUpdateCount,
estimatedValues = append(estimatedValues, call.AimedArrivalTime) )
//7 expectedarrivaltime }
estimatedValues = append(estimatedValues, call.ExpectedArrivalTime) }
//8 cancellation
estimatedValues = append(estimatedValues, call.Cancellation)
//9 estimated_data (JSON) for _, estimatedCall := range journey.EstimatedCalls {
estimatedJsonObject := make(map[string]interface{}) for _, call := range estimatedCall.EstimatedCall {
// data already logged var estimatedValues []interface{}
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
}
if call.RequestStop != "" {
estimatedJsonObject["RequestStop"] = call.RequestStop
}
if call.DepartureStatus != "" {
estimatedJsonObject["DepartureStatus"] = call.DepartureStatus
}
if call.DeparturePlatformName != "" {
estimatedJsonObject["DeparturePlatformName"] = call.DeparturePlatformName
}
if call.DepartureBoardingActivity != "" {
estimatedJsonObject["DepartureBoardingActivity"] = call.DepartureBoardingActivity
}
if call.DepartureStopAssignment.AimedQuayRef != "" {
estimatedJsonObject["DepartureStopAssignment.AimedQuayRef"] = call.DepartureStopAssignment.AimedQuayRef
}
if call.DepartureStopAssignment.ExpectedQuayRef != "" {
estimatedJsonObject["DepartureStopAssignment.ExpectedQuayRef"] = call.DepartureStopAssignment.ExpectedQuayRef
}
if call.DepartureStopAssignment.ActualQuayRef != "" {
estimatedJsonObject["DepartureStopAssignment.ActualQuayRef"] = call.DepartureStopAssignment.ActualQuayRef
}
if call.Extensions.StopsAtAirport != "" {
estimatedJsonObject["Extensions.StopsAtAirport"] = call.Extensions.StopsAtAirport
}
if call.ArrivalStatus != "" {
estimatedJsonObject["ArrivalStatus"] = call.ArrivalStatus
}
if call.ArrivalPlatformName != "" {
estimatedJsonObject["ArrivalPlatformName"] = call.ArrivalPlatformName
}
if call.ArrivalBoardingActivity != "" {
estimatedJsonObject["ArrivalBoardingActivity"] = call.ArrivalBoardingActivity
}
if call.ArrivalStopAssignment.AimedQuayRef != "" {
estimatedJsonObject["ArrivalStopAssignment.AimedQuayRef"] = call.ArrivalStopAssignment.AimedQuayRef
}
if call.ArrivalStopAssignment.ExpectedQuayRef != "" {
estimatedJsonObject["ArrivalStopAssignment.ExpectedQuayRef"] = call.ArrivalStopAssignment.ExpectedQuayRef
}
if call.ArrivalStopAssignment.ActualQuayRef != "" {
estimatedJsonObject["ArrivalStopAssignment.ActualQuayRef"] = call.ArrivalStopAssignment.ActualQuayRef
}
if call.CallNote != "" {
estimatedJsonObject["CallNote"] = call.CallNote
}
if call.DestinationDisplay != "" {
estimatedJsonObject["DestinationDisplay"] = call.DestinationDisplay
}
if call.ExpectedDeparturePredictionQuality.PredictionLevel != "" {
estimatedJsonObject["ExpectedDeparturePredictionQuality.PredictionLevel"] = call.ExpectedDeparturePredictionQuality.PredictionLevel
}
if call.ExpectedArrivalPredictionQuality.PredictionLevel != "" {
estimatedJsonObject["ExpectedArrivalPredictionQuality.PredictionLevel"] = call.ExpectedArrivalPredictionQuality.PredictionLevel
}
if call.TimingPoint != "" {
estimatedJsonObject["TimingPoint"] = call.TimingPoint
}
if call.SituationRef != "" {
estimatedJsonObject["SituationRef"] = call.SituationRef
}
if call.PredictionInaccurate != "" {
estimatedJsonObject["PredictionInaccurate"] = call.PredictionInaccurate
}
if call.Occupancy != "" {
estimatedJsonObject["Occupancy"] = call.Occupancy
}
// Convert the JSON object to a JSON string //1 estimatedvehiclejourney
jsonString, err := json.Marshal(estimatedJsonObject) estimatedValues = append(estimatedValues, id)
if err != nil { //2 order
log.Fatal(err) estimatedValues = append(estimatedValues, call.Order)
} //3 stoppointref
estimatedValues = append(estimatedValues, string(jsonString)) 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)
// Insert or update the record //9 estimated_data (JSON)
stringValues := make([]string, len(estimatedValues)) estimatedJsonObject := make(map[string]interface{})
for i, v := range stringValues { // data allrady loged
stringValues[i] = fmt.Sprintf("%v", v) if call.ExpectedDepartureTime != "" {
} estimatedJsonObject["ExpectedDepartureTime"] = call.ExpectedDepartureTime
interfaceValues := make([]interface{}, len(stringValues)) }
for i, v := range stringValues { if call.ExpectedArrivalTime != "" {
interfaceValues[i] = v estimatedJsonObject["ExpectedArrivalTime"] = call.ExpectedArrivalTime
} }
id, action, err := database.InsertOrUpdateEstimatedCall(db, interfaceValues) if call.Cancellation != "" {
if err != nil { estimatedJsonObject["Cancellation"] = call.Cancellation
fmt.Printf("Error inserting/updating estimated call: %v\n", err) }
} else { // The rest
if 1 == 0 { if call.StopPointName != "" {
fmt.Printf("Action: %s, ID: %d\n", action, id) estimatedJsonObject["StopPointName"] = call.StopPointName
} }
if call.RequestStop != "" {
estimatedJsonObject["RequestStop"] = call.RequestStop
}
if call.DepartureStatus != "" {
estimatedJsonObject["DepartureStatus"] = call.DepartureStatus
}
if call.DeparturePlatformName != "" {
estimatedJsonObject["DeparturePlatformName"] = call.DeparturePlatformName
}
if call.DepartureBoardingActivity != "" {
estimatedJsonObject["DepartureBoardingActivity"] = call.DepartureBoardingActivity
}
if call.DepartureStopAssignment.AimedQuayRef != "" {
estimatedJsonObject["DepartureStopAssignment.AimedQuayRef"] = call.DepartureStopAssignment.AimedQuayRef
}
if call.DepartureStopAssignment.ExpectedQuayRef != "" {
estimatedJsonObject["DepartureStopAssignment.ExpectedQuayRef"] = call.DepartureStopAssignment.ExpectedQuayRef
}
if call.DepartureStopAssignment.ActualQuayRef != "" {
estimatedJsonObject["DepartureStopAssignment.ActualQuayRef"] = call.DepartureStopAssignment.ActualQuayRef
}
if call.Extensions.StopsAtAirport != "" {
estimatedJsonObject["Extensions.StopsAtAirport"] = call.Extensions.StopsAtAirport
}
if call.ArrivalStatus != "" {
estimatedJsonObject["ArrivalStatus"] = call.ArrivalStatus
}
if call.ArrivalPlatformName != "" {
estimatedJsonObject["ArrivalPlatformName"] = call.ArrivalPlatformName
}
if call.ArrivalBoardingActivity != "" {
estimatedJsonObject["ArrivalBoardingActivity"] = call.ArrivalBoardingActivity
}
if call.ArrivalStopAssignment.AimedQuayRef != "" {
estimatedJsonObject["ArrivalStopAssignment.AimedQuayRef"] = call.ArrivalStopAssignment.AimedQuayRef
}
if call.ArrivalStopAssignment.ExpectedQuayRef != "" {
estimatedJsonObject["ArrivalStopAssignment.ExpectedQuayRef"] = call.ArrivalStopAssignment.ExpectedQuayRef
}
if call.ArrivalStopAssignment.ActualQuayRef != "" {
estimatedJsonObject["ArrivalStopAssignment.ActualQuayRef"] = call.ArrivalStopAssignment.ActualQuayRef
}
if call.CallNote != "" {
estimatedJsonObject["CallNote"] = call.CallNote
}
if call.DestinationDisplay != "" {
estimatedJsonObject["DestinationDisplay"] = call.DestinationDisplay
}
if call.ExpectedDeparturePredictionQuality.PredictionLevel != "" {
estimatedJsonObject["ExpectedDeparturePredictionQuality.PredictionLevel"] = call.ExpectedDeparturePredictionQuality.PredictionLevel
}
if call.ExpectedArrivalPredictionQuality.PredictionLevel != "" {
estimatedJsonObject["ExpectedArrivalPredictionQuality.PredictionLevel"] = call.ExpectedArrivalPredictionQuality.PredictionLevel
}
if call.TimingPoint != "" {
estimatedJsonObject["TimingPoint"] = call.TimingPoint
}
if call.SituationRef != "" {
estimatedJsonObject["SituationRef"] = call.SituationRef
}
if call.PredictionInaccurate != "" {
estimatedJsonObject["PredictionInaccurate"] = call.PredictionInaccurate
}
if call.Occupancy != "" {
estimatedJsonObject["Occupancy"] = call.Occupancy
}
if action == "insert" { // Convert the JSON object to a JSON string
estimatedCallInsertCount++ jsonString, err := json.Marshal(estimatedJsonObject)
} else if action == "update" { if err != nil {
estimatedCallUpdateCount++ log.Fatal(err)
} }
} estimatedValues = append(estimatedValues, string(jsonString))
}
}
for _, recordedCall := range journey.RecordedCalls {
for _, call := range recordedCall.RecordedCall {
var recordedValues []interface{}
//1 estimatedvehiclejourney // Insert or update the record
recordedValues = append(recordedValues, id) stringValues := make([]string, len(estimatedValues))
//2 order for i, v := range estimatedValues {
recordedValues = append(recordedValues, call.Order) stringValues[i] = fmt.Sprintf("%v", v)
//3 stoppointref }
recordedValues = append(recordedValues, call.StopPointRef) interfaceValues := make([]interface{}, len(stringValues))
//4 aimeddeparturetime for i, v := range stringValues {
recordedValues = append(recordedValues, call.AimedDepartureTime) interfaceValues[i] = v
//5 expecteddeparturetime }
recordedValues = append(recordedValues, call.ExpectedDepartureTime) id, action, err := database.InsertOrUpdateEstimatedCall(db, interfaceValues)
//6 aimedarrivaltime if err != nil {
recordedValues = append(recordedValues, call.AimedArrivalTime) fmt.Printf("Error inserting/updating estimated call: %v\n", err)
//7 expectedarrivaltime } else {
recordedValues = append(recordedValues, call.ExpectedArrivalTime) if 1 == 0 {
//8 cancellation fmt.Printf("Action: %s, ID: %d\n", action, id)
recordedValues = append(recordedValues, call.Cancellation) }
//9 actualdeparturetime
recordedValues = append(recordedValues, call.ActualDepartureTime)
//10 actualarrivaltime
recordedValues = append(recordedValues, call.ActualArrivalTime)
//11 recorded_data (JSON) if action == "insert" {
recordedJsonObject := make(map[string]interface{}) estimatedCallInsertCount++
if call.StopPointName != "" { } else if action == "update" {
recordedJsonObject["StopPointName"] = call.StopPointName estimatedCallUpdateCount++
} }
if call.ArrivalPlatformName != "" { }
recordedJsonObject["ArrivalPlatformName"] = call.ArrivalPlatformName }
} }
if call.DeparturePlatformName != "" { for _, recordedCall := range journey.RecordedCalls {
recordedJsonObject["DeparturePlatformName"] = call.DeparturePlatformName for _, call := range recordedCall.RecordedCall {
} var recordedValues []interface{}
if call.PredictionInaccurate != "" {
recordedJsonObject["PredictionInaccurate"] = call.PredictionInaccurate
}
if call.Occupancy != "" {
recordedJsonObject["Occupancy"] = call.Occupancy
}
// Convert the JSON object to a JSON string //1 estimatedvehiclejourney
jsonString, err := json.Marshal(recordedJsonObject) recordedValues = append(recordedValues, id)
if err != nil { //2 order
log.Fatal(err) recordedValues = append(recordedValues, call.Order)
} //3 stoppointref
recordedValues = append(recordedValues, string(jsonString)) recordedValues = append(recordedValues, call.StopPointRef)
//4 aimeddeparturetime
recordedValues = append(recordedValues, call.AimedDepartureTime)
//5 expecteddeparturetime
recordedValues = append(recordedValues, call.ExpectedDepartureTime)
//6 aimedarrivaltime
recordedValues = append(recordedValues, call.AimedArrivalTime)
//7 expectedarrivaltime
recordedValues = append(recordedValues, call.ExpectedArrivalTime)
//8 cancellation
recordedValues = append(recordedValues, call.Cancellation)
//9 actualdeparturetime
recordedValues = append(recordedValues, call.ActualDepartureTime)
//10 actualarrivaltime
recordedValues = append(recordedValues, call.ActualArrivalTime)
// Insert or update the record //11 recorded_data (JSON)
stringValues := make([]string, len(recordedValues)) recordedJsonObject := make(map[string]interface{})
for i, v := range stringValues { if call.StopPointName != "" {
stringValues[i] = fmt.Sprintf("%v", v) recordedJsonObject["StopPointName"] = call.StopPointName
} }
interfaceValues := make([]interface{}, len(stringValues)) if call.ArrivalPlatformName != "" {
for i, v := range stringValues { recordedJsonObject["ArrivalPlatformName"] = call.ArrivalPlatformName
interfaceValues[i] = v }
} if call.DeparturePlatformName != "" {
recordedJsonObject["DeparturePlatformName"] = call.DeparturePlatformName
}
if call.PredictionInaccurate != "" {
recordedJsonObject["PredictionInaccurate"] = call.PredictionInaccurate
}
if call.Occupancy != "" {
recordedJsonObject["Occupancy"] = call.Occupancy
}
id, action, err := database.InsertOrUpdateRecordedCall(db, interfaceValues) // Convert the JSON object to a JSON string
if err != nil { jsonString, err := json.Marshal(recordedJsonObject)
fmt.Printf("Error inserting/updating recorded call: %v\n", err) if err != nil {
} else { log.Fatal(err)
if 1 == 0 { }
fmt.Printf("Action: %s, ID: %d\n", action, id) recordedValues = append(recordedValues, string(jsonString))
}
if action == "insert" { // Insert or update the record
recordedCallInsertCount++ stringValues := make([]string, len(recordedValues))
//fmt.Printf("Action: %s, ID: %d\n", action, id) for i, v := range recordedValues {
} else if action == "update" { stringValues[i] = fmt.Sprintf("%v", v)
recordedCallUpdateCount++ }
} interfaceValues := make([]interface{}, len(stringValues))
} for i, v := range stringValues {
} interfaceValues[i] = v
} }
}
close(jobs)
}()
// Collect results id, action, err := database.InsertOrUpdateRecordedCall(db, interfaceValues)
go func() { if err != nil {
for result := range results { fmt.Printf("Error inserting/updating recorded call: %v\n", err)
if result.err != nil { } else {
fmt.Printf("Error inserting/updating estimated vehicle journey: %v\n", result.err) if 1 == 0 {
} else { fmt.Printf("Action: %s, ID: %d\n", action, id)
mu.Lock() }
if result.action == "insert" {
insertCount++
} else if result.action == "update" {
updateCount++
}
totalCount = insertCount + updateCount
mu.Unlock()
if totalCount%1000 == 0 { if action == "insert" {
fmt.Printf( recordedCallInsertCount++
"Inserts: %d, Updates: %d, Total: %d; estimatedCalls = I: %d U: %d; recordedCalls = I: %d U: %d\n", //fmt.Printf("Action: %s, ID: %d\n", action, id)
insertCount, } else if action == "update" {
updateCount, recordedCallUpdateCount++
totalCount, }
estimatedCallInsertCount, }
estimatedCallUpdateCount, }
recordedCallInsertCount, }
recordedCallUpdateCount,
)
}
}
}
}()
wg.Wait() }
close(results) fmt.Printf(
"DONE: Inserts: %d, Updates: %d, Total: %d; estimatedCalls = I: %d U: %d; recordedCalls = I: %d U: %d\n",
insertCount,
updateCount,
totalCount,
estimatedCallInsertCount,
estimatedCallUpdateCount,
recordedCallInsertCount,
recordedCallUpdateCount,
)
// Create map to hold JSON
serviceDeliveryJsonObject := make(map[string]interface{})
fmt.Printf( // Add fields to JSON
"DONE: Inserts: %d, Updates: %d, Total: %d; estimatedCalls = I: %d U: %d; recordedCalls = I: %d U: %d\n", serviceDeliveryJsonObject["Inserts"] = insertCount
insertCount, serviceDeliveryJsonObject["Updates"] = updateCount
updateCount, serviceDeliveryJsonObject["EstimatedCallInserts"] = estimatedCallInsertCount
totalCount, serviceDeliveryJsonObject["EstimatedCallUpdates"] = estimatedCallUpdateCount
estimatedCallInsertCount, serviceDeliveryJsonObject["RecordedCallInserts"] = recordedCallInsertCount
estimatedCallUpdateCount, serviceDeliveryJsonObject["RecordedCallUpdates"] = recordedCallUpdateCount
recordedCallInsertCount,
recordedCallUpdateCount,
)
serviceDeliveryJsonObject := make(map[string]interface{}) // Convert JSON object to JSON string
serviceDeliveryJsonObject["Inserts"] = insertCount serviceDeliveryJsonString, err := json.Marshal(serviceDeliveryJsonObject)
serviceDeliveryJsonObject["Updates"] = updateCount if err != nil {
serviceDeliveryJsonObject["EstimatedCallInserts"] = estimatedCallInsertCount log.Fatal(err)
serviceDeliveryJsonObject["EstimatedCallUpdates"] = estimatedCallUpdateCount }
serviceDeliveryJsonObject["RecordedCallInserts"] = recordedCallInsertCount
serviceDeliveryJsonObject["RecordedCallUpdates"] = recordedCallUpdateCount
serviceDeliveryJsonString, err := json.Marshal(serviceDeliveryJsonObject) // Update ServiceDelivery data in database
if err != nil { err = database.UpdateServiceDeliveryData(db, sid, string(serviceDeliveryJsonString))
log.Fatal(err) if err != nil {
} log.Fatal(err)
}
err = database.UpdateServiceDeliveryData(db, sid, string(serviceDeliveryJsonString)) }
if err != nil {
log.Fatal(err)
}
}