Exemplo n.º 1
0
class ComponentGroup():
    def __init__(self):
        self.__component_group_entity = ComponentGroupEntity()
        self.__component_entity = ComponentEntity()

    def get_one_by_id(self, id):
        group = self.__component_group_entity.get_one_by_id(id)

        if not group:
            return False

        return {
            "id": group.id,
            "name": group.name,
            "uptime": group.uptime,
            "description": group.description,
        }

    def get_one_by_name(self, name):
        group = self.__component_group_entity.get_one_by_name(name)

        if not group:
            return False

        return {
            "id": group.id,
            "name": group.name,
            "uptime": group.uptime,
            "description": group.description,
        }

    def insert_one(self, group):
        return self.__component_group_entity.insert_one(group)

    def update_one_by_id(self, id, group_data):
        return self.__component_group_entity.update_one_by_id(id, group_data)

    def count_all(self):
        return self.__component_group_entity.count_all()

    def count_components(self, group_id):
        return self.__component_entity.count(group_id)

    def get_all(self, offset=None, limit=None):
        return self.__component_group_entity.get_all(offset, limit)

    def delete_one_by_id(self, id):
        self.__component_entity.clear_group(id)
        return self.__component_group_entity.delete_one_by_id(id)
Exemplo n.º 2
0
class Dashboard():
    def __init__(self):
        self.__incident = IncidentEntity()
        self.__incident_update = IncidentUpdateEntity()
        self.__incident_update_notification = IncidentUpdateNotificationEntity(
        )
        self.__incident_update_component = IncidentUpdateComponentEntity()
        self.__subscriber = SubscriberEntity()
        self.__user = UserEntity()
        self.__component = ComponentEntity()
        self.__component_group = ComponentGroupEntity()
        self.__metric = MetricEntity()

    def incidents_count(self):
        return self.__incident.count_all()

    def subscribers_count(self):
        return self.__subscriber.count_all()

    def components_count(self):
        return self.__component.count_all()

    def component_groups_count(self):
        return self.__component_group.count_all()

    def metrics_count(self):
        return self.__metric.count_all()

    def users_count(self):
        return self.__user.count_all()

    def notifications_count(self, status):
        return self.__incident_update_notification.count_by_status(status)

    def subscribers_chart(self, days=14):
        subscribers = self.__subscriber.count_over_days(days)
        points = self.__build_points(days)
        for subscriber in subscribers:
            key = str(subscriber["day"].strftime("%d-%m-%Y"))
            if key in points.keys():
                points[key] = subscriber["count"]
        return ", ".join(str(x) for x in list(points.values()))

    def components_chart(self, days=14):
        components = self.__incident_update_component.count_over_days(days)
        points = self.__build_points(days)
        for component in components:
            key = str(component["day"].strftime("%d-%m-%Y"))
            if key in points.keys():
                points[key] = component["count"]
        return ", ".join(str(x) for x in list(points.values()))

    def notifications_chart(self, status, days=14):
        notifications = self.__incident_update_notification.count_over_days(
            status, days)
        points = self.__build_points(days)
        for notification in notifications:
            key = str(notification["day"].strftime("%d-%m-%Y"))
            if key in points.keys():
                points[key] = notification["count"]
        return ", ".join(str(x) for x in list(points.values()))

    def incidents_chart(self, days=14):
        incidents = self.__incident.count_over_days(days)
        points = self.__build_points(days)
        for incident in incidents:
            key = str(incident["day"].strftime("%d-%m-%Y"))
            if key in points.keys():
                points[key] = incident["count"]
        return ", ".join(str(x) for x in list(points.values()))

    def get_open_incidents(self):
        incidents = self.__incident.get_by_status("open")
        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")
            })

        return incidents_list

    def get_affected_components(self):
        affected_components = self.__incident_update_component.get_affected_components(
            0)
        affected_components_list = []

        for affected_component in affected_components:
            affected_components_list.append({
                "id":
                affected_component.id,
                "name":
                affected_component.component.name,
                "type":
                affected_component.type.replace("_", " ").title(),
                "update_id":
                affected_component.incident_update.id,
                "incident_id":
                affected_component.incident_update.incident.id
            })

        return affected_components_list

    def __build_points(self, days):
        i = days
        points = {}
        while i >= 0:
            last_x_days = timezone.now() - datetime.timedelta(i)
            points[str(last_x_days.strftime("%d-%m-%Y"))] = 0
            i -= 1
        return points