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: I46f023471ec87fdf74c18f33309279a2f39a4e2a
This commit is contained in:
Flaper Fesp 2013-01-21 11:00:58 +01:00
parent 43f8697696
commit 35be498112
3 changed files with 43 additions and 2 deletions

View File

@ -40,6 +40,7 @@ gettext.install('glance', unicode=1)
import glance.image_cache.client import glance.image_cache.client
from glance.common import exception from glance.common import exception
from glance.common import utils from glance.common import utils
from glance.openstack.common import timeutils
from glance.version import version_info as version from glance.version import version_info as version
@ -102,10 +103,19 @@ List all images currently cached"""
print pretty_table.make_header() print pretty_table.make_header()
for image in images: 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( print pretty_table.make_row(
image['image_id'], image['image_id'],
image['last_accessed'], last_accessed,
image['last_modified'], last_modified,
image['size'], image['size'],
image['hits']) image['hits'])

View File

@ -98,6 +98,11 @@ def utcnow():
return datetime.datetime.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 utcnow.override_time = None
@ -162,3 +167,16 @@ def delta_seconds(before, after):
except AttributeError: except AttributeError:
return ((delta.days * 24 * 3600) + delta.seconds + return ((delta.days * 24 * 3600) + delta.seconds +
float(delta.microseconds) / (10 ** 6)) 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

View File

@ -17,6 +17,7 @@
"""Functional test case that utilizes the bin/glance-cache-manage CLI tool""" """Functional test case that utilizes the bin/glance-cache-manage CLI tool"""
import datetime
import hashlib import hashlib
import httplib2 import httplib2
import json import json
@ -76,6 +77,16 @@ class TestBinGlanceCacheManage(functional.FunctionalTest):
self.assertEqual(0, exitcode) self.assertEqual(0, exitcode)
return image_id in out 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): def test_no_cache_enabled(self):
""" """
Test that cache index command works Test that cache index command works
@ -132,6 +143,8 @@ class TestBinGlanceCacheManage(functional.FunctionalTest):
self.assertTrue(self.is_image_cached(ids[1]), self.assertTrue(self.is_image_cached(ids[1]),
"%s is not cached." % ids[1]) "%s is not cached." % ids[1])
self.assertTrue(self.iso_date(ids[1]))
self.stop_servers() self.stop_servers()
def test_queue(self): def test_queue(self):