+function versionLog(msg) {
+ document.getElementById('versionCheckLog').innerHTML = msg;
+}
+
+function getNetdataVersion(callback) {
+ versionLog('Downloading installed version info from netdata...');
+
+ $.ajax({
+ url: 'version.txt',
+ async: true,
+ cache: false
+ })
+ .done(function(data) {
+ data = data.replace(/(\r\n|\n|\r)/gm,"")
+ console.log(data.length);
+ versionLog('Installed version of netdata is ' + data);
+ document.getElementById('netdataVersion').innerHTML = data;
+ callback(data);
+ })
+ .fail(function() {
+ versionLog('Failed to download installed version info from netdata!');
+ callback(null);
+ });
+}
+
+function getGithubLatestCommit(callback) {
+ versionLog('Downloading latest version info from github...');
+
+ $.ajax({
+ url: 'https://api.github.com/repos/firehol/netdata/commits',
+ async: true,
+ cache: false
+ })
+ .done(function(data) {
+ versionLog('Latest version info from github is ' + data[0].sha);
+ callback(data[0].sha);
+ })
+ .fail(function() {
+ versionLog('Failed to download installed version info from github!');
+ callback(null);
+ });
+}
+
+function checkForUpdate(callback) {
+ getNetdataVersion(function(sha1) {
+ if(sha1 === null) callback(null, null);
+
+ getGithubLatestCommit(function(sha2) {
+ callback(sha1, sha2);
+ });
+ });
+
+ return null;
+}
+
+var updateBlinkCounter = 0;
+function notifyForUpdate(force) {
+ var now = new Date().getTime();
+
+ if(typeof force === 'undefined' || force !== true) {
+ var last = loadLocalStorage('last_update_check');
+
+ if(typeof last === 'string')
+ last = parseInt(last);
+ else
+ last = 0;
+
+ if(now - last < 3600000 * 8) {
+ // no need to check it - too soon
+ return;
+ }
+ }
+
+ checkForUpdate(function(sha1, sha2) {
+ var save = false;
+
+ if(sha1 === null) {
+ save = false;
+ versionLog('<p><big>Failed to get your netdata version!</big></p>');
+ }
+ else if(sha2 === null) {
+ save = false;
+ versionLog('<p><big>Failed to get the latest version from github.</big></p>');
+ }
+ else if(sha1 === sha2) {
+ save = true;
+ versionLog('<p><big>You already have the latest version of netdata!</big></p>');
+ }
+ else {
+ save = true;
+ var compare = 'https://github.com/firehol/netdata/compare/' + sha1.toString() + '...' + sha2.toString();
+
+ versionLog('<p><big><strong>You don\'t have the latest version of netdata!</strong></big></p><p>Latest version: ' + sha2.toString() + '</p><p><a href="' + compare + '" target="_blank">Click here for the changes log</a> since your installed version, and<br/><a href="https://github.com/firehol/netdata/wiki/Updating-Netdata" target="_blank">click here for directions on updating</a> your netdata installation.</p><p>We suggest to review the changes log for new features you may be interested, or important bug fixes you may need.<br/>Keeping your netdata updated is generally a good idea.</p>');
+
+ function updateButtonBlink() {
+ updateBlinkCounter--;
+ if(updateBlinkCounter > 0)
+ $('#updateButton').fadeOut(500).fadeIn(500, updateButtonBlink);
+ }
+
+ if(updateBlinkCounter === 0) {
+ updateBlinkCounter = 300;
+ updateButtonBlink();
+ }
+ }
+
+ if(save)
+ saveLocalStorage('last_update_check', now.toString());
+ });
+}
+
+// ----------------------------------------------------------------------------
+