Exemplo n.º 1
0
    def __init__(self, user, report, view, page=1):
        self.user = user
        self.report = report
        self.view = view

        num_per_page = view['elements'][0] if view['elements'] else None
        self.paginator, self.page = paginate_queryset(self.__get_queryset(), page, num_per_page)

        self.header, self.values = self.__component_data()
Exemplo n.º 2
0
    def get_queryset(self):
        assert self.mark_reports_model is not None, 'Please define the associations model'
        # assert self.mark_table is not None, 'Please define marks table name'

        qs_filters = {'mark_id': self.mark.id}

        # Filters
        if 'similarity' in self.view:
            qs_filters['result__{}'.format(self.view['similarity'][0])] = int(
                self.view['similarity'][1]) / 100
        if 'associated' in self.view:
            qs_filters['associated'] = True
        if 'ass_type' in self.view:
            qs_filters['type__in'] = self.view['ass_type']

        # Sorting
        ordering = 'id'

        view_columns = set(self.view['columns'])
        select_only = [
            'id', 'report__decision_id', 'report__decision__title',
            'report__decision__start_date'
        ]
        if self.type == 'unsafe':
            select_only.append('report__trace_id')
        else:
            select_only.append('report__id')
        select_related = ['report__decision']

        if 'similarity' in view_columns:
            select_only.append('result')
        if 'associated' in view_columns:
            select_only.append('associated')
        if 'ass_type' in view_columns:
            select_only.append('type')
        if 'ass_author' in view_columns:
            select_related.append('author')
            select_only.extend([
                'author__id', 'author__first_name', 'author__last_name',
                'author__username'
            ])

        queryset = self.mark_reports_model.objects.filter(**qs_filters)\
            .select_related(*select_related).order_by(ordering).only(*select_only)
        num_per_page = self.view['elements'][0] if self.view[
            'elements'] else None
        return paginate_queryset(queryset, self._page_number, num_per_page)
Exemplo n.º 3
0
    def __get_queryset(self, report, query_params):
        qs_filters = {'leaves__report': report}
        annotations = {}
        ordering = 'id'

        # Filter by cpu time
        if 'parent_cpu' in self.view:
            value = self.__get_ms(self.view['parent_cpu'][1], self.view['parent_cpu'][2])
            qs_filters['cpu_time__{}'.format(self.view['parent_cpu'][0])] = value

        # Order by cpu time
        if 'order' in self.view and self.view['order'][1] == 'parent_cpu':
            ordering = 'cpu_time'

        # Filter by wall time
        if 'parent_wall' in self.view:
            value = self.__get_ms(self.view['parent_wall'][1], self.view['parent_wall'][2])
            qs_filters['wall_time__{}'.format(self.view['parent_wall'][0])] = value

        # Order by wall time
        if 'order' in self.view and self.view['order'][1] == 'parent_wall':
            ordering = 'wall_time'

        # Filter by memory
        if 'parent_memory' in self.view:
            value = float(self.view['parent_memory'][1].replace(',', '.'))
            if self.view['parent_memory'][2] == 'KB':
                value *= 1024
            elif self.view['parent_memory'][2] == 'MB':
                value *= 1024 * 1024
            elif self.view['parent_memory'][2] == 'GB':
                value *= 1024 ** 3
            qs_filters['memory__{}'.format(self.view['parent_memory'][0])] = value

        # Order by memory
        if 'order' in self.view and self.view['order'][1] == 'parent_memory':
            ordering = 'memory'

        # Filter by marks number
        if 'confirmed' in query_params:
            qs_filters['cache__marks_confirmed__gt'] = 0
        elif 'marks_number' in self.view:
            if self.view['marks_number'][0] == 'confirmed':
                field = 'cache__marks_confirmed'
            else:
                field = 'cache__marks_total'
            qs_filters["{0}__{1}".format(field, self.view['marks_number'][1])] = int(self.view['marks_number'][2])

        # Filter by attribute(s)
        if 'attr_name' in query_params and 'attr_value' in query_params:
            annotations['attr_value'] = RawSQL(
                "\"{}\".\"attrs\"->>%s".format(self.cache_table),
                (unquote(query_params['attr_name']),)
            )
            qs_filters['attr_value'] = unquote(query_params['attr_value'])
        elif 'attr' in self.view:
            annotations['attr_value'] = RawSQL(
                "\"{}\".\"attrs\"->>%s".format(self.cache_table),
                (self.view['attr'][0],)
            )
            qs_filters['attr_value__{}'.format(self.view['attr'][1])] = self.view['attr'][2]

        # Order by attribute value
        if 'order' in self.view and self.view['order'][1] == 'attr':
            annotations['ordering_attr'] = RawSQL(
                "\"{}\".\"attrs\"->>%s".format(self.cache_table),
                (self.view['order'][2],)
            )
            ordering = 'ordering_attr'

        # Filter by component
        if 'component' in query_params:
            qs_filters['component'] = unquote(query_params['component'])
        elif 'component' in self.view:
            qs_filters['component__{}'.format(self.view['component'][0])] = self.view['component'][1]

        # Filter by problem
        if 'problem' in query_params:
            problem = unquote(query_params['problem'])
            if problem == 'null':
                qs_filters['cache__problems'] = {}
            else:
                qs_filters['cache__problems__has_key'] = problem
        elif 'problem' in self.view:
            qs_filters['cache__problems__has_key'] = self.view['problem'][0].strip()

        # Order direction
        if 'order' in self.view and self.view['order'][0] == 'up':
            ordering = '-' + ordering

        queryset = ReportUnknown.objects
        if annotations:
            queryset = queryset.annotate(**annotations)
        queryset = queryset.filter(**qs_filters).exclude(cache=None).order_by(ordering).select_related('cache')
        num_per_page = self.view['elements'][0] if self.view['elements'] else None
        return paginate_queryset(queryset, query_params.get('page', 1), num_per_page)
Exemplo n.º 4
0
    def __get_queryset(self, report, query_params):
        qs_filters = {'leaves__report': report}
        annotations = {}
        ordering = 'id'

        # Filter by verdict
        if 'verdict' in query_params:
            qs_filters['cache__verdict'] = query_params['verdict']
        elif 'verdict' in self.view and len(self.view['verdict']):
            qs_filters['cache__verdict__in'] = self.view['verdict']

        # Filter by cpu time
        if 'parent_cpu' in self.view:
            value = self.__get_ms(self.view['parent_cpu'][1], self.view['parent_cpu'][2])
            qs_filters['cpu_time__{}'.format(self.view['parent_cpu'][0])] = value

        # Order by cpu time
        if 'order' in self.view and self.view['order'][1] == 'parent_cpu':
            ordering = 'cpu_time'

        # Filter by wall time
        if 'parent_wall' in self.view:
            value = self.__get_ms(self.view['parent_wall'][1], self.view['parent_wall'][2])
            qs_filters['wall_time__{}'.format(self.view['parent_wall'][0])] = value

        # Order by wall time
        if 'order' in self.view and self.view['order'][1] == 'parent_wall':
            ordering = 'wall_time'

        # Filter by memory
        if 'parent_memory' in self.view:
            value = float(self.view['parent_memory'][1].replace(',', '.'))
            if self.view['parent_memory'][2] == 'KB':
                value *= 1024
            elif self.view['parent_memory'][2] == 'MB':
                value *= 1024 * 1024
            elif self.view['parent_memory'][2] == 'GB':
                value *= 1024 ** 3
            qs_filters['memory__{}'.format(self.view['parent_memory'][0])] = value

        # Order by memory
        if 'order' in self.view and self.view['order'][1] == 'parent_memory':
            ordering = 'memory'

        # Filter by marks number
        if 'confirmed' in query_params:
            qs_filters['cache__marks_confirmed__gt'] = 0
        elif 'marks_number' in self.view:
            if self.view['marks_number'][0] == 'confirmed':
                field = 'cache__marks_confirmed'
            else:
                field = 'cache__marks_total'
            qs_filters["{0}__{1}".format(field, self.view['marks_number'][1])] = int(self.view['marks_number'][2])

        # Filter by tags
        if 'tag' in query_params:
            qs_filters['cache__tags__has_key'] = unquote(query_params['tag'])
        elif 'tags' in self.view:
            view_tags = set(x.strip() for x in self.view['tags'][0].split(';'))
            if '' in view_tags:
                view_tags.remove('')
            if len(view_tags):
                qs_filters['cache__tags__has_any_keys'] = list(view_tags)

        # Filter by attribute(s)
        if 'attr_name' in query_params and 'attr_value' in query_params:
            attr_name = unquote(query_params['attr_name'])
            attr_value = unquote(query_params['attr_value'])
            annotations['attr_value'] = RawSQL(
                "\"{}\".\"attrs\"->>%s".format(self.cache_table),
                (attr_name,)
            )
            qs_filters['attr_value'] = attr_value
        elif 'attr' in self.view:
            annotations['attr_value'] = RawSQL(
                "\"{}\".\"attrs\"->>%s".format(self.cache_table),
                (self.view['attr'][0],)
            )
            qs_filters['attr_value__{}'.format(self.view['attr'][1])] = self.view['attr'][2]

        # Sorting by attribute value
        if 'order' in self.view and self.view['order'][1] == 'attr':
            annotations['ordering_attr'] = RawSQL(
                "\"{}\".\"attrs\"->>%s".format(self.cache_table),
                (self.view['order'][2],)
            )
            ordering = 'ordering_attr'

        # Order direction
        if 'order' in self.view and self.view['order'][0] == 'up':
            ordering = '-' + ordering

        queryset = ReportSafe.objects
        if annotations:
            queryset = queryset.annotate(**annotations)
        queryset = queryset.filter(**qs_filters).exclude(cache=None).order_by(ordering).select_related('cache')
        num_per_page = self.view['elements'][0] if self.view['elements'] else None
        return paginate_queryset(queryset, query_params.get('page', 1), num_per_page)
Exemplo n.º 5
0
    def get_queryset(self):
        assert self.versions_model is not None, 'Please define the mark versions model'
        assert self.mark_table is not None, 'Please define marks table name'

        view_columns = set(self.view['columns'])
        qs_filters = {'version': F('mark__version')}
        annotations = {}

        if 'num_of_links' in view_columns:
            annotations['num_of_links'] = Count('mark__markreport_set')

        # Filters
        if 'identifier' in self.view:
            qs_filters['mark__identifier'] = self.view['identifier']
        if 'status' in self.view:
            qs_filters['status__in'] = self.view['status']
        if 'verdict' in self.view:
            qs_filters['verdict__in'] = self.view['verdict']
        if 'source' in self.view:
            qs_filters['mark__source__in'] = self.view['source']
        if 'author' in self.view:
            qs_filters['author_id'] = self.view['author'][0]
        if 'attr' in self.view:
            annotations['attr_value'] = RawSQL(
                "\"{}\".\"cache_attrs\"->>%s".format(self.mark_table),
                (self.view['attr'][0], ))
            qs_filters['attr_value__{}'.format(
                self.view['attr'][1])] = self.view['attr'][2]
        if 'change_date' in self.view:
            value = now() - timedelta(**{
                self.view['change_date'][2]:
                int(self.view['change_date'][1])
            })
            qs_filters['change_date__{}'.format(
                self.view['change_date'][0])] = value
        if 'component' in self.view:
            qs_filters['mark__component__{}'.format(
                self.view['component'][0])] = self.view['component'][1]

        # Sorting
        ordering = 'id'
        if 'order' in self.view:
            if self.view['order'][1] == 'change_date':
                ordering = 'change_date'
            elif self.view['order'][1] == 'component':
                ordering = 'mark__component'
            elif self.view['order'][1] == 'attr':
                annotations['ordering_attr'] = RawSQL(
                    "\"{}\".\"cache_attrs\"->>%s".format(self.mark_table),
                    (self.view['order'][2], ))
                ordering = 'ordering_attr'
            elif self.view['order'][
                    1] == 'num_of_links' and 'num_of_links' in annotations:
                ordering = 'num_of_links'
            if self.view['order'][0] == 'up':
                ordering = '-' + ordering

        select_only = ['mark__id']
        select_related = ['mark']
        if 'identifier' in view_columns:
            select_only.append('mark__identifier')
        if 'source' in view_columns:
            select_only.append('mark__source')
        if 'change_date' in view_columns:
            select_only.append('change_date')
        if 'author' in view_columns:
            select_related.append('author')
            select_only.extend([
                'author__id', 'author__first_name', 'author__last_name',
                'author__username'
            ])
        if 'status' in view_columns:
            select_only.append('status')
        if 'tags' in view_columns:
            select_only.append('mark__cache_tags')
        if 'verdict' in view_columns:
            select_only.append('verdict')
        if 'component' in view_columns:
            select_only.append('mark__component')
        if 'problem_pattern' in view_columns:
            select_only.append('problem_pattern')
        if 'threshold' in view_columns:
            select_only.append('threshold')

        queryset = self.versions_model.objects
        if annotations:
            queryset = queryset.annotate(**annotations)
        queryset = queryset.filter(
            **qs_filters).order_by(ordering).select_related(
                *select_related).only(*select_only)
        num_per_page = self.view['elements'][0] if self.view[
            'elements'] else None
        return paginate_queryset(queryset, self._page_number, num_per_page)