Skip to content

Commit

Permalink
Merge pull request hyperledger#936 from sergey-shilov/feature/INDY-1637
Browse files Browse the repository at this point in the history
INDY-1637: add fixes and printing improvements.
  • Loading branch information
ashcherbakov authored Sep 6, 2018
2 parents ad61020 + 97ca4e6 commit 54b25b1
Showing 1 changed file with 46 additions and 20 deletions.
66 changes: 46 additions & 20 deletions scripts/validator-info-history
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,6 @@ class FloatUnknown(BaseUnknown):
return "{:.2f}".format(self.val)


class TimestampUnknown(BaseUnknown):
def _str(self):
return "{} ({})".format(
datetime.datetime.fromtimestamp(self.val).strftime(
"%A, %B %{0}d, %Y %{0}I:%M:%S %p".format(
'#' if os.name == 'nt' else '-')),
self.val
)


class UptimeUnknown(BaseUnknown):
def _str(self):
days, remainder = divmod(self.val, 86400)
Expand Down Expand Up @@ -335,7 +325,7 @@ class SoftwareStats(BaseStats):
class ValidatorStats(BaseStats):
shema = [
("response-version", BaseUnknown),
("timestamp", TimestampUnknown),
("timestamp", BaseUnknown),
("Node_info", NodeStats),
("state", StateUnknown),
("enabled", BaseUnknown),
Expand Down Expand Up @@ -450,7 +440,7 @@ class ValidatorStats(BaseStats):
# will drop visibility of output
lines = [
"Validator {} is {}".format(self['Node_info']['Name'], self['state']),
"Current time: {}".format(self['timestamp']),
"Timestamp: {}".format(self['timestamp']),
"Validator DID: {}".format(self['Node_info']['did']),
"Verification Key: {}".format(self['Node_info']['verkey']),
"Node Port: {}".format(self['Node_info']['Node_port']),
Expand Down Expand Up @@ -496,6 +486,10 @@ class ValidatorStats(BaseStats):
)


def str_from_timestamp(ts):
return datetime.datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S")


def get_validator_stats(stats, verbose, _json):

logger.debug("Data {}".format(stats))
Expand Down Expand Up @@ -575,6 +569,13 @@ def read_json_data(info_path, num: int = 1, from_start = False, _from: int = Non

if _from != None:
it.seek_for_prev(db.to_byte_repr(_from))
# Handle the case when 'from' timestamp is ahead from the first record
try:
# Try to make a step forward and back
_ = next(it)
_ = next(reversed(it))
except StopIteration:
it.seek_to_first()
elif _to != None or from_start:
it.seek_to_first()
else:
Expand All @@ -591,11 +592,12 @@ def read_json_data(info_path, num: int = 1, from_start = False, _from: int = Non
print("DB '{}' contains invalid records".format(info_path))
return None

obj['timestamp'] = int(ts)
ts = int(ts)
obj['timestamp'] = "{} ({})".format(str_from_timestamp(ts), ts)

if _from != None or _to != None:
if _from == None or _from <= obj['timestamp']:
if _to == None or obj['timestamp'] <= _to:
if _from == None or _from <= ts:
if _to == None or ts <= _to:
json_data.append(obj)
else:
break
Expand Down Expand Up @@ -642,6 +644,30 @@ def get_info_paths(node_names, basedir):
return info_paths


def check_unsigned(s):
res = None
try:
res = int(s)
except ValueError:
pass
if res is None or res < 0:
raise argparse.ArgumentTypeError(("{!r} is incorrect, "
"should be int >= 0").format(s,))
else:
return res

def check_N_arg(s):
res = None
try:
res = int(s)
except ValueError:
pass
if res is None or (res <= 0 and res != -1):
raise argparse.ArgumentTypeError(("{!r} is incorrect, "
"should be int > 0 or -1").format(s,))
else:
return res

def parse_args():
config_helper = ConfigHelper(config)

Expand All @@ -653,7 +679,7 @@ def parse_args():
)

parser.add_argument(
"-n", "--num", metavar="NUM", type=int,
"-n", "--num", metavar="NUM", type=check_N_arg,
default=1,
help=("Number of records to print (from the end by default), -1 means all records")
)
Expand All @@ -670,13 +696,13 @@ def parse_args():
)

parser.add_argument(
"--frm", metavar="TIMESTAMP", type=int,
"--frm", metavar="TIMESTAMP", type=check_unsigned,
default=None,
help=("Print records from timestamp (ignores -n and -s)")
)

parser.add_argument(
"--to", metavar="TIMESTAMP", type=int,
"--to", metavar="TIMESTAMP", type=check_unsigned,
default=None,
help=("Print records to timestamp (ignores -n and -s)")
)
Expand Down Expand Up @@ -727,8 +753,8 @@ def parse_args():


def check_args(args):
if args.num <= 0 and args.num != -1:
print("Number of records to print must be greater than 0 or equal to -1")
if args.frm != None and args.to != None and args.frm > args.to:
print("'From' timestamp can not be greater that 'to' timestamp")
return False

return True
Expand Down

0 comments on commit 54b25b1

Please sign in to comment.