class Components(View): """Create and List Components Private Endpoint Controller""" def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__component = ComponentModule() self.__component_group = ComponentGroupModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) self.__user_id = None self.__correlation_id = "" @allow_if_authenticated def post(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__request.set_request(request) request_data = self.__request.get_request_data("post", { "name": "", "description": "", "uptime": "", "group": "" }) self.__form.add_inputs({ 'name': { 'value': request_data["name"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [1, 60], 'error': _('Error! Component name must be 1 to 60 characters long.' ) } } }, 'description': { 'value': request_data["description"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [0, 150], 'error': _('Error! Component name description must be less than 150 characters long.' ) }, 'optional': {} } }, 'uptime': { 'value': request_data["uptime"], 'validate': { 'any_of': { 'param': [["on", "off"]], 'error': _('Error! Uptime is invalid.') } } }, 'group': { 'value': int(request_data["group"]), 'sanitize': { 'strip': {} }, 'validate': { 'greater_than': { 'error': _('Error! Component group is invalid.'), 'param': [0] }, 'optional': {} } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_errors_failure(self.__form.get_errors(), {}, self.__correlation_id)) # Check if component name not used if self.__component.get_one_by_name(self.__form.get_sinput("name")): return JsonResponse( self.__response.send_private_failure( [{ "type": "error", "message": _("Error! Component name is used before.") }], {}, self.__correlation_id)) # Check if group id is valid if self.__form.get_sinput( "group") and not self.__component_group.get_one_by_id( self.__form.get_sinput("group")): return JsonResponse( self.__response.send_private_failure( [{ "type": "error", "message": _("Error! Component group is invalid.") }], {}, self.__correlation_id)) result = self.__component.insert_one({ "name": self.__form.get_sinput("name"), "description": self.__form.get_sinput("description"), "uptime": self.__form.get_sinput("uptime"), "group_id": None if self.__form.get_sinput("group") == "" else self.__form.get_sinput("group") }) if result: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Component created successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating component.") }], {}, self.__correlation_id)) @allow_if_authenticated def get(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__request.set_request(request) request_data = self.__request.get_request_data("get", { "offset": 0, "limit": 20 }) try: offset = int(request_data["offset"]) limit = int(request_data["limit"]) except Exception: offset = 0 limit = 20 return JsonResponse( self.__response.send_private_success( [], { 'components': self.__format_components( self.__component.get_all(offset, limit)), 'metadata': { 'offset': offset, 'limit': limit, 'count': self.__component.count_all() } }, self.__correlation_id)) def __format_components(self, components): components_list = [] for component in components: components_list.append({ "id": component.id, "name": component.name, "description": component.description, "uptime": component.uptime, "group": "---" if component.group is None else component.group.name, "created_at": component.created_at.strftime("%b %d %Y %H:%M:%S"), "edit_url": reverse("app.web.admin.component.edit", kwargs={'component_id': component.id}), "delete_url": reverse("app.api.private.v1.admin.component.endpoint", kwargs={'component_id': component.id}) }) return components_list
class Builder(View): """Builder Page Controller""" template_name = 'templates/admin/builder.html' @login_if_not_authenticated_or_no_permission("manage_settings") def get(self, request): self.__context = Context() self.__metric = MetricModule() self.__component = ComponentModule() self.__component_group = ComponentGroupModule() self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__context.autoload_options() self.__context.autoload_user( request.user.id if request.user.is_authenticated else None) self.__context.load_options({ "builder_headline": "", "builder_favicon_url": "", "builder_logo_url": "", "builder_about": "", "builder_components": json.dumps([]), "builder_metrics": json.dumps([]) }) self.__context.push({ "page_title": _("Status Page Builder · %s") % self.__context.get( "app_name", os.getenv("APP_NAME", "Silverback")), "groups": self.__format_groups(self.__component.get_all_groups()), "components": self.__format_components(self.__component.get_all()), "metrics": self.__format_metrics(self.__metric.get_all()) }) self.__context.push({ "builder_components": json.loads(str(self.__context.get("builder_components"))), "builder_metrics": json.loads(str(self.__context.get("builder_metrics"))) }) return render(request, self.template_name, self.__context.get()) def __format_components(self, components): components_list = [] for component in components: components_list.append({ "id": "c-%d" % component.id, "name": component.name }) return components_list def __format_groups(self, groups): groups_list = [] for group in groups: groups_list.append({"id": "g-%d" % group.id, "name": group.name}) return groups_list def __format_metrics(self, metrics): metrics_list = [] for metric in metrics: metrics_list.append({ "id": "m-%d" % metric.id, "title": metric.title }) return metrics_list
class IncidentUpdateView(View): """Incident Update View Page Controller""" template_name = 'templates/admin/incident/update/view.html' @login_if_not_authenticated def get(self, request, incident_id, update_id): self.__context = Context() self.__incident = IncidentModule() self.__incident_update = IncidentUpdateModule() self.__incident_update_component = IncidentUpdateComponentModule() self.__component = ComponentModule() self.__component_group = ComponentGroupModule() self.__incident_update_notification = IncidentUpdateNotificationModule( ) self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" incident = self.__incident.get_one_by_id(incident_id) if not incident: raise Http404("Incident not found.") update = self.__incident_update.get_one_by_id(update_id) if not update: raise Http404("Incident update not found.") update["datetime"] = update["datetime"].strftime("%b %d %Y %H:%M:%S") update["message"] = markdown2.markdown(update["message"]) update[ "notified_subscribers"] = self.__incident_update_notification.count_by_update_status( update["id"], IncidentUpdateNotificationModule.SUCCESS) update[ "failed_subscribers"] = self.__incident_update_notification.count_by_update_status( update["id"], IncidentUpdateNotificationModule.FAILED) components = self.__format_components(self.__component.get_all()) affected_components = self.__format_affected_components( self.__incident_update_component.get_all(update_id)) self.__context.autoload_options() self.__context.autoload_user( request.user.id if request.user.is_authenticated else None) self.__context.push({ "page_title": _("View Incident Update · %s") % self.__context.get( "app_name", os.getenv("APP_NAME", "Silverback")), "update": update, "incident": incident, "components": components, "affected_components": affected_components }) return render(request, self.template_name, self.__context.get()) def __format_components(self, components): components_list = [] for component in components: components_list.append({ "id": component.id, "name": component.name }) return components_list def __format_affected_components(self, affected_components): affected_components_list = [] for affected_component in affected_components: affected_components_list.append({ "id": affected_component.id, "component_id": affected_component.component.id, "component_name": affected_component.component.name, "type": affected_component.type }) return affected_components_list