/* * Server Info scripts. * * requires: * common_js.js * tajax.js */ sinfo = {} /** * Logic behind a list of server infos. */ sinfo.list = {} /** * Creates a sinfo.list instance. Takes a variable list of sinfo.controller * instances as arguments. */ sinfo.list.create = function() { var x = {}; // member fields x.servers = new Array(); for (var i = 0; i < arguments.length; i++) { if (arguments[i]) { x.servers.push(arguments[i]); } } // member functions x.update = sinfo.list.update; x.bodyShow = sinfo.list.bodyShow; return x; } sinfo.list.update = function() { for (var i = 0; i < this.servers.length; i++) { var si = this.servers[i]; if (si) { si.update(); } } } sinfo.list.bodyShow = function() { js.debug("Showing bodies for " + this.servers.length + " servers"); for (var i = 0; i < this.servers.length; i++) { var si = this.servers[i]; if (si) { js.debug("showing body for [" + i + "]: " + si.view.title); si.view.bodyShow(); } } } /** * Creates the model & view during construction */ sinfo.createController = function(title, url, templateId) { if (! sinfo.__ids[title]) { sinfo.__ids[title] = 0; } sinfo.__ids[title] += 1; var id = title.replace(/\W/g, '_') + '_' + sinfo.__ids[title]; var model = sinfo.model.create(id); var view = sinfo.view.create(title, templateId); var controller = sinfo.controller.create(url, model, view); return controller; } sinfo.__ids = {} /* ========================================================================= * Model: defines what's stored and referenced by the view and controller. */ sinfo.model = {} sinfo.model.count = 0; /** * sinfo.model create(String url, String title, int width, int height) * * Creates a sinfo.model object that can be displayed, showing some information * from the server. Should not be invoked with "new". * * This object handles storing client-side data, and knows how to parse * the data from the server. * * @param id the identifier for the model, or null if you want one to be * generated for you. * @return new sinfo.model instance */ sinfo.model.create = function(id) { var x = {}; // member fields if (! id) { id = sinfo.__ids.length + 1; sinfo.__ids[id] = id; } x.index = sinfo.model.count++; x.id = id; // object methods //x.show = sinfo.show; x._parse = sinfo.model._parse; // default data from server x.data = '<p><i>No data retrieved yet</i></p>'; x.lastUpdate = 'Never'; return x; } /** * protected int parse(String data) * * Parses the response from the server for proper storage in the model. * Default behavior stores the data as-is, and sets the lastUpdate to now. * * @param data the data from the server. * @return 0 if there was no error, otherwise an error message code */ sinfo.model._parse = function(response) { if (response.error) { this.data = '<p><font color="red"><b>' + response.error + '</b></font></p>'; } else { this.data = response.text; } this.lastUpdate = (new Date()).toString(); } /* ========================================================================= * controller - knows how to handle communication between the model and the * viewer */ sinfo.controller = {} /** * public sinfo.controller create(String url, sinfo.model model, sinfo.view view) * * Creates the model & view during construction */ sinfo.controller.create = function(url, model, view) { var x = {}; x.model = model; x.view = view; x.update = tajax.createUpdate(url, 'sinfo.controller.__update_cb'); view._setup(model, x); return x; } /** * void __update_cb(data, si) * * Static callback on server response. */ sinfo.controller.__update_cb = function(data, si) { si.model._parse(data); si.view.update(); } /* ========================================================================= * view - knows how to render the model to the screen, and tie it in to the * controller. * * This implementation is a rudimentary implementation of a view. It just * shows the contents of the server file. */ sinfo.view = {} /** * public sinfo.view sinfo.view.create(String title) * * Create a new sinfo.view instance. * * @param title the title of the form * @return a new default sinfo.view instance */ sinfo.view.create = function(title, templateId) { var x = {} x.title = title; if (! templateId) { templateId = 'serverinfo-base'; } x.templateId = templateId; x.model = null; x.controller = null; // Member functions x._setup = sinfo.view._setup; x.bodyShow = sinfo.view.bodyShow; x._showTemplate = sinfo.view._showTemplate; x._getTemplateArguments = sinfo.view._getTemplateArguments; x._fillTemplateArguments = sinfo.view._fillTemplateArguments; x.update = sinfo.view.update; x._updateInner = sinfo.view._updateInner; return x; } /** * void sinfo.view._setup(sinfo.model, sinfo.controller) * * Package-protected method to initialize the view after construction * of the controller and model. */ sinfo.view._setup = function(model, controller) { this.model = model; this.controller = controller; } /** * public void sinfo.view.bodyShow() * * Performs initial rendering of the DHTML elements. Should only be called * within the body of the HTML. */ sinfo.view.bodyShow = function() { js.debug('inside view.bodyShow: ' + this.model.id); //document.writeln(js.msg(sinfo.messages.view_base_start, // this.model.id, this.title, this.model.lastUpdate)); document.writeln(js.msg(sinfo.messages.view_wrapper_start, this.model.id)); js.debug('template: getElementById(' + this.templateId + ')'); var d = document.getElementById(this.templateId); if (! d) { js.failure('Could not find template element ' + this.templateId); return; } this._showTemplate(d); document.writeln(js.msg(sinfo.messages.view_wrapper_end)); } sinfo.view._showTemplate = function(d) { var args = new Array(d.innerHTML); if (! args.push) { args.push = function(x) { args[args.length] = x; } } //this._fillTemplateArguments(args); //document.writeln(js.msg2(args)); js.debug("Parsing " + newText); var newText = ''; try { newText = js.parseMsg(d.innerHTML, this); } catch (e) { js.except("Error parsing template", e); } js.debug("parsed template as: " + newText); document.writeln(newText); } sinfo.view._fillTemplateArguments = function(x) { x.push(this.model.id); x.push(this.title); x.push(this.model.lastUpdated); return x; } /** * public void sinfo.view.update() * * Populates the ServerInfo view's HTML elements with the data from the model. */ sinfo.view.update = function() { js.debug("show for this: id = " + this.model.id); //js.debug('getElementById(' + this.model.id + ') = ' + document.getElementById(this.model.id)); var d = document.getElementById(this.model.id); if (! d) { js.failure("Could not find doc id " + this.model.id); return; } //js.debug('getElementById(' + this.model.id + '_updated) = ' + document.getElementById(this.model.id + '_updated')); d = document.getElementById(this.model.id + '_updated'); if (! d) { js.failure("Could not find doc id " + this.model.id + "_updated"); //return; } d.innerHTML = this.model.lastUpdate; this._updateInner(); } sinfo.view._updateInner = function() { //js.debug('getElementById(' + this.model.id + '_data) = ' + document.getElementById(this.model.id + '_data')); var d = document.getElementById(this.model.id + '_data'); if (! d) { js.failure("Could not find doc id " + this.model.id + "_data"); return; } d.innerHTML = this.model.data; } /* ========================================================================= * DEFAULT MESSAGES */ sinfo.messages = {} sinfo.messages.language_code = "en"; sinfo.messages.language = "US English"; sinfo.messages.view_wrapper_start = "<div id='{1}' class='server-info'>"; sinfo.messages.view_wrapper_end = "</div>";
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#3 | 5925 | Matt Albrecht | Updates to the server status page | ||
#2 | 5816 | Matt Albrecht | Add / update the server status with a much better AJAX model. | ||
#1 | 5810 | Matt Albrecht |
New version of logDaemon that has better bug fixes. Also added server-status web page for a pure client-side AJAX app that pulls together different server-side statistics. |