def recent(self): updated_col = self.table._columns_by_name[self.update_col] columns = list() columns.append(updated_col.identifier) self.columns = columns options = SqlQueryOptions() options.sort_column = updated_col options.sort_ascending = False samples = self.get_data({}, options) return len(samples) and samples[0][0] or None
def avg_samples(self, stat, secs, interval, secs2): stat_col = self.table._columns_by_name[stat.name] updated_col = self.table._columns_by_name[self.update_col] org_filters = list(self.query.filters) max_col = "max(%s) as interval_end" % updated_col.identifier value_col = "cast(avg(%s) as integer) as value" % stat_col.identifier dev_col = "stddev(%s) as dev" % stat_col.identifier columns = list() columns.append(max_col) columns.append(value_col) columns.append(dev_col) self.columns = columns when = "now() - interval '%i seconds'" % int(secs + secs2) filter = SqlComparisonFilter(updated_col, when, ">=") self.query.add_filter(filter) when2 = "" filter2 = None if secs2: when2 = "now() - interval '%i seconds'" % int(secs2) filter2 = SqlComparisonFilter(updated_col, when2, "<=") self.query.add_filter(filter2) options = SqlQueryOptions() options.sort_column = "interval_end" options.sort_ascending = False options.group_column = "floor(extract(epoch from %s) / %i)" % (updated_col.identifier, interval) samples = self.get_data({}, options) # reset the filters in case we need to re-run the query in this same request del self.query.filters[:] self.query.filters = org_filters #Too many samples will result in a horribly cluttered chart, lets max_samples (parameter) max_samples = self.session.get("maxsamp") reduced_samples = None if max_samples is not None and max_samples > 0 and len(samples) > max_samples: reduced_interval = int(len(samples) / max_samples) # show every N samples + we always want the first and last reduced_samples = [sample for i, sample in enumerate(samples) if i % reduced_interval == 0 or i == 0 or i == len(samples) -1] samples = reduced_samples return samples
def get_records(self, session): values = dict() options = SqlQueryOptions() options.sort_column = self.sum_column options.group_column = ",".join((self.sum_column, self.act_state_column.identifier)) records = self.get_data(values, options) # accumulate records by os/used|unused manually recs_by_os = dict() for record in records: os, count, act_state = record if os not in recs_by_os: recs_by_os[os] = dict() recs_by_os[os]["total"] = 0 recs_by_os[os]["used"] = 0 recs_by_os[os]["unused"] = 0 recs_by_os[os]["total"] += count if act_state == "IdleUnclaimed": recs_by_os[os]["unused"] += count else: recs_by_os[os]["used"] += count records = list() for os in sorted(recs_by_os.keys()): records.append(("%s used" % os, recs_by_os[os]["used"])) records.append(("%s unused" % os, recs_by_os[os]["unused"])) records.append(("%s total" % os, recs_by_os[os]["total"])) ########## dummy test data """ records.append(("WINNT80 Used", 10)) records.append(("WINNT80 Unused", 20)) records.append(("WINNT80 Total", 30)) records.append(("SOLARIS27 Used", 12)) records.append(("SOLARIS27 Unused", 34)) records.append(("SOLARIS27 Total", 46)) """ return records
def samples(self, stat, secs, interval, method, secs2=0, delta=False): if method == "avg": return self.avg_samples(stat, secs, interval, secs2) org_filters = list(self.query.filters) stat_col = self.table._columns_by_name[stat.name] updated_col = self.table._columns_by_name[self.update_col] columns = list() columns.append(updated_col.identifier) columns.append(stat_col.identifier) self.columns = columns if not delta: when = "now() - interval '%i seconds'" % int(secs + secs2) filter = SqlComparisonFilter(updated_col, when, ">=") self.query.add_filter(filter) when2 = "" if secs2: when2 = "now() - interval '%i seconds'" % int(secs2) filter = SqlComparisonFilter(updated_col, when2, "<=") self.query.add_filter(filter) options = SqlQueryOptions() options.sort_column = updated_col options.sort_ascending = False if delta: options.limit = 2 samples = self.get_data({}, options) # restore the filters del self.query.filters[:] self.query.filters = org_filters return samples