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
import (
"context"
"crypto/md5"
"database/sql"
"encoding/hex"
"fmt"
"log"
"ti1/config"
"ti1/valkey"
"ti1/valki"
"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
for i, v := range values {
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))
hashString := hex.EncodeToString(hash[:])
println(hashString)
fmt.Println("HashString:", hashString)
estimatedVehicleJourneyID := values[0]
orderID := values[1]
key := fmt.Sprintf("%v.%v", 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
err = valkey.SetValkeyValue(valkeyClient, key, hashString)
err := valki.SetValkeyValue(ctx, valkeyClient, key, hashString)
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
retrievedHash, err := valkey.GetValkeyValue(valkeyClient, key)
retrievedHash, err := valki.GetValkeyValue(ctx, valkeyClient, key)
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
if retrievedHash != hashString {
log.Fatalf("Retrieved hash does not match the original hash. Original: %s, Retrieved: %s", hashString, retrievedHash)
} else {
fmt.Println("Retrieved hash matches the original hash.")
return 0, "", fmt.Errorf("hash mismatch: original %s, retrieved %s", hashString, retrievedHash)
}
fmt.Println("Retrieved hash matches the original hash.")
query := `
INSERT INTO calls (
@@ -89,13 +82,6 @@ func InsertOrUpdateEstimatedCall(db *sql.DB, values []interface{}) (int, string,
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

View File

@@ -1,6 +1,7 @@
package export
import (
"context"
"encoding/json"
"fmt"
"log"
@@ -20,6 +21,15 @@ func DBData(data *data.Data) {
}
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
sid, err := database.InsertServiceDelivery(db, data.ServiceDelivery.ResponseTimestamp, data.ServiceDelivery.EstimatedTimetableDelivery[0].EstimatedJourneyVersionFrame.RecordedAtTime)
if err != nil {
@@ -291,9 +301,9 @@ func DBData(data *data.Data) {
for i, v := range stringValues {
interfaceValues[i] = v
}
id, action, err := database.InsertOrUpdateEstimatedCall(db, interfaceValues)
id, action, err := database.InsertOrUpdateEstimatedCall(ctx, db, interfaceValues, valkeyClient)
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 {
if 1 == 0 {
fmt.Printf("Action: %s, ID: %d\n", action, id)

View File

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