Repository: getredash/redash

Issue:
Improve tag list ordering on query and dashboard list
<!--
#####################################################################
#
# Need support? USE THE FORUM! https://discuss.redash.io/c/support.
#
# Don't have steps to reproduce and actually not sure it's a bug?
# Use the forum! https://discuss.redash.io/c/support.
#
#####################################################################

**Got an idea for a new feature?** Check if it isn't on the roadmap already: http://bit.ly/redash-roadmap and start a new discussion in the features category: https://discuss.redash.io/c/feature-requests 🌟.

Found a bug? Please fill out the sections below... thank you 👍
-->

### Issue Summary

On the queries and dashboard list view the tags are ordered by usage count, but the sort order is ascending, meaning the tags with the *least* usage are shown on the top, the tags with the most tagged items on the bottom of the list.

I would suggest to do that to descending.

In terms of visualization, I wonder if it would make sense to show the number of items that individual tags have been assigned to?

Our QA team has filed a related issue to sort the tags alphabetical: https://github.com/mozilla/redash/issues/481

### Steps to Reproduce

1. Go to either queries or dashboard list with some tags assigned
2. See the list of tags being shown with the least tagged items first, not last.

### Technical details:

* Redash Version: 5.0beta
* Browser/OS: Firefox 62 / macOS 10.13.6
* How did you install Redash: Docker



Discussion:
"I wonder if it would make sense to show the number of items that individual tags have been assigned to?"

We could just use tag (x) to show how many items has this tag. I like the idea but now sure if this would be actually useful. WDYT @arikfr 
I think it's useful to show the number of uses of a tag, to help direct users into what tags to use. Because if two tags have similar meaning but one is more popular, it will direct people in the right direction.

About whether to use popularity order or alphabetical one -- I'm not sure. Both makes sense, but for sure we should at least reverse the current order (major fail on my part 🤦‍♂️ ). 

Resolving patch:
diff --git a/redash/handlers/dashboards.py b/redash/handlers/dashboards.py
--- a/redash/handlers/dashboards.py
+++ b/redash/handlers/dashboards.py
@@ -291,10 +291,20 @@ def delete(self, dashboard_id):
             'object_type': 'dashboard',
         })
 
+
 class DashboardTagsResource(BaseResource):
     @require_permission('list_dashboards')
     def get(self):
         """
         Lists all accessible dashboards.
         """
-        return {t[0]: t[1] for t in models.Dashboard.all_tags(self.current_org, self.current_user)}
+        tags = models.Dashboard.all_tags(self.current_org, self.current_user)
+        return {
+            'tags': [
+                {
+                    'name': name,
+                    'count': count,
+                }
+                for name, count in tags
+            ]
+        }
diff --git a/redash/handlers/queries.py b/redash/handlers/queries.py
--- a/redash/handlers/queries.py
+++ b/redash/handlers/queries.py
@@ -382,4 +382,16 @@ def post(self, query_id):
 
 class QueryTagsResource(BaseResource):
     def get(self):
-        return {t[0]: t[1] for t in models.Query.all_tags(self.current_user, True)}
+        """
+        Returns all query tags including those for drafts.
+        """
+        tags = models.Query.all_tags(self.current_user, include_drafts=True)
+        return {
+            'tags': [
+                {
+                    'name': name,
+                    'count': count,
+                }
+                for name, count in tags
+            ]
+        }


Test patch: