def _fetchSubs(container, parent=None, level=-1): # If no subelements in container, return if len(container) == 0: return (0, None) # Adding titles GValues = [] selected_items = 0 for item in container: indicator_selected = 0 if item.tag.lower() != 'item': continue indicator_name = item.attrib['indicator'] #if items and not indicator_name in items: continue if not items or indicator_name in items: data_dict['titles'].append(indent_str * level + ' ' + _T(item.attrib['title'])) indicator_selected = 1 # temp list to do arithmetic operations values = [] for i in xrange(len(period)): date = period[i] # Creating a timestamp range for the specified date ts_min = int(time.mktime(datetime.strptime(date, "%Y-%m-%d").timetuple())) ts_max = ts_min + 86400 # max = min + 1day (sec) # value = ReportDatabase().get_indicator_value_at_time(indicator_name, ts_min, ts_max, entities) values.append(value) # if item is not selected, don't add value to the Data Dict if not items or indicator_name in items: data_dict['values'][i].append(value) # If item is not selected don't add values to arithmetic table list # if items and not indicator_name in items: (subCount, childGValues) = _fetchSubs(item, container, level + 1) if subCount != 0: GValues.append(values) indicator_selected = 1 else: # If parent is checked, we don't look at childs (subCount, childGValues) = (0, None) GValues.append(values) # Calcating "other" line if indicator type is numeric if ReportDatabase().get_indicator_datatype(indicator_name) == 0 and subCount != 0: #and (not items or indicator_name in items): data_dict['titles'].append(indent_str * (level + 1) + ' %s (%s)' % (_T(item.attrib['title']), locale['STR_OTHER'])) for i in xrange(len(period)): child_sum = _sum_None([l[i] for l in childGValues]) if child_sum is not None and values[i] is not None: other_value = values[i] - child_sum else: other_value = None #print other_value data_dict['values'][i].append(other_value) if indicator_selected != 0: selected_items += 1 return (selected_items, GValues)
def activate(): config = ReportConfig("report") if config.disabled: logger.warning("Plugin report: disabled by configuration.") return False if not ReportDatabase().activate(config): logger.error("Report database not activated") return False # Add historization task in the task manager TaskManager().addTask("report.historize_all", (ReportDatabase().historize_all, ), cron_expression=config.historization) # Import indicators from XML import_indicators() return True
def get_indicator_value_at_time(self, indicator_name, ts_min, ts_max, entities=[]): return ReportDatabase().get_indicator_value_at_time( indicator_name, ts_min, ts_max, entities)
def import_indicators(): config = ReportConfig("report") xmltemp = ET.parse(os.path.join(reportconfdir, config.indicators)).getroot() # Indicator names list available_indicators = [] for module in xmltemp.getiterator('module'): module_name = module.attrib['name'] for indicator in module.getiterator('indicator'): indicator_attr = indicator.attrib indicator_attr['module'] = module_name available_indicators.append(indicator_attr['name']) # Add indicator if not exists ReportDatabase().add_indicator(indicator_attr) # Disable all deleted indicators ReportDatabase().disable_indicators_by_name(excludes=available_indicators) return True
def _fetchSubs(container, parent=None, level=-1, parent_value=0): values = [] for item in container: if item.tag.lower() != 'item': continue indicator_name = item.attrib['indicator'] indicator_label = _T(item.attrib['title']) indicator_value = ReportDatabase( ).get_indicator_current_value(indicator_name, entities) # indicator_value is a list of dict {'entity_id' : .., 'value' .. } # ============================================================== # ==> Generate one entry for each entity [disabled] #for entry in indicator_value: # if entry['entity_id'] in entity_names: # entity_name = entity_names[entry['entity_id']] # else: # entity_name = entry['entity_id'] # if not items or indicator_name in items: # data_dict['values'].append([ indent_str * level + indicator_label + (' (%s)' % entity_name ), entry['value']]) # ================================================================= # Calculating sum value for entities value = _sum_None([x['value'] for x in indicator_value]) values.append(value) if not items or indicator_name in items: data_dict['values'].append( [indent_str * level + indicator_label, value]) # TODO: Calculate other cols # Fetch this item subitems _fetchSubs(item, container, level + 1, value) # Generating others value if parent and values: others_value = parent_value - _sum_None(values) data_dict['values'].append([ indent_str * level + ' %s (%s)' % (parent.attrib['title'], locale['STR_OTHER']), others_value ])
def get_indicator_current_value(self, indicator_name, entities=[]): return ReportDatabase().get_indicator_current_value( indicator_name, entities)
def historize_overwrite_last(self, timestamp=None): # If timestamp is not specified, taking actual time if not timestamp: timestamp = int(time.time()) ReportDatabase().historize_overwrite_last(timestamp)
def historize_all(self, timestamp=None): # If timestamp is not specified, taking actual time if timestamp is None: timestamp = int(time.time()) ReportDatabase().historize_all(timestamp)
def calldb(self, func, *args, **kw): return getattr(ReportDatabase(), func).__call__(*args, **kw)
def get_indicator_current_value(self, indicator_name, entities=[]): #Mutable list entities used as default argument to a method or function return ReportDatabase().get_indicator_current_value(indicator_name, entities)
def get_indicator_value_at_time(self, indicator_name, ts_min, ts_max, entities=[]): # Mutable list entities used as default argument to a method or function #entities = entities or [] return ReportDatabase().get_indicator_value_at_time(indicator_name, ts_min, ts_max, entities)