line / promgen

Promgen is a configuration file generator for Prometheus
https://line.github.io/promgen/
MIT License
1.04k stars 150 forks source link

Fix DatasourceList view #405

Closed vincent-olivert-riera closed 1 year ago

vincent-olivert-riera commented 1 year ago

The view was looping through the Shard objects using "project_set.count" as the argument for "dictsortreversed". This was causing an empty loop because "project_set.count" is not an attribute in the Shard objects.

"project_set" is a backwards relationship [1] created by Django, and "count" is a function that returns the number of objects in that relationship.

The reason why "project_set.count" was used in the view was to display the shards sorted by the number of projects, so the shards with most projects appear at the top, and the ones with less projects appear at the bottom.

To fix the issue we just create a "num_projects" annotation in the query that retrieves the shards, and use it as the argument for "dictsortreversed".

1: https://docs.djangoproject.com/en/dev/topics/db/queries/#backwards-related-objects