-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
225 lines (197 loc) · 4.27 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
218
219
220
221
222
223
224
225
package main
import (
"database/sql"
"encoding/json"
"fmt"
"math"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
"unicode"
_ "github.com/lib/pq"
"golang.org/x/text/language"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
var nameFixReplace = regexp.MustCompile(`[^a-zA-Z0-9- ]`)
func fixName(s string) string {
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
result, _, _ := transform.String(t, s)
return strings.Title(strings.ToLower(nameFixReplace.ReplaceAllString(result, "")))
}
func toInt(s string) int {
id, e := strconv.Atoi(s)
if e != nil {
return 0
}
return id
}
func findPrefereLanguage(locales string) string {
lst := strings.Split(locales, ",")
if len(lst) == 0 {
return ""
}
if len(lst[0]) < 2 {
return ""
}
return lst[0][0:2]
}
func NewNullString(s string) sql.NullString {
if len(s) == 0 {
return sql.NullString{}
}
return sql.NullString{
String: s,
Valid: true,
}
}
func NewNullInt64(s string) sql.NullInt64 {
if len(s) == 0 || s == "" {
return sql.NullInt64{}
}
num, e := strconv.Atoi(s)
if e != nil {
return sql.NullInt64{}
}
return sql.NullInt64{
Int64: int64(num),
Valid: true,
}
}
func NewNullInt64FromInt(ii int) sql.NullInt64 {
if ii > 0 {
return sql.NullInt64{
Int64: int64(ii),
Valid: true,
}
}
return sql.NullInt64{}
}
func NewNullFloatInt(s string) sql.NullInt64 {
if len(s) == 0 || s == "" {
return sql.NullInt64{}
}
num, e := strconv.ParseFloat(s, 64)
if e != nil {
return sql.NullInt64{}
}
return sql.NullInt64{
Int64: int64(math.RoundToEven(num)),
Valid: true,
}
}
func parseLocales(locales string) []string {
result := []string{}
lst := strings.Split(locales, ",")
if len(lst) == 0 {
return result
}
for _, ll := range lst {
_, err := language.Parse(ll)
if err == nil {
result = append(result, ll)
}
}
return result
}
// print the contents of the obj
func PrettyPrint(data interface{}) {
var p []byte
// var err := error
p, err := json.MarshalIndent(data, "", "\t")
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s \n", p)
}
var isoToGeonameID map[string]int
var adminCodeMapList map[string]int
func getDb() (*sql.DB, bool) {
connectionString := os.Getenv("POSTGRES_URI")
if connectionString == "" {
connectionString = "postgres://geonames:geonames@127.0.0.1/geonames?sslmode=disable"
}
db, err1 := sql.Open("postgres", connectionString)
if err1 == nil {
rows, err2 := db.Query(`
SELECT
COUNT(*)
FROM "tmp_ready"
`)
if err2 != nil {
fmt.Println(err2)
db.Close()
return nil, false
}
defer rows.Close()
var num int = 0
for rows.Next() {
err3 := rows.Scan(&num)
if err3 != nil {
fmt.Println(err3)
db.Close()
return nil, false
}
}
if num > 0 {
return db, true
}
} else {
fmt.Println(err1)
}
return nil, false
}
func dockerLogs() {
out1, err1 := exec.Command("/usr/local/bin/docker", "ps", "-a").Output()
if err1 != nil {
fmt.Println("error: =====")
fmt.Println(err1)
fmt.Println("============")
}
fmt.Printf("docker ps\n%s\n", out1)
out2, err2 := exec.Command("/usr/local/bin/docker", "logs", "geonames-postgis").Output()
if err2 != nil {
fmt.Println("error: =====")
fmt.Println(err2)
fmt.Println("============")
}
fmt.Printf("docker logs\n%s\n", out2)
}
func main() {
isoToGeonameID = make(map[string]int)
adminCodeMapList = make(map[string]int)
var db *sql.DB
var okDB bool = false
for !okDB {
fmt.Println("Try connect to database")
db, okDB = getDb()
if okDB {
break
}
// dockerLogs()
time.Sleep(time.Second * 15)
}
isoToGeonameID, _ = getIsoToGeonameID(db)
fmt.Println("Process countries")
processCountryInfo(db)
fmt.Println("Process admin codes")
adminCodeMapList = adminCode(db)
fmt.Println("Process geo data")
buildGeoData(db)
fmt.Println("Drop temporary tables")
db.Exec(`DROP TABLE "tmp_shapesAllLow";`)
db.Exec(`DROP TABLE "tmp_countryInfo";`)
db.Exec(`DROP TABLE "tmp_geonameid";`)
db.Exec(`DROP TABLE "tmp_ready";`)
db.Exec(`DROP TABLE "tmp_admin1Codes";`)
fmt.Println("Process geo data")
db.Exec(`VACUUM(FULL, ANALYZE) "adminCode";`)
db.Exec(`VACUUM(FULL, ANALYZE) "countryInfo";`)
db.Exec(`VACUUM(FULL, ANALYZE) "geo";`)
fmt.Println("Process application done")
}