class NewRelicApps(View, Controller): """List NewRelic Apps Private Endpoint Controller""" def __init__(self): self.__metric = MetricModule() @allow_if_authenticated def get(self, request): result = False try: result = self.__metric.get_new_relic_apps() except Exception as e: self.logger().error(_("Error while listing newrelic applications: %(error)s") % { "error": str(e) }) if result is False: return self.json([{ "type": "error", "message": _("Error! Connecting to New Relic.") }]) return self.json([], { 'apps': result })
class MetricEdit(View, Controller): """Metric Edit Page Controller""" template_name = 'templates/admin/metric/edit.html' @login_if_not_authenticated def get(self, request, metric_id): self.__metric = MetricModule() metric = self.__metric.get_one_by_id(metric_id) if not metric: raise Http404("Metric not found.") self.autoload_options() self.autoload_user( request.user.id if request.user.is_authenticated else None) self.context_push({ "page_title": _("Edit Metric · %s") % self.context_get("app_name", os.getenv("APP_NAME", "Silverback")), "metric": metric }) metric["data"] = json.loads(metric["data"]) return render(request, self.template_name, self.context_get())
def get(self, request, metric_id): self.__context = Context() self.__metric = MetricModule() self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" metric = self.__metric.get_one_by_id(metric_id) if not metric: raise Http404("Metric 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 Metric · %s") % self.__context.get( "app_name", os.getenv("APP_NAME", "Silverback")), "metric": metric }) metric["data"] = json.loads(metric["data"]) return render(request, self.template_name, self.__context.get())
def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__metric = MetricModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules())
def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__settings = Settings() self.__metric = MetricModule() self.__form = Form() self.__logger = self.__helpers.get_logger(__name__) self.__user_id = None self.__correlation_id = "" self.__form.add_validator(ExtraRules())
class NewRelicApps(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __metric = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__metric = MetricModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) @allow_if_authenticated def get(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" result = False try: result = self.__metric.get_new_relic_apps() except Exception as e: self.__logger.error( _("Error while listing newrelic applications: %(error)s {'correlationId':'%(correlationId)s'}" ) % { "error": str(e), "correlationId": self.__correlation_id }) if result is False: return JsonResponse( self.__response.send_private_failure( [{ "type": "error", "message": _("Error! Connecting to New Relic.") }], {}, self.__correlation_id)) return JsonResponse( self.__response.send_private_success([], {'apps': result}, self.__correlation_id))
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 __init__(self): self.__settings = Settings() self.__metric = MetricModule()
class BuilderSystemMetrics(View, Controller): """Add and Remove Builder System Metrics Private Endpoint Controller""" def __init__(self): self.__settings = Settings() self.__metric = MetricModule() @allow_if_authenticated_and_has_permission("manage_settings") def post(self, request): request_data = self.get_request_data(request, "post", {"metric_id": ""}) if request_data["metric_id"] == "" or not self.__metric.get_one_by_id( request_data["metric_id"].replace("m-", "")): return self.json([{ "type": "error", "message": _("Error! Metric is required.") }]) metrics = self.__settings.get_value_by_key("builder_metrics", json.dumps([])) metrics = json.loads(metrics) if request_data["metric_id"] in metrics: return self.json([{ "type": "success", "message": _("Metric added successfully.") }]) metrics.append(request_data["metric_id"]) result = self.__settings.update_options( {"builder_metrics": json.dumps(metrics)}) if result: return self.json([{ "type": "success", "message": _("Metric added successfully.") }]) else: return self.json([{ "type": "error", "message": _("Error! Something goes wrong while adding metric.") }]) @allow_if_authenticated_and_has_permission("manage_settings") def delete(self, request, metric_id): metrics = self.__settings.get_value_by_key("builder_metrics", json.dumps([])) metrics = json.loads(metrics) if metric_id not in metrics: return self.json([{ "type": "success", "message": _("Metric deleted successfully.") }]) metrics.remove(metric_id) result = self.__settings.update_options( {"builder_metrics": json.dumps(metrics)}) if result: return self.json([{ "type": "success", "message": _("Metric deleted successfully.") }]) else: return self.json([{ "type": "error", "message": _("Error! Something goes wrong while deleting metric.") }])
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
def __init__(self): self.__metric = MetricModule()
class Metrics(View, Controller): """Create and List Metrics Private Endpoint Controller""" def __init__(self): self.__metric = MetricModule() @allow_if_authenticated def post(self, request): request_data = self.get_request_data(request, "post", { "title": "", "description": "", "source": "", "application": "", "metric": "", "x_axis": "", "y_axis": "" }) self.form().add_inputs({ 'title': { 'value': request_data["title"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [1, 60], 'error': _('Error! Metric title must be 1 to 60 characters long.') } } }, 'description': { 'value': request_data["description"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [0, 150], 'error': _('Error! Metric description must be less than 150 characters long.') }, 'optional': {} } }, 'source': { 'value': request_data["source"], 'sanitize': { 'strip': {} }, 'validate': { 'any_of': { 'param': [["newrelic"]], 'error': _('Error! Source is invalid.') } } }, 'application': { 'value': request_data["application"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [1, 60], 'error': _('Error! Application must be 1 to 60 characters long.') } } }, 'metric': { 'value': request_data["metric"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [1, 60], 'error': _('Error! Metric must be 1 to 60 characters long.') } } }, 'x_axis': { 'value': request_data["x_axis"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [1, 40], 'error': _('Error! X-Axis label must be 1 to 40 characters long.') } } }, 'y_axis': { 'value': request_data["y_axis"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [1, 40], 'error': _('Error! Y-Axis label must be 1 to 40 characters long.') } } } }) self.form().process() if not self.form().is_passed(): return self.json(self.form().get_errors()) if self.__metric.get_one_by_title(self.form().get_sinput("title")): return self.json([{ "type": "error", "message": _("Error! Metric title is used before.") }]) result = self.__metric.insert_one({ "title": self.form().get_sinput("title"), "description": self.form().get_sinput("description"), "source": self.form().get_sinput("source"), "x_axis": self.form().get_sinput("x_axis"), "y_axis": self.form().get_sinput("y_axis"), "data": '{"application":"%s", "metric":"%s"}' % ( self.form().get_sinput("application"), self.form().get_sinput("metric") ) }) if result: return self.json([{ "type": "success", "message": _("Metric created successfully.") }]) else: return self.json([{ "type": "error", "message": _("Error! Something goes wrong while creating metric.") }]) @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([], { 'metrics': self.__format_metrics(self.__metric.get_all(offset, limit)), 'metadata': { 'offset': offset, 'limit': limit, 'count': self.__metric.count_all() } }) def __format_metrics(self, metrics): metrics_list = [] for metric in metrics: metrics_list.append({ "id": metric.id, "title": metric.title, "source": metric.source.title(), "created_at": metric.created_at.strftime("%b %d %Y %H:%M:%S"), "edit_url": reverse("app.web.admin.metric.edit", kwargs={'metric_id': metric.id}), "delete_url": reverse("app.api.private.v1.admin.metric.endpoint", kwargs={'metric_id': metric.id}) }) return metrics_list
class Metric(View, Controller): """Update and Delete Metric Private Endpoint Controller""" def __init__(self): self.__metric = MetricModule() @allow_if_authenticated def post(self, request, metric_id): request_data = self.get_request_data(request, "post", { "title": "", "description": "", "source": "", "application": "", "metric": "", "x_axis": "", "y_axis": "" }) self.form().add_inputs({ 'title': { 'value': request_data["title"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [1, 60], 'error': _('Error! Metric title must be 1 to 60 characters long.') } } }, 'description': { 'value': request_data["description"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [0, 150], 'error': _('Error! Metric description must be less than 150 characters long.') }, 'optional': {} } }, 'source': { 'value': request_data["source"], 'sanitize': { 'strip': {} }, 'validate': { 'any_of': { 'param': [["newrelic"]], 'error': _('Error! Source is invalid.') } } }, 'application': { 'value': request_data["application"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [1, 60], 'error': _('Error! Application must be 1 to 60 characters long.') } } }, 'metric': { 'value': request_data["metric"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [1, 60], 'error': _('Error! Metric must be 1 to 60 characters long.') } } }, 'x_axis': { 'value': request_data["x_axis"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [1, 40], 'error': _('Error! X-Axis label must be 1 to 40 characters long.') } } }, 'y_axis': { 'value': request_data["y_axis"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [1, 40], 'error': _('Error! Y-Axis label must be 1 to 40 characters long.') } } } }) self.form().process() if not self.form().is_passed(): return self.json(self.form().get_errors()) current_metric = self.__metric.get_one_by_title(self.form().get_sinput("title")) if current_metric and not current_metric["id"] == metric_id: return self.json([{ "type": "error", "message": _("Error! Metric title is used before.") }]) result = self.__metric.update_one_by_id(metric_id, { "title": self.form().get_sinput("title"), "description": self.form().get_sinput("description"), "source": self.form().get_sinput("source"), "x_axis": self.form().get_sinput("x_axis"), "y_axis": self.form().get_sinput("y_axis"), "data": '{"application":"%s", "metric":"%s"}' % ( self.form().get_sinput("application"), self.form().get_sinput("metric") ) }) if result: return self.json([{ "type": "success", "message": _("Metric updated successfully.") }]) else: return self.json([{ "type": "error", "message": _("Error! Something goes wrong while updating metric.") }]) @allow_if_authenticated def delete(self, request, metric_id): if self.__metric.delete_one_by_id(metric_id): return self.json([{ "type": "success", "message": _("Metric deleted successfully.") }]) else: return self.json([{ "type": "error", "message": _("Error! Something goes wrong while deleting metric.") }])
class Metric(View): """Update and Delete Metric Private Endpoint Controller""" def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__metric = MetricModule() self.__logger = self.__helpers.get_logger(__name__) self.__user_id = None self.__correlation_id = "" self.__form.add_validator(ExtraRules()) @allow_if_authenticated def post(self, request, metric_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", { "title": "", "description": "", "source": "", "application": "", "metric": "", "x_axis": "", "y_axis": "" }) self.__form.add_inputs({ 'title': { 'value': request_data["title"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [1, 60], 'error': _('Error! Metric title must be 1 to 60 characters long.' ) } } }, 'description': { 'value': request_data["description"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [0, 150], 'error': _('Error! Metric description must be less than 150 characters long.' ) }, 'optional': {} } }, 'source': { 'value': request_data["source"], 'sanitize': { 'strip': {} }, 'validate': { 'any_of': { 'param': [["newrelic"]], 'error': _('Error! Source is invalid.') } } }, 'application': { 'value': request_data["application"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [1, 60], 'error': _('Error! Application must be 1 to 60 characters long.' ) } } }, 'metric': { 'value': request_data["metric"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [1, 60], 'error': _('Error! Metric must be 1 to 60 characters long.') } } }, 'x_axis': { 'value': request_data["x_axis"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [1, 40], 'error': _('Error! X-Axis label must be 1 to 40 characters long.' ) } } }, 'y_axis': { 'value': request_data["y_axis"], 'sanitize': { 'strip': {} }, 'validate': { 'length_between': { 'param': [1, 40], 'error': _('Error! Y-Axis label must be 1 to 40 characters long.' ) } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_errors_failure(self.__form.get_errors(), {}, self.__correlation_id)) current_metric = self.__metric.get_one_by_title( self.__form.get_sinput("title")) if current_metric and not current_metric["id"] == metric_id: return JsonResponse( self.__response.send_private_failure( [{ "type": "error", "message": _("Error! Metric title is used before.") }], {}, self.__correlation_id)) result = self.__metric.update_one_by_id( metric_id, { "title": self.__form.get_sinput("title"), "description": self.__form.get_sinput("description"), "source": self.__form.get_sinput("source"), "x_axis": self.__form.get_sinput("x_axis"), "y_axis": self.__form.get_sinput("y_axis"), "data": '{"application":"%s", "metric":"%s"}' % (self.__form.get_sinput("application"), self.__form.get_sinput("metric")) }) if result: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Metric updated successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while updating metric.") }], {}, self.__correlation_id)) @allow_if_authenticated def delete(self, request, metric_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.__metric.delete_one_by_id(metric_id): return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Metric deleted successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while deleting metric.") }], {}, self.__correlation_id))
class Metric(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __metric = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__metric = MetricModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) @allow_if_authenticated def post(self, request, metric_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", { "title": "", "description": "", "source": "", "application": "", "metric": "", "x_axis": "", "y_axis": "" }) self.__form.add_inputs({ 'title': { 'value': request_data["title"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'description': { 'value': request_data["description"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'source': { 'value': request_data["source"], 'validate': { 'any_of': { 'param': [["newrelic"]], 'error': _('Error! Source is invalid.') } } }, 'application': { 'value': request_data["application"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'metric': { 'value': request_data["metric"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'x_axis': { 'value': request_data["x_axis"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'y_axis': { 'value': request_data["y_axis"], 'sanitize': { 'strip': {} }, 'validate': {} } }) 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.__metric.update_one_by_id( metric_id, { "title": self.__form.get_sinput("title"), "description": self.__form.get_sinput("description"), "source": self.__form.get_sinput("source"), "x_axis": self.__form.get_sinput("x_axis"), "y_axis": self.__form.get_sinput("y_axis"), "data": '{"application":"%s", "metric":"%s"}' % (self.__form.get_sinput("application"), self.__form.get_sinput("metric")) }) if result: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Metric updated successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while updating metric.") }], {}, self.__correlation_id)) @allow_if_authenticated def delete(self, request, metric_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.__metric.delete_one_by_id(metric_id): return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Metric deleted successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while deleting metric.") }], {}, self.__correlation_id))
class Metrics(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __metric = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__metric = MetricModule() 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", { "title": "", "description": "", "source": "", "application": "", "metric": "", "x_axis": "", "y_axis": "" }) self.__form.add_inputs({ 'title': { 'value': request_data["title"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'description': { 'value': request_data["description"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'source': { 'value': request_data["source"], 'validate': { 'any_of': { 'param': [["newrelic"]], 'error': _('Error! Source is invalid.') } } }, 'application': { 'value': request_data["application"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'metric': { 'value': request_data["metric"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'x_axis': { 'value': request_data["x_axis"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'y_axis': { 'value': request_data["y_axis"], 'sanitize': { 'strip': {} }, 'validate': {} } }) 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.__metric.insert_one({ "title": self.__form.get_sinput("title"), "description": self.__form.get_sinput("description"), "source": self.__form.get_sinput("source"), "x_axis": self.__form.get_sinput("x_axis"), "y_axis": self.__form.get_sinput("y_axis"), "data": '{"application":"%s", "metric":"%s"}' % (self.__form.get_sinput("application"), self.__form.get_sinput("metric")) }) if result: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Metric created successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating metric.") }], {}, 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( [], { 'metrics': self.__format_metrics(self.__metric.get_all(offset, limit)), 'metadata': { 'offset': offset, 'limit': limit, 'count': self.__metric.count_all() } }, self.__correlation_id)) def __format_metrics(self, metrics): metrics_list = [] for metric in metrics: metrics_list.append({ "id": metric.id, "title": metric.title, "source": metric.source.title(), "created_at": metric.created_at.strftime("%b %d %Y %H:%M:%S"), "edit_url": reverse("app.web.admin.metric.edit", kwargs={'metric_id': metric.id}), "delete_url": reverse("app.api.private.v1.admin.metric.endpoint", kwargs={'metric_id': metric.id}) }) return metrics_list
class BuilderSystemMetrics(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __settings = None __metric = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__settings = Settings() self.__metric = MetricModule() self.__form = Form() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) @allow_if_authenticated_and_has_permission("manage_settings") 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", {"metric_id": ""}) if request_data["metric_id"] == "" or not self.__metric.get_one_by_id( request_data["metric_id"]): return JsonResponse( self.__response.send_private_failure( [{ "type": "error", "message": _("Error! Metric is required.") }], {}, self.__correlation_id)) metrics = self.__settings.get_value_by_key("builder_metrics", json.dumps([])) metrics = json.loads(metrics) if request_data["metric_id"] in metrics: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Metric added successfully.") }], {}, self.__correlation_id)) metrics.append(request_data["metric_id"]) result = self.__settings.update_options( {"builder_metrics": json.dumps(metrics)}) if result: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Metric added successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while adding metric.") }], {}, self.__correlation_id)) @allow_if_authenticated_and_has_permission("manage_settings") def delete(self, request, metric_id): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" metrics = self.__settings.get_value_by_key("builder_metrics", json.dumps([])) metrics = json.loads(metrics) if metric_id not in metrics: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Metric deleted successfully.") }], {}, self.__correlation_id)) metrics.remove(metric_id) result = self.__settings.update_options( {"builder_metrics": json.dumps(metrics)}) if result: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Metric deleted successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while deleting metric.") }], {}, self.__correlation_id))