Navigation Menu

Skip to content

Commit

Permalink
Prints list-cached dates in isoformat
Browse files Browse the repository at this point in the history
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
  • Loading branch information
flaper87 committed Mar 4, 2013
1 parent ee13560 commit afe6166
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
14 changes: 12 additions & 2 deletions bin/glance-cache-manage
Expand Up @@ -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


Expand Down Expand Up @@ -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'])

Expand Down
42 changes: 38 additions & 4 deletions glance/openstack/common/timeutils.py
Expand Up @@ -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


Expand Down Expand Up @@ -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
13 changes: 13 additions & 0 deletions glance/tests/functional/v1/test_bin_glance_cache_manage.py
Expand Up @@ -17,6 +17,7 @@

"""Functional test case that utilizes the bin/glance-cache-manage CLI tool"""

import datetime
import hashlib
import httplib2
import json
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit afe6166

Please sign in to comment.