BuddhaNexus / buddhanexus-frontend-next

BuddhaNexus Frontend 2.0
buddhanexus-frontend-next.vercel.app
2 stars 1 forks source link

Refactor Numbers View endpoint #126

Closed aminahbl closed 6 months ago

aminahbl commented 7 months ago

The current numbers view return is an array of objects like this:

{
  "segmentnr": "dn4:1.1_0",
  "parallels": [
    ["dn5:0.3_0", "dn5:1.1_0", "dn5:1.2_0"],
    {
      "segments": ["sn7:11.1.1_0", "sn7:11.1.2_0"],
      "text_name": "Sn 7",
      "link1": null,
      "link2": null
    },
   …
  ]
}

There are a few issues with this, among them:

Proposal

numbers-view/collections

Stable table headers can be created with a new numbers-view/collections endpoint. If:

[
    {
      "id": "mn",
      "displayname": "Majjhima Nikaya",
    },
    {
      "id": "ud",
      "displayname": "Udana",
    },
    ...
]

numbers-view/numbers

Match data will return an array of objects containing segment and parallels props. Both will contain data necessary to build links to segment text (filename and segmentnr) and parallel objects will also have a collection prop so the parallel can be linked to the correct table column, and a displayname prop for hover-over / focus element info.

[
  {
    "segment": { "segmentnr": "mn47:1.1", "filename": "mn47" },
    "parallels": [
      {
        "filename": "dn7",
        "segmentnr": "dn7:1.2-1.2",
        "displayname": "Jāliya Sutta",
        "collection": "dn"
      },
      {
        "filename": "dn31",
        "segmentnr": "dn31:1.2-1.2",
        "displayname": "Siṅgāla Sutta",
        "collection": "dn"
      },
    ]
  },
  {
    "segment": { "segmentnr": "mn47:3.2", "filename": "mn47" },
    "parallels": []
  },
  {
    "segment": { "segmentnr": "mn47:3.4", "filename": "mn47" },
    "parallels": [
      {
        "filename": "mn55",
        "segmentnr": "mn55:1.2-1.2",
        "displayname": "Jīvaka Sutta",
        "collection": "mn"
      },
    ]
   }
]

The endpoint will remain paged. To help calculate table size it will be helpful if a total item count is returned in the response. There is a FastAPI plugin (https://uriyyo-fastapi-pagination.netlify.app/tutorials/page-number-pagination/) that might make this relatively straightforward, but if returning a total is overly complicated, an alternative solution can be sought.

ayya-vimala commented 7 months ago

I can see if I can make these endpoints work in the backend.

ayya-vimala commented 6 months ago

@aminahbl ... would an output like this work for you?

  {
    "dn4:1.0": [
        {
            "display_name": "Kūṭadanta Sutta",
            "file_name": "dn5",
            "category": "dn",
            "segmentnr": "dn5:1.1-1.2"
        }
    ],
    "dn4:1.1": [
        {
            "display_name": "Kūṭadanta Sutta",
            "file_name": "dn5",
            "category": "dn",
            "segmentnr": "dn5:1.1-1.2"
        },
        {
            "display_name": "Sāleyyaka Sutta",
            "file_name": "mn41",
            "category": "mn",
            "segmentnr": "mn41:1.2"
        },
        {
            "display_name": "Gopālaka Sutta",
            "file_name": "ud4.3",
            "category": "ud",
            "segmentnr": "ud4.3:1.2"
        },
        {
            "display_name": "Sela Sutta",
            "file_name": "mn92",
            "category": "mn",
            "segmentnr": "mn92:1.2"
        }
    ],
    "dn4:1.2": [
        {
            "display_name": "Kūṭadanta Sutta",
            "file_name": "dn5",
            "category": "dn",
            "segmentnr": "dn5:1.1-1.2"
        },
        {
            "display_name": "Sāleyyaka Sutta",
            "file_name": "mn41",
            "category": "mn",
            "segmentnr": "mn41:1.2"
        },
        {
            "display_name": "Gopālaka Sutta",
            "file_name": "ud4.3",
            "category": "ud",
            "segmentnr": "ud4.3:1.2"
        },
        {
            "display_name": "Sela Sutta",
            "file_name": "mn92",
            "category": "mn",
            "segmentnr": "mn92:1.2"
        }
    ]
}

Having the filename for the root segment seemed a bit superfluous because you ask it to get the numbers for that specific filename.

ayya-vimala commented 6 months ago

Then next to that I made a collections query that just returns all the collections for that language. If that is too much (as some collections might be empty), I can also add a list of the collections used per page so you can compare both of those and just use the ones that are needed. Then with the next page you can add both together.

I also made the number of entries per page to 500 (was 100). If this becomes too slow, let me know and I can change that.