def usage()

in glam/api/views.py [0:0]


def usage(request):
    """
    Provides individual or aggregated (count) metrics on probe searching.

    Possible query parameters are:
    * fromDate: Date to start the search with.  Format: YYYYMMDD
    * toDate: Date to end the search with.  Format: YYYYMMDD
    * fields: Name of fields to return. See models.UsageInstrumentation for the full
            list. This parameter is needed for aggregation.
    * actionType: The type of action that triggered the metric.
            The only possible value now is: PROBE_SEARCH
    * agg: The "Aggregate" flag. The only possible value now is: count. Note that if
           "fields" is not supplied, this parameter is ignored.
    """
    if request.method == "GET":
        dimensions = []

        if q_action_type := request.GET.get("actionType"):
            dimensions.append(Q(action_type=q_action_type))
        if q_from := request.GET.get("fromDate"):
            min_date = dateutil.parser.parse(q_from)
            dimensions.append(Q(timestamp__gte=min_date))
        if q_to := request.GET.get("toDate"):
            max_date = dateutil.parser.parse(q_to)
            dimensions.append(Q(timestamp__lte=max_date))

        result = UsageInstrumentation.objects.filter(*dimensions)

        if q_fields := request.GET.get("fields"):
            fields = q_fields.split(",")
            response = result.values(*fields)
            if request.GET.get("agg") == "count":
                response = response.annotate(total=Count("*")).order_by("-total")
        else:
            response = result.values("action_type", "timestamp", "probe_name")
        return Response(response, 200)