-
Notifications
You must be signed in to change notification settings - Fork 3
/
fits-loader.go
181 lines (150 loc) · 4.24 KB
/
fits-loader.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
package main
import (
"database/sql"
"encoding/json"
"flag"
"fmt"
"log"
"log/syslog"
"os"
"strings"
_ "github.com/lib/pq"
)
type Config struct {
DataBase DataBase
}
type DataBase struct {
Host, User, Password, SSLMode string
MaxOpenConns, MaxIdleConns int
}
// version 1.x no longer uses the network code in the DB.
const vers = "1.0"
var (
config = initConfig()
db *sql.DB
dataDir string
configFile string
dryRun, deleteFirst, slog, version, locValid bool
)
func initConfig() Config {
flag.StringVar(&dataDir, "data-dir", "", "path to directory of observation and source files.")
flag.StringVar(&configFile, "config-file", "fits-loader.json", "optional file to load the config from.")
flag.BoolVar(&slog, "syslog", false, "output log messages to syslog instead of stdout.")
flag.BoolVar(&deleteFirst, "delete-first", false, "sync the FITS DB data with the information in each observation file.")
flag.BoolVar(&dryRun, "dry-run", false, "data is parsed and validated but not loaded to the DB. A DB connection is needed for validation.")
flag.BoolVar(&locValid, "local-validate", false, "data is parsed and validated without a connection to the DB.")
flag.BoolVar(&version, "version", false, "prints the version and exits.")
flag.Parse()
if version {
fmt.Printf("fits-loader version %s\n", vers)
os.Exit(1)
}
if locValid {
fmt.Println("Validating without DB connection")
}
if slog {
logwriter, err := syslog.New(syslog.LOG_NOTICE, "fits-loader")
if err == nil {
log.Println("** logging to syslog **")
log.SetOutput(logwriter)
} else {
log.Println("problem switching to syslog. Contiuning.")
log.Println(err)
}
}
var c Config
if !locValid {
f, err := os.ReadFile(configFile)
if err != nil {
log.Printf("ERROR - problem loading %s - can't find any config.", configFile)
log.Fatal(err)
}
err = json.Unmarshal(f, &c)
if err != nil {
log.Println("Problem parsing config file.")
log.Fatal(err)
}
}
return c
}
func main() {
if dataDir == "" {
log.Fatal("please specify the data directory")
}
if !locValid {
if err := config.initDB(); err != nil {
log.Fatal(err)
}
defer db.Close()
}
log.Printf("searching for observation and source data in %s", dataDir)
files, err := os.ReadDir(dataDir)
if err != nil {
log.Fatal(err)
}
var proc []data
for _, f := range files {
info, err := f.Info()
if err != nil {
log.Fatalf("error getting file info for %s: %s", f.Name(), err.Error())
}
if !f.IsDir() && strings.HasSuffix(f.Name(), `.csv`) && info.Size() > 0 {
meta := f.Name()
meta = strings.TrimSuffix(meta, `.csv`) + `.json`
if _, err := os.Stat(dataDir + "/" + meta); os.IsNotExist(err) {
log.Fatalf("found no json source file for %s", f.Name())
}
proc = append(proc, data{
sourceFile: dataDir + "/" + meta,
observationFile: dataDir + "/" + f.Name(),
})
}
}
log.Printf("found %d observation files to process", len(proc))
for _, d := range proc {
log.Printf("reading and validating %s", d.observationFile)
if err := d.parseAndValidate(); err != nil {
log.Fatal(err)
}
if !dryRun && !locValid {
log.Printf("saving site information from %s", d.sourceFile)
if err := d.saveSite(); err != nil {
log.Fatal(err)
}
log.Printf("saving observations from %s", d.observationFile)
if !deleteFirst {
if err := d.updateOrAdd(); err != nil {
log.Fatal(err)
}
} else {
if err := d.deleteThenSave(); err != nil {
log.Fatal(err)
}
}
}
}
}
// initDB starts the DB connection pool. Defer a db.Close() after calling this.
func (c *Config) initDB() (err error) {
db, err = sql.Open("postgres", "connect_timeout=1 user="+c.DataBase.User+
" password="+c.DataBase.Password+
" host="+c.DataBase.Host+
" connect_timeout=30"+
" dbname=fits"+
" sslmode="+c.DataBase.SSLMode)
if err != nil {
return err
}
db.SetMaxIdleConns(c.DataBase.MaxIdleConns)
db.SetMaxOpenConns(c.DataBase.MaxOpenConns)
if err := db.Ping(); err != nil {
return err
}
if err := initData(); err != nil {
return err
}
if err := initSource(); err != nil {
return err
}
return err
}