From afe61664ac5f933622e349da1c0a92d134a81230 Mon Sep 17 00:00:00 2001 From: Flaper Fesp Date: Mon, 21 Jan 2013 11:00:58 +0100 Subject: [PATCH] Prints list-cached dates in isoformat Converts dates printed by list-cached to a human readable format (isoformat). It now checks whether the image last_access time is == 0.0 and prints "Not Accessed Yet" if so. * Updates openstack.common.timeutils * Fixes bug 1102334 Change-Id: I6f60525d8419d45e6962b936a2661825b606cba2 --- bin/glance-cache-manage | 14 ++++++- glance/openstack/common/timeutils.py | 42 +++++++++++++++++-- .../v1/test_bin_glance_cache_manage.py | 13 ++++++ 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/bin/glance-cache-manage b/bin/glance-cache-manage index 2f42c21a8b..4e39f11558 100755 --- a/bin/glance-cache-manage +++ b/bin/glance-cache-manage @@ -40,6 +40,7 @@ gettext.install('glance', unicode=1) from glance import client as glance_client from glance.common import exception from glance.common import utils +from glance.openstack.common import timeutils from glance.version import version_info as version @@ -102,10 +103,19 @@ List all images currently cached""" print pretty_table.make_header() for image in images: + last_modified = image['last_modified'] + last_modified = timeutils.iso8601_from_timestamp(last_modified) + + last_accessed = image['last_accessed'] + if last_accessed == 0: + last_accessed = "N/A" + else: + last_accessed = timeutils.iso8601_from_timestamp(last_accessed) + print pretty_table.make_row( image['image_id'], - image['last_accessed'], - image['last_modified'], + last_accessed, + last_modified, image['size'], image['hits']) diff --git a/glance/openstack/common/timeutils.py b/glance/openstack/common/timeutils.py index c4f6cf0497..96ed72ce87 100644 --- a/glance/openstack/common/timeutils.py +++ b/glance/openstack/common/timeutils.py @@ -84,6 +84,11 @@ def utcnow(): return datetime.datetime.utcnow() +def iso8601_from_timestamp(timestamp): + """Returns a iso8601 formated date from timestamp""" + return isotime(datetime.datetime.utcfromtimestamp(timestamp)) + + utcnow.override_time = None @@ -120,7 +125,36 @@ def marshall_now(now=None): def unmarshall_time(tyme): - """Unmarshall a datetime dict.""" - return datetime.datetime(day=tyme['day'], month=tyme['month'], - year=tyme['year'], hour=tyme['hour'], minute=tyme['minute'], - second=tyme['second'], microsecond=tyme['microsecond']) + return datetime.datetime(day=tyme['day'], + month=tyme['month'], + year=tyme['year'], + hour=tyme['hour'], + minute=tyme['minute'], + second=tyme['second'], + microsecond=tyme['microsecond']) + + +def delta_seconds(before, after): + """ + Compute the difference in seconds between two date, time, or + datetime objects (as a float, to microsecond resolution). + """ + delta = after - before + try: + return delta.total_seconds() + except AttributeError: + return ((delta.days * 24 * 3600) + delta.seconds + + float(delta.microseconds) / (10 ** 6)) + + +def is_soon(dt, window): + """ + Determines if time is going to happen in the next window seconds. + + :params dt: the time + :params window: minimum seconds to remain to consider the time not soon + + :return: True if expiration is within the given duration + """ + soon = (utcnow() + datetime.timedelta(seconds=window)) + return normalize_time(dt) < soon diff --git a/glance/tests/functional/v1/test_bin_glance_cache_manage.py b/glance/tests/functional/v1/test_bin_glance_cache_manage.py index 4cccfa1a8c..67b2d9688b 100644 --- a/glance/tests/functional/v1/test_bin_glance_cache_manage.py +++ b/glance/tests/functional/v1/test_bin_glance_cache_manage.py @@ -17,6 +17,7 @@ """Functional test case that utilizes the bin/glance-cache-manage CLI tool""" +import datetime import hashlib import httplib2 import json @@ -76,6 +77,16 @@ def is_image_cached(self, image_id): self.assertEqual(0, exitcode) return image_id in out + def iso_date(self, image_id): + """ + Return True if supplied image ID is cached, False otherwise + """ + cmd = "bin/glance-cache-manage --port=%d list-cached" % self.api_port + + exitcode, out, err = execute(cmd) + + return datetime.date.today().isoformat() in out + def test_no_cache_enabled(self): """ Test that cache index command works @@ -132,6 +143,8 @@ def test_cache_index(self): self.assertTrue(self.is_image_cached(ids[1]), "%s is not cached." % ids[1]) + self.assertTrue(self.iso_date(ids[1])) + self.stop_servers() def test_queue(self):