wtf so mutch pain cus i named a package wrong lol

This commit is contained in:
pigwin
2025-01-07 19:51:14 +00:00
parent a2c1766dd1
commit df0b5135bd
3 changed files with 27 additions and 33 deletions

View File

@@ -1,16 +1,17 @@
package database package database
import ( import (
"context"
"crypto/md5" "crypto/md5"
"database/sql" "database/sql"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"log" "ti1/valki"
"ti1/config"
"ti1/valkey" "github.com/valkey-io/valkey-go"
) )
func InsertOrUpdateEstimatedCall(db *sql.DB, values []interface{}) (int, string, error) { func InsertOrUpdateEstimatedCall(ctx context.Context, db *sql.DB, values []interface{}, valkeyClient valkey.Client) (int, string, error) {
// Replace empty strings with nil for timestamp fields // Replace empty strings with nil for timestamp fields
for i, v := range values { for i, v := range values {
if str, ok := v.(string); ok && str == "" { if str, ok := v.(string); ok && str == "" {
@@ -27,38 +28,30 @@ func InsertOrUpdateEstimatedCall(db *sql.DB, values []interface{}) (int, string,
} }
hash := md5.Sum([]byte(valuesString)) hash := md5.Sum([]byte(valuesString))
hashString := hex.EncodeToString(hash[:]) hashString := hex.EncodeToString(hash[:])
println(hashString) fmt.Println("HashString:", hashString)
estimatedVehicleJourneyID := values[0] estimatedVehicleJourneyID := values[0]
orderID := values[1] orderID := values[1]
key := fmt.Sprintf("%v.%v", estimatedVehicleJourneyID, orderID) key := fmt.Sprintf("%v.%v", estimatedVehicleJourneyID, orderID)
fmt.Printf("Estimated Vehicle Journey ID: %v, Order ID: %v\n", estimatedVehicleJourneyID, orderID) fmt.Printf("Estimated Vehicle Journey ID: %v, Order ID: %v\n", estimatedVehicleJourneyID, orderID)
// Connect to Valkey
valkeyClient, err := config.ConnectToValkey("config/conf.json")
if err != nil {
log.Fatalf("Failed to connect to Valkey: %v", err)
}
defer config.DisconnectFromValkey(valkeyClient)
// Set the MD5 hash in Valkey // Set the MD5 hash in Valkey
err = valkey.SetValkeyValue(valkeyClient, key, hashString) err := valki.SetValkeyValue(ctx, valkeyClient, key, hashString)
if err != nil { if err != nil {
log.Fatalf("Failed to set value in Valkey: %v", err) return 0, "", fmt.Errorf("failed to set value in Valkey: %v", err)
} }
// Get the MD5 hash from Valkey // Get the MD5 hash from Valkey
retrievedHash, err := valkey.GetValkeyValue(valkeyClient, key) retrievedHash, err := valki.GetValkeyValue(ctx, valkeyClient, key)
if err != nil { if err != nil {
log.Fatalf("Failed to get value from Valkey: %v", err) return 0, "", fmt.Errorf("failed to get value from Valkey: %v", err)
} }
// Check if the retrieved value matches the original MD5 hash // Check if the retrieved value matches the original MD5 hash
if retrievedHash != hashString { if retrievedHash != hashString {
log.Fatalf("Retrieved hash does not match the original hash. Original: %s, Retrieved: %s", hashString, retrievedHash) return 0, "", fmt.Errorf("hash mismatch: original %s, retrieved %s", hashString, retrievedHash)
} else {
fmt.Println("Retrieved hash matches the original hash.")
} }
fmt.Println("Retrieved hash matches the original hash.")
query := ` query := `
INSERT INTO calls ( INSERT INTO calls (
@@ -89,13 +82,6 @@ func InsertOrUpdateEstimatedCall(db *sql.DB, values []interface{}) (int, string,
var id int var id int
err = stmt.QueryRow(values...).Scan(&action, &id) err = stmt.QueryRow(values...).Scan(&action, &id)
if err != nil { 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 0, "", fmt.Errorf("error executing statement: %v", err)
} }
return id, action, nil return id, action, nil

View File

@@ -1,6 +1,7 @@
package export package export
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
@@ -20,6 +21,15 @@ func DBData(data *data.Data) {
} }
defer db.Close() defer db.Close()
// Connect to Valkey
valkeyClient, err := config.ConnectToValkey("config/conf.json")
if err != nil {
log.Fatalf("Failed to connect to Valkey: %v", err)
}
defer config.DisconnectFromValkey(valkeyClient)
ctx := context.Background()
// 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 {
@@ -291,9 +301,9 @@ func DBData(data *data.Data) {
for i, v := range stringValues { for i, v := range stringValues {
interfaceValues[i] = v interfaceValues[i] = v
} }
id, action, err := database.InsertOrUpdateEstimatedCall(db, interfaceValues) id, action, err := database.InsertOrUpdateEstimatedCall(ctx, db, interfaceValues, valkeyClient)
if err != nil { if err != nil {
fmt.Printf("Error inserting/updating estimated call: %v\n", err) log.Fatalf("Failed to insert or update estimated call: %v", err)
} else { } else {
if 1 == 0 { if 1 == 0 {
fmt.Printf("Action: %s, ID: %d\n", action, id) fmt.Printf("Action: %s, ID: %d\n", action, id)

View File

@@ -1,4 +1,4 @@
package valkey package valki
import ( import (
"context" "context"
@@ -7,8 +7,7 @@ import (
"github.com/valkey-io/valkey-go" "github.com/valkey-io/valkey-go"
) )
func SetValkeyValue(client valkey.Client, key, value string) error { func SetValkeyValue(ctx context.Context, client valkey.Client, key, value string) error {
ctx := context.Background()
err := client.Do(ctx, client.B().Set().Key(key).Value(value).Build()).Error() err := client.Do(ctx, client.B().Set().Key(key).Value(value).Build()).Error()
if err != nil { if err != nil {
return fmt.Errorf("failed to set value in Valkey: %v", err) return fmt.Errorf("failed to set value in Valkey: %v", err)
@@ -16,8 +15,7 @@ func SetValkeyValue(client valkey.Client, key, value string) error {
return nil return nil
} }
func GetValkeyValue(client valkey.Client, key string) (string, error) { func GetValkeyValue(ctx context.Context, client valkey.Client, key string) (string, error) {
ctx := context.Background()
value, err := client.Do(ctx, client.B().Get().Key(key).Build()).ToString() value, err := client.Do(ctx, client.B().Get().Key(key).Build()).ToString()
if err != nil { if err != nil {
return "", fmt.Errorf("failed to get value from Valkey: %v", err) return "", fmt.Errorf("failed to get value from Valkey: %v", err)