Skip to content

Instantly share code, notes, and snippets.

@juslintek
Last active March 21, 2018 17:31
Show Gist options
  • Save juslintek/293f016dceb4b20a42df4f1ceb738f9f to your computer and use it in GitHub Desktop.
Save juslintek/293f016dceb4b20a42df4f1ceb738f9f to your computer and use it in GitHub Desktop.
(function ($) {
/**
* @var object uptimeArgs
* @var object monitor = {
* all_time_uptime_ratio: "100.000",
* average_response_time: "94.647",
* create_datetime: 1501761134,
* friendly_name: "Hostinger",
* http_password: "",
* http_username: "",
* id: 779282900,
* interval: 60,
* keyword_type: 2,
* keyword_value: "WordPress",
* port: "",
* response_times: [{datetime: 1520528001, value: 94}, {datetime: 1520527941, value: 235}],
* ssl: {brand: "", product: null, expires: 0},
* status: 2,
* sub_type: "",
* type: 2,
* url: "http://www.dummyonline.xyz/"
* };
*
*/
var shouldUpdate = false,
uptimePlaceholder = $('.tudosobresites_hosting .uptime, .tudosobresites_hosting .response-time'),
updateUptime = function (firstRun) {
firstRun = typeof firstRun === 'undefined' ? false : firstRun;
var providers = [],
foundProviders = [],
notFoundProviders = [];
uptimePlaceholder.each(function () {
var currentProvider = $(this).data('provider');
if ($.inArray(currentProvider, providers) === -1) {
providers.push(currentProvider);
}
});
shouldUpdate = false;
$.ajax({
data: {
'action': 'show_uptime',
'providers': providers
},
url: uptimeArgs.ajaxUrl,
type: 'post',
dataType: 'json',
beforeSend: function () {
},
error: function (jqXHR, textStatus, errorThrown) {
var message = typeof jqXHR.responseJSON !== 'undefined' ?
jqXHR.responseJSON.message :
'Server error: ' + textStatus + '. Server code: ' + errorThrown;
uptimePlaceholder.html('<div class="dslc-notification dslc-red">' + message + '</div>');
},
success: function (data) {
uptimePlaceholder.each(function (index, placeholder) {
var providerDataHolder = $(placeholder),
statusIndicator = $('<div class="status-indicator"></div>'),
providerName = providerDataHolder.data('provider'),
statusPageUrl = typeof data['status_page_url'] !== 'undefined' ?
data['status_page_url'] :
'https://status.hosting.review/';
statusIndicator.removeClass('paused not-checked-yet up seems-down down');
var monitor = data.monitors[providerName];
if (typeof monitor !== 'undefined') {
var uptimeRatio = monitor['all_time_uptime_ratio'] === '100.00' ?
Number(monitor['all_time_uptime_ratio']).toFixed(0) + '%' :
Number(monitor['all_time_uptime_ratio']).toFixed(2) + '%',
avgResponseTime = typeof monitor['average_response_time'] === 'string' ?
Number(monitor['average_response_time']).toFixed(0) + 'ms' :
'';
foundProviders.push(providerName);
if (firstRun) {
providerDataHolder.wrap('<a href="' + statusPageUrl + monitor.id +
'" class="wrapping-link black-font" rel="noopener nofollow" title="Go ' + providerName +
' monitor page" target="_blank"></a>');
}
switch (monitor.status) {
case 0:
statusIndicator.attr('data-tooltip', 'Server Uptime Monitoring is Paused').addClass('paused');
uptimeRatio = 'Paused';
avgResponseTime = 'Paused';
break;
case 1:
statusIndicator.attr('data-tooltip', 'Server Status not checked yet').addClass('not-checked-yet');
break;
case 2:
statusIndicator.attr('data-tooltip', 'Server is Up and Running').addClass('up');
break;
case 8:
statusIndicator.attr('data-tooltip', 'Server seems to be down').addClass('seems-down');
break;
case 9:
statusIndicator.attr('data-tooltip', 'Server is Down').addClass('down');
uptimeRatio = 'Down';
avgResponseTime = 'Down';
break;
}
var upTimeHtml = '<span class="uptime-ratio" data-tooltip="Uptime Ratio">' + uptimeRatio + '</span>',
avgResponseTimeHtml = '<span class="uptime-ratio" data-tooltip="Response Time">' + avgResponseTime + '</span>';
if (providerDataHolder.hasClass('response-time')) {
providerDataHolder.html(avgResponseTimeHtml).prepend(statusIndicator);
} else if (providerDataHolder.hasClass('uptime')) {
providerDataHolder.html(upTimeHtml).prepend(statusIndicator);
}
notFoundProviders = providers.filter(function (provider) {
return $.inArray(provider, foundProviders) === -1;
});
} else {
providerDataHolder.html('<span class="uptime-ratio" data-tooltip="Uptime Ratio">Unavailable</span>')
.prepend('<div class="status-indicator paused"></div>');
}
});
}
});
};
$(document).ready(function () {
setInterval(function () {
updateUptime();
}, 60000);
updateUptime(true);
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment