Skip to content

Commit 7e77f6d

Browse files
committed
Added environment variable to decrease verbosity of Saved: log messages
1 parent a99241f commit 7e77f6d

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ RUN echo '#!/bin/sh' > /app/entrypoint.sh && \
4040
echo 'echo "CITY=$CITY" >> /app/.env' >> /app/entrypoint.sh && \
4141
echo 'echo "ASN=$ASN" >> /app/.env' >> /app/entrypoint.sh && \
4242
echo 'echo "UPDATE_TIME=$UPDATE_TIME" >> /app/.env' >> /app/entrypoint.sh && \
43+
echo 'echo "LOAD_LOG_FREQ=$LOAD_LOG_FREQ" >> /app/.env' >> /app/entrypoint.sh && \
4344
echo 'echo "DB_TYPE=$DB_TYPE" >> /app/.env' >> /app/entrypoint.sh && \
4445
echo '' >> /app/entrypoint.sh && \
4546
echo '# Add database variables if they are set' >> /app/entrypoint.sh && \
@@ -94,6 +95,7 @@ ENV COUNTRY="geo-whois-asn-country"
9495
ENV CITY=""
9596
ENV ASN="asn"
9697
ENV UPDATE_TIME="01:30"
98+
ENV LOAD_LOG_FREQ=50000
9799
# DB_TYPE can be mmdb, postgres, mysql, sqlite or :memory:
98100
ENV DB_TYPE="mmdb"
99101
# Database connection variables (used when DB_TYPE is set to postgres/mysql/sqlite)

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ If you wish to expose the system without a reverse proxy, you may wish to update
8787

8888
`API_KEY` allows a very basic protection of the system to be applied, a header named `API-KEY` *(hyphen not underscore!)* with a matching value must be passed if this variable is populated. If left blank, the API is open.
8989

90+
`LOAD_LOG_FREQ` allows adjusting how frequently progress in saving records will be logged.
91+
9092
`COUNTRY`, `CITY` and `ASN` are the databases that will be loaded. **If you don't need cities or ASNs, just leave them blank.** The values / names used should mirror the directory values found in the [ip-location-db](https://github.com/sapics/ip-location-db) project:
9193

9294
### Allowed `COUNTRY` values

load.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ func loadCities(dataToLoad DataToLoad) {
5656
version := dbQueryMaxVersion("ip_city", dataToLoad.Version) + 1
5757
cities := []IpCity{}
5858
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+
5967

6068
fmt.Println("rebuilding: ip_city ipv", dataToLoad.Version)
6169
fmt.Print("\033[s") // Save the cursor position
@@ -71,8 +79,11 @@ func loadCities(dataToLoad DataToLoad) {
7179

7280
if len(cities) == 100 {
7381
count += len(cities);
74-
fmt.Print("\033[u\033[K") // Restore the cursor position and clear the line
75-
fmt.Printf("Saved: %d entries\n", count)
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+
}
7687
dbSaveCities(cities)
7788
cities = []IpCity{}
7889
}
@@ -99,6 +110,12 @@ func loadASNs(dataToLoad DataToLoad) {
99110
version := dbQueryMaxVersion("ip_asn", dataToLoad.Version) + 1
100111
ASNs := []IpASN{}
101112
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+
}
102119

103120
fmt.Println("rebuilding: ip_asn ipv", dataToLoad.Version)
104121
fmt.Print("\033[s") // Save the cursor position
@@ -112,8 +129,11 @@ func loadASNs(dataToLoad DataToLoad) {
112129

113130
if len(ASNs) == 100 {
114131
count += len(ASNs);
115-
fmt.Print("\033[u\033[K") // Restore the cursor position and clear the line
116-
fmt.Printf("Saved: %d entries\n", count)
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+
}
117137
dbSaveASNs(ASNs)
118138
ASNs = []IpASN{}
119139
}
@@ -140,6 +160,12 @@ func loadCountries(dataToLoad DataToLoad) {
140160
version := dbQueryMaxVersion("ip_country", dataToLoad.Version) + 1
141161
countries := []IpCountry{}
142162
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+
}
143169

144170
fmt.Println("rebuilding: ip_country ipv", dataToLoad.Version)
145171
fmt.Print("\033[s") // Save the cursor position
@@ -152,8 +178,11 @@ func loadCountries(dataToLoad DataToLoad) {
152178

153179
if len(countries) == 100 {
154180
count += len(countries);
155-
fmt.Print("\033[u\033[K") // Restore the cursor position and clear the line
156-
fmt.Printf("Saved: %d entries\n", count)
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+
}
157186
dbSaveCountries(countries)
158187
countries = []IpCountry{}
159188
}

0 commit comments

Comments
 (0)