UNPKG

nsolid-manager

Version:

A manager that sets up and runs N|Solid for you

82 lines (69 loc) 2.67 kB
/** * Assemble full response Structure for clientside polling requests. */ import spec from './graphs/spectrogram' import computeSparklines from './graphs/sparkline' module.exports = buildResponse function buildResponse (cache, processes, duration) { // given the fact that we _may_ have a set of valid proceses // loop through all of the process and ensure that they are // valid and can be linked to a valid host // // valid processes mean that they have received both info and process_stats responses // valid hosts mean that they have received both system_info and system_stats responses // // once we have this information we can build the array of applications. This is done // at runtime and is not stored because doing so would add state without any discernible // advantage const hosts = cache.hosts const l = processes.length for (let i = 0; i < l; i++) { const processResponse = processes[i] const meta = processResponse.meta const reply = processResponse.reply const processId = meta.id const hostname = meta.hostname // we need to ensure that the intersection between // process_stats & (system_info ∩ system_stats) // is valid. if not then we can drop the process, and // hopefully pick it up on the next polling cycle. if (hosts && hosts[hostname] && hosts[hostname].stats) { if (!hosts[hostname].stats.starttime) { hosts[hostname].stats.starttime = Math.round((Date.now() - hosts[hostname].stats.uptime * 1000) / 1000) * 1000 } // ignore last three 3 digits to get consistent start time const startTime = Math.round((Date.now() - reply.uptime * 1000) / 1000) * 1000 cache.addProcess({ id: processId, app: meta.app, pid: meta.pid, hub: meta.hub, uptime: reply.uptime, starttime: startTime, version: '', tags: meta.tags, title: reply.title, async: { handles: reply.activeHandles || 0, streams: reply.activeStreams || 0, requests: reply.activeRequests || 0 }, memory: { rss: reply.rss, total: reply.heapTotal, used: reply.heapUsed }, host: hostname, cpu: reply.cpu }) } } // return the array of _computed_ applications w/ valid (complete) processes return cache.getApp.array.map(function (application) { computeSparklines(application, hosts, duration) spec.computeRSSSpectrogram(application, 25, 10) spec.computeCPUSpectrogram(application, 25, 10) spec.computeSpiffygram(application, 5, 5) return application }) }