Exemple #1
0
    def get_dict(self,
                 request,
                 details=False,
                 exclude_keys=None,
                 include_keys=None):
        from appenlight.models.services.report_group import ReportGroupService
        instance_dict = super(Report, self).get_dict()
        instance_dict['req_stats'] = self.req_stats()
        instance_dict['group'] = {}
        instance_dict['group']['id'] = self.report_group.id
        instance_dict['group'][
            'total_reports'] = self.report_group.total_reports
        instance_dict['group']['last_report'] = self.report_group.last_report
        instance_dict['group']['priority'] = self.report_group.priority
        instance_dict['group']['occurences'] = self.report_group.occurences
        instance_dict['group'][
            'last_timestamp'] = self.report_group.last_timestamp
        instance_dict['group'][
            'first_timestamp'] = self.report_group.first_timestamp
        instance_dict['group']['public'] = self.report_group.public
        instance_dict['group']['fixed'] = self.report_group.fixed
        instance_dict['group']['read'] = self.report_group.read
        instance_dict['group'][
            'average_duration'] = self.report_group.average_duration

        instance_dict[
            'resource_name'] = self.report_group.application.resource_name
        instance_dict['report_type'] = self.report_type

        if instance_dict['http_status'] == 404 and not instance_dict['error']:
            instance_dict['error'] = '404 Not Found'

        if details:
            instance_dict['affected_users_count'] = \
                ReportGroupService.affected_users_count(self.report_group)
            instance_dict['top_affected_users'] = [{
                'username': u.username,
                'count': u.count
            } for u in ReportGroupService.top_affected_users(self.report_group)
                                                   ]
            instance_dict['application'] = {'integrations': []}
            for integration in self.report_group.application.integrations:
                if integration.front_visible:
                    instance_dict['application']['integrations'].append({
                        'name':
                        integration.integration_name,
                        'action':
                        integration.integration_action
                    })
            instance_dict['comments'] = [
                c.get_dict() for c in self.report_group.comments
            ]

            instance_dict['group']['next_report'] = None
            instance_dict['group']['previous_report'] = None
            next_in_group = self.get_next_in_group(request)
            previous_in_group = self.get_previous_in_group(request)
            if next_in_group:
                instance_dict['group']['next_report'] = next_in_group
            if previous_in_group:
                instance_dict['group']['previous_report'] = previous_in_group

            # slow call ordering
            def find_parent(row, data):
                for r in reversed(data):
                    try:
                        if (row['timestamp'] > r['timestamp']
                                and row['end_time'] < r['end_time']):
                            return r
                    except TypeError as e:
                        log.warning('reports_view.find_parent: %s' % e)
                return None

            new_calls = []
            calls = [c.get_dict() for c in self.slow_calls]
            while calls:
                # start from end
                for x in range(len(calls) - 1, -1, -1):
                    parent = find_parent(calls[x], calls)
                    if parent:
                        parent['children'].append(calls[x])
                    else:
                        # no parent at all? append to new calls anyways
                        new_calls.append(calls[x])
                        # print 'append', calls[x]
                    del calls[x]
                    break
            instance_dict['slow_calls'] = new_calls

        instance_dict['front_url'] = self.get_public_url(request)

        exclude_keys_list = exclude_keys or []
        include_keys_list = include_keys or []
        for k in list(instance_dict.keys()):
            if k == 'group':
                continue
            if (k in exclude_keys_list
                    or (k not in include_keys_list and include_keys)):
                del instance_dict[k]
        return instance_dict
Exemple #2
0
    def get_dict(self,
                 request,
                 details=False,
                 exclude_keys=None,
                 include_keys=None):
        from appenlight.models.services.report_group import ReportGroupService

        instance_dict = super(Report, self).get_dict()
        instance_dict["req_stats"] = self.req_stats()
        instance_dict["group"] = {}
        instance_dict["group"]["id"] = self.report_group.id
        instance_dict["group"][
            "total_reports"] = self.report_group.total_reports
        instance_dict["group"]["last_report"] = self.report_group.last_report
        instance_dict["group"]["priority"] = self.report_group.priority
        instance_dict["group"]["occurences"] = self.report_group.occurences
        instance_dict["group"][
            "last_timestamp"] = self.report_group.last_timestamp
        instance_dict["group"][
            "first_timestamp"] = self.report_group.first_timestamp
        instance_dict["group"]["public"] = self.report_group.public
        instance_dict["group"]["fixed"] = self.report_group.fixed
        instance_dict["group"]["read"] = self.report_group.read
        instance_dict["group"][
            "average_duration"] = self.report_group.average_duration

        instance_dict[
            "resource_name"] = self.report_group.application.resource_name
        instance_dict["report_type"] = self.report_type

        if instance_dict["http_status"] == 404 and not instance_dict["error"]:
            instance_dict["error"] = "404 Not Found"

        if details:
            instance_dict[
                "affected_users_count"] = ReportGroupService.affected_users_count(
                    self.report_group)
            instance_dict["top_affected_users"] = [{
                "username": u.username,
                "count": u.count
            } for u in ReportGroupService.top_affected_users(self.report_group)
                                                   ]
            instance_dict["application"] = {"integrations": []}
            for integration in self.report_group.application.integrations:
                if integration.front_visible:
                    instance_dict["application"]["integrations"].append({
                        "name":
                        integration.integration_name,
                        "action":
                        integration.integration_action,
                    })
            instance_dict["comments"] = [
                c.get_dict() for c in self.report_group.comments
            ]

            instance_dict["group"]["next_report"] = None
            instance_dict["group"]["previous_report"] = None
            next_in_group = self.get_next_in_group(request)
            previous_in_group = self.get_previous_in_group(request)
            if next_in_group:
                instance_dict["group"]["next_report"] = next_in_group
            if previous_in_group:
                instance_dict["group"]["previous_report"] = previous_in_group

            # slow call ordering
            def find_parent(row, data):
                for r in reversed(data):
                    try:
                        if (row["timestamp"] > r["timestamp"]
                                and row["end_time"] < r["end_time"]):
                            return r
                    except TypeError as e:
                        log.warning("reports_view.find_parent: %s" % e)
                return None

            new_calls = []
            calls = [c.get_dict() for c in self.slow_calls]
            while calls:
                # start from end
                for x in range(len(calls) - 1, -1, -1):
                    parent = find_parent(calls[x], calls)
                    if parent:
                        parent["children"].append(calls[x])
                    else:
                        # no parent at all? append to new calls anyways
                        new_calls.append(calls[x])
                        # print 'append', calls[x]
                    del calls[x]
                    break
            instance_dict["slow_calls"] = new_calls

        instance_dict["front_url"] = self.get_public_url(request)

        exclude_keys_list = exclude_keys or []
        include_keys_list = include_keys or []
        for k in list(instance_dict.keys()):
            if k == "group":
                continue
            if k in exclude_keys_list or (k not in include_keys_list
                                          and include_keys):
                del instance_dict[k]
        return instance_dict