forked from vmiklos/osm-gimmisn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.py
executable file
·338 lines (282 loc) · 12.6 KB
/
cron.py
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env python3
#
# Copyright 2019 Miklos Vajna. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
"""The cron module allows doing nightly tasks."""
from typing import Any
from typing import Dict
import argparse
import datetime
import glob
import logging
import os
import time
import traceback
import urllib.error
import areas
import config
import overpass_query
import stats
import util
def get_date_prefix() -> str:
"""Generates the current date as a log prefix."""
return time.strftime("%Y-%m-%d %H:%M:%S")
def info(msg: str, *args: Any, **kwargs: Any) -> None:
"""Wrapper around logging.info()."""
logging.info(get_date_prefix() + " INFO " + msg, *args, **kwargs)
def error(msg: str, *args: Any, **kwargs: Any) -> None:
"""Wrapper around logging.error()."""
logging.error(get_date_prefix() + " ERROR" + msg, *args, **kwargs)
def overpass_sleep() -> None:
"""Sleeps to respect overpass rate limit."""
while True:
sleep = overpass_query.overpass_query_need_sleep()
if not sleep:
break
info("overpass_sleep: waiting for %s seconds", sleep)
time.sleep(sleep)
def should_retry(retry: int) -> bool:
"""Decides if we should retry a query or not."""
return retry < 20
def update_osm_streets(relations: areas.Relations, update: bool) -> None:
"""Update the OSM street list of all relations."""
for relation_name in relations.get_active_names():
relation = relations.get_relation(relation_name)
if not update and os.path.exists(relation.get_files().get_osm_streets_path()):
continue
info("update_osm_streets: start: %s", relation_name)
retry = 0
while should_retry(retry):
if retry > 0:
info("update_osm_streets: try #%s", retry)
retry += 1
try:
overpass_sleep()
query = relation.get_osm_streets_query()
relation.get_files().write_osm_streets(overpass_query.overpass_query(query))
break
except urllib.error.HTTPError as http_error:
info("update_osm_streets: http error: %s", str(http_error))
info("update_osm_streets: end: %s", relation_name)
def update_osm_housenumbers(relations: areas.Relations, update: bool) -> None:
"""Update the OSM housenumber list of all relations."""
for relation_name in relations.get_active_names():
relation = relations.get_relation(relation_name)
if not update and os.path.exists(relation.get_files().get_osm_housenumbers_path()):
continue
info("update_osm_housenumbers: start: %s", relation_name)
retry = 0
while should_retry(retry):
if retry > 0:
info("update_osm_housenumbers: try #%s", retry)
retry += 1
try:
overpass_sleep()
query = relation.get_osm_housenumbers_query()
relation.get_files().write_osm_housenumbers(overpass_query.overpass_query(query))
break
except urllib.error.HTTPError as http_error:
info("update_osm_housenumbers: http error: %s", str(http_error))
info("update_osm_housenumbers: end: %s", relation_name)
def update_ref_housenumbers(relations: areas.Relations, update: bool) -> None:
"""Update the reference housenumber list of all relations."""
for relation_name in relations.get_active_names():
relation = relations.get_relation(relation_name)
if not update and os.path.exists(relation.get_files().get_ref_housenumbers_path()):
continue
references = config.Config.get_reference_housenumber_paths()
streets = relation.get_config().should_check_missing_streets()
if streets == "only":
continue
info("update_ref_housenumbers: start: %s", relation_name)
relation.write_ref_housenumbers(references)
info("update_ref_housenumbers: end: %s", relation_name)
def update_ref_streets(relations: areas.Relations, update: bool) -> None:
"""Update the reference street list of all relations."""
for relation_name in relations.get_active_names():
relation = relations.get_relation(relation_name)
if not update and os.path.exists(relation.get_files().get_ref_streets_path()):
continue
reference = config.Config.get_reference_street_path()
streets = relation.get_config().should_check_missing_streets()
if streets == "no":
continue
info("update_ref_streets: start: %s", relation_name)
relation.write_ref_streets(reference)
info("update_ref_streets: end: %s", relation_name)
def update_missing_housenumbers(relations: areas.Relations, update: bool) -> None:
"""Update the relation's house number coverage stats."""
info("update_missing_housenumbers: start")
for relation_name in relations.get_active_names():
relation = relations.get_relation(relation_name)
if not update and os.path.exists(relation.get_files().get_housenumbers_percent_path()):
continue
streets = relation.get_config().should_check_missing_streets()
if streets == "only":
continue
relation.write_missing_housenumbers()
info("update_missing_housenumbers: end")
def update_missing_streets(relations: areas.Relations, update: bool) -> None:
"""Update the relation's street coverage stats."""
info("update_missing_streets: start")
for relation_name in relations.get_active_names():
relation = relations.get_relation(relation_name)
if not update and os.path.exists(relation.get_files().get_streets_percent_path()):
continue
streets = relation.get_config().should_check_missing_streets()
if streets == "no":
continue
relation.write_missing_streets()
info("update_missing_streets: end")
def update_stats_count(today: str) -> None:
"""Counts the # of all house numbers as of today."""
statedir = config.get_abspath("workdir/stats")
csv_path = os.path.join(statedir, "%s.csv" % today)
count_path = os.path.join(statedir, "%s.count" % today)
city_count_path = os.path.join(statedir, "%s.citycount" % today)
house_numbers = set()
cities: Dict[str, int] = {}
first = True
with open(csv_path, "r") as stream:
for line in stream.readlines():
if first:
# Ignore the oneliner header.
first = False
continue
cells = line.split("\t")
# Ignore last column, which is the user who touched the object last.
house_numbers.add("\t".join(cells[:4]))
city_key = util.get_city_key(cells[0], cells[1])
if city_key in cities:
cities[city_key] += 1
else:
cities[city_key] = 1
with open(count_path, "w") as stream:
house_numbers_len = str(len(house_numbers))
stream.write(house_numbers_len + "\n")
with open(city_count_path, "w") as stream:
for key, value in cities.items():
stream.write(key + "\t" + str(value) + "\n")
def update_stats_topusers(today: str) -> None:
"""Counts the top housenumber editors as of today."""
statedir = config.get_abspath("workdir/stats")
csv_path = os.path.join(statedir, "%s.csv" % today)
topusers_path = os.path.join(statedir, "%s.topusers" % today)
usercount_path = os.path.join(statedir, "%s.usercount" % today)
users: Dict[str, int] = {}
with open(csv_path, "r") as stream:
for line in stream.readlines():
# Only care about the last column.
user = line[line.rfind("\t"):].strip()
if user in users:
users[user] += 1
else:
users[user] = 1
with open(topusers_path, "w") as stream:
for user in sorted(users, key=users.get, reverse=True)[:20]:
line = str(users[user]) + " " + user
stream.write(line + "\n")
with open(usercount_path, "w") as stream:
stream.write(str(len(users)) + "\n")
def update_stats(overpass: bool) -> None:
"""Performs the update of country-level stats."""
# Fetch house numbers for the whole country.
info("update_stats: start, updating whole-country csv")
query = util.get_content(config.get_abspath("data/street-housenumbers-hungary.txt"))
statedir = config.get_abspath("workdir/stats")
os.makedirs(statedir, exist_ok=True)
today = time.strftime("%Y-%m-%d")
csv_path = os.path.join(statedir, "%s.csv" % today)
if overpass:
retry = 0
while should_retry(retry):
if retry > 0:
info("update_stats: try #%s", retry)
retry += 1
try:
overpass_sleep()
response = overpass_query.overpass_query(query)
with open(csv_path, "w") as stream:
stream.write(response)
break
except urllib.error.HTTPError as http_error:
info("update_stats: http error: %s", str(http_error))
update_stats_count(today)
update_stats_topusers(today)
# Remove old CSV files as they are created daily and each is around 11M.
current_time = time.time()
for csv in glob.glob(os.path.join(statedir, "*.csv")):
creation_time = os.path.getmtime(csv)
if (current_time - creation_time) // (24 * 3600) >= 7:
os.unlink(csv)
info("update_stats: removed old %s", csv)
info("update_stats: generating json")
json_path = os.path.join(statedir, "stats.json")
with open(json_path, "w") as stream:
stats.generate_json(statedir, stream)
info("update_stats: end")
def our_main(relations: areas.Relations, mode: str, update: bool, overpass: bool) -> None:
"""Performs the actual nightly task."""
if mode in ("all", "stats"):
update_stats(overpass)
if mode in ("all", "relations"):
update_osm_streets(relations, update)
update_osm_housenumbers(relations, update)
update_ref_streets(relations, update)
update_ref_housenumbers(relations, update)
update_missing_streets(relations, update)
update_missing_housenumbers(relations, update)
pid = str(os.getpid())
with open("/proc/" + pid + "/status", "r") as stream:
vm_peak = ""
while True:
line = stream.readline()
if line.startswith("VmPeak:"):
vm_peak = line.strip()
if vm_peak or not line:
info("our_main: %s", line.strip())
break
def main() -> None:
"""Commandline interface to this module."""
util.set_locale()
workdir = config.Config.get_workdir()
relations = areas.Relations(workdir)
logpath = os.path.join(workdir, "cron.log")
logging.basicConfig(filename=logpath,
level=logging.INFO,
format='%(message)s')
handler = logging.StreamHandler()
logging.getLogger().addHandler(handler)
parser = argparse.ArgumentParser()
parser.add_argument("--refcounty", type=str,
help="limit the list of relations to a given refcounty")
parser.add_argument("--refsettlement", type=str,
help="limit the list of relations to a given refsettlement")
parser.add_argument('--no-update', dest='update', action='store_false',
help="don't update existing state of relations")
parser.add_argument("--mode", choices=["all", "stats", "relations"],
help="only perform the given sub-task or all of them")
parser.add_argument("--no-overpass", dest="overpass", action="store_false",
help="when updating stats, don't perform any overpass update")
parser.set_defaults(update=True, overpass=True, mode="relations")
args = parser.parse_args()
start = time.time()
# Query inactive relations once a month.
first_day_of_month = time.localtime(start).tm_mday == 1
relations.activate_all(config.Config.get_cron_update_inactive() or first_day_of_month)
relations.limit_to_refcounty(args.refcounty)
relations.limit_to_refsettlement(args.refsettlement)
try:
our_main(relations, args.mode, args.update, args.overpass)
# pylint: disable=broad-except
except Exception:
error("main: unhandled exception: %s", traceback.format_exc())
delta = time.time() - start
info("main: finished in %s", str(datetime.timedelta(seconds=delta)))
logging.getLogger().removeHandler(handler)
logging.shutdown()
if __name__ == "__main__":
main()
# vim:set shiftwidth=4 softtabstop=4 expandtab: