def uptime(request): """ Returns uptime information: current uptime, daily and monthly stats """ application = request.context.resource now = datetime.utcnow().replace(microsecond=0, second=0) location_dict = {} row = PluginConfigService.by_query( section="global", plugin_name=PLUGIN_DEFINITION["name"] ).first() if row: for x, location in enumerate(row.config["uptime_regions_map"], 1): location_dict[x] = { "country": location["country"].lower(), "city": location["name"], } if request.GET.get("start_date"): delta = now - PermissiveDate().deserialize(None, request.GET.get("start_date")) else: delta = timedelta(hours=1) stats_since = now - delta uptime = UptimeMetricService.get_uptime_by_app( application.resource_id, stats_since, until=now ) latest_stats = UptimeMetricService.get_uptime_stats(application.resource_id).all() daily_stats = UptimeMetricService.get_daily_uptime_stats( application.resource_id ).all() daily_list = [] monthly_list = [] for i, stat_list in enumerate([daily_stats, latest_stats]): for j, entry in enumerate(stat_list): item = { "id": j, "total_checks": getattr(entry, "total_checks", 0), "retries": entry.tries, "avg_response_time": entry.response_time or 0, "status_code": entry.status_code, "location": location_dict.get( getattr(entry, "location", None), {"city": "unknown", "country": "us"}, ), } if i == 1: item["interval"] = entry.interval.strftime("%Y-%m-%dT%H:%M") item["timestamp"] = entry.interval daily_list.append(item) else: item["interval"] = entry.interval.strftime("%Y-%m-%d") monthly_list.append(item) return { "current_uptime": uptime, "latest_stats": daily_list, "monthly_stats": monthly_list, }
def query(request): configs = PluginConfigService.by_query( request.params.get("resource_id"), plugin_name=request.matchdict.get("plugin_name"), section=request.params.get("section"), ) return [c for c in configs]
def get_uptime_app_list(request): """ Returns list of all applications with their uptime urls requires create permissions because this is what local security policy returns by default """ rows = PluginConfigService.by_query(plugin_name="ae_uptime_ce", section="resource") return [{"id": r.resource_id, "url": r.config["uptime_url"]} for r in rows]
def patch(request): row = PluginConfigService.by_id(plugin_id=request.matchdict.get("id")) if not row: raise HTTPNotFound() json_body = request.unsafe_json_body if json_body["section"] == "global": row.config = json_body["config"] else: schema = UptimeConfigSchema() deserialized = schema.deserialize(json_body["config"]) row.config = deserialized return row
def set_default_values(): row = PluginConfigService.by_query(plugin_name=PLUGIN_DEFINITION["name"], section="global").first() if not row: plugin = PluginConfig() plugin.config = {"uptime_regions_map": [], "json_config_version": 1} plugin.section = "global" plugin.plugin_name = PLUGIN_DEFINITION["name"] plugin.config["json_config_version"] = 1 DBSession.add(plugin) DBSession.flush()
def __init__(self, request): Resource = appenlight.models.resource.Resource self.__acl__ = [] self.resource = None plugin_id = to_integer_safe(request.matchdict.get("id")) self.plugin = PluginConfigService.by_id(plugin_id) if not self.plugin: raise HTTPNotFound() if self.plugin.resource_id: self.resource = ResourceService.by_resource_id(self.plugin.resource_id) if self.resource: self.__acl__ = self.resource.__acl__ if request.user and self.resource: permissions = ResourceService.perms_for_user(self.resource, request.user) for perm_user, perm_name in permission_to_04_acls(permissions): self.__acl__.append(rewrite_root_perm(perm_user, perm_name)) add_root_superperm(request, self)
def query(request): configs = PluginConfigService.by_query( request.params.get('resource_id'), plugin_name=request.matchdict.get('plugin_name'), section=request.params.get('section')) return [c for c in configs]