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 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 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