Skip to content

Instantly share code, notes, and snippets.

@jwalton
Last active October 13, 2022 07:03
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwalton/6616670 to your computer and use it in GitHub Desktop.
Save jwalton/6616670 to your computer and use it in GitHub Desktop.
Remote-reload widget for Dashing

So, you made a change to your dashboard, and now you have to run all over the building, plugging in a keyboard, and reloading the dashboard on the various TVs in your office. What if you could reload all the dashboard from the comfort of your desk?

Add this widget to the very bottom of your dashboard, after the "gridster" div:

<div class="gridster">
  <ul>
    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
      <div data-view="Image" data-image="/benbria/loop.png" style="background-color:#666766"></div>
    </li>

    <!-- Blah blah blah - widgets go here -->

  </ul>
</div>

<!-- Special reload widget, doesn't display on the page. -->
<div data-id="reload" data-view="RemoteReload" style="padding: 0px"></div>

Note the style="padding: 0px"; this is important to make the widget take up no space on the page. Now you can use curl to remotely force all dashboards to reload their page:

curl -d '{ "auth_token": "YOUR_AUTH_TOKEN" }' \http://mydashboard.server.com/widgets/reload

You can also install this across all your dashboards by modifying layout.erb:

<div id="container">
  <%= yield %>
  <div data-id="reload" data-view="RemoteReload" style="padding: 0px"></div>
</div>
class Dashing.RemoteReload extends Dashing.Widget
# Wait a short while between reloads. Sometimes dashing will send a `onData()` event
# right when we startup, so without this we can get into a reload-forever loop.
minTimesBetweenReload: 1000 * 5 # 5 seconds
ready: ->
@lastLoad = Date.now()
@initialized = true
# Handle new data from Dashing.
onData: (data) ->
if !@initialized or (Date.now() < (@lastLoad + @minTimesBetweenReload))
console.log "Too soon to reload. Wait #{(@lastLoad + @minTimesBetweenReload - Date.now()) / 1000} seconds."
else if data.url
location.replace(data.url)
else
location.reload()
<!-- This file intentionally left blank -->
@jwalton
Copy link
Author

jwalton commented Sep 19, 2013

Ugh. This "command" thing is a bit of a hack. Without it, though, I've run into cases where we see the dashboard reload continuously.

I wonder if we could store a timestamp when "ready()" gets called, and then not do the reload unless onData() is called more than 30 (?) seconds afterwards? This way an onData() from Dashing right at load time would be ignored (you probably don't want to reload the dashboard if it was just reloaded anyways.) I suspect this would mostly work, but likely would force a dashboard reload if the dashing server ever goes down and comes back up (although that's maybe not a bad thing...) Thoughts?

@orenkatz
Copy link

After adding it, it's add the following text:
"
["str", [Tilt::StringTemplate]]["erb", [Tilt::ErubisTemplate, Tilt::ERBTemplate]]["rhtml", [Tilt::ErubisTemplate, Tilt::ERBTemplate]]["erubis", [Tilt::ErubisTemplate]]["etn", [Tilt::EtanniTemplate]]["etanni", [Tilt::EtanniTemplate]]["haml", [Tilt::HamlTemplate]]["mab", [Tilt::MarkabyTemplate]]["liquid", [Tilt::LiquidTemplate]]["radius", [Tilt::RadiusTemplate]]["markdown", [Tilt::RedcarpetTemplate, Tilt::RedcarpetTemplate::Redcarpet2, Tilt::RedcarpetTemplate::Redcarpet1, Tilt::RDiscountTemplate, Tilt::BlueClothTemplate, Tilt::KramdownTemplate, Tilt::MarukuTemplate]]["mkd", [Tilt::RedcarpetTemplate, Tilt::RedcarpetTemplate::Redcarpet2, Tilt::RedcarpetTemplate::Redcarpet1, Tilt::RDiscountTemplate, Tilt::BlueClothTemplate, Tilt::KramdownTemplate, Tilt::MarukuTemplate]]["md", [Tilt::RedcarpetTemplate, Tilt::RedcarpetTemplate::Redcarpet2, Tilt::RedcarpetTemplate::Redcarpet1, Tilt::RDiscountTemplate, Tilt::BlueClothTemplate, Tilt::KramdownTemplate, Tilt::MarukuTemplate]]["textile", [Tilt::RedClothTemplate]]["rdoc", [Tilt::RDocTemplate]]["wiki", [Tilt::WikiClothTemplate, Tilt::CreoleTemplate]]["creole", [Tilt::CreoleTemplate]]["mediawiki", [Tilt::WikiClothTemplate]]["mw", [Tilt::WikiClothTemplate]]["ad", [Tilt::AsciidoctorTemplate]]["adoc", [Tilt::AsciidoctorTemplate]]["asciidoc", [Tilt::AsciidoctorTemplate]]["html", [Tilt::PlainTemplate]]
"

The reload is working but any idea how to remove the text?
Thanks!

@ryanvito
Copy link

@orenkatz I had the same exact problem. I modified the style on that widget to make it display:none and that solved the issue for me.

@terraboops
Copy link

You don't need a custom widget for this:

curl -i -d '{"auth_token": "MY_AUTH_TOKEN", "event": "reload"}' http://dashing:3030/dashboards/mydashboard

or, in job format:

require 'uri'
require 'net/http'

uri = URI.parse('http://dashing:3030')
http = Net::HTTP.new(uri.host, uri.port)

SCHEDULER.every '30s' do
    data = '{"auth_token": "AUTH_TOKEN", "event": "reload"}'
    http.post('/dashboards/mydashboard', data)
end

@terraboops
Copy link

A better way that can be run from within a job:

send_event('refresh_sample', {event: 'reload', dashboard: 'sample'}, 'dashboards')

You can replace refresh_sample with anything you want, but you must replace dashboard: 'sample' with the name of the dashboard you wish to reload.

@jorgemorgado
Copy link

Here is a tip that will make this widget much simpler. Create a new file in the widget's directory like:

.widget-remote-reload {
  display: none !important;
  padding: 0px;
}

Now, remove the style attribute from the div tag when calling the widget. Simple and effective.

Copy link

ghost commented Mar 31, 2016

How do i get this to work? I get the following when trying to use CURL from Windows...

c:\cURL\bin>curl -d '{ "auth_token": "MyTokenHere" }' \http://MyIPHere:3030/widgets/reload
curl: (6) Could not resolve host: auth_token
curl: (6) Could not resolve host: MyTokenHere
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (1) Protocol "\http" not supported or disabled in libcurl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment