Skip to content

Commit

Permalink
Merge pull request #1041 from larsbutler/add-timestamps-to-list-output
Browse files Browse the repository at this point in the history
Add timestamps to calculation summary output

[r=micheles] [f=1129271]
  • Loading branch information
larsbutler committed Feb 21, 2013
2 parents eb56f1e + 4f19208 commit faa12c0
Showing 1 changed file with 64 additions and 41 deletions.
105 changes: 64 additions & 41 deletions bin/openquake
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,23 @@ import getpass
import os
import sys

from os.path import abspath
from os.path import dirname
from os.path import expanduser
from os.path import join

from django.core.exceptions import ObjectDoesNotExist

# just in the case that are you using oq-engine from sources
# with the rest of oq libraries installed into the system (or a
# virtual environment) you must set this environment variable
if os.environ.get("OQ_ENGINE_USE_SRCDIR") != None:
if os.environ.get("OQ_ENGINE_USE_SRCDIR") is not None:
sys.modules['openquake'].__dict__["__path__"].insert(
0, os.path.join(os.path.dirname(
os.path.dirname(__file__)), "openquake"))
0, join(dirname(dirname(__file__)), "openquake")
)

from openquake.engine.utils import config

config.abort_if_no_config_available()

try:
Expand All @@ -81,12 +86,14 @@ except ImportError:
pass

import openquake.engine

from openquake.engine import __version__
from openquake.engine import engine2, utils
from openquake.engine import engine2
from openquake.engine.db import models
from openquake.engine.export import hazard as hazard_export
from openquake.engine.export import risk as risk_export
from openquake.engine.job.validation import validate
from openquake.engine.utils import version


HAZARD_OUTPUT_ARG = "--hazard-output-id"
Expand Down Expand Up @@ -248,47 +255,58 @@ def run_hazard(cfg_file, log_level, log_file, force_inputs, exports):


def list_hazard_calculations():
print "Hazard calculations:"
"""
Print a summary of past hazard calculations.
"""
hcs = models.HazardCalculation.objects.filter(
owner__user_name=getpass.getuser())
if len(hcs) == 0:
print 'None'
else:
for hc in hcs:
print 'Calculation ID: %s, Description: %s' % (
hc.id, hc.description)
jobs = models.OqJob.objects.filter(hazard_calculation=hc.id)
for j in jobs:
if j.is_running:
status = 'pending'
else:
if j.status == 'complete':
status = 'successful'
else:
status = 'failed'
print '\tJob ID: %s, Status: %s' % (j.id, status)
_print_calcs_summary(hcs)


def list_risk_calculations():
print "Risk calculations:"
"""
Print a summary of past risk calculations.
"""
rcs = models.RiskCalculation.objects.filter(
owner__user_name=getpass.getuser())
if len(rcs) == 0:
_print_calcs_summary(rcs)


def _print_calcs_summary(calcs):
"""
:param calcs:
List of :class:`openquake.engine.db.models.HazardCalculation` or
:class:`openquake.engine.db.models.RiskCalculation` objects.
"""
if len(calcs) == 0:
print 'None'
else:
for rc in rcs:
print 'Calculation ID: %s, Description: %s' % (
rc.id, rc.description)
jobs = models.OqJob.objects.filter(risk_calculation=rc.id)
for j in jobs:
if j.is_running:
print ('calc_id | num_jobs | latest_job_status | last_update | '
'description')
for calc in calcs:
jobs = calc.oqjob_set.all()

try:
latest_job = jobs.latest('id')
except ObjectDoesNotExist:
# no jobs associated with the calculation
status = ''
last_update = ''
else:
if latest_job.is_running:
status = 'pending'
else:
if j.status == 'complete':
if latest_job.status == 'complete':
status = 'successful'
else:
status = 'failed'
print '\tJob ID: %s, Status: %s' % (j.id, status)
last_update = latest_job.last_update.strftime(
'%Y-%m-%d %H:%M:%S %Z'
)

print '%s | %s | %s | %s | %s' % (
calc.id, len(jobs), status, last_update, calc.description
)


def list_hazard_outputs(hc_id):
Expand All @@ -300,10 +318,7 @@ def list_hazard_outputs(hc_id):
ID of a hazard calculation.
"""
outputs = models.Output.objects.filter(oq_job__hazard_calculation=hc_id)
if len(outputs) > 0:
print 'ID\tOuput Type\tName'
for o in outputs:
print '%s\t%s\t%s' % (o.id, o.output_type, o.display_name)
_print_outputs_summary(outputs)


def list_risk_outputs(rc_id):
Expand All @@ -315,10 +330,17 @@ def list_risk_outputs(rc_id):
ID of a risk calculation.
"""
outputs = models.Output.objects.filter(oq_job__risk_calculation=rc_id)
_print_outputs_summary(outputs)


def _print_outputs_summary(outputs):
"""
List of :class:`openquake.engine.db.models.Output` objects.
"""
if len(outputs) > 0:
print 'ID\tOutput Type\tName'
print 'id | output_type | name'
for o in outputs:
print '%s\t%s\t%s' % (o.id, o.output_type, o.display_name)
print '%s | %s | %s' % (o.id, o.output_type, o.display_name)


def export_hazard(haz_output_id, target_dir):
Expand Down Expand Up @@ -427,7 +449,7 @@ def _touch_log_file(log_file):
"""If a log file destination is specified, attempt to open the file in
'append' mode ('a'). If the specified file is not writable, an
:exc:`IOError` will be raised."""
open(os.path.abspath(log_file), 'a').close()
open(abspath(log_file), 'a').close()


def complain_and_exit(msg, exit_code=0):
Expand All @@ -441,7 +463,7 @@ def main():
args = arg_parser.parse_args()

if args.version:
complain_and_exit(utils.version.info(__version__))
complain_and_exit(version.info(__version__))

if args.no_distribute:
os.environ[openquake.engine.NO_DISTRIBUTE_VAR] = '1'
Expand Down Expand Up @@ -472,7 +494,8 @@ def main():
elif args.run_risk is not None:
if args.hazard_output_id is None and args.hazard_calculation_id is None:
complain_and_exit(MISSING_HAZARD_MSG)
log_file = expanduser(args.log_file) if args.log_file is not None else None
log_file = expanduser(args.log_file) \
if args.log_file is not None else None
run_risk(expanduser(args.run_risk), args.log_level, log_file,
args.force_inputs, args.exports,
hazard_output_id=args.hazard_output_id,
Expand Down

0 comments on commit faa12c0

Please sign in to comment.