Skip to content

Commit

Permalink
Merge pull request #536 from sarco3t/ws-status-updating
Browse files Browse the repository at this point in the history
added update [standby] on /status through ws #504
  • Loading branch information
ArtOfCode- committed Dec 13, 2018
2 parents 481eef3 + 47b7808 commit 95d0134
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 13 deletions.
11 changes: 11 additions & 0 deletions app/channels/application_cable/connection.rb
Expand Up @@ -3,5 +3,16 @@
# Be sure to restart your server when you modify this file. Action Cable runs in a loop that does not support auto reloading.
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user

def connect
self.current_user = find_current_user
end

private

def find_current_user
cookies.signed['user.expires_at'] && cookies.signed['user.expires_at'] > Time.now && User.find_by(id: cookies.signed['user.id'])
end
end
end
6 changes: 5 additions & 1 deletion app/channels/status_channel.rb
Expand Up @@ -2,6 +2,10 @@

class StatusChannel < ApplicationCable::Channel
def subscribed
stream_from 'status'
if current_user&.has_role?(:code_admin)
stream_from 'status_code_admin'
else
stream_from 'status'
end
end
end
29 changes: 24 additions & 5 deletions app/controllers/status_controller.rb
Expand Up @@ -23,11 +23,8 @@ def status_update
@smoke_detector.save!

Thread.new do
ActionCable.server.broadcast 'status', id: @smoke_detector.id,
ts_unix: @smoke_detector.last_ping.to_i,
ts_ago: time_ago_in_words(@smoke_detector.last_ping, include_seconds: true),
ts_raw: @smoke_detector.last_ping.to_s,
location: @smoke_detector.location
ActionCable.server.broadcast 'status', status_channel_data
ActionCable.server.broadcast 'status_code_admin', status_channel_data.merge(failover_link: failover_link)
ActionCable.server.broadcast 'topbar', last_ping: @smoke_detector.last_ping.to_f
ActionCable.server.broadcast 'smokey_pings', smokey: @smoke_detector.as_json
end
Expand All @@ -49,4 +46,26 @@ def kill
flash[:success] = 'Kill command sent. I hope you know what you\'re doing.'
redirect_to status_path
end

private

def status_channel_data
{
id: @smoke_detector.id,
ts_unix: @smoke_detector.last_ping.to_i,
ts_ago: time_ago_in_words(@smoke_detector.last_ping, include_seconds: true),
ts_raw: @smoke_detector.last_ping.to_s,
location: @smoke_detector.location,
is_standby: @smoke_detector.is_standby,
active: active?
}
end

def failover_link
active? && @smoke_detector.is_standby && smoke_detector_force_failover_path(@smoke_detector.id)
end

def active?
@active ||= @smoke_detector.last_ping > 3.minutes.ago
end
end
22 changes: 21 additions & 1 deletion app/javascript/cable/status.js
Expand Up @@ -4,20 +4,40 @@ import cable from './cable';

const debug = createDebug('ms:status');

const failoverElement = link => {
return `<a class="text-danger"
data-confirm="This will take effect at the next ping, within a minute. Sure?" rel="nofollow" data-method="post" href="${link}">
Failover
</a>`;
};

let statusSocket;
route('/status', () => {
statusSocket = cable.subscriptions.create('StatusChannel', {
received(data) {
debug('received', data);

const { id, ts_unix: tsUnix, ts_ago: tsAgo, ts_raw: tsRaw, location } = data;
const { id, ts_unix: tsUnix, ts_ago: tsAgo, ts_raw: tsRaw, location, is_standby: isStandby, active, failover_link: failoverLink } = data;
if ([id, tsUnix, tsAgo, tsRaw, location].some(x => x === null || x === undefined)) {
return;
}

const row = $(`.status-row[data-id=${id}]`);
row.find('.location-cell').find('.location').text(location);

const standByLabel = row.find('.location-cell').find('.label');
if (isStandby) {
standByLabel.show();
const newLabelClass = `label-${active ? 'primary' : 'default'}`;
standByLabel.removeClass((_, classNames) => (classNames.match(/label-\S+/) || []).join(' '));
standByLabel.addClass(newLabelClass);
}
else {
standByLabel.hide();
}

row.children('td').last().html(failoverLink ? failoverElement(failoverLink) : '');

const pingCell = row.find('.ping-cell');
pingCell.attr('data-livestamp', tsUnix).attr('title', tsRaw);
pingCell.text(`${tsAgo} ago`);
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/packs/application.js
Expand Up @@ -101,14 +101,14 @@ onLoad(() => {
const requestId = `${dedupUuid}/${hashCode(data)}`;
xhr.setRequestHeader('X-AJAX-Deduplicate', requestId);
});

const reviewCounter = $('.reviews-count');
if (reviewCounter.length > 0 && parseInt(reviewCounter.text().trim(), 10) > 50) {
const reviewAlertedAt = parseInt(localStorage['ms-review-alerted-at'] || 0, 10);
const diff = (Date.now() - reviewAlertedAt) / 1000;
if (diff >= 14400) {
reviewCounter.attr('data-toggle', 'tooltip').attr('data-placement', 'bottom')
.attr('title', 'Got 5 minutes to do 10 reviews?');
.attr('title', 'Got 5 minutes to do 10 reviews?');
reviewCounter.tooltip('show');
localStorage['ms-review-alerted-at'] = Date.now().toString();
}
Expand Down
6 changes: 2 additions & 4 deletions app/views/status/index.html.erb
Expand Up @@ -17,10 +17,8 @@
<% end %>
<td class="location-cell">
<%= link_to smoke_detector_statistics_path(sd.id) do %>
<span class="location"><%= sd.location %></span>
<% if sd.is_standby %>
<span class="label label-<%= sd.last_ping > 3.minutes.ago ? "primary" : "default" %> ">Standby</span>
<% end %>
<span class="location"><%= sd.location %></span>
<span class="label label-<%= sd.last_ping > 3.minutes.ago ? "primary" : "default" %> " style="display: <%= sd.is_standby ? 'inline' : 'none'%>">Standby</span>
<% end %>
</td>
<td class="status-<%= sd.status_color %> ping-cell" data-livestamp="<%= sd.last_ping.to_i %>" title="<%= sd.last_ping.to_s %>">
Expand Down
13 changes: 13 additions & 0 deletions config/initializers/warden_hooks.rb
@@ -0,0 +1,13 @@
# frozen_string_literal: true

Warden::Manager.after_set_user do |user, auth, opts|
scope = opts[:scope]
auth.cookies.signed["#{scope}.id"] = user.id
auth.cookies.signed["#{scope}.expires_at"] = 30.minutes.from_now
end

Warden::Manager.before_logout do |_user, auth, opts|
scope = opts[:scope]
auth.cookies.signed["#{scope}.id"] = nil
auth.cookies.signed["#{scope}.expires_at"] = nil
end

0 comments on commit 95d0134

Please sign in to comment.