Skip to content

Commit a19f603

Browse files
committed
Improve logging
1 parent d98915f commit a19f603

File tree

2 files changed

+46
-57
lines changed

2 files changed

+46
-57
lines changed

helpers.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ import (
2222
func decompressFile(filePath string, compression string) error {
2323
reader, err := os.Open(filePath)
2424
if err != nil {
25-
panic(err)
25+
panic(err)
2626
}
2727
defer reader.Close()
2828

2929
uncompressedStream, err := gzip.NewReader(reader)
3030
if err != nil {
31-
panic(err)
31+
panic(err)
3232
}
3333

3434
decompressedFilePath := strings.Replace(filePath, compression, "", -1)
3535
decompressedFile, err := os.Create(decompressedFilePath)
3636
if err != nil {
37-
panic(err)
37+
panic(err)
3838
}
3939
defer decompressedFile.Close()
4040

@@ -209,6 +209,20 @@ func getIpVersion(ipString string) int {
209209
return ipVersion
210210
}
211211

212+
func getLogFrequency() int {
213+
loadLogFrequency := os.Getenv("LOAD_LOG_FREQ")
214+
if len(loadLogFrequency) > 0 {
215+
loadLogFrequencyInt, err := strconv.Atoi(loadLogFrequency)
216+
if err != nil {
217+
panic(err)
218+
}
219+
220+
return loadLogFrequencyInt
221+
}
222+
223+
return 1000
224+
}
225+
212226
func hasASNDatabase() bool {
213227
return len(os.Getenv("ASN")) > 0
214228
}

load.go

Lines changed: 29 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,9 @@ func loadCities(dataToLoad DataToLoad) {
5353
defer csvFile.Close()
5454
csvFileReader := csv.NewReader(csvFile)
5555

56-
version := dbQueryMaxVersion("ip_city", dataToLoad.Version) + 1
57-
cities := []IpCity{}
58-
count := 0;
59-
lastLog := 0;
60-
logFS := os.Getenv("LOAD_LOG_FREQ")
61-
logFreq, err := strconv.Atoi(logFS)
62-
if err != nil {
63-
logFreq = 100;
64-
}
65-
66-
56+
version := dbQueryMaxVersion("ip_city", dataToLoad.Version) + 1
57+
cities := []IpCity{}
58+
numSaved := 0;
6759

6860
fmt.Println("rebuilding: ip_city ipv", dataToLoad.Version)
6961
fmt.Print("\033[s") // Save the cursor position
@@ -78,22 +70,15 @@ func loadCities(dataToLoad DataToLoad) {
7870
cities = append(cities, IpCity{ record[0], record[1], record[2], record[3], record[4], record[5], record[6], lat, lon, record[9], dataToLoad.Version, version })
7971

8072
if len(cities) == 100 {
81-
count += len(cities);
82-
if (count >= lastLog + logFreq) {
83-
fmt.Print("\033[u\033[K") // Restore the cursor position and clear the line
84-
fmt.Printf("Saved: %d entries\n", count)
85-
lastLog = count
86-
}
8773
dbSaveCities(cities)
74+
logEntriesConditionally(&numSaved, &cities)
8875
cities = []IpCity{}
8976
}
9077
}
9178

9279
if len(cities) > 0 {
93-
count += len(cities);
94-
fmt.Print("\033[u\033[K") // Restore the cursor position and clear the line
95-
fmt.Printf("Saved: %d entries\n", count)
9680
dbSaveCities(cities)
81+
logEntries(&cities)
9782
}
9883

9984
dbDropOld("ip_city", dataToLoad.Version, version)
@@ -107,15 +92,9 @@ func loadASNs(dataToLoad DataToLoad) {
10792
defer csvFile.Close()
10893
csvFileReader := csv.NewReader(csvFile)
10994

110-
version := dbQueryMaxVersion("ip_asn", dataToLoad.Version) + 1
111-
ASNs := []IpASN{}
112-
count := 0;
113-
lastLog := 0;
114-
logFS := os.Getenv("LOAD_LOG_FREQ")
115-
logFreq, err := strconv.Atoi(logFS)
116-
if err != nil {
117-
logFreq = 100;
118-
}
95+
version := dbQueryMaxVersion("ip_asn", dataToLoad.Version) + 1
96+
ASNs := []IpASN{}
97+
numSaved := 0;
11998

12099
fmt.Println("rebuilding: ip_asn ipv", dataToLoad.Version)
121100
fmt.Print("\033[s") // Save the cursor position
@@ -128,22 +107,15 @@ func loadASNs(dataToLoad DataToLoad) {
128107
ASNs = append(ASNs, IpASN{ record[0], record[1], asn, record[3], dataToLoad.Version, version })
129108

130109
if len(ASNs) == 100 {
131-
count += len(ASNs);
132-
if (count >= lastLog + logFreq) {
133-
fmt.Print("\033[u\033[K") // Restore the cursor position and clear the line
134-
fmt.Printf("Saved: %d entries\n", count)
135-
lastLog = count
136-
}
137110
dbSaveASNs(ASNs)
111+
logEntriesConditionally(&numSaved, &ASNs)
138112
ASNs = []IpASN{}
139113
}
140114
}
141115

142116
if len(ASNs) > 0 {
143-
count += len(ASNs);
144-
fmt.Print("\033[u\033[K") // Restore the cursor position and clear the line
145-
fmt.Printf("Saved: %d entries\n", count)
146117
dbSaveASNs(ASNs)
118+
logEntries(&ASNs)
147119
}
148120

149121
dbDropOld("ip_asn", dataToLoad.Version, version)
@@ -159,13 +131,7 @@ func loadCountries(dataToLoad DataToLoad) {
159131

160132
version := dbQueryMaxVersion("ip_country", dataToLoad.Version) + 1
161133
countries := []IpCountry{}
162-
count := 0;
163-
lastLog := 0;
164-
logFS := os.Getenv("LOAD_LOG_FREQ")
165-
logFreq, err := strconv.Atoi(logFS)
166-
if err != nil {
167-
logFreq = 100;
168-
}
134+
numSaved := 0
169135

170136
fmt.Println("rebuilding: ip_country ipv", dataToLoad.Version)
171137
fmt.Print("\033[s") // Save the cursor position
@@ -177,27 +143,36 @@ func loadCountries(dataToLoad DataToLoad) {
177143
countries = append(countries, IpCountry{ record[0], record[1], record[2], dataToLoad.Version, version })
178144

179145
if len(countries) == 100 {
180-
count += len(countries);
181-
if (count >= lastLog + logFreq) {
182-
fmt.Print("\033[u\033[K") // Restore the cursor position and clear the line
183-
fmt.Printf("Saved: %d entries\n", count)
184-
lastLog = count
185-
}
186146
dbSaveCountries(countries)
147+
logEntriesConditionally(&numSaved, &countries)
187148
countries = []IpCountry{}
188149
}
189150
}
190151

191152
if len(countries) > 0 {
192-
count += len(countries);
193-
fmt.Print("\033[u\033[K") // Restore the cursor position and clear the line
194-
fmt.Printf("Saved: %d entries\n", count)
195153
dbSaveCountries(countries)
154+
logEntries(&countries)
196155
}
197156

198157
dbDropOld("ip_country", dataToLoad.Version, version)
199158
}
200159

201160
func loadDbStructure() {
202161
dbFile()
162+
}
163+
164+
func logEntries[T any](list *[]T) {
165+
fmt.Print("\033[u\033[K") // Restore the cursor position and clear the line
166+
fmt.Printf("Saved: %d entries\n", len(*list))
167+
}
168+
169+
// Don't spam the logs, only display every message after `logFreq` records
170+
func logEntriesConditionally[T any](numSaved *int, list *[]T) {
171+
logFreq := getLogFrequency()
172+
173+
*numSaved += len(*list)
174+
175+
if len(*list) >= *numSaved + logFreq {
176+
logEntries(list)
177+
}
203178
}

0 commit comments

Comments
 (0)