コード例 #1
0
ファイル: test_event.py プロジェクト: szyz/sentry
 def test_user(self):
     event = SnubaEvent({
         'event_id': 'a',
         'project_id': 1,
         'message': 'hello there',
         'title': 'hi',
         'location': 'somewhere',
         'culprit': 'foo',
         'timestamp': datetime.now(),
         'user_id': 123,
         'email': '*****@*****.**',
         'username': '******',
         'ip_address': '192.168.0.1',
         'tags.key': ['sentry:user'],
         'tags.value': ['email:[email protected]'],
     })
     result = serialize(event)
     assert result['eventID'] == event.event_id
     assert result['projectID'] == six.text_type(event.project_id)
     assert result['message'] == event.message
     assert result['title'] == event.title
     assert result['location'] == event.location
     assert result['culprit'] == event.culprit
     assert result['dateCreated'] == event.timestamp
     assert result['user']['id'] == event.user_id
     assert result['user']['email'] == event.email
     assert result['user']['username'] == event.username
     assert result['user']['ipAddress'] == event.ip_address
     assert result['tags'] == [{
         'key': 'user',
         'value': 'email:[email protected]',
         'query': 'user.email:[email protected]',
     }]
コード例 #2
0
    def get(self, request, organization):
        try:
            snuba_args = self.get_snuba_query_args(request, organization)
        except OrganizationEventsError as exc:
            return Response({'detail': exc.message}, status=400)
        except NoProjects:
            # return empty result if org doesn't have projects
            # or user doesn't have access to projects in org
            data_fn = lambda *args, **kwargs: []
        else:
            data_fn = partial(
                # extract 'data' from raw_query result
                lambda *args, **kwargs: raw_query(*args, **kwargs)['data'],
                selected_columns=SnubaEvent.selected_columns,
                orderby='-timestamp',
                referrer='api.organization-events',
                **snuba_args
            )

        return self.paginate(
            request=request,
            on_results=lambda results: serialize(
                [SnubaEvent(row) for row in results], request.user),
            paginator=GenericOffsetPaginator(data_fn=data_fn)
        )
コード例 #3
0
    def get(self, request, organization):
        query = request.GET.get('query')
        conditions = []
        if query:
            conditions.append(['message', 'LIKE', '%%%s%%' % (query, )])

        now = timezone.now()

        data_fn = partial(
            # extract 'data' from raw_query result
            lambda *args, **kwargs: raw_query(*args, **kwargs)['data'],
            start=now - timedelta(days=90),
            end=now,
            conditions=conditions,
            filter_keys={
                'project_id': self.get_project_ids(request, organization)
            },
            selected_columns=SnubaEvent.selected_columns,
            orderby='-timestamp',
        )

        return self.paginate(request=request,
                             on_results=lambda results: serialize(
                                 [SnubaEvent(row)
                                  for row in results], request.user),
                             paginator=GenericOffsetPaginator(data_fn=data_fn))
コード例 #4
0
    def test_no_group(self):
        """
        Use the SimpleEventSerializer to serialize an event without group
        """

        event = SnubaEvent({
            'event_id': 'a',
            'project_id': 1,
            'message': 'hello there',
            'title': 'hi',
            'type': 'default',
            'location': 'somewhere',
            'culprit': 'foo',
            'timestamp': '2011-01-01T00:00:00Z',
            'user_id': '123',
            'email': '*****@*****.**',
            'username': '******',
            'ip_address': '192.168.0.1',
            'platform': 'asdf',
            'group_id': None,
            'tags.key': ['sentry:user'],
            'tags.value': ['email:[email protected]'],
        })
        result = serialize(event, None, SimpleEventSerializer())
        assert result['groupID'] is None
コード例 #5
0
    def test_no_group(self):
        """
        Use the SimpleEventSerializer to serialize an event without group
        """

        event = SnubaEvent({
            "event_id": "a",
            "project_id": 1,
            "message": "hello there",
            "title": "hi",
            "type": "default",
            "location": "somewhere",
            "culprit": "foo",
            "timestamp": "2011-01-01T00:00:00Z",
            "user_id": "123",
            "email": "*****@*****.**",
            "username": "******",
            "ip_address": "192.168.0.1",
            "platform": "asdf",
            "group_id": None,
            "tags.key": ["sentry:user"],
            "tags.value": ["email:[email protected]"],
        })
        result = serialize(event, None, SimpleEventSerializer())
        assert result["groupID"] is None
コード例 #6
0
ファイル: project_events.py プロジェクト: yangzaiCN/sentry
    def __search_events_snuba(self, request, project):
        from functools32 import partial
        from sentry.api.paginator import GenericOffsetPaginator
        from sentry.api.serializers.models.event import SnubaEvent
        from sentry.utils.snuba import raw_query

        query = request.GET.get('query')
        conditions = []
        if query:
            conditions.append(
                [['positionCaseInsensitive', ['message',
                                              "'%s'" % (query, )]], '!=', 0])

        now = timezone.now()
        data_fn = partial(
            # extract 'data' from raw_query result
            lambda *args, **kwargs: raw_query(*args, **kwargs)['data'],
            start=now - timedelta(days=90),
            end=now,
            conditions=conditions,
            filter_keys={'project_id': [project.id]},
            selected_columns=SnubaEvent.selected_columns)

        return self.paginate(request=request,
                             on_results=lambda results: serialize(
                                 [SnubaEvent(row)
                                  for row in results], request.user),
                             paginator=GenericOffsetPaginator(data_fn=data_fn))
コード例 #7
0
    def get(self, request, organization):
        query = request.GET.get('query')
        conditions = []
        if query:
            conditions.append(
                [['positionCaseInsensitive', ['message',
                                              "'%s'" % (query, )]], '!=', 0])

        try:
            start, end = get_date_range_from_params(request.GET)
        except InvalidParams as exc:
            return Response({'detail': exc.message}, status=400)

        try:
            project_ids = self.get_project_ids(request, organization)
        except ValueError:
            return Response({'detail': 'Invalid project ids'}, status=400)

        data_fn = partial(
            # extract 'data' from raw_query result
            lambda *args, **kwargs: raw_query(*args, **kwargs)['data'],
            start=start,
            end=end,
            conditions=conditions,
            filter_keys={'project_id': project_ids},
            selected_columns=SnubaEvent.selected_columns,
            orderby='-timestamp',
            referrer='api.organization-events',
        )

        return self.paginate(request=request,
                             on_results=lambda results: serialize(
                                 [SnubaEvent(row)
                                  for row in results], request.user),
                             paginator=GenericOffsetPaginator(data_fn=data_fn))
コード例 #8
0
    def test_user(self):
        """
        Use the SimpleEventSerializer to serialize an event
        """

        group = self.create_group()
        event = SnubaEvent(
            {
                "event_id": "a",
                "project_id": 1,
                "message": "hello there",
                "title": "hi",
                "type": "default",
                "location": "somewhere",
                "culprit": "foo",
                "timestamp": "2011-01-01T00:00:00Z",
                "user_id": "123",
                "email": "*****@*****.**",
                "username": "******",
                "ip_address": "192.168.0.1",
                "platform": "asdf",
                "group_id": group.id,
                "tags.key": ["sentry:user"],
                "tags.value": ["email:[email protected]"],
            }
        )
        result = serialize(event, None, SimpleEventSerializer())

        # Make sure we didn't have to call out to Nodestore to get the data
        # required to serialize this event and the NodeData is still empty.
        assert (
            event.data._node_data is None
        ), "SimpleEventSerializer should not load Nodestore data."

        assert result["eventID"] == event.event_id
        assert result["projectID"] == six.text_type(event.project_id)
        assert result["groupID"] == six.text_type(group.id)
        assert result["message"] == event.message
        assert result["title"] == event.title
        assert result["location"] == event.location
        assert result["culprit"] == event.culprit
        assert result["dateCreated"] == event.datetime
        assert result["user"]["id"] == event.user_id
        assert result["user"]["email"] == event.email
        assert result["user"]["username"] == event.username
        assert result["user"]["ip_address"] == event.ip_address
        assert result["tags"] == [
            {"key": "user", "value": "email:[email protected]", "query": "user.email:[email protected]"}
        ]
コード例 #9
0
    def test_user(self):
        """
        Use the SimpleEventSerializer to serialize an event
        """

        group = self.create_group()
        event = SnubaEvent({
            'event_id': 'a',
            'project_id': 1,
            'message': 'hello there',
            'title': 'hi',
            'type': 'default',
            'location': 'somewhere',
            'culprit': 'foo',
            'timestamp': '2011-01-01T00:00:00Z',
            'user_id': '123',
            'email': '*****@*****.**',
            'username': '******',
            'ip_address': '192.168.0.1',
            'platform': 'asdf',
            'group_id': group.id,
            'tags.key': ['sentry:user'],
            'tags.value': ['email:[email protected]'],
        })
        result = serialize(event, None, SimpleEventSerializer())

        # Make sure we didn't have to call out to Nodestore to get the data
        # required to serialize this event and the NodeData is still empty.
        assert event.data._node_data is None, "SimpleEventSerializer should not load Nodestore data."

        assert result['eventID'] == event.event_id
        assert result['projectID'] == six.text_type(event.project_id)
        assert result['groupID'] == six.text_type(group.id)
        assert result['message'] == event.message
        assert result['title'] == event.title
        assert result['location'] == event.location
        assert result['culprit'] == event.culprit
        assert result['dateCreated'] == event.datetime
        assert result['user']['id'] == event.user_id
        assert result['user']['email'] == event.email
        assert result['user']['username'] == event.username
        assert result['user']['ip_address'] == event.ip_address
        assert result['tags'] == [{
            'key': 'user',
            'value': 'email:[email protected]',
            'query': 'user.email:[email protected]',
        }]
コード例 #10
0
    def _get_events_snuba(self, request, group, environments, query, tags,
                          start, end):
        conditions = []
        if query:
            msg_substr = [
                'positionCaseInsensitive', ['message',
                                            "'%s'" % (query, )]
            ]
            message_condition = [msg_substr, '!=', 0]
            if is_event_id(query):
                or_condition = [message_condition, ['event_id', '=', query]]
                conditions.append(or_condition)
            else:
                conditions.append(message_condition)

        if tags:
            for tag_name, tag_val in tags.items():
                operator = 'IN' if isinstance(tag_val, list) else '='
                conditions.append(
                    [u'tags[{}]'.format(tag_name), operator, tag_val])

        default_end = timezone.now()
        default_start = default_end - timedelta(days=90)

        data_fn = partial(
            # extract 'data' from raw_query result
            lambda *args, **kwargs: raw_query(*args, **kwargs)['data'],
            start=max(start, default_start) if start else default_start,
            end=min(end, default_end) if end else default_end,
            conditions=conditions,
            filter_keys={
                'project_id': [group.project_id],
                'issue': [group.id]
            },
            selected_columns=SnubaEvent.selected_columns +
            ['tags.key', 'tags.value'],
            orderby='-timestamp',
            referrer='api.group-events',
        )

        return self.paginate(request=request,
                             on_results=lambda results: serialize(
                                 [SnubaEvent(row)
                                  for row in results], request.user),
                             paginator=GenericOffsetPaginator(data_fn=data_fn))
コード例 #11
0
    def _get_events_snuba(self, request, group, environment, query, tags):
        conditions = []
        if query:
            msg_substr = [
                'positionCaseInsensitive', ['message',
                                            "'%s'" % (query, )]
            ]
            message_condition = [msg_substr, '!=', 0]
            if is_event_id(query):
                or_condition = [message_condition, ['event_id', '=', query]]
                conditions.append(or_condition)
            else:
                conditions.append(message_condition)

        if tags:
            conditions.extend([[u'tags[{}]'.format(k), '=', v]
                               for (k, v) in tags.items()])

        now = timezone.now()
        data_fn = partial(
            # extract 'data' from raw_query result
            lambda *args, **kwargs: raw_query(*args, **kwargs)['data'],
            start=now - timedelta(days=90),
            end=now,
            conditions=conditions,
            filter_keys={
                'project_id': [group.project_id],
                'issue': [group.id]
            },
            selected_columns=SnubaEvent.selected_columns +
            ['tags.key', 'tags.value'],
            orderby='-timestamp',
            referrer='api.group-events',
        )

        return self.paginate(request=request,
                             on_results=lambda results: serialize(
                                 [SnubaEvent(row)
                                  for row in results], request.user),
                             paginator=GenericOffsetPaginator(data_fn=data_fn))
コード例 #12
0
    def get(self, request, organization):
        try:
            start, end = get_date_range_from_params(request.GET)
        except InvalidParams as exc:
            return Response({'detail': exc.message}, status=400)

        try:
            project_ids = self.get_project_ids(request, organization)
        except ValueError:
            return Response({'detail': 'Invalid project ids'}, status=400)

        environments = self.get_environments(request, organization)
        params = {
            'start': start,
            'end': end,
            'project_id': project_ids,
        }
        if environments:
            params['environment'] = environments

        try:
            snuba_args = get_snuba_query_args(query=request.GET.get('query'),
                                              params=params)
        except InvalidSearchQuery as exc:
            return Response({'detail': exc.message}, status=400)

        data_fn = partial(
            # extract 'data' from raw_query result
            lambda *args, **kwargs: raw_query(*args, **kwargs)['data'],
            selected_columns=SnubaEvent.selected_columns,
            orderby='-timestamp',
            referrer='api.organization-events',
            **snuba_args)

        return self.paginate(request=request,
                             on_results=lambda results: serialize(
                                 [SnubaEvent(row)
                                  for row in results], request.user),
                             paginator=GenericOffsetPaginator(data_fn=data_fn))