From 830341a687e2e0aeac3304f8508c20fc1b47f9c4 Mon Sep 17 00:00:00 2001 From: ljf Date: Sun, 9 Dec 2018 19:27:44 +0100 Subject: [PATCH] [enh] Display date correctly --- data/actionsmap/yunohost.yml | 5 ----- debian/control | 2 +- src/yunohost/app.py | 14 ++++++-------- src/yunohost/log.py | 13 ++++++++++--- src/yunohost/monitor.py | 2 +- src/yunohost/service.py | 17 ++++++++--------- 6 files changed, 26 insertions(+), 27 deletions(-) diff --git a/data/actionsmap/yunohost.yml b/data/actionsmap/yunohost.yml index a6bc8ec3e..ba5f1d505 100644 --- a/data/actionsmap/yunohost.yml +++ b/data/actionsmap/yunohost.yml @@ -470,11 +470,6 @@ app: listlists: action_help: List registered application lists api: GET /appslists - arguments: - -H: - full: --human-readable - help: Return the lastUpdate with a human readable date - action: store_true ### app_removelist() removelist: diff --git a/debian/control b/debian/control index 9f72bf11a..f0423c4a3 100644 --- a/debian/control +++ b/debian/control @@ -11,7 +11,7 @@ Package: yunohost Architecture: all Depends: ${python:Depends}, ${misc:Depends} , moulinette (>= 2.7.1), ssowat (>= 2.7.1) - , python-psutil, python-requests, python-dnspython, python-openssl + , python-psutil, python-requests, python-dnspython, python-openssl, python-tz , python-apt, python-miniupnpc, python-dbus, python-jinja2 , glances , dnsutils, bind9utils, unzip, git, curl, cron, wget, jq diff --git a/src/yunohost/app.py b/src/yunohost/app.py index 66d83db9c..d5121a418 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -37,6 +37,7 @@ import pwd import grp from collections import OrderedDict import datetime +import pytz from moulinette import msignals, m18n, msettings from moulinette.core import MoulinetteError @@ -67,7 +68,7 @@ re_app_instance_name = re.compile( ) -def app_listlists(human_readable=False): +def app_listlists(): """ List fetched lists @@ -84,13 +85,10 @@ def app_listlists(human_readable=False): # Get the list appslist_list = _read_appslist_list() - # Human readable date - if human_readable: - for app in appslist_list: - now_for_humans = datetime.datetime.fromtimestamp( - appslist_list[app].get("lastUpdate")) - appslist_list[app]["lastUpdate"] = now_for_humans.strftime( - '%Y-%m-%d %H:%M:%S') + for app in appslist_list: + last_update = datetime.datetime.utcfromtimestamp( + appslist_list[app].get("lastUpdate")) + appslist_list[app]["lastUpdate"] = last_update.replace(tzinfo=pytz.utc) return appslist_list diff --git a/src/yunohost/log.py b/src/yunohost/log.py index c105b8279..c75300d3d 100644 --- a/src/yunohost/log.py +++ b/src/yunohost/log.py @@ -28,6 +28,7 @@ import os import yaml import errno import collections +import pytz from datetime import datetime from logging import FileHandler, getLogger, Formatter @@ -100,7 +101,7 @@ def log_list(category=[], limit=None): except ValueError: pass else: - entry["started_at"] = log_datetime + entry["started_at"] = log_datetime.replace(tzinfo=pytz.utc) result[category].append(entry) @@ -182,6 +183,10 @@ def log_display(path, number=50, share=False): metadata = yaml.safe_load(md_file) infos['metadata_path'] = md_path infos['metadata'] = metadata + if 'started_at' in infos['metadata']: + infos['metadata']['started_at'] = infos['metadata']['started_at'].replace(tzinfo=pytz.utc) + if 'ended_at' in infos['metadata']: + infos['metadata']['ended_at'] = infos['metadata']['ended_at'].replace(tzinfo=pytz.utc) if 'log_path' in metadata: log_path = metadata['log_path'] except yaml.YAMLError: @@ -316,7 +321,8 @@ class OperationLogger(object): """ if self.started_at is None: - self.started_at = datetime.now() + self.started_at = datetime.utcnow() + self.started_at = self.started_at.replace(tzinfo=pytz.utc) self.flush() self._register_log() @@ -408,7 +414,8 @@ class OperationLogger(object): return if error is not None and not isinstance(error, basestring): error = str(error) - self.ended_at = datetime.now() + self.ended_at = datetime.utcnow() + self.ended_at = self.ended_at.replace(tzinfo=pytz.utc) self._error = error self._success = error is None if self.logger is not None: diff --git a/src/yunohost/monitor.py b/src/yunohost/monitor.py index fc10a4fbc..cad467ed5 100644 --- a/src/yunohost/monitor.py +++ b/src/yunohost/monitor.py @@ -283,7 +283,7 @@ def monitor_system(units=None, human_readable=False): elif u == 'process': result[u] = json.loads(glances.getProcessCount()) elif u == 'uptime': - result[u] = (str(datetime.now() - datetime.fromtimestamp(psutil.boot_time())).split('.')[0]) + result[u] = (str(datetime.utcnow() - datetime.utcfromtimestamp(psutil.boot_time())).split('.')[0]) elif u == 'infos': result[u] = json.loads(glances.getSystem()) else: diff --git a/src/yunohost/service.py b/src/yunohost/service.py index 5b7680a80..73383cb44 100644 --- a/src/yunohost/service.py +++ b/src/yunohost/service.py @@ -31,6 +31,7 @@ import subprocess import errno import shutil import hashlib +import pytz from difflib import unified_diff from datetime import datetime @@ -248,10 +249,7 @@ def service_status(names=[]): 'status': "unknown", 'loaded': "unknown", 'active': "unknown", - 'active_at': { - "timestamp": "unknown", - "human": "unknown", - }, + 'active_at': "unknown", 'description': "Error: failed to get information for this service, it doesn't exists for systemd", 'service_file_path': "unknown", } @@ -273,13 +271,14 @@ def service_status(names=[]): 'status': str(status.get("SubState", "unknown")), 'loaded': "enabled" if str(status.get("LoadState", "unknown")) == "loaded" else str(status.get("LoadState", "unknown")), 'active': str(status.get("ActiveState", "unknown")), - 'active_at': { - "timestamp": str(status.get("ActiveEnterTimestamp", "unknown")), - "human": datetime.fromtimestamp(status["ActiveEnterTimestamp"] / 1000000).strftime("%F %X") if "ActiveEnterTimestamp" in status else "unknown", - }, 'description': description, 'service_file_path': str(status.get("FragmentPath", "unknown")), } + if "ActiveEnterTimestamp" in status: + result[name]['active_at'] = datetime.utcfromtimestamp(status["ActiveEnterTimestamp"] / 1000000) + result[name]['active_at'] = result[name]['active_at'].replace(tzinfo=pytz.utc) + else: + result[name]['active_at'] = "unknown" if len(names) == 1: return result[names[0]] @@ -293,7 +292,7 @@ def _get_service_information_from_systemd(service): d = dbus.SystemBus() - systemd = d.get_object('org.freedesktop.systemd1','/org/freedesktop/systemd1') + systemd = d.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1') manager = dbus.Interface(systemd, 'org.freedesktop.systemd1.Manager') try: