def post(self, request, organization): """ Create a saved query """ if not self.has_feature(organization, request): return self.respond(status=404) serializer = DiscoverSavedQuerySerializer( data=request.data, context={ "params": self.get_filter_params( request, organization, project_ids=request.data.get("projects")) }, ) if not serializer.is_valid(): return Response(serializer.errors, status=400) data = serializer.validated_data model = DiscoverSavedQuery.objects.create( organization=organization, name=data["name"], query=data["query"], version=data["version"], created_by=request.user if request.user.is_authenticated() else None, ) model.set_projects(data["project_ids"]) return Response(serialize(model), status=201)
def put(self, request, organization, query_id): """ Modify a saved query """ if not self.has_feature(organization, request): return self.respond(status=404) try: model = DiscoverSavedQuery.objects.get(id=query_id, organization=organization) except DiscoverSavedQuery.DoesNotExist: raise ResourceDoesNotExist serializer = DiscoverSavedQuerySerializer( data=request.data, context={ "params": self.get_filter_params( request, organization, project_ids=request.data.get("projects") ) }, ) if not serializer.is_valid(): return Response(serializer.errors, status=400) data = serializer.validated_data model.update( organization=organization, name=data["name"], query=data["query"], version=data["version"], ) model.set_projects(data["project_ids"]) return Response(serialize(model), status=200)
def post(self, request, organization): """ Create a saved query """ if not features.has( "organizations:discover", organization, actor=request.user): return self.respond(status=404) serializer = DiscoverSavedQuerySerializer( data=request.data, context={"organization": organization}) if not serializer.is_valid(): return Response(serializer.errors, status=400) data = serializer.validated_data model = DiscoverSavedQuery.objects.create( organization=organization, name=data["name"], query=data["query"], created_by=request.user if request.user.is_authenticated() else None, ) model.set_projects(data["project_ids"]) return Response(serialize(model), status=201)
def put(self, request, organization, query_id): """ Modify a saved query """ if not features.has("organizations:discover", organization, actor=request.user): return self.respond(status=404) try: model = DiscoverSavedQuery.objects.get(id=query_id, organization=organization) except DiscoverSavedQuery.DoesNotExist: raise ResourceDoesNotExist serializer = DiscoverSavedQuerySerializer( data=request.data, context={"organization": organization} ) if not serializer.is_valid(): return Response(serializer.errors, status=400) data = serializer.validated_data model.update(organization=organization, name=data["name"], query=data["query"]) model.set_projects(data["project_ids"]) return Response(serialize(model), status=200)
def validate(self, data): super(WidgetDataSourceSerializer, self).validate(data) if data["type"] == WidgetDataSourceTypes.DISCOVER_SAVED_SEARCH: serializer = DiscoverSavedQuerySerializer(data=data["data"], context=self.context) if not serializer.is_valid(): raise ValidationError("Error validating DiscoverSavedQuery: %s" % serializer.errors) else: raise ValidationError("Widget data source type %s not recognized." % data["type"]) return data
def generate_saved_query(project, transaction_title, name): org = project.organization start, end = get_date_range_from_params({}) params = { "start": start, "end": end, "project_id": [project.id], "organization_id": org.id } data = { "version": 2, "name": name, "fields": [ "title", "browser.name", "count()", "p75(transaction.duration)", "p95(transaction.duration)", "p99(transaction.duration)", ], "widths": ["-1", "-1", "-1", "-1", "-1", "-1"], "orderby": "-count", "query": f"title:{transaction_title}", "projects": [project.id], "range": "7d", "environment": [], "yAxis": "p75(transaction.duration)", "display": "daily", } serializer = DiscoverSavedQuerySerializer(data=data, context={"params": params}) if not serializer.is_valid(): raise Exception(serializer.errors) data = serializer.validated_data DiscoverSavedQuery.objects.create( organization=org, name=data["name"], query=data["query"], version=data["version"], )