-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
141 lines (114 loc) · 3.02 KB
/
functions.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
package functions
import (
"context"
"embed"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
"cloud.google.com/go/bigquery"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"github.com/johannaojeling/go-data-ingestion/pkg"
)
const (
dateLayout = "2006-01-02"
configPath = "resources/config/batch-ingestion.yaml"
)
var (
//go:embed resources
resources embed.FS
config []byte
client *bigquery.Client
projectID = os.Getenv("PROJECT")
bucket = os.Getenv("BUCKET")
)
func init() {
var err error
config, err = resources.ReadFile(configPath)
if err != nil {
log.Fatalf("Error reading config file: %v", err)
}
client, err = bigquery.NewClient(context.Background(), projectID)
if err != nil {
log.Fatalf("Error initializing BigQuery client: %v", err)
}
functions.HTTP("IngestBatch", IngestBatch)
}
type batchRequest struct {
Date string
}
type templateFields struct {
Bucket string
Date time.Time
}
func IngestBatch(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
log.Printf("Invalid HTTP method: %v\n", r.Method)
http.Error(w, "Only POST method is supported", http.StatusMethodNotAllowed)
return
}
var batchReq batchRequest
if err := json.NewDecoder(r.Body).Decode(&batchReq); err != nil {
log.Printf("Error unmarshaling request body: %v\n", err)
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
date, err := time.Parse(dateLayout, batchReq.Date)
if err != nil {
log.Printf("Error parsing date string to time: %v\n", err)
http.Error(w, "Invalid date format", http.StatusBadRequest)
return
}
fields := templateFields{
Bucket: bucket,
Date: date,
}
var configMap map[string]pkg.DataSource
if err := pkg.ParseConfig(string(config), fields, &configMap); err != nil {
log.Printf("Error parsing config to map of data sources: %v\n", err)
http.Error(w, "Unable to parse configuration", http.StatusInternalServerError)
return
}
if err := ingestAll(r.Context(), client, configMap); err != nil {
log.Printf("Error ingesting data sources: %v\n", err)
http.Error(w, "Unable to ingest data sources", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
func ingestAll(
ctx context.Context,
client *bigquery.Client,
configMap map[string]pkg.DataSource,
) error {
count := len(configMap)
log.Printf("Starting ingestion of %d data source(s)\n", count)
start := time.Now()
errChan := make(chan error)
for name, source := range configMap {
go func(name string, source pkg.DataSource) {
errChan <- pkg.LoadToBigQuery(ctx, client, name, source)
}(name, source)
}
errCount := 0
for range configMap {
if err := <-errChan; err != nil {
log.Println(err.Error())
errCount++
}
}
elapsed := time.Since(start).Seconds()
log.Printf(
"Finished ingestion of %d data source(s) after %f seconds. Succeeded: %d. Failed: %d\n",
count,
elapsed,
count-errCount,
errCount,
)
if errCount != 0 {
return fmt.Errorf("failed ingestion of %d data sources", errCount)
}
return nil
}