-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
174 lines (151 loc) · 4.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"context"
"encoding/csv"
"flag"
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/jackc/pgx/v4"
"github.com/joho/godotenv"
)
type S3Config struct {
Region string
Bucket string
Key string
}
// Connect to S3 and get records from CSV file
func ReadCSVFromS3(config S3Config) ([][]string, error) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(config.Region)},
)
if err != nil {
return nil, err
}
svc := s3.New(sess)
resp, err := svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(config.Bucket),
Key: aws.String(config.Key),
})
if err != nil {
return nil, err
}
defer resp.Body.Close()
reader := csv.NewReader(resp.Body)
records, err := reader.ReadAll()
if err != nil {
return nil, err
}
return records, nil
}
// ProcessData calculates total sales for each product and each country
func ProcessData(records [][]string) (map[string]float64, map[string]float64) {
productSales := make(map[string]float64)
countrySales := make(map[string]float64)
for _, record := range records[1:] {
productID := record[1]
country := record[7]
quantity, err := strconv.ParseFloat(record[3], 64)
if err != nil {
fmt.Println("Error parsing quantity:", err)
continue
}
unitPrice, err := strconv.ParseFloat(record[5], 64)
if err != nil {
fmt.Println("Error parsing unit price:", err)
continue
}
sales := quantity * unitPrice
productSales[productID] += sales
countrySales[country] += sales
}
return productSales, countrySales
}
// Print processed data into console (use for testing)
func PrintProcessedData(data map[string]float64, dataType string) {
fmt.Printf("%s:\n", dataType)
for key, value := range data {
fmt.Printf("%s: %.2f\n", key, value)
}
}
// Insert data into Redshift
func InsertDataToRedshift(conn *pgx.Conn, tableName string, data map[string]float64) error {
for key, value := range data {
_, err := conn.Exec(context.Background(), fmt.Sprintf("INSERT INTO %s (id, total_sales) VALUES ($1, $2)", tableName), key, value)
if err != nil {
return fmt.Errorf("failed to insert data %v into %s: %v", key, tableName, err)
}
log.Printf("Successfully inserted %v into %s", key, tableName)
}
return nil
}
// handleAction performs the specified action (print or insert)
func handleAction(conn *pgx.Conn, config S3Config, action string) error {
records, err := ReadCSVFromS3(config)
if err != nil {
return fmt.Errorf("Error reading CSV from S3: %v", err)
}
productSales, countrySales := ProcessData(records)
switch action {
case "print":
PrintProcessedData(productSales, "Product Sales")
PrintProcessedData(countrySales, "Country Sales")
case "insert":
err = InsertDataToRedshift(conn, "product_sales", productSales)
if err != nil {
return fmt.Errorf("Error inserting product sales data: %v", err)
}
err = InsertDataToRedshift(conn, "country_sales", countrySales)
if err != nil {
return fmt.Errorf("Error inserting country sales data: %v", err)
}
default:
return fmt.Errorf("Invalid action specified. Please use -action=print or -action=insert")
}
return nil
}
func handleRequest(conn *pgx.Conn, config S3Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
action := r.URL.Query().Get("action")
err := handleAction(conn, config, action)
if err != nil {
fmt.Fprintf(w, "%v", err)
return
}
fmt.Fprintf(w, "Action %s executed successfully", action)
}
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file")
}
connStr := os.Getenv("REDSHIFT_CONN_STRING")
conn, err := pgx.Connect(context.Background(), connStr)
if err != nil {
log.Fatalf("Unable to connect to database: %v\n", err)
}
defer conn.Close(context.Background())
log.Println("Successfully connected to the database")
config := S3Config{
Region: os.Getenv("REGION"),
Bucket: os.Getenv("BUCKET"),
Key: os.Getenv("KEY"),
}
action := flag.String("action", "", "Specify the action to perform: print or insert")
flag.Parse()
if *action == "" {
http.HandleFunc("/", handleRequest(conn, config))
log.Fatal(http.ListenAndServe(":8080", nil))
} else {
err := handleAction(conn, config, *action)
if err != nil {
log.Fatalf("%v", err)
}
}
}