Retrieve list of favorite dashboard/look contents for each user

Retrieving a list of favorited content for each user, via the Python SDK (Admin access required)

Click the “Show Content” buttons to see the relevant picture.

Update: Thoroughly improved version of the environment available here

Background:

It can be important to understand what your users’ are favoriting.

That is the case whether you need to cut down on existing content and want to know which content is more highly prized, or you are creating a new and better dashboard, and want to know who has favorited the old dashboard so you can inform them.

However, Looker’s methods of retrieving your user’s favorited contents are severely limited. If you are a normal user, you can only see your own favorited content. Which might be fine for you.. but if you are an admin, you also only see your own favorite content? Whatever arcane reason led to this situation, there is a way to get the full list. But you DO need to be an admin for it.

 

The HOW:

First, we need to be an admin.

After that, we can create an API ID/Secret for ourselves.  

And after that… we create our development environment and get to work tinkering away. (Breakdown shown below if you can’t view the page)

In the above link, I have shared my completed work environment and method for solving this issue, but lets continue.

If we use the looker api explorer to search for sdk related to favorite contents, and test different content ids we quickly find that you can only retrieve favorite contents for the current user. This is why we need to be an admin for this workaround. The fix is that we need to loop authenticating as every single user in the instance and perform the sdk call as each user.

Breakdown of the SDK:

In order to get the list of favorited content we need to perform 4 actions:

  1. Create a list of all users in your instance
  2. Create a list of all dashboards (and/or boards/looks etc.)
  3. Create a function that checks all favorited content for the current user
  4. Create a loop that returns matches when logging into every user in the user_list and checking all content in the content list.

Environment Construction (With pictures):

Below I will detail the methods with picture, but because pictures are big, they are hidden by default. You can click on the button below to show them.
I HIGHLY RECOMMEND COPYING THE ENVIRONMENT TO YOUR OWN gDRIVE. HOWEVER, ANYTHING YOU WRITE IN THE NOTEBOOK IS NOT ACCESSIBLE BY ANY OTHER USER.

First, there are two main phases to creating the environment, the first on is getting the SDK Initialization working, and the second is to get the code functional.

d7143763-b0d2-4bbb-aca3-46e43e3e030f.png
Colab Summary

If we open up the “Run me to initialize SDK” section, we can see there are a few code snippets broken up. In shared Colab environment, I have added the parameters we need to adjust on the right-hand side to make them easy to setup.
The code is a little updated from the displayed code below, but is functionally the same.

f4c0efd1-836a-4225-a5b3-c631bdfeff29.png
50750e50-bb2b-4f2e-ad55-f011c39b0706.png

After you have each of the form fields to your specifications (NOTE: you really only need to change the base_url, client_id and client_secret), we can get to the SDK calling.
I am not a Python savant, so there may be a better way, and please let me know if there is one!

Borrowing from our above list:

  1. Create a list of all users in your instance
    user_list = dict()
    users = sdk.all_users()
    for user in users:
    if user['email'] and not user['is_disabled']: #removing users that don't have emails setup and aren't disabled
    user_list[user['email']] = user['id']
    print(user_list)

    The first few code snippets don’t check if you are actually correctly authenticated in, so you might get some errors at this point.
    If everything works out, you should see a printed list, and you can open the variables tab to see the full list of users.

    1d2c24dd-2775-42e7-9f12-1610c3b04e4c.png
  2. Create a list of all dashboards (and/or boards/looks etc.)
    dashboard_list = dict()
    dashboards = sdk.all_dashboards()
    for dashboard in dashboards:
    if dashboard['content_metadata_id']: #removing dashboards without content_metadata_id
    dashboard_list[dashboard['title']] = dashboard['content_metadata_id']
    print(dashboard_list)

    You can use the same method as above to check if the dashboards are getting retrieved.

  3. Create a function that checks all favorited content for the current user
    favorited_content = dict()

    def retrieve_favorite_content(): #Create a function so we can loop it for each user
    for k,v in dashboard_list.items():
    response = sdk.search_content_favorites(content_metadata_id=v)
    if response:
    email = [k for k,v in user_list.items() if v == response[0]['user_id']][0] #Change user_id into email for data readability
    dashboard = [k for k,v in dashboard_list.items() if v == response[0]['content_metadata_id']][0] #Change content_metadata_id into dashboard name for data readability
    if email in favorited_content: #If we already have a favorited content, we need to append the new content
    favorited_content[email].append(dashboard)
    else: #If we don't have any favorited content yet, we add the new content as a list
    favorited_content[email] = []
    favorited_content[email].append(dashboard)
  4. Create a loop that returns matches when logging into every user in the user_list and checking all content in the content list.
    for k,v in user_list.items():
    sdk.auth.logout() #This is required if you stopped the sdk mid-way
    sudo_auth = sdk.login_user(v)
    sdk.auth.login_user(v)
    retrieve_favorite_content()
    print(favorited_content)

    Before running this.. I must say that it takes a LONG time. I ran this for 30 minutes on my instance to retrieve the values, and it might take much longer if you have a bigger instance. Hopefully someone can mention a more performant way in the comments…
    Having said that, you can test that it is working correctly, by stopping at any time, or during the process you can check the favorited_contents dictionary in the variables view.

    66597291-29aa-4828-91bb-daacf039531f.png

Conclusions:

The above processes’ setup shouldn’t take more than a few minutes. However, the SDK call is unfortunately very long because of the looping through each user and then each content. Again, probably a better way to do this, and if someone shares it I will update this. 
However, it IS possible to get the information in exchange for runtime. From there, you can copy/paste the favorited_contents dictionary, or you can add a write_file method etc. 
 

0 1 860
1 REPLY 1

The above method has been thoroughly improved, and now takes under 3 minutes for a 150 user size instance. Additionally, and retrieves all the favorited content and separates them into different dictionaries, not just dashboards as was in the above article. Link