Пример #1
0
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.")
            }])
Пример #2
0
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
Пример #3
0
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))