Example #1
0
    def get(self, request, incident_id):
        self.__context = Context()
        self.__incident = IncidentModule()
        self.__incident_update = IncidentUpdateModule()
        self.__component = ComponentModule()
        self.__component_group = ComponentGroupModule()
        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.")

        self.__context.autoload_options()
        self.__context.autoload_user(
            request.user.id if request.user.is_authenticated else None)
        self.__context.push({
            "page_title":
            _("Add Incident Update  · %s") % self.__context.get(
                "app_name", os.getenv("APP_NAME", "Silverback")),
            "incident":
            incident
        })

        return render(request, self.template_name, self.__context.get())
Example #2
0
class IncidentEdit(View, Controller):
    """Incident Edit Page Controller"""

    template_name = 'templates/admin/incident/edit.html'

    @login_if_not_authenticated
    def get(self, request, incident_id):

        self.__incident = IncidentModule()
        self.__incident_update = IncidentUpdateModule()
        self.__component = ComponentModule()
        self.__component_group = ComponentGroupModule()
        incident = self.__incident.get_one_by_id(incident_id)

        if not incident:
            raise Http404("Incident not found.")

        self.autoload_options()
        self.autoload_user(
            request.user.id if request.user.is_authenticated else None)
        self.context_push({
            "page_title":
            _("Edit Incident · %s") %
            self.context_get("app_name", os.getenv("APP_NAME", "Silverback")),
            "incident":
            incident
        })

        return render(request, self.template_name, self.context_get())
Example #3
0
 def __init__(self):
     self.__request = Request()
     self.__response = Response()
     self.__helpers = Helpers()
     self.__form = Form()
     self.__incident = IncidentModule()
     self.__logger = self.__helpers.get_logger(__name__)
     self.__form.add_validator(ExtraRules())
Example #4
0
    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())
Example #5
0
 def __init__(self):
     self.__request = Request()
     self.__response = Response()
     self.__helpers = Helpers()
     self.__form = Form()
     self.__incident = IncidentModule()
     self.__incident_update = IncidentUpdateModule()
     self.__task = Task_Module()
     self.__notification = NotificationModule()
     self.__subscriber = SubscriberModule()
     self.__incident_update_notification = IncidentUpdateNotificationModule()
     self.__logger = self.__helpers.get_logger(__name__)
     self.__form.add_validator(ExtraRules())
Example #6
0
class IncidentUpdateEdit(View):
    """Incident Update Edit Page Controller"""

    template_name = 'templates/admin/incident/update/edit.html'

    @login_if_not_authenticated
    def get(self, request, incident_id, update_id):

        self.__context = Context()
        self.__incident = IncidentModule()
        self.__incident_update = IncidentUpdateModule()
        self.__component = ComponentModule()
        self.__component_group = ComponentGroupModule()
        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.")

        self.__context.autoload_options()
        self.__context.autoload_user(
            request.user.id if request.user.is_authenticated else None)
        self.__context.push({
            "page_title":
            _("Edit Incident Update  · %s") % self.__context.get(
                "app_name", os.getenv("APP_NAME", "Silverback")),
            "update":
            update,
            "incident":
            incident
        })

        return render(request, self.template_name, self.__context.get())
Example #7
0
class Incidents(View):

    __request = None
    __response = None
    __helpers = None
    __form = None
    __logger = None
    __user_id = None
    __incident = None
    __correlation_id = None

    def __init__(self):
        self.__request = Request()
        self.__response = Response()
        self.__helpers = Helpers()
        self.__form = Form()
        self.__incident = IncidentModule()
        self.__logger = self.__helpers.get_logger(__name__)
        self.__form.add_validator(ExtraRules())

    @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": "",
            "status": "",
            "datetime": "",
        })

        self.__form.add_inputs({
            'name': {
                'value': request_data["name"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [1, 200],
                        'error':
                        _('Error! Incident name must be 1 to 200 characters long.'
                          )
                    }
                }
            },
            'datetime': {
                'value': request_data["datetime"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {}
            },
            'status': {
                'value': request_data["status"],
                'validate': {
                    'any_of': {
                        'param': [["open", "closed"]],
                        'error': _('Error! Incident is invalid.')
                    }
                }
            }
        })

        self.__form.process()

        if not self.__form.is_passed():
            return JsonResponse(
                self.__response.send_errors_failure(self.__form.get_errors(),
                                                    {}, self.__correlation_id))

        result = self.__incident.insert_one({
            "name":
            self.__form.get_sinput("name"),
            "status":
            self.__form.get_sinput("status"),
            "datetime":
            DateTimeField().clean(self.__form.get_sinput("datetime")),
            "uri":
            self.__incident.generate_uri(6)
        })

        if result:
            return JsonResponse(
                self.__response.send_private_success(
                    [{
                        "type": "success",
                        "message": _("Incident created successfully.")
                    }], {}, self.__correlation_id))
        else:
            return JsonResponse(
                self.__response.send_private_failure([{
                    "type":
                    "error",
                    "message":
                    _("Error! Something goes wrong while creating incident.")
                }], {}, 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": "",
            "limit": ""
        })

        try:
            offset = int(request_data["offset"])
            limit = int(request_data["limit"])
        except Exception:
            offset = 0
            limit = 0

        return JsonResponse(
            self.__response.send_private_success(
                [], {
                    'incidents':
                    self.__format_incidents(
                        self.__incident.get_all(offset, limit)),
                    'metadata': {
                        'offset': offset,
                        'limit': limit,
                        'count': self.__incident.count_all()
                    }
                }, self.__correlation_id))

    def __format_incidents(self, incidents):
        incidents_list = []

        for incident in incidents:
            incidents_list.append({
                "id":
                incident.id,
                "name":
                incident.name,
                "uri":
                incident.uri,
                "status":
                incident.status.title(),
                "created_at":
                incident.created_at.strftime("%b %d %Y %H:%M:%S"),
                "view_url":
                reverse("app.web.admin.incident.view",
                        kwargs={'incident_id': incident.id}),
                "view_status_url":
                reverse("app.web.status_page_single",
                        kwargs={'uri': incident.uri}),
                "edit_url":
                reverse("app.web.admin.incident.edit",
                        kwargs={'incident_id': incident.id}),
                "delete_url":
                reverse("app.api.private.v1.admin.incident.endpoint",
                        kwargs={'incident_id': incident.id})
            })

        return incidents_list
Example #8
0
class Incident(View):

    __request = None
    __response = None
    __helpers = None
    __form = None
    __logger = None
    __user_id = None
    __incident = None
    __correlation_id = None

    def __init__(self):
        self.__request = Request()
        self.__response = Response()
        self.__helpers = Helpers()
        self.__form = Form()
        self.__incident = IncidentModule()
        self.__logger = self.__helpers.get_logger(__name__)
        self.__form.add_validator(ExtraRules())

    @allow_if_authenticated
    def post(self, request, incident_id):

        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": "",
            "status": "",
            "datetime": "",
        })

        self.__form.add_inputs({
            'name': {
                'value': request_data["name"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [1, 200],
                        'error':
                        _('Error! Incident name must be 1 to 200 characters long.'
                          )
                    }
                }
            },
            'datetime': {
                'value': request_data["datetime"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {}
            },
            'status': {
                'value': request_data["status"],
                'validate': {
                    'any_of': {
                        'param': [["open", "closed"]],
                        'error': _('Error! Incident is invalid.')
                    }
                }
            }
        })

        self.__form.process()

        if not self.__form.is_passed():
            return JsonResponse(
                self.__response.send_errors_failure(self.__form.get_errors(),
                                                    {}, self.__correlation_id))

        result = self.__incident.update_one_by_id(
            incident_id, {
                "name":
                self.__form.get_sinput("name"),
                "status":
                self.__form.get_sinput("status"),
                "datetime":
                DateTimeField().clean(self.__form.get_sinput("datetime"))
            })

        if result:
            return JsonResponse(
                self.__response.send_private_success(
                    [{
                        "type": "success",
                        "message": _("Incident updated successfully.")
                    }], {}, self.__correlation_id))
        else:
            return JsonResponse(
                self.__response.send_private_failure([{
                    "type":
                    "error",
                    "message":
                    _("Error! Something goes wrong while updating incident.")
                }], {}, self.__correlation_id))

    @allow_if_authenticated
    def delete(self, request, incident_id):

        self.__correlation_id = request.META[
            "X-Correlation-ID"] if "X-Correlation-ID" in request.META else ""
        self.__user_id = request.user.id

        if self.__incident.delete_one_by_id(incident_id):
            return JsonResponse(
                self.__response.send_private_success(
                    [{
                        "type": "success",
                        "message": _("Incident deleted successfully.")
                    }], {}, self.__correlation_id))

        else:
            return JsonResponse(
                self.__response.send_private_failure([{
                    "type":
                    "error",
                    "message":
                    _("Error! Something goes wrong while deleting incident.")
                }], {}, self.__correlation_id))
Example #9
0
class IncidentUpdates(View):

    __request = None
    __response = None
    __helpers = None
    __form = None
    __logger = None
    __user_id = None
    __incident = None
    __incident_update = None
    __task = None
    __notification = None
    __subscriber = None
    __incident_update_notification = None
    __correlation_id = None

    def __init__(self):
        self.__request = Request()
        self.__response = Response()
        self.__helpers = Helpers()
        self.__form = Form()
        self.__incident = IncidentModule()
        self.__incident_update = IncidentUpdateModule()
        self.__task = Task_Module()
        self.__notification = NotificationModule()
        self.__subscriber = SubscriberModule()
        self.__incident_update_notification = IncidentUpdateNotificationModule()
        self.__logger = self.__helpers.get_logger(__name__)
        self.__form.add_validator(ExtraRules())

    @allow_if_authenticated
    def post(self, request, incident_id):

        self.__correlation_id = request.META["X-Correlation-ID"] if "X-Correlation-ID" in request.META else ""
        self.__user_id = request.user.id
        self.__request.set_request(request)

        request_data = self.__request.get_request_data("post", {
            "status": "",
            "notify_subscribers": "",
            "message": "",
            "datetime": "",
        })

        self.__form.add_inputs({
            'message': {
                'value': request_data["message"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {}
            },
            'datetime': {
                'value': request_data["datetime"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {}
            },
            'status': {
                'value': request_data["status"],
                'validate': {
                    'any_of': {
                        'param': [["investigating", "identified", "monitoring", "update", "resolved"]],
                        'error': _('Error! Status is invalid.')
                    }
                }
            },
            'notify_subscribers': {
                'value': request_data["notify_subscribers"],
                'validate': {
                    'any_of': {
                        'param': [["on", "off"]],
                        'error': _('Error! Notify subscribers is invalid.')
                    }
                }
            }
        })

        self.__form.process()

        if not self.__form.is_passed():
            return JsonResponse(self.__response.send_errors_failure(self.__form.get_errors(), {}, self.__correlation_id))

        result = self.__incident_update.insert_one({
            "notify_subscribers": self.__form.get_sinput("notify_subscribers"),
            "datetime": DateTimeField().clean(self.__form.get_sinput("datetime")),
            "total_suscribers": self.__subscriber.count_by_status(SubscriberModule.VERIFIED),
            "message": self.__form.get_sinput("message"),
            "status": self.__form.get_sinput("status"),
            "incident_id": incident_id
        })

        if self.__form.get_sinput("status") == "resolved":
            self.__incident.update_one_by_id(incident_id, {
                "status": "closed"
            })
        else:
            self.__incident.update_one_by_id(incident_id, {
                "status": "open"
            })

        if result:
            return JsonResponse(self.__response.send_private_success([{
                "type": "success",
                "message": _("Incident update created successfully.")
            }], {}, self.__correlation_id))
        else:
            return JsonResponse(self.__response.send_private_failure([{
                "type": "error",
                "message": _("Error! Something goes wrong while creating update.")
            }], {}, self.__correlation_id))

    @allow_if_authenticated
    def get(self, request, incident_id):

        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": "",
            "limit": ""
        })

        try:
            offset = int(request_data["offset"])
            limit = int(request_data["limit"])
        except Exception:
            offset = 0
            limit = 0

        return JsonResponse(self.__response.send_private_success([], {
            'updates': self.__format_incident_updates(self.__incident_update.get_all(incident_id, offset, limit), incident_id),
            'metadata': {
                'offset': offset,
                'limit': limit,
                'count': self.__incident_update.count_all(incident_id)
            }
        }, self.__correlation_id))

    def __format_incident_updates(self, updates, incident_id):
        updates_list = []

        for update in updates:

            notified_subscribers = self.__incident_update_notification.count_by_update_status(
                update.id,
                IncidentUpdateNotificationModule.SUCCESS
            )
            progress = int(notified_subscribers/update.total_suscribers) * 100 if update.total_suscribers > 0 else 0

            updates_list.append({
                "id": update.id,
                "status": update.status.title(),
                "notify_subscribers": update.notify_subscribers.title(),
                "datetime": update.datetime.strftime("%b %d %Y %H:%M:%S"),
                "progress": progress if progress <= 100 else 100,
                "created_at": update.created_at.strftime("%b %d %Y %H:%M:%S"),
                "view_url": reverse("app.web.admin.incident_update.view", kwargs={'incident_id': incident_id, "update_id": update.id}),
                "edit_url": reverse("app.web.admin.incident_update.edit", kwargs={'incident_id': incident_id, "update_id": update.id}),
                "delete_url": reverse("app.api.private.v1.admin.incident_update.endpoint", kwargs={'incident_id': incident_id, "update_id": update.id})
            })

        return updates_list
Example #10
0
 def __init__(self):
     self.__incident = IncidentModule()
Example #11
0
class Incidents(View, Controller):
    """Create and List Incidents Private Endpoint Controller"""
    def __init__(self):
        self.__incident = IncidentModule()

    @allow_if_authenticated
    def post(self, request):

        request_data = self.get_request_data(request, "post", {
            "name": "",
            "status": "",
            "datetime": "",
        })

        self.form().add_inputs({
            'name': {
                'value': request_data["name"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [1, 200],
                        'error':
                        _('Error! Incident name must be 1 to 200 characters long.'
                          )
                    }
                }
            },
            'datetime': {
                'value': request_data["datetime"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'sv_datetime': {
                        'error': _('Error! Datetime is invalid.')
                    }
                }
            },
            'status': {
                'value': request_data["status"],
                'validate': {
                    'any_of': {
                        'param': [["open", "closed"]],
                        'error': _('Error! Incident is invalid.')
                    }
                }
            }
        })

        self.form().process()

        if not self.form().is_passed():
            return self.json(self.form().get_errors())

        result = self.__incident.insert_one({
            "name":
            self.form().get_sinput("name"),
            "status":
            self.form().get_sinput("status"),
            "datetime":
            DateTimeField().clean(self.form().get_sinput("datetime")),
            "uri":
            self.__incident.generate_uri(6)
        })

        if result:
            return self.json([{
                "type": "success",
                "message": _("Incident created successfully.")
            }])
        else:
            return self.json([{
                "type":
                "error",
                "message":
                _("Error! Something goes wrong while creating incident.")
            }])

    @allow_if_authenticated
    def get(self, request):

        request_data = self.get_request_data(request, "get", {
            "offset": 0,
            "limit": 20
        })

        try:
            offset = int(request_data["offset"])
            limit = int(request_data["limit"])
        except Exception:
            offset = 0
            limit = 20

        return self.json(
            [], {
                'incidents':
                self.__format_incidents(self.__incident.get_all(offset,
                                                                limit)),
                'metadata': {
                    'offset': offset,
                    'limit': limit,
                    'count': self.__incident.count_all()
                }
            })

    def __format_incidents(self, incidents):
        incidents_list = []

        for incident in incidents:
            incidents_list.append({
                "id":
                incident.id,
                "name":
                incident.name,
                "uri":
                incident.uri,
                "status":
                incident.status.title(),
                "created_at":
                incident.created_at.strftime("%b %d %Y %H:%M:%S"),
                "view_url":
                reverse("app.web.admin.incident.view",
                        kwargs={'incident_id': incident.id}),
                "view_status_url":
                reverse("app.web.status_page_single",
                        kwargs={'uri': incident.uri}),
                "edit_url":
                reverse("app.web.admin.incident.edit",
                        kwargs={'incident_id': incident.id}),
                "delete_url":
                reverse("app.api.private.v1.admin.incident.endpoint",
                        kwargs={'incident_id': incident.id})
            })

        return incidents_list
Example #12
0
class Incident(View, Controller):
    """Update Incident Private Endpoint Controller"""
    def __init__(self):
        self.__incident = IncidentModule()

    @allow_if_authenticated
    def post(self, request, incident_id):

        request_data = self.get_request_data(request, "post", {
            "name": "",
            "status": "",
            "datetime": "",
        })

        self.form().add_inputs({
            'name': {
                'value': request_data["name"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [1, 200],
                        'error':
                        _('Error! Incident name must be 1 to 200 characters long.'
                          )
                    }
                }
            },
            'datetime': {
                'value': request_data["datetime"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'sv_datetime': {
                        'error': _('Error! Datetime is invalid.')
                    }
                }
            },
            'status': {
                'value': request_data["status"],
                'validate': {
                    'any_of': {
                        'param': [["open", "closed"]],
                        'error': _('Error! Incident is invalid.')
                    }
                }
            }
        })

        self.form().process()

        if not self.form().is_passed():
            return self.json(self.form().get_errors())

        result = self.__incident.update_one_by_id(
            incident_id, {
                "name":
                self.form().get_sinput("name"),
                "status":
                self.form().get_sinput("status"),
                "datetime":
                DateTimeField().clean(self.form().get_sinput("datetime"))
            })

        if result:
            return self.json([{
                "type": "success",
                "message": _("Incident updated successfully.")
            }])
        else:
            return self.json([{
                "type":
                "error",
                "message":
                _("Error! Something goes wrong while updating incident.")
            }])

    @allow_if_authenticated
    def delete(self, request, incident_id):

        if self.__incident.delete_one_by_id(incident_id):
            return self.json([{
                "type": "success",
                "message": _("Incident deleted successfully.")
            }])
        else:
            return self.json([{
                "type":
                "error",
                "message":
                _("Error! Something goes wrong while deleting incident.")
            }])