goroutines??? trying to make go fast lol

This commit is contained in:
pigwin
2025-01-04 20:11:30 +00:00
parent c12ec02270
commit 7b96214476

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"log"
"strings"
"sync"
"ti1/config"
"ti1/data"
"ti1/database"
@@ -30,14 +31,20 @@ func DBData(data *data.Data) {
// counters
var insertCount, updateCount, totalCount, estimatedCallInsertCount, estimatedCallUpdateCount, recordedCallInsertCount, recordedCallUpdateCount int
var wg sync.WaitGroup
var mu sync.Mutex
for _, journey := range data.ServiceDelivery.EstimatedTimetableDelivery[0].EstimatedJourneyVersionFrame.EstimatedVehicleJourney {
wg.Add(1)
go func(journey data.EstimatedVehicleJourney) {
defer wg.Done()
var values []interface{}
var datedVehicleJourneyRef, otherJson string
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)
@@ -145,18 +152,15 @@ func DBData(data *data.Data) {
if err != nil {
fmt.Printf("Error inserting/updating estimated vehicle journey: %v\n", err)
} else {
if 1 == 0 {
fmt.Printf("Action: %s, ID: %d\n", action, id)
}
mu.Lock()
if action == "insert" {
insertCount++
} else if action == "update" {
updateCount++
}
totalCount = insertCount + updateCount
mu.Unlock()
//fmt.Printf("Inserts: %d, Updates: %d, Total: %d\n", insertCount, updateCount, totalCount)
if totalCount%1000 == 0 {
fmt.Printf(
"Inserts: %d, Updates: %d, Total: %d; estimatedCalls = I: %d U: %d; recordedCalls = I: %d U: %d\n",
@@ -173,6 +177,10 @@ func DBData(data *data.Data) {
for _, estimatedCall := range journey.EstimatedCalls {
for _, call := range estimatedCall.EstimatedCall {
wg.Add(1)
go func(call data.EstimatedCall) {
defer wg.Done()
var estimatedValues []interface{}
//1 estimatedvehiclejourney
@@ -194,7 +202,7 @@ func DBData(data *data.Data) {
//9 estimated_data (JSON)
estimatedJsonObject := make(map[string]interface{})
// data allrady loged
// data already logged
if call.ExpectedDepartureTime != "" {
estimatedJsonObject["ExpectedDepartureTime"] = call.ExpectedDepartureTime
}
@@ -283,32 +291,28 @@ func DBData(data *data.Data) {
estimatedValues = append(estimatedValues, string(jsonString))
// Insert or update the record
stringValues := make([]string, len(estimatedValues))
for i, v := range estimatedValues {
stringValues[i] = fmt.Sprintf("%v", v)
}
interfaceValues := make([]interface{}, len(stringValues))
for i, v := range stringValues {
interfaceValues[i] = v
}
id, action, err := database.InsertOrUpdateEstimatedCall(db, interfaceValues)
id, action, err := database.InsertOrUpdateEstimatedCall(db, estimatedValues)
if err != nil {
fmt.Printf("Error inserting/updating estimated call: %v\n", err)
} else {
if 1 == 0 {
fmt.Printf("Action: %s, ID: %d\n", action, id)
}
mu.Lock()
if action == "insert" {
estimatedCallInsertCount++
} else if action == "update" {
estimatedCallUpdateCount++
}
mu.Unlock()
}
}(call)
}
}
}
for _, recordedCall := range journey.RecordedCalls {
for _, call := range recordedCall.RecordedCall {
wg.Add(1)
go func(call data.RecordedCall) {
defer wg.Done()
var recordedValues []interface{}
//1 estimatedvehiclejourney
@@ -358,34 +362,26 @@ func DBData(data *data.Data) {
recordedValues = append(recordedValues, string(jsonString))
// Insert or update the record
stringValues := make([]string, len(recordedValues))
for i, v := range recordedValues {
stringValues[i] = fmt.Sprintf("%v", v)
}
interfaceValues := make([]interface{}, len(stringValues))
for i, v := range stringValues {
interfaceValues[i] = v
}
id, action, err := database.InsertOrUpdateRecordedCall(db, interfaceValues)
id, action, err := database.InsertOrUpdateRecordedCall(db, recordedValues)
if err != nil {
fmt.Printf("Error inserting/updating recorded call: %v\n", err)
} else {
if 1 == 0 {
fmt.Printf("Action: %s, ID: %d\n", action, id)
}
mu.Lock()
if action == "insert" {
recordedCallInsertCount++
//fmt.Printf("Action: %s, ID: %d\n", action, id)
} else if action == "update" {
recordedCallUpdateCount++
}
mu.Unlock()
}
}(call)
}
}
}(journey)
}
}
wg.Wait()
fmt.Printf(
"DONE: Inserts: %d, Updates: %d, Total: %d; estimatedCalls = I: %d U: %d; recordedCalls = I: %d U: %d\n",
insertCount,
@@ -396,6 +392,7 @@ func DBData(data *data.Data) {
recordedCallInsertCount,
recordedCallUpdateCount,
)
// Create map to hold JSON
serviceDeliveryJsonObject := make(map[string]interface{})