Metrics

Below is an example configuration of a panel using the Metrics module:

{
    "width": 12,
    "height": 180,
    "full_row": True,
    "url_name": "frame_empty_frame",
    "url_action_name": "metrics_survey",
    "content": {
        "view": custom_views.MonitorMetrics,
        "view_params": {
            "model": custom_models.Monitor,
            "group_by": ["survey", "surveygroup__name"],
            "aggregations": [
                {
                    "function": "Count",
                    "value": "id",
                },  # annotation
            ],
        },
    },
},

The view_params are:

  • model the model this table is based on

  • group_by and order_by determine how the data is organized.

  • aggregations specify how to summarize the data (e.g., counting records).

The custom_views can be defined as followed:

class MonitorMetrics(module_views.Kpi):
    def set_context(self, request):
        datasets = []
        surveys = []
        surveygroup__names = []

        queryset = self.get_queryset()
        if queryset:
            for item in queryset:
                if item["survey"]:
                    surveys.append(item["survey"])
                if item["surveygroup__name"]:
                    surveygroup__names.append(item["surveygroup__name"])

        datasets.append(
            {
                "width": 6,
                "value": "Surveys",
                "label": ", ".join(sorted(set(surveys))),
            }
        )
        datasets.append(
            {
                "width": 6,
                "value": "Survey Groups",
                "label": ", ".join(sorted(set(surveygroup__names))),
            }
        )
        self.context = {"metrics_new": datasets}

This custom_views can generate the card with the surveys and survey group.

Formatting You can use formatting functions from str_utils for nicer visuals.

Last updated