Merge branch 'issue2004' into develop

This commit is contained in:
nicolargo 2022-03-05 14:38:35 +01:00
commit 0c14e966a7
6 changed files with 65 additions and 3 deletions

View File

@ -29,6 +29,7 @@ import unicodedata
import types
import subprocess
import os
from datetime import datetime
from glances.logger import logger
@ -311,3 +312,47 @@ def enable(class_name, var):
"""Set disable_<var> to False in the class class_name."""
setattr(class_name, 'enable_' + var, True)
setattr(class_name, 'disable_' + var, False)
def pretty_date(time=False):
"""
Get a datetime object or a int() Epoch timestamp and return a
pretty string like 'an hour ago', 'Yesterday', '3 months ago',
'just now', etc
Source: https://stackoverflow.com/questions/1551382/user-friendly-time-format-in-python
"""
now = datetime.now()
if type(time) is int:
diff = now - datetime.fromtimestamp(time)
elif isinstance(time, datetime):
diff = now - time
elif not time:
diff = 0
second_diff = diff.seconds
day_diff = diff.days
if day_diff < 0:
return ''
if day_diff == 0:
if second_diff < 10:
return "just now"
if second_diff < 60:
return str(second_diff) + " seconds"
if second_diff < 120:
return "a minute"
if second_diff < 3600:
return str(second_diff // 60) + " minutes"
if second_diff < 7200:
return "an hour"
if second_diff < 86400:
return str(second_diff // 3600) + " hours"
if day_diff == 1:
return "Yesterday"
if day_diff < 7:
return str(day_diff) + " days"
if day_diff < 31:
return str(day_diff // 7) + " weeks"
if day_diff < 365:
return str(day_diff // 30) + " months"
return str(day_diff // 365) + " years"

View File

@ -17,6 +17,7 @@ export default function GlancesPluginDockerController($scope, GlancesStats) {
'id': containerData.Id,
'name': containerData.name,
'status': containerData.Status,
'uptime': containerData.Uptime,
'cpu': containerData.cpu.total,
'memory': containerData.memory.usage != undefined ? containerData.memory.usage : '?',
'rss': containerData.memory.rss != undefined ? containerData.memory.rss : '?',

View File

@ -5,6 +5,7 @@
<div class="table-row">
<div class="table-cell text-left">Name</div>
<div class="table-cell">Status</div>
<div class="table-cell">Uptime</div>
<div class="table-cell">CPU%</div>
<div class="table-cell">MEM</div>
<div class="table-cell">RSS</div>
@ -18,6 +19,8 @@
<div class="table-cell text-left">{{ container.name }}</div>
<div class="table-cell" ng-class="container.status == 'Paused' ? 'careful' : 'ok'">{{ container.status }}
</div>
<div class="table-cell" ng-class="container.status == 'Paused' ? 'careful' : 'ok'">{{ container.uptime }}
</div>
<div class="table-cell">{{ container.cpu | number:1 }}</div>
<div class="table-cell">{{ container.memory | bytes }}</div>
<div class="table-cell">{{ container.rss | bytes }}</div>

View File

@ -1018,6 +1018,7 @@ function GlancesPluginDockerController($scope, GlancesStats) {
'id': containerData.Id,
'name': containerData.name,
'status': containerData.Status,
'uptime': containerData.Uptime,
'cpu': containerData.cpu.total,
'memory': containerData.memory.usage != undefined ? containerData.memory.usage : '?',
'rss': containerData.memory.rss != undefined ? containerData.memory.rss : '?',
@ -60178,7 +60179,7 @@ module.exports = path;
/***/ ((module) => {
// Module
var code = "<section id=\"containers-plugin\" class=\"plugin\" ng-if=\"vm.containers.length\">\n <span class=\"title\">CONTAINERS</span> {{ vm.containers.length }} (served by Docker {{ vm.version }})\n\n <div class=\"table\">\n <div class=\"table-row\">\n <div class=\"table-cell text-left\">Name</div>\n <div class=\"table-cell\">Status</div>\n <div class=\"table-cell\">CPU%</div>\n <div class=\"table-cell\">MEM</div>\n <div class=\"table-cell\">RSS</div>\n <div class=\"table-cell\">IOR/s</div>\n <div class=\"table-cell\">IOW/s</div>\n <div class=\"table-cell\">RX/s</div>\n <div class=\"table-cell\">TX/s</div>\n <div class=\"table-cell text-left\">Command</div>\n </div>\n <div class=\"table-row\" ng-repeat=\"container in vm.containers track by container.id\">\n <div class=\"table-cell text-left\">{{ container.name }}</div>\n <div class=\"table-cell\" ng-class=\"container.status == 'Paused' ? 'careful' : 'ok'\">{{ container.status }}\n </div>\n <div class=\"table-cell\">{{ container.cpu | number:1 }}</div>\n <div class=\"table-cell\">{{ container.memory | bytes }}</div>\n <div class=\"table-cell\">{{ container.rss | bytes }}</div>\n <div class=\"table-cell\">{{ container.ior / container.io_time_since_update | bits }}</div>\n <div class=\"table-cell\">{{ container.iow / container.io_time_since_update | bits }}</div>\n <div class=\"table-cell\">{{ container.rx / container.net_time_since_update | bits }}</div>\n <div class=\"table-cell\">{{ container.tx / container.net_time_since_update | bits }}</div>\n <div class=\"table-cell text-left\">{{ container.command }}</div>\n </div>\n </div>\n</section>\n";
var code = "<section id=\"containers-plugin\" class=\"plugin\" ng-if=\"vm.containers.length\">\n <span class=\"title\">CONTAINERS</span> {{ vm.containers.length }} (served by Docker {{ vm.version }})\n\n <div class=\"table\">\n <div class=\"table-row\">\n <div class=\"table-cell text-left\">Name</div>\n <div class=\"table-cell\">Status</div>\n <div class=\"table-cell\">Uptime</div>\n <div class=\"table-cell\">CPU%</div>\n <div class=\"table-cell\">MEM</div>\n <div class=\"table-cell\">RSS</div>\n <div class=\"table-cell\">IOR/s</div>\n <div class=\"table-cell\">IOW/s</div>\n <div class=\"table-cell\">RX/s</div>\n <div class=\"table-cell\">TX/s</div>\n <div class=\"table-cell text-left\">Command</div>\n </div>\n <div class=\"table-row\" ng-repeat=\"container in vm.containers track by container.id\">\n <div class=\"table-cell text-left\">{{ container.name }}</div>\n <div class=\"table-cell\" ng-class=\"container.status == 'Paused' ? 'careful' : 'ok'\">{{ container.status }}\n </div>\n <div class=\"table-cell\" ng-class=\"container.status == 'Paused' ? 'careful' : 'ok'\">{{ container.uptime }}\n </div>\n <div class=\"table-cell\">{{ container.cpu | number:1 }}</div>\n <div class=\"table-cell\">{{ container.memory | bytes }}</div>\n <div class=\"table-cell\">{{ container.rss | bytes }}</div>\n <div class=\"table-cell\">{{ container.ior / container.io_time_since_update | bits }}</div>\n <div class=\"table-cell\">{{ container.iow / container.io_time_since_update | bits }}</div>\n <div class=\"table-cell\">{{ container.rx / container.net_time_since_update | bits }}</div>\n <div class=\"table-cell\">{{ container.tx / container.net_time_since_update | bits }}</div>\n <div class=\"table-cell text-left\">{{ container.command }}</div>\n </div>\n </div>\n</section>\n";
// Exports
var _module_exports =code;;
var path = '/home/nicolargo/dev/glances/glances/outputs/static/js/components/plugin-docker/view.html';

File diff suppressed because one or more lines are too long

View File

@ -23,8 +23,9 @@ import os
import threading
import time
from copy import deepcopy
from dateutil import parser
from glances.compat import iterkeys, itervalues, nativestr
from glances.compat import iterkeys, itervalues, nativestr, pretty_date
from glances.logger import logger
from glances.plugins.glances_plugin import GlancesPlugin
from glances.processes import sort_stats as sort_stats_processes, glances_processes
@ -290,6 +291,8 @@ class Plugin(GlancesPlugin):
)
container_stats['network_rx'] = container_stats['network'].get('rx', None)
container_stats['network_tx'] = container_stats['network'].get('tx', None)
# Uptime
container_stats['Uptime'] = pretty_date(parser.parse(container.attrs['State']['StartedAt']).replace(tzinfo=None))
else:
container_stats['cpu'] = {}
container_stats['cpu_percent'] = None
@ -301,6 +304,7 @@ class Plugin(GlancesPlugin):
container_stats['network'] = {}
container_stats['network_rx'] = None
container_stats['network_tx'] = None
container_stats['Uptime'] = None
# Add current container stats to the stats list
stats['containers'].append(container_stats)
@ -555,6 +559,8 @@ class Plugin(GlancesPlugin):
ret.append(self.curse_add_line(msg))
msg = '{:>10}'.format('Status')
ret.append(self.curse_add_line(msg))
msg = '{:>10}'.format('Uptime')
ret.append(self.curse_add_line(msg))
msg = '{:>6}'.format('CPU%')
ret.append(self.curse_add_line(msg))
msg = '{:>7}'.format('MEM')
@ -580,6 +586,12 @@ class Plugin(GlancesPlugin):
status = self.container_alert(container['Status'])
msg = '{:>10}'.format(container['Status'][0:10])
ret.append(self.curse_add_line(msg, status))
# Uptime
if container['Uptime']:
msg = '{:>10}'.format(container['Uptime'])
else:
msg = '{:>10}'.format('_')
ret.append(self.curse_add_line(msg, status))
# CPU
try:
msg = '{:>6.1f}'.format(container['cpu']['total'])