onaio / onadata

Collect, Analyze and Share
https://ona.io
Other
183 stars 133 forks source link

Change response from charts `group by` endpoint #2179

Open FrankApiyo opened 2 years ago

FrankApiyo commented 2 years ago

Suggested Feature / Enhancement

Currently, when you make a request to the charts endpoint asking to group data by another field, response looks like this

{
  "data": [
    {
      "_submitted_by": "x",
      "items": [
        {
          "INTRO_DISTRICT": [
            "abcd"
          ],
          "count": 1
        },
        {
          "INTRO_DISTRICT": [
            "efgh"
          ],
          "count": 15
        },
        {
          "INTRO_DISTRICT": [
            "Ijkl"
          ],
          "count": 14
        },
        {
          "INTRO_DISTRICT": [
            "mnop"
          ],
          "count": 24
        },
        {
          "INTRO_DISTRICT": [
            "qrst"
          ],
          "count": 5
        }
      ]
    }
  ],
  "data_type": "categorized",
  "field_label": "Submission By",
  "field_xpath": "_submitted_by",
  "field_name": "_submitted_by",
  "field_type": "text",
  "grouped_by": "INTRO_DISTRICT",
  "xform": 2852
}

I wonder if it would be possible to modify it to look as follows

{
  "data":  [
        {
         "_submitted_by": "x",
          "INTRO_DISTRICT": [
            "abcd"
          ],
          "count": 1
        },
        {
          "_submitted_by": "x",
          "INTRO_DISTRICT": [
            "efgh"
          ],
          "count": 15
        },
        {
          "_submitted_by": "x",
          "INTRO_DISTRICT": [
            "Ijkl"
          ],
          "count": 14
        },
        {
         "_submitted_by": "x",
          "INTRO_DISTRICT": [
            "mnop"
          ],
          "count": 24
        },
        {
          "_submitted_by": "x",
          "INTRO_DISTRICT": [
            "qrst"
          ],
          "count": 5
        }
      ],
  "data_type": "categorized",
  "field_label": "Submission By",
  "field_xpath": "_submitted_by",
  "field_name": "_submitted_by",
  "field_type": "text",
  "grouped_by": "INTRO_DISTRICT",
  "xform": 2852
}

Benefits of implementing the feature/enhancement

we might need to rearrange the data in order to graph it properly with vega as follows https://gist.github.com/FrankApiyo/3add6159296d3fb2e36f73fb1d838269

denniswambua commented 2 years ago

@FrankApiyo This has a lot of duplication and i feel it can be easily achieved on the frontend.