jsonrubyassociative-arraydashing

JSON Summarize for Dashing List


I am creating a Dashing job that pulls down some data from an API (this runs Ruby)

SCHEDULER.every '15m', :first_in => 0 do |job|
    url = "https://blah.com
    response = RestClient.get(url, {:Authorization => 'blahblah'})
    current_timelist = JSON.parse(response)

    acctitems = current_timelist.map do |row|
    row = {
      :label => row['member']['name'], 
      :value => row['actualHours']
    }
    end

    # Update the List widget
      send_event('timelist', { items: acctitems } )
end

I want to summarize based on the member name, but it lists every entry.

The JSON that is received from the API looks as follows (I have shortened this, and changed the names only), note the actualHours can be 0:

[
    {
        "member": {
            "name": "User 1"
        },
        "actualHours": 0.2
    },
    {
        "member": {
            "name": "User 2"
        },
        "actualHours": 1.5
    },
    {
        "member": {
            "name": "User 2"
        },
        "actualHours": 0.17
    }
]

I would also like to sort this so that I can have the top member at the top ect. I would also like to send a second event with the top person in the list (so they can get a gold star).


Solution

  • Thanks to @Stanislav for his code as that formed the basis of the fix I managed to get working.

    SCHEDULER.every '15m', :first_in => 0 do |job|
    
      url = "website.com"
      response = RestClient.get(url, {:Authorization => 'BlahBlah'})
      current_timelist = JSON.parse(response)
    
      time = {}
      acctitems = current_timelist.map do |row|
        key = row['member']['name']
        value = row['actualHours']
    
        if time[key].nil?
          time[key] = [value]
        else
          time[key].push(value)
        end
      end 
    
      resulttime = time.map do |result|
      result = {
        :label => result[0],
        :value => result[1].inject(:+)
      }
      end
    
      # Update the List widget
      send_event('timelist', { items: resulttime })
    end