def default(self, module, run_id, **kwargs): response = {"status": "unkown", "data": []} try: if module not in self.modules: raise Exception("Module not found") module = self.modules[module] if module.isUnauthorized(): raise cp.HTTPError(status=403, message="You are not allowed to access this resource.") run = hf_runs.select(hf_runs.c.id == run_id).execute().fetchone() if run is None: raise cp.HTTPError(status=404, message="The specified run ID was not found!") specific_module = module.getModule(run) if not hasattr(specific_module, "ajax"): raise cp.HTTPError(status=404, message="Module does not export data via Ajax") if specific_module.error_string: raise Exception(specific_module.error_string) if specific_module.dataset is None: raise cp.HTTPError(status=404, message="No data at this time") self.logger.debug(specific_module.error_string, specific_module.dataset) response["data"] = specific_module.ajax(**kwargs) response["status"] = "success" # ajax data never goes bad, since it is supposed to be static cp.lib.caching.expires(secs=9999999, force=True) except cp.HTTPError, e: cp.lib.caching.expires(secs=0, force=False) response = { "status": "error", "code": e.code, "reason": "%i: %s" % (e.code, e.reason), "data": [] }
def prepareDisplay(self, category=None, **kwargs): """ generate the data and template context required to display a page containing the navbar. :param string: Name of the ca :returns: tuple (template_context, category_dict, run) """ ''' Select a hf run based on the passed 'date' and 'time' parameters. If not specified, use most recent run. If they are specified, make sure they do not mark a point in the future. Because (usually) microseconds are stored in the database, too, we have to pad with extra 59 seconds (note: 59 because it will be the same minute but there will only be a single "dead" second, we can live with that). ''' time_error_message = '' time_obj = datetime.datetime.fromtimestamp(int(time.time())) try: timestamp = kwargs['date'] if 'date' in kwargs is not None else time_obj.strftime('%Y-%m-%d') timestamp += '_' + (kwargs['time'] if 'time' in kwargs else time_obj.strftime('%H:%M')) # notice the extra seconds to avoid microsecond and minute issues time_obj = datetime.datetime.fromtimestamp(time.mktime(time.strptime(timestamp, "%Y-%m-%d_%H:%M"))+59) except Exception: time_error_message = "The passed time was invalid" if time_obj > datetime.datetime.fromtimestamp(int(time.time())+59): time_error_message = "HappyFace is not an oracle" time_obj = datetime.datetime.fromtimestamp(int(time.time())+59) try: test = hf_runs.select().execute().fetchone() self.logger.error("Test "+str(test)) except Exception, e: self.logger.error(traceback.format_exc())
def prepareDisplay(self, category=None, **kwargs): """ generate the data and template context required to display a page containing the navbar. :param string: Name of the ca :returns: tuple (template_context, category_dict, run) """ ''' Select a hf run based on the passed 'date' and 'time' parameters. If not specified, use most recent run. If they are specified, make sure they do not mark a point in the future. Because (usually) microseconds are stored in the database, too, we have to pad with extra 59 seconds (note: 59 because it will be the same minute but there will only be a single "dead" second, we can live with that). ''' time_error_message = '' time_obj = datetime.datetime.fromtimestamp(int(time.time())) try: timestamp = kwargs['date'] if 'date' in kwargs is not None else time_obj.strftime('%Y-%m-%d') timestamp += '_' + (kwargs['time'] if 'time' in kwargs else time_obj.strftime('%H:%M')) # notice the extra seconds to avoid microsecond and minute issues time_obj = datetime.datetime.fromtimestamp(time.mktime(time.strptime(timestamp, "%Y-%m-%d_%H:%M")) + 59) except Exception: time_error_message = "The passed time was invalid" requested_time_obj = time_obj # store time as specified by the user if time_obj > datetime.datetime.fromtimestamp(int(time.time()) + 59): time_error_message = "HappyFace is not an oracle" time_obj = datetime.datetime.fromtimestamp(int(time.time()) + 59) run = hf_runs.select(hf_runs.c.time <= time_obj). \ where(or_(hf_runs.c.completed == True, hf_runs.c.completed == None)). \ order_by(hf_runs.c.time.desc()). \ execute().fetchone() if run is None: time_error_message = "No data so far in past" run = hf_runs.select(hf_runs.c.time >= time_obj). \ where(or_(hf_runs.c.completed == True, hf_runs.c.completed == None)). \ order_by(hf_runs.c.time.asc()). \ execute().fetchone() time_obj = run["time"] run = {"id": run["id"], "time": run["time"], "requested_time": requested_time_obj} # if the run is older than a certain time threshold, # then mark it as stale stale_threshold = datetime.timedelta(0, 0, 0, 0, int(hf.config.get('happyface', 'stale_data_threshold_minutes'))) data_stale = (run['time'] + stale_threshold) < datetime.datetime.now() run['stale'] = data_stale category_list = [cat.getCategory(run) for cat in self.category_list] category_dict = dict((cat.name, cat) for cat in category_list) selected_category = None for c in category_list: if c.name == category: selected_category = c break lock_icon = 'lock_icon_on.png' if cp.request.cert_authorized else 'lock_icon_off.png' lock_icon = os.path.join(hf.config.get('paths', 'template_icons_url'), lock_icon) template_context = { "static_url": hf.config.get('paths', 'static_url'), "happyface_url": hf.config.get('paths', 'happyface_url'), "category_list": category_list, "module_list": [], "hf": hf, "time_specified": ('date' in kwargs or 'time' in kwargs), "date_string": time_obj.strftime('%Y-%m-%d'), "time_string": time_obj.strftime('%H:%M'), "histo_step": kwargs['s'] if 's' in kwargs else "00:15", "run": run, 'selected_module': None, 'selected_category': selected_category, 'time_error_message': time_error_message, 'data_stale': data_stale, 'svn_rev': hf.__version__, 'lock_icon': lock_icon, 'include_time_in_url': ('date' in kwargs or 'time' in kwargs), 'automatic_reload': not ('date' in kwargs or 'time' in kwargs), 'reload_interval': int(hf.config.get('happyface', 'reload_interval')), 'hf_css': cp.request.hf_css } for cat in category_list: template_context["module_list"].extend(cat.module_list) return template_context, category_dict, run
def default(self, plt_type=None, img=None, **kwargs): if hf.config.get('plotgenerator', 'enabled').lower() != 'true': return "Plot Generator disabled by HappyFace configuration" if img == "img": if plt_type == "time": return hf.plotgenerator.timeseriesPlot(self.category_list, **kwargs) elif plt_type == "custom": return hf.plotgenerator.customPlot(**kwargs) else: try: # just get the lastest run, we don't really need it run = hf_runs.select(). \ where(or_(hf_runs.c.completed == True, hf_runs.c.completed == None)). \ order_by(hf_runs.c.time.desc()). \ execute().fetchone() category_list = [cat.getCategory(run) for cat in self.category_list] start_date = kwargs['start_date'] if 'start_date' in kwargs else run["time"].strftime('%Y-%m-%d') start_time = kwargs['start_time'] if 'start_time' in kwargs else run["time"].strftime('%H:%M') start = datetime.datetime.fromtimestamp(time.mktime(time.strptime(start_date + '_' + start_time, "%Y-%m-%d_%H:%M"))) past = start - datetime.timedelta(days=2) end_date = kwargs['end_date'] if 'end_date' in kwargs else past.strftime('%Y-%m-%d') end_time = kwargs['end_time'] if 'end_time' in kwargs else past.strftime('%H:%M') end = datetime.datetime.fromtimestamp(time.mktime(time.strptime(end_date + '_' + end_time, "%Y-%m-%d_%H:%M"))) plot_cfg = hf.plotgenerator.getTimeseriesPlotConfig(**kwargs) curve_dict = {} for name, curve in plot_cfg["curve_dict"].iteritems(): #curve <=> (title, table, module_instance, col_expr) curve_dict[name] = (curve[2], \ curve[4], \ curve[3], \ curve[0]) self.logger.debug(curve_dict) # Parse the constraints constraint_dict = {} for name, cond_list in kwargs.iteritems(): if name.startswith('filter'): include = True name = name[6:] if len(name) > 6 else '' elif name.startswith('exclude'): include = False name = name[7:] if len(name) > 7 else '' else: continue if not isinstance(cond_list, list): cond_list = [cond_list, ] for cond in cond_list: var = cond.split(',')[0] value = ','.join(cond.split(',')[1:]) if name in constraint_dict: constraint_dict[name].append([include, var, value]) else: constraint_dict[name] = [[include, var, value], ] trendplot = (kwargs['renormalize'].lower() if 'renormalize' in kwargs else 'false') in ['1', 'true'] legend_select = dict((i, "") for i in xrange(11)) if 'legend' in kwargs: legend_select[int(kwargs['legend'])] = "selected='selected'" template_context = { "static_url": hf.config.get('paths', 'static_url'), "url_short_api_key": hf.config.get('plotgenerator', 'url_short_api_key'), "happyface_url": hf.config.get('paths', 'happyface_url'), "category_list": category_list, "module_list": [], "start": start, "end": end, "curve_dict": curve_dict, "constraint_dict": constraint_dict, "trendplot": trendplot, "title": kwargs["title"] if 'title' in kwargs else '', "hf": hf, "legend_select": legend_select, } for cat in category_list: template_context["module_list"].extend(cat.module_list) if plt_type == "time": test = self.timeseries_template.render_unicode(**template_context) return test except Exception, e: self.logger.error("Plot interface hander failed: " + str(e)) self.logger.error(traceback.format_exc()) raise