Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 48 additions & 5 deletions bin/clb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import socket
import sys
import time
import types
import pytz

import ago
import cloudlb
import cloudlb.errors
import ConfigParser
Expand Down Expand Up @@ -331,7 +331,7 @@ class CloudloadbalancersShell(object):
lb.id,
lb.name,
lb.status,
ago.human(lb.created),
pretty_date(lb.created),
lb.port,
lb.protocol,
lb.algorithm,
Expand All @@ -341,7 +341,7 @@ class CloudloadbalancersShell(object):
)
else:
rows.append([lb.id, lb.name, lb.status,
ago.human(lb.created),
pretty_date(lb.created),
lb.port, lb.protocol, lb.algorithm,
len(lb.nodes), ''])
# Throttle requests if there are more than 5 LBs
Expand Down Expand Up @@ -431,9 +431,9 @@ class CloudloadbalancersShell(object):

rows.append(['Connection Logging', lb.connection_logging().get()])
rows.append(['Created', '%s (%s)' % (lb.created,
ago.human(lb.created))])
pretty_date(lb.created))])
rows.append(['Updated', '%s (%s)' % (lb.updated,
ago.human(lb.updated))])
pretty_date(lb.updated))])

print_list2(fields, rows, args.batch, args.delimiter)

Expand Down Expand Up @@ -960,6 +960,49 @@ def print_list(objs, fields, formatters={}):

print(pt.get_string(sortby=fields[0]))

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
"""
from datetime import datetime
now = datetime.utcnow()
now = now.replace(tzinfo=pytz.utc)
if type(time) is int:
diff = now - datetime.fromtimestamp(time)
elif isinstance(time,datetime):
diff = now - time
elif not time:
diff = now - now
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 ago"
if second_diff < 120:
return "a minute ago"
if second_diff < 3600:
return str( second_diff / 60 ) + " minutes ago"
if second_diff < 7200:
return "an hour ago"
if second_diff < 86400:
return str( second_diff / 3600 ) + " hours ago"
if day_diff == 1:
return "Yesterday"
if day_diff < 7:
return str(day_diff) + " days ago"
if day_diff < 31:
return str(day_diff/7) + " weeks ago"
if day_diff < 365:
return str(day_diff/30) + " months ago"
return str(day_diff/365) + " years ago"

def print_dict(d):
pt = prettytable.PrettyTable(['Property', 'Value'], caching=False)
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
ago==0.0.6
prettytable==0.7.2
python-cloudlb>=0.6.1