Skip to content

Instantly share code, notes, and snippets.

@MGTHEBOSS

MGTHEBOSS/README.md

Last active Sep 10, 2016
Embed
What would you like to do?
ntas-alerts-widget-web

ntas-alerts-widget-web

© Manojit Ghosh

#= require jquery
#= require es5-shim
#= require batman
#= require batman.jquery
Batman.Filters.prettyNumber = (num) ->
num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") unless isNaN(num)
Batman.Filters.dashize = (str) ->
dashes_rx1 = /([A-Z]+)([A-Z][a-z])/g;
dashes_rx2 = /([a-z\d])([A-Z])/g;
return str.replace(dashes_rx1, '$1_$2').replace(dashes_rx2, '$1_$2').replace(/_/g, '-').toLowerCase()
Batman.Filters.shortenedNumber = (num) ->
return num if isNaN(num)
if num >= 1000000000
(num / 1000000000).toFixed(1) + 'B'
else if num >= 1000000
(num / 1000000).toFixed(1) + 'M'
else if num >= 1000
(num / 1000).toFixed(1) + 'K'
else
num
class window.Dashing extends Batman.App
@on 'reload', (data) ->
window.location.reload(true)
@root ->
Dashing.params = Batman.URI.paramsFromQuery(window.location.search.slice(1));
class Dashing.Widget extends Batman.View
constructor: ->
# Set the view path
@constructor::source = Batman.Filters.underscore(@constructor.name)
super
@mixin($(@node).data())
Dashing.widgets[@id] ||= []
Dashing.widgets[@id].push(@)
@mixin(Dashing.lastEvents[@id]) # in case the events from the server came before the widget was rendered
type = Batman.Filters.dashize(@view)
$(@node).addClass("widget widget-#{type} #{@id}")
@accessor 'updatedAtMessage', ->
if updatedAt = @get('updatedAt')
timestamp = new Date(updatedAt * 1000)
hours = timestamp.getHours()
minutes = ("0" + timestamp.getMinutes()).slice(-2)
seconds = ("0"+timestamp.getSeconds()).slice(-2)
"Last updated at #{hours}:#{minutes}:#{seconds}"
@::on 'ready', ->
Dashing.Widget.fire 'ready'
receiveData: (data) =>
@mixin(data)
@onData(data)
onData: (data) =>
# Widgets override this to handle incoming data
Dashing.AnimatedValue =
get: Batman.Property.defaultAccessor.get
set: (k, to) ->
if !to? || isNaN(to)
@[k] = to
else
timer = "interval_#{k}"
num = if (!isNaN(@[k]) && @[k]?) then @[k] else 0
unless @[timer] || num == to
to = parseFloat(to)
num = parseFloat(num)
up = to > num
num_interval = Math.abs(num - to) / 90
@[timer] =
setInterval =>
num = if up then Math.ceil(num+num_interval) else Math.floor(num-num_interval)
if (up && num > to) || (!up && num < to)
num = to
clearInterval(@[timer])
@[timer] = null
delete @[timer]
@[k] = num
@set k, to
, 10
@[k] = num
Dashing.widgets = widgets = {}
Dashing.lastEvents = lastEvents = {}
Dashing.debugMode = false
source = new EventSource('/events')
source.addEventListener 'open', (e) ->
console.log("Connection opened")
source.addEventListener 'error', (e)->
console.log("Connection error")
if (e.readyState == EventSource.CLOSED)
console.log("Connection closed")
source.addEventListener 'message', (e) ->
data = JSON.parse(e.data)
if lastEvents[data.id]?.updatedAt != data.updatedAt
if Dashing.debugMode
console.log("Received data for #{data.id}", data)
lastEvents[data.id] = data
if widgets[data.id]?.length > 0
for widget in widgets[data.id]
widget.receiveData(data)
source.addEventListener 'dashboards', (e) ->
data = JSON.parse(e.data)
if Dashing.debugMode
console.log("Received data for dashboards", data)
if data.dashboard is '*' or window.location.pathname is "/#{data.dashboard}"
Dashing.fire data.event, data
$(document).ready ->
Dashing.run()
<!DOCTYPE html>
<!--404.html-->
<html>
<head>
<title>This Dashboard doesn't exist.</title>
<style>
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
div.dialog {
width: 25em;
padding: 0 4em;
margin: 4em auto 0 auto;
border: 1px solid #ccc;
border-right-color: #999;
border-bottom-color: #999;
}
h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
</style>
</head>
<body>
<!-- This file lives in public/404.html -->
<div class="dialog">
<h1>The Dashboard doesn't exist.</h1>
<p>You may have mistyped the address or the page may have moved.</p>
</div>
</body>
</html>
<style>
.center
{
margin-left:auto;
margin-right:auto;
}
</style>
<% content_for :title do %>Alerts Dashboard<% end %>
<div class="gridster">
<ul>
<li data-row="1" data-col="2" data-sizex="2" data-sizey="1">
<div data-id="no_of_alerts" data-view="Number" data-title="Alerts" data-moreinfo="Click on the NTAS signature to see current and expired alerts"></div>
</li>
</ul>
</div>
<br><br><br><br><br><br><br><br><br>
<div class="center">
<a href="http://www.dhs.gov/alerts" target="_blank"><img src="http://www.dhs.gov/xlibrary/graphics/ntas/dhs-ntas-logo.jpg" alt="National Terrorism Advisory System (NTAS). Check current status" /></a>
</div>
require 'rubygems'
require 'nokogiri'
require 'open-uri'
# :first_in sets how long it takes before the job is first run. In this case, it is run immediately.
SCHEDULER.every '2s', :first_in => 0 do |job|
doc = Nokogiri::XML(open("http://www.dhs.gov/ntas/1.0/alerts.xml"))
x=0
doc.xpath("//alert").each do
x+=1
end
send_event('no_of_alerts', { current: x })
end
<style>
.center
{
margin-left:auto;
margin-right:auto;
}
</style>
<% content_for :title do %>Alerts Dashboard<% end %>
<div class="gridster">
<ul>
<li data-row="1" data-col="2" data-sizex="4" data-sizey="2">
<div data-id="no_of_alerts" data-view="Number" data-title="Alerts" data-moreinfo="Click on the NTAS signature to see current and expired alerts"></div>
</li>
</ul>
</div>
<br><br><br><br><br><br><br><br><br>
<div class="center">
<a href="http://www.dhs.gov/alerts" target="_blank"><img src="http://www.dhs.gov/xlibrary/graphics/ntas/dhs-ntas-logo.jpg" alt="National Terrorism Advisory System (NTAS). Check current status" /></a>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment