Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.stsci.edu/spst/UnixTransition/doc/calstats.py
Дата изменения: Fri Feb 28 14:46:08 2014
Дата индексирования: Sat Mar 1 14:17:13 2014
Кодировка:
#!/usr/bin/env python
# Module calstats.py
#
# Report for calibration statistics.
#
# Translated from Ron Downes' SQR by Don Chance
#
# Modification history:
# 08-21-2013 drc Initial translation.
# 09-09-2013 drc Make get_latest_endtime function to use in treasury.py as well.
#
import sys
import stpydb
import time_util
import spss_sys_util
import proposal_util

ASSIST_DB = spss_sys_util.get_environ_variable("ASSIST_DB")[0]
METRICS_DB = "hst_metrics"

def get_max_pw_begin(visit):
"""Return the latest plan window begin time regardless of LRP.
"""
db = stpydb.stpydb(dbmsName=ASSIST_DB)
db.query('select max(window_begin)')
db.query('from plan_windows')
db.query('where prop_id = %i' % visit['prop_id'])
db.query('and visit_id = "%s"' % visit['visit_id'])
r = []
while db.execute(r):
pass
return time_util.spss_time(r[0])


def get_cal_proposals(cycle):
"""Return calibration proposals for a particular cycle.
"""
db = stpydb.stpydb(dbmsName=ASSIST_DB)
db.query('select *')
db.query('from prop_track p, coverpage c')
db.query('where p.prop_id = c.prop_id')
db.query('and p.cycle = %i' % cycle)
db.query('and p.prop_id > 1000')
db.query('and c.type like "CAL%"')
db.query("and p.init_submit > '01-JAN-1980'")
db.query('and p.status not in ("withdrawn")')
db.query('order by c.type, c.prop_id')
r = {}
props = []
while db.execute(r):
props.append(proposal_util.proposal(r))
return props

def add_calibration_progress_data(prop_list):
"""Add data from the calibration_progress table from the hst_metrics database
to each proposal object in the input list.
"""
db = stpydb.stpydb(dbmsName=METRICS_DB)
for prop in prop_list:
db.query('select * from calibration_progress where prop_id = %i' % prop['prop_id'])
r = {}
while db.execute(r):
pass
if r['prop_id'] is not None:
prop.update(r)

def get_latest_end_time(prop):
"""Return total number of visits, the number of completed visits and the spss_time
the latest visit completed or will complete.

If the input proposal is complete, return the spss_time it completed.
If the prosposal is not complete, return the maximum plan window begin time of
the unfinished visits.
"""
visits = prop.get_visits()
total_visits = 0
comp_visits = 0
max_pw_end = pw_end = time_util.spss_time(0)
for v in visits:
if v.get('status') not in ('withdrawn', 'failed'):
if v.get('active_flag') != 'Y':
continue
pw_end = get_max_pw_begin(v)
total_visits += 1
if v.get('status') == 'completed':
comp_visits += 1
pw_end = v.get('status_time')
if pw_end > max_pw_end:
max_pw_end = pw_end
return total_visits, comp_visits, max_pw_end

def run(*args):
"""Output a summary report for calibration statistics during the input
cycle or cycles.

Defaults to the current cycle.
"""
cycles = []
if not args:
cycles.append(spss_sys_util.get_current_cycle())
for arg in args:
try:
cycles.append(int(arg))
except:
print "Invalid cycle number ", arg
return not spss_sys_util.SUCCESS
outfile = "calstats.rpt"
now = time_util.spss_time()
outfileId = open(outfile, 'w')
for cycle in cycles:
outfileId.write('Calibration Program for Cycle %i\n\n\n' % cycle)
outfileId.write('Type Prop_ID Visits Comp_Visits Percent Date_Last_Visit Report_Date Lead Analysis_Progress Products_Delivered ISRs\n')
props = get_cal_proposals(cycle)
add_calibration_progress_data(props)
for prop in props:
total_visits, comp_visits, max_pw_end = get_latest_end_time(prop)
if total_visits > 0:
try:
outfileId.write('%-8s %5i %3i %3i %5.1f %s %s %-11s %-17s %-32s %-30s\n' % (prop['type'],
prop['prop_id'],
total_visits,
comp_visits,
100.0 * float(comp_visits)/float(total_visits),
max_pw_end.strftime('%d-%b-%Y'),
now.strftime('%d-%b-%Y'),
prop.get('analysis_lead', ' ')[:11],
prop.get('analysis_progress', ' '),
prop.get('products_delivered', ' '),
prop.get('isr_name', ' ')))
except:
print "Error processing ", prop['prop_id']
raise



print "Output sent to file", outfile


if __name__ == '__main__':
if len(sys.argv) > 1:
run(*tuple(sys.argv[1:]))
else:
run()