stay code :P
This commit is contained in:
@@ -7,6 +7,8 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const configFilePath = "config/conf.json"
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Database struct {
|
Database struct {
|
||||||
Host string `json:"host"`
|
Host string `json:"host"`
|
||||||
@@ -23,14 +25,14 @@ type Config struct {
|
|||||||
TimeoutMs int `json:"timeout_ms"`
|
TimeoutMs int `json:"timeout_ms"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
} `json:"valkey"`
|
} `json:"valkey"`
|
||||||
Temp string `json:"temp"`
|
Temp string `json:"temp"`
|
||||||
DatasetId string `json:"dataset_id"`
|
DatasetId string `json:"dataset_id"`
|
||||||
ExcludedDatasetIds string `json:"excluded_dataset_ids"`
|
ExcludedDatasetIds string `json:"excluded_dataset_ids"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfig(file string) (Config, error) {
|
func LoadConfig() (Config, error) {
|
||||||
var config Config
|
var config Config
|
||||||
configFile, err := os.Open(file)
|
configFile, err := os.Open(configFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return config, fmt.Errorf("failed to open config file: %w", err)
|
return config, fmt.Errorf("failed to open config file: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
func ConnectToPostgreSQL() (*sql.DB, error) {
|
func ConnectToPostgreSQL() (*sql.DB, error) {
|
||||||
fmt.Println("Connecting to PostgreSQL...")
|
fmt.Println("Connecting to PostgreSQL...")
|
||||||
config, err := LoadConfig("config/conf.json")
|
config, err := LoadConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -57,7 +57,7 @@ func DisconnectFromPostgreSQL(db *sql.DB) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func PrintDBConfig() {
|
func PrintDBConfig() {
|
||||||
config, err := LoadConfig("config/conf.json")
|
config, err := LoadConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error loading config:", err)
|
fmt.Println("Error loading config:", err)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ type ValkeyConfig struct {
|
|||||||
Port string `json:"port"`
|
Port string `json:"port"`
|
||||||
MaxConns int `json:"max_conns"`
|
MaxConns int `json:"max_conns"`
|
||||||
TimeoutMs int `json:"timeout_ms"`
|
TimeoutMs int `json:"timeout_ms"`
|
||||||
Password string `json:"password"` // Add this line
|
Password string `json:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadValkeyConfig(file string) (ValkeyConfig, error) {
|
func LoadValkeyConfig(file string) (ValkeyConfig, error) {
|
||||||
@@ -56,9 +56,9 @@ func LoadValkeyConfig(file string) (ValkeyConfig, error) {
|
|||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConnectToValkey(configPath string) (valkey.Client, error) {
|
func ConnectToValkey() (valkey.Client, error) {
|
||||||
fmt.Println("Loading configuration...")
|
fmt.Println("Loading configuration...")
|
||||||
config, err := LoadConfig(configPath)
|
config, err := LoadConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to load config: %v", err)
|
return nil, fmt.Errorf("failed to load config: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,13 +3,37 @@ package database
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"ti1/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func GetDatasetVariable(config config.Config) string {
|
||||||
|
if config.DatasetId != "" {
|
||||||
|
fmt.Println(config.DatasetId)
|
||||||
|
return config.DatasetId
|
||||||
|
} else if config.ExcludedDatasetIds != "" {
|
||||||
|
result := "EX." + config.ExcludedDatasetIds
|
||||||
|
fmt.Println(result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
fmt.Println("")
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func InsertServiceDelivery(db *sql.DB, responseTimestamp string, recordedAtTime string) (int, error) {
|
func InsertServiceDelivery(db *sql.DB, responseTimestamp string, recordedAtTime string) (int, error) {
|
||||||
fmt.Println("Inserting ServiceDelivery...")
|
fmt.Println("Inserting ServiceDelivery...")
|
||||||
var id int
|
var id int
|
||||||
|
|
||||||
err := db.QueryRow("INSERT INTO public.ServiceDelivery (ResponseTimestamp, RecordedAtTime) VALUES ($1, $2) RETURNING ID", responseTimestamp, recordedAtTime).Scan(&id)
|
// Load configuration
|
||||||
|
config, err := config.LoadConfig()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error loading config:", err)
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get dataset variable
|
||||||
|
datasetVariable := GetDatasetVariable(config)
|
||||||
|
|
||||||
|
err = db.QueryRow("INSERT INTO public.ServiceDelivery (ResponseTimestamp, RecordedAtTime, DatasetVariable) VALUES ($1, $2, $3) RETURNING ID", responseTimestamp, recordedAtTime, datasetVariable).Scan(&id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return 0, err
|
return 0, err
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ func SetupDB() error {
|
|||||||
id INTEGER PRIMARY KEY DEFAULT nextval('public.servicedelivery_id_seq'),
|
id INTEGER PRIMARY KEY DEFAULT nextval('public.servicedelivery_id_seq'),
|
||||||
responsetimestamp TIMESTAMPTZ,
|
responsetimestamp TIMESTAMPTZ,
|
||||||
recordedattime TIMESTAMPTZ,
|
recordedattime TIMESTAMPTZ,
|
||||||
|
source VARCHAR,
|
||||||
data JSON
|
data JSON
|
||||||
);`,
|
);`,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ func DBData(data *data.Data) {
|
|||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
// Connect to Valkey
|
// Connect to Valkey
|
||||||
valkeyClient, err := config.ConnectToValkey("config/conf.json")
|
valkeyClient, err := config.ConnectToValkey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to connect to Valkey: %v", err)
|
log.Fatalf("Failed to connect to Valkey: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -14,7 +14,7 @@ func main() {
|
|||||||
log.Println("Starting...")
|
log.Println("Starting...")
|
||||||
|
|
||||||
// Load configuration
|
// Load configuration
|
||||||
cfg, err := config.LoadConfig("config/conf.json")
|
cfg, err := config.LoadConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to load config: %v", err)
|
log.Fatalf("Failed to load config: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user