Combine data from multiple jobs in dashing

551 views Asked by At

I have Dashing working and now want to add a few 'combined' widgets.

So a widget that adds up numbers from (for example) Facebook and Twitter. I also have two separate widgets, one for Facebook and one for Twitter.

I can just make one job named twitter+facebook.rb and that would solve the problem, but I now want to add a third and a fourth source. I don't want to end up with one big jobs file, but a twitter.rb and google-analytics.rb and facebook.rb and mailcimp.rb etc. combined with a (for example) calaculation.rb.

Is it possible to pass values between jobs?

2

There are 2 answers

0
wvengen On

I do not see a method within Dashing for derived values at the moment.

One way to achieve this right now would be monkey-patching send_event to compute a derived value. Having defined your source-specific jobs, add a job for computing the combined data with something like this:

# List of event ids you use to compute the combined value
INPUT_VALUES=%w(facebook_likes twitter_followers)

# This method is run when one of INPUT_VALUES changes
def compute_combined_social(values)
  value = values['facebook_likes']['current'] +
    values['twitter_followers']['current']
  send_event('social-combined', {current: value})
end

# Hook send_event to update aggregates when depending values change
alias :orig_send_event :send_event
def send_event(id, body, target=nil)
  r = orig_send_event(id, body, target)
  if INPUT_VALUES.include? id
    # get current value for each input value
    values = Hash[INPUT_VALUES.map do |id|
      [id, YAML.load(Sinatra::Application.settings.history[id])['data']]
    end]
    compute_combined_social values
  end
  r
end

If there's enough interest, it would be useful to add a more general solution to Dashing itself.

0
tylermauthe On

You could make a widget that binds data from two sources and combines it in the presentation layer.