def daily(self, monitor, target_tz): # select earliest and latest ts for main monitor facet query = select([func.min(t_monitor_log.c.ts), func.max(t_monitor_log.c.ts)], t_monitor_log.c.monitor_id == monitor.monitor_id) start, end = self.session.execute(query).fetchall()[0] if start and end: start = ts_to_datetime(start).astimezone(target_tz) end = ts_to_datetime(end).astimezone(target_tz) return [day for day in self.date_range(start.date(), end.date())] return []
def generate(self, monitor, target_tz, date_for): start = utcdatetimefromiso(date_for, target_tz) end = start + timedelta(days=1) facet_names = [item['facet'] for item in self.report_plan if item.has_key('facet')] facets = self.session.query(Facet).filter(Facet.kind==monitor.kind).filter(Facet.name.in_(facet_names)).all() facet_ids = [] facet_by_id = dict() for facet in facets: facet_ids.append(facet.facet_id) facet_by_id[facet.facet_id] = facet pre_result = dict() # get facets query = select([t_monitor_log.c.facet_id, t_monitor_log.c.ts, t_monitor_log.c.node, t_monitor_log.c.value], and_(t_monitor_log.c.monitor_id==monitor.monitor_id, t_monitor_log.c.facet_id.in_(facet_ids), t_monitor_log.c.ts >= datetime_to_ts(start), t_monitor_log.c.ts < datetime_to_ts(end))) for facet_id, ts, node, value in self.session.execute(query): facet = facet_by_id[facet_id] pre_result.setdefault(ts, dict())[facet.name] = value if node is not None: pre_result.setdefault(ts, dict())['node'] = node # get errors query = select([t_error_log.c.ts, t_error_log.c.node, t_error_log.c.error], and_(t_error_log.c.monitor_id==monitor.monitor_id, t_error_log.c.ts >= datetime_to_ts(start), t_error_log.c.ts < datetime_to_ts(end))) for ts, node, err in self.session.execute(query): pre_result.setdefault(ts, dict())['error'] = err result = [] sorted_ts = pre_result.keys() sorted_ts.sort() for ts in sorted_ts: data = pre_result[ts] data['ts'] = long(datetime_to_ts(ts_to_datetime(ts).astimezone(target_tz))) result.append(data) return [self.report_plan, result]
def hist_downtime(session, monitor, facet, ts_start, ts_end): q = select([t_monitor_log.c.ts, t_monitor_log.c.value], and_(t_monitor_log.c.monitor_id == monitor.monitor_id, t_monitor_log.c.facet_id == facet.facet_id, t_monitor_log.c.ts >= ts_start, t_monitor_log.c.ts < ts_end)) q = q.order_by(t_monitor_log.c.ts.asc()) samples = [] for ts, value in session.execute(q).fetchall(): samples.append((ts_to_datetime(ts), value)) downtime = timedelta(0) for i in range(len(samples)-1): # disregard the last sample (TODO: really?) ts, value = samples[i] next_ts, next_value = samples[i+1] if value is None: downtime = downtime + next_ts - ts result = downtime.seconds return result
def monthly(self, monitor, target_tz): monthly_results = [] query = select([func.min(t_monitor_log_hist.c.ts)], t_monitor_log_hist.c.monitor_id == monitor.monitor_id) start = self.session.execute(query).fetchall()[0][0] this_month = datetime.now(tz=utc).astimezone(target_tz).date().replace(day=1) if start: start = ts_to_datetime(start).date() # months starting from earliest date in monitor_log_daily # but not current month current = start.replace(day=1) while current < this_month: monthly_results.append(current) if current.month == 12: current = current.replace(year=current.year+1, month=1) else: current = current.replace(month=current.month+1) # always add the current month monthly_results.append(this_month) return monthly_results
def daily_stats(self, r, name): target_tz = pytz.timezone(r.user.timezone) now = datetime.utcnow() monitor = r.user.monitors[name] facet_name_by_kind = { Monitor.KIND_HTTP: 'time', Monitor.KIND_HTTPS: 'time', Monitor.KIND_PING: 'rtt_avg', Monitor.KIND_DNS: 'time', } facet_name = facet_name_by_kind[monitor.kind] facet = r.session.query(Facet).filter_by(kind=monitor.kind, name=facet_name).one() query = select([t_monitor_log.c.ts, t_monitor_log.c.node, t_monitor_log.c.value], and_(t_monitor_log.c.facet_id == facet.facet_id, t_monitor_log.c.monitor_id == monitor.monitor_id, t_monitor_log.c.ts > datetime_to_ts(now - timedelta(days=1)))) query = query.order_by(t_monitor_log.c.ts.desc()) if monitor.kind in [Monitor.KIND_HTTP, Monitor.KIND_HTTPS]: title = "Response time and availability for last 24 hours" elif monitor.kind == Monitor.KIND_PING: title = "Round trip time and availability for last 24 hours" else: title = "Unknown" all_series = [dict(title=title), [dict(legend='Missouri, US')], [dict(legend='Georgia, US')], [dict(legend='London, UK')], ] result = r.session.execute(query) for ts, node, value in result: all_series[node].append(datetime_to_ts(ts_to_datetime(ts).astimezone(target_tz))) all_series[node].append(value) return all_series