-
Notifications
You must be signed in to change notification settings - Fork 1
/
importer.go
143 lines (117 loc) · 2.99 KB
/
importer.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
package covid19brazilimporter
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"time"
"cloud.google.com/go/firestore"
"github.com/go-git/go-git/v5"
)
// PubSubMessage is the struct that define the received through the PubSub trigger
type PubSubMessage struct {
Data []byte `json:"data"`
}
type LastUpdated struct {
Date time.Time `json:"date"`
}
func writeToFile(filepath string, data interface{}) (err error) {
var file *os.File
file, err = os.Create(filepath)
if err != nil {
return
}
encoder := json.NewEncoder(file)
encoder.Encode(data)
return file.Close()
}
func dumpRegions(folder string, regions Regions) (err error) {
for key, value := range regions {
filepath := fmt.Sprintf("%s/%d.json", folder, key)
err = writeToFile(filepath, value)
if err != nil {
return
}
}
return
}
func dumpIndexes(folder string, pages []*Page) error {
return writeToFile(fmt.Sprintf("%s/page-index.json", folder), pages)
}
func dumpDataLists(folder string, lists map[int][]*DataListing) (err error) {
for key, value := range lists {
filepath := fmt.Sprintf("%s/list-%d.json", folder, key)
err = writeToFile(filepath, value)
if err != nil {
return
}
}
return
}
// ImportData is the main function that will trigger the import of data
func ImportData(ctx context.Context, message PubSubMessage) error {
firestoreClient, firestoreError := firestore.NewClient(ctx, projectID)
defer firestoreClient.Close()
if firestoreError != nil {
log.Panicln(firestoreError.Error())
return firestoreError
}
metadata := GetMetaData()
lastUpdate, lastUpdateError := getLastUpdate(ctx, firestoreClient)
if lastUpdateError != nil {
return lastUpdateError
}
if lastUpdate.After(metadata.UpdatedAt) {
log.Println("already updated")
return nil
}
fmt.Println(metadata.File.URL)
properties, propertiesError := getProperties(ctx, firestoreClient)
if propertiesError != nil {
return propertiesError
}
var regions Regions
var err error
regions, err = ReadData(metadata.File.URL)
if err != nil {
return err
}
folder, _ := ioutil.TempDir("", "regions-*")
fmt.Println(folder)
repository, _ := cloneRepo(folder, os.Getenv("GITHUB_TOKEN"), properties.Branch)
dataFolder := fmt.Sprintf("%s/data", folder)
os.MkdirAll(dataFolder, 0700)
pages, lists, indexesError := buildIndexes(regions)
if indexesError != nil {
return indexesError
}
err = dumpRegions(dataFolder, regions)
if err != nil {
return err
}
err = dumpIndexes(dataFolder, pages)
if err != nil {
return err
}
err = dumpDataLists(dataFolder, lists)
if err != nil {
return err
}
err = writeToFile(fmt.Sprintf("%s/last-updated.json", dataFolder), &LastUpdated{Date: time.Now()})
if err != nil {
return err
}
_, err = commitAll(repository, properties.CommitMessage, properties.CommiterName, properties.CommiterEmail)
if err != nil {
return err
}
err = repository.Push(&git.PushOptions{})
if err != nil {
return err
}
setLastUpdate(ctx, firestoreClient)
log.Println("update done")
return nil
}