-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
217 lines (187 loc) · 5.57 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"database/sql"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"regexp"
"time"
"github.com/howeyc/fsnotify"
_ "github.com/lib/pq"
)
var (
// example: 2014-10-31-15-15.json
filenameRegex = regexp.MustCompile(`(\d{4}(-\d{2}){4})\.json$`)
datetimeRegex = regexp.MustCompile(`([\d-]*)`)
// datetimeFormat is the timestamp format used in the filenames.
datetimeFormat = "2006-01-02-15-04"
NY *time.Location
materializedViews = []string{
"hour_window",
"day_window",
"week_window",
"month_window",
}
PG_USER, PG_PASSWORD, PG_DB, PG_HOST, PG_PORT, PG_SSL string
)
// init is called on startup
func init() {
var err error
NY, err = time.LoadLocation("America/New_York")
if err != nil {
log.Fatalf("Cannot load NYC tz => {%s}", err)
}
}
// configure runs before startup
func configure() {
PG_USER = getOrElse("PG_USER", "adicu")
PG_PASSWORD = getOrElse("PG_PASSWORD", "")
PG_DB = getOrElse("PG_DB", "")
PG_HOST = getOrElse("PG_HOST", "localhost")
PG_PORT = getOrElse("PG_PORT", "5432")
PG_SSL = getOrElse("PG_SSL", "disable")
}
// getDate parses a filepath to get a date from the filename given the regex
// declared in `filenameRegex`.
func getDate(s string) (time.Time, error) {
return time.ParseInLocation(
datetimeFormat,
datetimeRegex.FindString(path.Base(s)),
NY)
}
// getOrElse checks the specified environment variable, returns the value if found, otherwise
// will return the default value provided. If there is no default then makes a fatal log.
func getOrElse(key, standard string) string {
if val := os.Getenv(key); val != "" {
return val
} else if standard == "" {
log.Fatalf("ERROR: The environment variable, %s, must be set", key)
}
return standard
}
// dbConnect yanks db configurations from the environment variables and returns a postgres
// connection
func dbConnect() *sql.DB {
db, err := sql.Open("postgres",
fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%s sslmode=%s",
PG_USER,
PG_PASSWORD,
PG_DB,
PG_HOST,
PG_PORT,
PG_SSL,
))
if err != nil {
log.Fatalf("ERROR: Error connecting to Postgres => %s", err.Error())
}
log.Printf("PQ Database connection made to %s", PG_DB)
return db
}
// handleFile processes new files
//
// The file is read into memory, parsed then inserted to the database.
func handleFile(filename string, db *sql.DB) {
log.Printf("Processing, %s", filename)
fileContents, err := ioutil.ReadFile(filename)
if err != nil {
log.Printf("ERROR: Failed to read in file, %s => %s", filename, err.Error())
return
}
tm, err := getDate(filename)
if err != nil {
log.Printf("ERROR: Failed to parse date from file, %s, ignored.", filename)
return
}
data, err := parseData(tm, fileContents)
if err != nil {
log.Printf("ERROR: Failed to parse data from %s => %s", filename, err.Error())
return
}
if err = dataset(data).insert(db); err != nil {
log.Printf("ERROR: Failed to insert data from, %s => %s", filename, err.Error())
}
}
// Update the materialized views listed in `materializedViews`
func updateViews(db *sql.DB) {
txn, err := db.Begin()
if err != nil {
log.Printf("ERROR: failed to start pq txn for materialized view updates => %s", err.Error())
return
}
for _, view := range materializedViews {
if _, err = txn.Exec(fmt.Sprintf("REFRESH MATERIALIZED VIEW %s", view)); err != nil {
log.Printf("ERROR: Failed to update materialized view, %s => %s", view, err.Error())
}
}
err = txn.Commit()
if err != nil {
log.Printf("ERROR: Failed to commit transaction => {%s}", err)
}
}
func LoadAllFiles(watchDir string) {
log.Printf("Loading all files in directory, %s", watchDir)
db := dbConnect()
defer db.Close()
files, err := ioutil.ReadDir(watchDir)
if err != nil {
log.Fatalf("ERROR: Failed to read in directory info => %s", err.Error())
}
// handle every data file
for _, f := range files {
handleFile(path.Join(watchDir, f.Name()), db)
}
updateViews(db) // refresh the materialized views afterwards
}
func watchDirectory(watchDir string) {
// start watching for new files
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal("ERROR: Failed to instantiate file watcher")
}
defer watcher.Close()
// start the file system watcher
if err = watcher.WatchFlags(watchDir, fsnotify.FSN_CREATE); err != nil {
log.Fatalf("ERROR: Failed to start watching directory, %s => %s", watchDir, err.Error())
}
// wait for any new files to be added, then process them
for {
select {
case event := <-watcher.Event:
// reconnect to the DB for each event because otherwise the connection gets stale
db := dbConnect()
if filenameRegex.MatchString(event.Name) {
// sleep to allow the whole file to be transmitted.
// otherwise we get a parsing error because it's incomplete.
time.Sleep(time.Duration(2 * time.Second))
handleFile(event.Name, db)
updateViews(db)
}
db.Close()
case err := <-watcher.Error:
log.Printf("ERROR: fsnotify err channel => {%s}", err)
}
}
}
func main() {
configure() // set up all configuration variables
// gather CLI configurations
var (
watchDir = flag.String("dir", ".", "directory to watch for new files")
loadAll = flag.Bool("all", false, "load all dump file in the directory")
keepWatching = flag.Bool("watch", true, "continue to watch for new files in the directory")
)
flag.Parse()
// if all the files currently in the directory should be loaded
if *loadAll {
LoadAllFiles(*watchDir)
}
// exits if flag turned on
if *keepWatching {
watchDirectory(*watchDir)
}
// log because it's an unexpected answer
log.Println("Not watching for files as specified")
}