def get_attrs(self, item_list, user): prefetch_related_objects(item_list, "created_by") result = defaultdict(lambda: {"created_by": {}}) user_serializer = UserSerializer() serialized_users = { user["id"]: user for user in serialize( [ discover_saved_query.created_by for discover_saved_query in item_list if discover_saved_query.created_by ], user=user, serializer=user_serializer, ) } for discover_saved_query in item_list: result[discover_saved_query]["created_by"] = serialized_users.get( str(discover_saved_query.created_by_id) ) return result
def serialize(self, obj, attrs, user, **kwargs): data = { "id": str(obj.id), "title": obj.title, "dateCreated": obj.date_added, "createdBy": serialize(obj.created_by, serializer=UserSerializer()), "widgetDisplay": attrs.get("widget_display", []), } return data
def get_attrs(self, item_list, user, **kwargs): attach_foreignkey(item_list, IncidentActivity.incident, related=("organization",)) attach_foreignkey(item_list, IncidentActivity.event_stats_snapshot) attach_foreignkey(item_list, IncidentActivity.user) user_serializer = UserSerializer() serialized_users = serialize( set(item.user for item in item_list if item.user_id), user=user, serializer=user_serializer, ) user_lookup = {user["id"]: user for user in serialized_users} return {item: {"user": user_lookup.get(six.text_type(item.user_id))} for item in item_list}
def serialize(self, obj, attrs, user, **kwargs): query_keys = [ "environment", "query", "fields", "widths", "conditions", "aggregations", "range", "start", "end", "orderby", "limit", "yAxis", "display", "topEvents", ] data = { "id": str(obj.id), "name": obj.name, "projects": [project.id for project in obj.projects.all()], "version": obj.version or obj.query.get("version", 1), "expired": False, "dateCreated": obj.date_created, "dateUpdated": obj.date_updated, "createdBy": serialize(obj.created_by, serializer=UserSerializer()) if obj.created_by else None, } for key in query_keys: if obj.query.get(key) is not None: data[key] = obj.query[key] # expire queries that are beyond the retention period if "start" in obj.query: start, end = parse_timestamp(obj.query["start"]), parse_timestamp( obj.query["end"]) if start and end: data["expired"], data[ "start"] = outside_retention_with_modified_start( start, end, obj.organization) if obj.query.get("all_projects"): data["projects"] = list(ALL_ACCESS_PROJECTS) return data
def get_attrs(self, item_list, user): item_dict = {i.id: i for i in item_list} prefetch_related_objects(item_list, "created_by") widgets = (DashboardWidget.objects.filter( dashboard_id__in=item_dict.keys()).order_by("order").values( "dashboard_id", "order", "display_type", "detail", "id")) result = defaultdict(lambda: { "widget_display": [], "widget_preview": [], "created_by": {} }) for widget in widgets: dashboard = item_dict[widget["dashboard_id"]] display_type = DashboardWidgetDisplayTypes.get_type_name( widget["display_type"]) result[dashboard]["widget_display"].append(display_type) for widget in widgets: dashboard = item_dict[widget["dashboard_id"]] widget_preview = { "displayType": DashboardWidgetDisplayTypes.get_type_name( widget["display_type"]), "layout": None, } if widget.get("detail"): detail = json.loads(widget["detail"]) if detail.get("layout"): widget_preview["layout"] = detail["layout"] result[dashboard]["widget_preview"].append(widget_preview) user_serializer = UserSerializer() serialized_users = { user["id"]: user for user in serialize( [ dashboard.created_by for dashboard in item_list if dashboard.created_by ], user=user, serializer=user_serializer, ) } for dashboard in item_dict.values(): result[dashboard]["created_by"] = serialized_users.get( str(dashboard.created_by_id)) return result
def get_attrs(self, item_list, user, **kwargs): prefetch_related_objects(item_list, "incident__organization") prefetch_related_objects(item_list, "user") user_serializer = UserSerializer() serialized_users = serialize( {item.user for item in item_list if item.user_id}, user=user, serializer=user_serializer, ) user_lookup = {user["id"]: user for user in serialized_users} return { item: { "user": user_lookup.get(str(item.user_id)) } for item in item_list }
def serialize(self, obj, attrs, user, **kwargs): query_keys = [ "environment", "query", "fields", "widths", "conditions", "aggregations", "range", "start", "end", "orderby", "limit", "yAxis", "display", ] data = { "id": six.text_type(obj.id), "name": obj.name, "projects": [project.id for project in obj.projects.all()], "version": obj.version or obj.query.get("version", 1), "dateCreated": obj.date_created, "dateUpdated": obj.date_updated, "createdBy": serialize(obj.created_by, serializer=UserSerializer()) if obj.created_by else None, } for key in query_keys: if obj.query.get(key) is not None: data[key] = obj.query[key] if obj.query.get("all_projects"): data["projects"] = list(ALL_ACCESS_PROJECTS) return data