Let MongoClient be tz_aware

- output datetime objects from mongodb are now tz_aware with tzinfo=utc
- fixes #11
This commit is contained in:
Dominik Heidler 2016-02-15 18:01:26 +01:00
parent 4cddcbfec3
commit c34246f3a8
6 changed files with 19 additions and 19 deletions

View File

@ -155,14 +155,14 @@ def crawl(router):
try:
if router["system"]["uptime"] > router_update["system"]["uptime"]:
events.append({
"time": datetime.datetime.utcnow(),
"time": datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc),
"type": "reboot",
})
except:
pass
if router["status"] != status:
events.append({
"time": datetime.datetime.utcnow(),
"time": datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc),
"type": status,
})
db.routers.update_one({"_id": router["_id"]}, {"$push": {"events": {

View File

@ -79,7 +79,7 @@ for r in tree.xpath("/netmon_response/routerlist/router"):
"ipv6_fe80_addr": netif_ip
}]
router["created"] = datetime.datetime.utcnow()
router["created"] = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
router["status"] = "unknown"
db.routers.insert_one(router)

View File

@ -9,7 +9,7 @@ class FreifunkDB(object):
@classmethod
def handle(cls):
if not cls.client:
cls.client = MongoClient()
cls.client = MongoClient(tz_aware=True)
if not cls.db:
cls.db = cls.client.freifunk
return cls.db

View File

@ -37,7 +37,7 @@ def import_nodewatcher_xml(mac, xml):
router_info = netmon_fetch_router_info(mac)
if router_info:
events.append({
"time": datetime.datetime.utcnow(),
"time": datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc),
"type": "netmon",
"comment": "Fetched metadata from netmon",
})
@ -64,11 +64,11 @@ def import_nodewatcher_xml(mac, xml):
}})
else:
# insert new router
router_update["created"] = datetime.datetime.utcnow()
router_update["created"] = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
router_update["stats"] = []
events = [] # don't fire sub-events of created events
router_update["events"] = [{
"time": datetime.datetime.utcnow(),
"time": datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc),
"type": "created",
}]
router_id = db.routers.insert_one(router_update).inserted_id
@ -84,14 +84,14 @@ def import_nodewatcher_xml(mac, xml):
with suppress(KeyError, TypeError):
if router["system"]["uptime"] > router_update["system"]["uptime"]:
events.append({
"time": datetime.datetime.utcnow(),
"time": datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc),
"type": "reboot",
})
with suppress(KeyError, TypeError):
if router["software"]["firmware"] != router_update["software"]["firmware"]:
events.append({
"time": datetime.datetime.utcnow(),
"time": datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc),
"type": "update",
"comment": "%s -> %s" % (router["software"]["firmware"], router_update["software"]["firmware"]),
})
@ -99,7 +99,7 @@ def import_nodewatcher_xml(mac, xml):
with suppress(KeyError, TypeError):
if router["hostname"] != router_update["hostname"]:
events.append({
"time": datetime.datetime.utcnow(),
"time": datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc),
"type": "hostname",
"comment": "%s -> %s" % (router["hostname"], router_update["hostname"]),
})
@ -107,7 +107,7 @@ def import_nodewatcher_xml(mac, xml):
with suppress(KeyError, TypeError):
if router["hood"] != router_update["hood"]:
events.append({
"time": datetime.datetime.utcnow(),
"time": datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc),
"type": "hood",
"comment": "%s -> %s" % (router["hood"], router_update["hood"]),
})
@ -115,7 +115,7 @@ def import_nodewatcher_xml(mac, xml):
with suppress(KeyError, TypeError):
if router["status"] != status:
events.append({
"time": datetime.datetime.utcnow(),
"time": datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc),
"type": status,
})
@ -127,12 +127,12 @@ def import_nodewatcher_xml(mac, xml):
def detect_offline_routers():
db.routers.update_many({
"last_contact": {"$lt": datetime.datetime.utcnow() - datetime.timedelta(minutes=CONFIG["offline_threshold_minutes"])},
"last_contact": {"$lt": datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc) - datetime.timedelta(minutes=CONFIG["offline_threshold_minutes"])},
"status": {"$ne": "offline"}
}, {
"$set": {"status": "offline", "system.clients": 0},
"$push": {"events": {
"time": datetime.datetime.utcnow(),
"time": datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc),
"type": "offline"
}
}})
@ -150,7 +150,7 @@ def new_router_stats(router, router_update):
with suppress(KeyError):
neighbours[neighbour["mac"]] = neighbour["quality"]
return [{
"time": datetime.datetime.utcnow(),
"time": datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc),
"netifs": netifs,
"neighbours": neighbours,
"memory": router_update["system"]["memory"],
@ -190,7 +190,7 @@ def parse_nodewatcher_xml(xml):
router_update = {
"status": tree.xpath("/data/system_data/status/text()")[0],
"hostname": tree.xpath("/data/system_data/hostname/text()")[0],
"last_contact": datetime.datetime.utcnow(),
"last_contact": datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc),
"neighbours": [],
"netifs": [],
"system": {

View File

@ -75,7 +75,7 @@ def hoods_sum():
def record_global_stats():
db.stats.insert_one({
"time": datetime.datetime.utcnow(),
"time": datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc),
"router_status": router_status(),
"total_clients": total_clients()
})

View File

@ -29,7 +29,7 @@ def neighbour_color(quality):
@filters.app_template_filter('utc2local')
def utc2local(dt):
return dt.replace(tzinfo=tz.tzutc()).astimezone(tz.tzlocal())
return dt.astimezone(tz.tzlocal())
@filters.app_template_filter('format_dt')
def format_dt(dt):
@ -45,7 +45,7 @@ def dt2jstimestamp(dt):
@filters.app_template_filter('format_dt_ago')
def format_dt_ago(dt):
diff = datetime.datetime.utcnow() - dt
diff = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc) - dt
s = diff.seconds
if diff.days > 1:
return '%i days ago' % diff.days