def test_state_info(self): """Test retrieval of state information.""" # We should get an error if the state does not exist. with self.assertRaises(JSAProcError): JSAProcState.get_info('!') # Try a small sample of info values self.assertEqual( JSAProcState.get_info(JSAProcState.WAITING).active, False) self.assertEqual( JSAProcState.get_info(JSAProcState.FETCHING).active, True) self.assertEqual( JSAProcState.get_info(JSAProcState.INGESTION).phase, JSAProcState.PHASE_RUN) self.assertEqual( JSAProcState.get_info(JSAProcState.UNKNOWN).phase, JSAProcState.PHASE_QUEUE) self.assertEqual( JSAProcState.get_info(JSAProcState.RUNNING).name, 'Running') self.assertEqual( JSAProcState.get_info(JSAProcState.COMPLETE).name, 'Complete') self.assertEqual( JSAProcState.get_info(JSAProcState.INGESTING).final, False) self.assertEqual( JSAProcState.get_info(JSAProcState.DELETED).final, True)
def reset_jobs(task, date_start, date_end, instrument=None, state=None, force=False, dry_run=False): """Change the state of the specified jobs back to "Unknown". If a state is specified, select only that state. Active jobs are skipped unless the force argument is set. """ db = get_database() obsquery = {} if date_start is not None and date_end is not None: obsquery['utdate'] = Range(date_start, date_end) elif date_start is None and date_end is None: pass else: raise CommandError('only one of start and end date specified') if instrument is not None: obsquery['instrument'] = instrument if state is not None: state = JSAProcState.lookup_name(state) n_active = 0 for job in db.find_jobs(location='JAC', task=task, obsquery=obsquery, state=state): state_info = JSAProcState.get_info(job.state) # Check if the job is in an "active" state. if state_info.active and not force: logger.warning('Skipping active job %i (%s)', job.id, state_info.name) n_active += 1 continue logger.info('Resetting status of job %i (was %s)', job.id, state_info.name) if not dry_run: db.change_state(job.id, JSAProcState.UNKNOWN, 'Resetting job', state_prev=job.state) if n_active: raise CommandError('Could not reset {0} active jobs'.format(n_active))
def state_phase_filter(state): phase = JSAProcState.get_info(state).phase if phase == JSAProcState.PHASE_QUEUE: return 'queue' elif phase == JSAProcState.PHASE_FETCH: return 'fetch' elif phase == JSAProcState.PHASE_RUN: return 'run' elif phase == JSAProcState.PHASE_COMPLETE: return 'complete' elif phase == JSAProcState.PHASE_ERROR: return 'error' raise HTTPError('Unknown phase {0}'.format(phase))
def test_state_info(self): """Test retrieval of state information.""" # We should get an error if the state does not exist. with self.assertRaises(JSAProcError): JSAProcState.get_info('!') # Try a small sample of info values self.assertEqual(JSAProcState.get_info(JSAProcState.WAITING).active, False) self.assertEqual(JSAProcState.get_info(JSAProcState.FETCHING).active, True) self.assertEqual(JSAProcState.get_info(JSAProcState.INGESTION).phase, JSAProcState.PHASE_RUN) self.assertEqual(JSAProcState.get_info(JSAProcState.UNKNOWN).phase, JSAProcState.PHASE_QUEUE) self.assertEqual(JSAProcState.get_info(JSAProcState.RUNNING).name, 'Running') self.assertEqual(JSAProcState.get_info(JSAProcState.COMPLETE).name, 'Complete') self.assertEqual(JSAProcState.get_info(JSAProcState.INGESTING).final, False) self.assertEqual(JSAProcState.get_info(JSAProcState.DELETED).final, True)
def reset_jobs(task, date_start, date_end, instrument=None, state=None, force=False, dry_run=False): """Change the state of the specified jobs back to "Unknown". If a state is specified, select only that state. Active jobs are skipped unless the force argument is set. """ db = get_database() obsquery = {} if date_start is not None and date_end is not None: obsquery['utdate'] = Range(date_start, date_end) elif date_start is None and date_end is None: pass else: raise CommandError('only one of start and end date specified') if instrument is not None: obsquery['instrument'] = instrument if state is not None: state = JSAProcState.lookup_name(state) n_active = 0 for job in db.find_jobs(location='JAC', task=task, obsquery=obsquery, state=state): state_info = JSAProcState.get_info(job.state) # Check if the job is in an "active" state. if state_info.active and not force: logger.warning('Skipping active job %i (%s)', job.id, state_info.name) n_active += 1 continue logger.info('Resetting status of job %i (was %s)', job.id, state_info.name) if not dry_run: db.change_state(job.id, JSAProcState.UNKNOWN, 'Resetting job', state_prev=job.state) if n_active: raise CommandError( 'Could not reset {0} active jobs'.format(n_active))
def state_active_test(state): return JSAProcState.get_info(state).active
def prepare_summary_piechart(db, task=None, obsquerydict=None, date_min=None, date_max=None): """ Create a piechart of number of jobs in each state for a given task and obsquery. *task*: name of task in database *obsquerydict*: dictionary of values that match the jcmtobsinfo.ObsQueryDict. Returns a sendfile object of mime-type image/png. """ # Dictionaries for the result job_summary_dict = OrderedDict() # Fix up the obsquery to the right format for find jobs obsquery = {} for key, value in obsquerydict.items(): if value: obsquery.update(ObsQueryDict[key][value].where) # Sort out dates if date_min is not None or date_max is not None: obsquery['utdate'] = Range(date_min, date_max) # Perform the find_jobs task for the given constraints in each # JSAProcState. for s in JSAProcState.STATE_ALL: # Don't include deleted jobs in pie chart if JSAProcState.get_name(s) != 'Deleted': job_summary_dict[s] = db.find_jobs(state=s, task=task, obsquery=obsquery, count=True) # Get numbers, names and colors for the pie chart. values = job_summary_dict.values() names = [JSAProcState.get_name(i) for i in JSAProcState.STATE_ALL[:-1]] # This should probably be done better... phase_colors = {} phase_colors[JSAProcState.PHASE_QUEUE] = 'red' phase_colors[JSAProcState.PHASE_FETCH] = 'yellow' phase_colors[JSAProcState.PHASE_RUN] = 'green' phase_colors[JSAProcState.PHASE_COMPLETE] = 'blue' phase_colors[JSAProcState.PHASE_ERROR] = 'black' phases = [phase_colors[JSAProcState.get_info(s).phase] for s in JSAProcState.STATE_ALL[:-1]] # Remove any states that don't have any jobs in them i = 0 while i < len(values): if values[i] == 0: values.pop(i) names.pop(i) phases.pop(i) else: i += 1 # Create pie chart fig = Figure(figsize=(6, 5)) ax = fig.add_subplot(111) ax.set_aspect(1) p, t, a = ax.pie(values, labels=names, colors=phases, autopct='%.1F') for i in range(len(a)): if p[i].get_facecolor() == (1.0, 1.0, 0.0, 1.0): a[i].set_color('black') else: a[i].set_color('white') p[i].set_edgecolor('none') ax.patch.set_visible(False) fig.patch.set_visible(False) # Put figure into a send_file object canvas = FigureCanvas(fig) img = StringIO.StringIO() canvas.print_png(img) img.seek(0) return send_file(img, mimetype='image/png')
def prepare_summary_piechart(db, task=None, obsquerydict=None, date_min=None, date_max=None): """ Create a piechart of number of jobs in each state for a given task and obsquery. *task*: name of task in database *obsquerydict*: dictionary of values that match the jcmtobsinfo.ObsQueryDict. Returns a sendfile object of mime-type image/png. """ # Dictionaries for the result job_summary_dict = OrderedDict() # Fix up the obsquery to the right format for find jobs obsquery = {} for key, value in obsquerydict.items(): if value: obsquery.update(ObsQueryDict[key][value].where) # Sort out dates if date_min is not None or date_max is not None: obsquery['utdate'] = Range(date_min, date_max) # Perform the find_jobs task for the given constraints in each # JSAProcState. for s in JSAProcState.STATE_ALL: # Don't include deleted jobs in pie chart if JSAProcState.get_name(s) != 'Deleted': job_summary_dict[s] = db.find_jobs(state=s, task=task, obsquery=obsquery, count=True) # Get numbers, names and colors for the pie chart. values = job_summary_dict.values() names = [JSAProcState.get_name(i) for i in JSAProcState.STATE_ALL[:-1]] # This should probably be done better... phase_colors = {} phase_colors[JSAProcState.PHASE_QUEUE] = 'red' phase_colors[JSAProcState.PHASE_FETCH] = 'yellow' phase_colors[JSAProcState.PHASE_RUN] = 'green' phase_colors[JSAProcState.PHASE_COMPLETE] = 'blue' phase_colors[JSAProcState.PHASE_ERROR] = 'black' phases = [ phase_colors[JSAProcState.get_info(s).phase] for s in JSAProcState.STATE_ALL[:-1] ] # Remove any states that don't have any jobs in them i = 0 while i < len(values): if values[i] == 0: values.pop(i) names.pop(i) phases.pop(i) else: i += 1 # Create pie chart fig = Figure(figsize=(6, 5)) ax = fig.add_subplot(111) ax.set_aspect(1) p, t, a = ax.pie(values, labels=names, colors=phases, autopct='%.1F') for i in range(len(a)): if p[i].get_facecolor() == (1.0, 1.0, 0.0, 1.0): a[i].set_color('black') else: a[i].set_color('white') p[i].set_edgecolor('none') ax.patch.set_visible(False) fig.patch.set_visible(False) # Put figure into a send_file object canvas = FigureCanvas(fig) img = StringIO() canvas.print_png(img) img.seek(0) return send_file(img, mimetype='image/png')