Пример #1
0
def get_metric_chart_for_services_group():
    """
        request_json:
        {
            "services_group_id": int,
            "time_from": string,
            "time_until": string,
            "metric_type": string
        }

        response_json:
       {
            "timeline": [string],
            "values": [int]
        }
    """
    request_json = request.get_json()
    is_valid, error_data, valid_data = validators.validate_request_json(
        schemas.BILLING_TABLE_SERVICES_GROUP_CHART_SCHEMA, request_json
    )
    if is_valid:
        metrics = get_services_group_data_chart(**valid_data)
        resp_data = format_metrics_list_for_general_table(metrics)
        response = jsonify(resp_data)
    else:
        response = jsonify(error_data)
        response.status_code = 400
    return response
Пример #2
0
def get_filtered_billing_table_data():
    """
        Endpoint with average cpu and memory values for billing table.

        Incoming json:
        {
            "page": integer,
            "target_type": string, | required
            "target_ids": list of integers, | can be empty
            "time_from": string, | required
            "time_until": string, | required
        }


        time_from and time_until are mandatory fields

        If there are no filters (except for time_from and time_until),
        return all service groups with average cpu and memory

        Result json:
        {
            "table_data":
                [
                    {
                        "services_group_id": "id" | integer
                        "services_group": "title" | string,
                        "team": "title" | string,
                        "project": "title" | string,
                        # TODO: evaluate cpu
                        # "cpu_sum": "value" | float,
                        "memory_sum": "value" | integer
                    }
                    ...
                ],
            "has_next": bool,
            "has_prev": bool,
            "pages": integer
        }
    """
    request_json = request.get_json() or {}
    valid, error_message, formatted_json = validators.validate_request_json(
        schemas.SCHEMA_FOR_BILLING_TABLE, request_json
    )

    if not valid:
        response = jsonify(error_message)
        response.status_code = 400
    else:
        table_data = get_billing_table_data(formatted_json)
        result = {
            "table_data": [
                format_row_for_billing_table(i) for i in table_data.items
            ],
            "has_next": table_data.has_next,
            "has_prev": table_data.has_prev,
            "pages": table_data.pages,
        }
        response = jsonify(result)
    return response
Пример #3
0
def get_billing_metric_values_by_services_group():
    """
        request_json:
        {
            "services_group_id": int,
            "time_from": string,
            "time_until": string
        }

        response_json:
        [
            {
                "service_id": int,
                "service_title": string,
                "cpu": int,
                "memory": int
            }
        ]
    """
    request_json = request.get_json()
    is_valid, error_data, valid_data = validators.validate_request_json(
        schemas.BILLING_TABLE_SERVICES_GROUP_SCHEMA, request_json
    )
    if is_valid:
        metrics = get_services_group_data_group_by_services(**valid_data)
        resp_data = tuple(
            {
                "cpu": round(metric.cpu, 0),
                "memory": round(metric.memory, 0),
                "service_id": metric.service_id,
                "service_title": metric.service_title,
            }
            for metric in metrics
        )
        response = jsonify(resp_data)
    else:
        response = jsonify(error_data)
        response.status_code = 400
    return response
Пример #4
0
    def dispatch_request(self):
        request_data = request.get_json() or {}
        valid, error_message, formatted_json = validators.validate_request_json(
            schemas.PIE_CHART_ENDPOINT_SCHEMA, request_data
        )
        if not valid:
            response = jsonify(error_message)
            response.status_code = 400
            return response

        total_initial_query = Service.query
        total_metric_sum = self.get_query_metric_sum(
            total_initial_query, request_data
        )

        target_type = request_data["target_type"]
        target_ids = request_data["target_ids"]

        if target_type == "all":
            initial_query = total_initial_query
        elif target_type == "cluster":
            initial_query = (
                Cluster.query.filter(Cluster.id.in_(target_ids))
                .join(Server, Server.cluster_id == Cluster.id)
                .join(Service, Server.id == Service.server_id)
            )
        elif target_type == "server":
            initial_query = Server.query.filter(Server.id.in_(target_ids)).join(
                Service, Service.server_id == Server.id
            )
        elif target_type == "services_group":
            initial_query = ServicesGroup.query.filter(
                ServicesGroup.id.in_(target_ids)
            ).join(Service, Service.services_group_id == ServicesGroup.id)
        elif target_type == "team":
            initial_query = (
                Team.query.filter(Team.id.in_(target_ids))
                .join(ServicesGroup, ServicesGroup.team_id == Team.id)
                .join(Service, Service.services_group_id == ServicesGroup.id)
            )
        elif target_type == "project":
            initial_query = (
                Project.query.filter(Project.id.in_(target_ids))
                .join(ServicesGroup, ServicesGroup.team_id == Project.id)
                .join(Service, Service.services_group_id == ServicesGroup.id)
            )
        else:
            raise Exception("Problem with request data validation")

        selected_metric_sum = self.get_query_metric_sum(
            initial_query, request_data
        )
        # todo: move this part to frontend
        if selected_metric_sum:
            response_data = {
                "labels": ("selected", "other"),
                "values": (
                    # todo: solve problem with ceiling in big number
                    round(selected_metric_sum, 0),
                    round(total_metric_sum - selected_metric_sum, 0),
                ),
            }
        else:
            response_data = {"labels": [], "values": []}
        return jsonify(response_data)
Пример #5
0
    def dispatch_request(self):
        request_data = request.get_json() or {}
        valid, error_message, formatted_json = validators.validate_request_json(
            schemas.PIE_CHART_ENDPOINT_SCHEMA, request_data
        )
        if not valid:
            response = jsonify(error_message)
            response.status_code = 400
            return response

        target_type = request_data["target_type"]
        target_ids = request_data["target_ids"]

        if target_type == "all":
            initial_query = ServicesGroup.query
        elif target_type == "cluster":
            initial_query = (
                Cluster.query.filter(Cluster.id.in_(target_ids))
                .join(Server, Server.cluster_id == Cluster.id)
                .join(Service, Service.server_id == Server.id)
                .join(
                    ServicesGroup, Service.services_group_id == ServicesGroup.id
                )
            )
        elif target_type == "server":
            initial_query = (
                Server.query.filter(Server.id.in_(target_ids))
                .join(Service, Service.server_id == Server.id)
                .join(
                    ServicesGroup, ServicesGroup.id == Service.services_group_id
                )
            )
        elif target_type == "services_group":
            initial_query = ServicesGroup.query.filter(
                ServicesGroup.id.in_(target_ids)
            )
        elif target_type == "project":
            initial_query = Project.query.filter(
                Project.id.in_(target_ids)
            ).join(ServicesGroup, Project.id == ServicesGroup.project_id)
        elif target_type == "team":
            initial_query = Team.query.filter(Team.id.in_(target_ids)).join(
                ServicesGroup, Team.id == ServicesGroup.team_id
            )
        else:
            raise Exception(
                "Problem with request data validation, wrong target type"
            )

        groups_metric_sum_values = self.get_metric_sum_for_each_group(
            initial_query, request_data
        )

        # todo: move this part to frontend
        if groups_metric_sum_values:
            labels, values = zip(*groups_metric_sum_values)
        else:
            labels, values = [], []

        response_data = {"labels": labels, "values": values}
        return jsonify(response_data)