<commit_before>module.exports = createServer

const express = require('express')
const fs = require('fs')
const https = require('https')
const http = require('http')
const SolidWs = require('solid-ws')
const debug = require('./debug')
const createApp = require('./create-app')
const globalTunnel = require('global-tunnel-ng')

function createServer (argv, app) {
  argv = argv || {}
  app = app || express()
  const ldpApp = createApp(argv)
  const ldp = ldpApp.locals.ldp || {}
  let mount = argv.mount || '/'
  // Removing ending '/'
  if (mount.length > 1 &&
    mount[mount.length - 1] === '/') {
    mount = mount.slice(0, -1)
  }
  app.use(mount, ldpApp)
  debug.settings('Base URL (--mount): ' + mount)

  if (argv.idp) {
    console.warn('The idp configuration option has been renamed to multiuser.')
    argv.multiuser = argv.idp
    delete argv.idp
  }

  if (argv.httpProxy) {
    globalTunnel.initialize(argv.httpProxy)
  }

  let server
  const needsTLS = argv.sslKey || argv.sslCert
  if (!needsTLS) {
    server = http.createServer(app)
  } else {
    debug.settings('SSL Private Key path: ' + argv.sslKey)
    debug.settings('SSL Certificate path: ' + argv.sslCert)

    if (!argv.sslCert && !argv.sslKey) {
      throw new Error('Missing SSL cert and SSL key to enable WebIDs')
    }

    if (!argv.sslKey && argv.sslCert) {
      throw new Error('Missing path for SSL key')
    }

    if (!argv.sslCert && argv.sslKey) {
      throw new Error('Missing path for SSL cert')
    }

    let key
    try {
      key = fs.readFileSync(argv.sslKey)
    } catch (e) {
      throw new Error('Can\'t find SSL key in ' + argv.sslKey)
    }

    let cert
    try {
      cert = fs.readFileSync(argv.sslCert)
    } catch (e) {
      throw new Error('Can\'t find SSL cert in ' + argv.sslCert)
    }

    const credentials = Object.assign({
      key: key,
      cert: cert
    }, argv)

    if (ldp.webid && ldp.auth === 'tls') {
      credentials.requestCert = true
    }

    server = https.createServer(credentials, app)
  }

  // Look for port or list of ports to redirect to argv.port
  if ('redirectHttpFrom' in argv) {
    const redirectHttpFroms = argv.redirectHttpFrom.constructor === Array
      ? argv.redirectHttpFrom
      : [argv.redirectHttpFrom]
    const portStr = argv.port === 443 ? '' : ':' + argv.port
    redirectHttpFroms.forEach(redirectHttpFrom => {
      debug.settings('will redirect from port ' + redirectHttpFrom + ' to port ' + argv.port)
      const redirectingServer = express()
      redirectingServer.get('*', function (req, res) {
        const host = req.headers.host.split(':') // ignore port
        debug.server(host, '=> https://' + host + portStr + req.url)
        res.redirect('https://' + host + portStr + req.url)
      })
      redirectingServer.listen(redirectHttpFrom)
    })
  }

  // Setup Express app
  if (ldp.live) {
    const solidWs = SolidWs(server, ldpApp)
    console.log(solidWs.publish)
    ldpApp.locals.ldp.live = solidWs.publish.bind(solidWs)
    // setInterval(() => {
    //   console.log("publishing")
    //   ldpApp.locals.ldp.live("https://jackson.localhost:8443/profile/card#me")
    // }, 1000)
  }

  return server
}
<commit_msg>Removed test code<commit_after>module.exports = createServer

const express = require('express')
const fs = require('fs')
const https = require('https')
const http = require('http')
const SolidWs = require('solid-ws')
const debug = require('./debug')
const createApp = require('./create-app')
const globalTunnel = require('global-tunnel-ng')

function createServer (argv, app) {
  argv = argv || {}
  app = app || express()
  const ldpApp = createApp(argv)
  const ldp = ldpApp.locals.ldp || {}
  let mount = argv.mount || '/'
  // Removing ending '/'
  if (mount.length > 1 &&
    mount[mount.length - 1] === '/') {
    mount = mount.slice(0, -1)
  }
  app.use(mount, ldpApp)
  debug.settings('Base URL (--mount): ' + mount)

  if (argv.idp) {
    console.warn('The idp configuration option has been renamed to multiuser.')
    argv.multiuser = argv.idp
    delete argv.idp
  }

  if (argv.httpProxy) {
    globalTunnel.initialize(argv.httpProxy)
  }

  let server
  const needsTLS = argv.sslKey || argv.sslCert
  if (!needsTLS) {
    server = http.createServer(app)
  } else {
    debug.settings('SSL Private Key path: ' + argv.sslKey)
    debug.settings('SSL Certificate path: ' + argv.sslCert)

    if (!argv.sslCert && !argv.sslKey) {
      throw new Error('Missing SSL cert and SSL key to enable WebIDs')
    }

    if (!argv.sslKey && argv.sslCert) {
      throw new Error('Missing path for SSL key')
    }

    if (!argv.sslCert && argv.sslKey) {
      throw new Error('Missing path for SSL cert')
    }

    let key
    try {
      key = fs.readFileSync(argv.sslKey)
    } catch (e) {
      throw new Error('Can\'t find SSL key in ' + argv.sslKey)
    }

    let cert
    try {
      cert = fs.readFileSync(argv.sslCert)
    } catch (e) {
      throw new Error('Can\'t find SSL cert in ' + argv.sslCert)
    }

    const credentials = Object.assign({
      key: key,
      cert: cert
    }, argv)

    if (ldp.webid && ldp.auth === 'tls') {
      credentials.requestCert = true
    }

    server = https.createServer(credentials, app)
  }

  // Look for port or list of ports to redirect to argv.port
  if ('redirectHttpFrom' in argv) {
    const redirectHttpFroms = argv.redirectHttpFrom.constructor === Array
      ? argv.redirectHttpFrom
      : [argv.redirectHttpFrom]
    const portStr = argv.port === 443 ? '' : ':' + argv.port
    redirectHttpFroms.forEach(redirectHttpFrom => {
      debug.settings('will redirect from port ' + redirectHttpFrom + ' to port ' + argv.port)
      const redirectingServer = express()
      redirectingServer.get('*', function (req, res) {
        const host = req.headers.host.split(':') // ignore port
        debug.server(host, '=> https://' + host + portStr + req.url)
        res.redirect('https://' + host + portStr + req.url)
      })
      redirectingServer.listen(redirectHttpFrom)
    })
  }

  // Setup Express app
  if (ldp.live) {
    const solidWs = SolidWs(server, ldpApp)
    ldpApp.locals.ldp.live = solidWs.publish.bind(solidWs)
  }

  return server
}
<|endoftext|>


<commit_before>"use strict";

const config = require("../util/config");
const { AlphaEngine } = require("../strategy/alpha-engine");

module.exports = ontick;

const instruments = [
    "EUR_USD"
];

const engines = {};

instruments.forEach(instrument => {
    engines[instrument] = new AlphaEngine(instrument);

});

function ontick(tick) {
    const instrument = tick.instrument;

    if (!instruments.includes(instrument)) {
        return;
    }

    const ask = parseFloat(tick.ask);
    const bid = parseFloat(tick.bid);
    const time = tick.time;

    const pip = config.pips[instrument];
    const countDecimals = pip.toString().split(".")[1].length;
    const mid = parseFloat(((bid + ask) / 2).toFixed(countDecimals));
    const spread = parseFloat(((ask - bid) / pip).toFixed(1));

    const price = {
        instrument,
        ask,
        bid,
        time,
        mid,
        spread,
        countDecimals
    };

    engines[instrument].run(price);
}
<commit_msg>feat(ontick): add multi currencies<commit_after>"use strict";

const config = require("../util/config");
const { AlphaEngine } = require("../strategy/alpha-engine");

module.exports = ontick;

const instruments = [
    "EUR_USD",
    "USD_JPY",
    "GBP_USD",
    "EUR_GBP",
    "USD_CHF",
    "EUR_JPY",
    "EUR_CHF",
    "USD_CAD",
    "AUD_USD",
    "GBP_JPY"
];

const engines = {};

instruments.forEach(instrument => {
    engines[instrument] = new AlphaEngine(instrument);

});

function ontick(tick) {
    const instrument = tick.instrument;

    if (!instruments.includes(instrument)) {
        return;
    }

    const ask = parseFloat(tick.ask);
    const bid = parseFloat(tick.bid);
    const time = tick.time;

    const pip = config.pips[instrument];
    const countDecimals = pip.toString().split(".")[1].length;
    const mid = parseFloat(((bid + ask) / 2).toFixed(countDecimals));
    const spread = parseFloat(((ask - bid) / pip).toFixed(1));

    const price = {
        instrument,
        ask,
        bid,
        time,
        mid,
        spread,
        countDecimals
    };

    engines[instrument].run(price);
}
<|endoftext|>


<commit_before>var events = require('events');
var util = require('util');
var MultiserverClient = require('../gearman/multiserver-client')
  .MultiserverClient;
var gearsloth = require('../gearsloth');
var log = require('../log');

/**
 * Runner component. Emits 'connect' when at least one server is connected.
 */
function Runner(conf) {
  events.EventEmitter.call(this);
  var that = this;
  this.component_name = 'runner';
  this._default_controller = conf.controllername || 'retryController';
  this._dbconn = conf.dbconn;
  this._client = new MultiserverClient(conf.servers,
    {
      component_name: this.component_name,
      debug: conf.verbose > 2
    });
  this._client.on('connect', function() {
    log.info(that.component_name, 'Connected');
    that.emit('connect');
    that.startPolling();
  });
  this._client.on('disconnect', function(callback) {
    if (that.db_poll_stop)
      that.db_poll_stop();
    if (callback)
      callback(exit);
    log.info(that.component_name, 'Disconnected');
    that.emit('disconnect');
  });
}

util.inherits(Runner, events.EventEmitter);

Runner.prototype.startPolling = function() {
  var that = this;
  try {
    this.db_poll_stop = this._dbconn.listenTask(function(err, task) {
      if (err) {
        log.debug(that.component_name, 'Error polling database:', err)
        that.stop(1);
        return;
      }

      // check default controller on a best effort basis
      task.controller = task.controller ?
        task.controller : that._default_controller;

      that.updateTask(task);
      that.sendToController(task);
    });
  } catch (err) {
    log.err(this.component_name, 'Error:', err.message);
    this.stop(1);
  };
};

/**
 * Update the information of the task according to possibly set variables:
 * runner_retry_timeout: how long to wait until anohter runner may retry.
 * Default is 1000 seconds.  runner_retry_count: if zero is reached from
 * non-zero positive value the task is disables else it is decreased or left
 * untouched.
 */

Runner.prototype.updateTask = function(task) {
  var that = this;
  task.after = (!isNaN(task.runner_retry_timeout)) ?
    task.runner_retry_timeout : 1000;

  if (!isNaN(task.runner_retry_count))
    task.runner_retry_count--;

  if (task.runner_retry_count === 0) {
    this._dbconn.disableTask(task, function(error, change) {
        if (error) {
          that.stop(1);
          log.err(that.component_name, 'Error:', error.message);
        }
        if (change>0) {
          log.debug(that.component_name, 'Task reached maximum time-to-live:', task);
        } else {
          log.debug(that.component_name, 'Unable to disable task:', task);
        }
      });
    }

  this._dbconn.updateTask(task, function(err) {
    if (err) {
      log.err(that.component_name, 'Error updating task:', task, err.message);
      that.stop(1);
    }
  });
};

/**
 * Send given task to controller at a precise time.
 * Task must contain valid `.controller`,
 * `.at` and `.runner_retry_timeout` fields.
 */
Runner.prototype.stop = function(exit, callback) {
  this._client.disconnect();
}

Runner.prototype.sendToController = function(task) {
  log.debug(this.component_name, 'Sending task to controller:', task);
  var task_json = JSON.stringify(task);
  this._client.submitJobBg(task.controller, task_json);
};

module.exports = function(conf) {
  return new Runner(conf);
};

module.exports.Runner = Runner;
<commit_msg>fix updateTask changing task before runner could send it<commit_after>var events = require('events');
var util = require('util');
var MultiserverClient = require('../gearman/multiserver-client')
  .MultiserverClient;
var gearsloth = require('../gearsloth');
var log = require('../log');

/**
 * Runner component. Emits 'connect' when at least one server is connected.
 */
function Runner(conf) {
  events.EventEmitter.call(this);
  var that = this;
  this.component_name = 'runner';
  this._default_controller = conf.controllername || 'retryController';
  this._dbconn = conf.dbconn;
  this._client = new MultiserverClient(conf.servers,
    {
      component_name: this.component_name,
      debug: conf.verbose > 2
    });
  this._client.on('connect', function() {
    log.info(that.component_name, 'Connected');
    that.emit('connect');
    that.startPolling();
  });
  this._client.on('disconnect', function(callback) {
    if (that.db_poll_stop)
      that.db_poll_stop();
    if (callback)
      callback(exit);
    log.info(that.component_name, 'Disconnected');
    that.emit('disconnect');
  });
}

util.inherits(Runner, events.EventEmitter);

Runner.prototype.startPolling = function() {
  var that = this;
  try {
    this.db_poll_stop = this._dbconn.listenTask(function(err, task) {
      if (err) {
        log.debug(that.component_name, 'Error polling database:', err)
        that.stop(1);
        return;
      }

      // check default controller on a best effort basis
      task.controller = task.controller ?
        task.controller : that._default_controller;

      that.sendToController(task);
      that.updateTask(task);
    });
  } catch (err) {
    log.err(this.component_name, 'Error:', err.message);
    this.stop(1);
  };
};

/**
 * Update the information of the task according to possibly set variables:
 * runner_retry_timeout: how long to wait until anohter runner may retry.
 * Default is 1000 seconds.  runner_retry_count: if zero is reached from
 * non-zero positive value the task is disables else it is decreased or left
 * untouched.
 */

Runner.prototype.updateTask = function(task) {
  var that = this;
  task.after = (!isNaN(task.runner_retry_timeout)) ?
    task.runner_retry_timeout : 1000;

  if (!isNaN(task.runner_retry_count))
    task.runner_retry_count--;

  if (task.runner_retry_count === 0) {
    this._dbconn.disableTask(task, function(error, change) {
        if (error) {
          that.stop(1);
          log.err(that.component_name, 'Error:', error.message);
        }
        if (change>0) {
          log.debug(that.component_name, 'Task reached maximum time-to-live:', task);
        } else {
          log.debug(that.component_name, 'Unable to disable task:', task);
        }
      });
    }

  this._dbconn.updateTask(task, function(err) {
    if (err) {
      log.err(that.component_name, 'Error updating task:', task, err.message);
      that.stop(1);
    }
  });
};

/**
 * Send given task to controller at a precise time.
 * Task must contain valid `.controller`,
 * `.at` and `.runner_retry_timeout` fields.
 */
Runner.prototype.stop = function(exit, callback) {
  this._client.disconnect();
}

Runner.prototype.sendToController = function(task) {
  log.debug(this.component_name, 'Sending task to controller:', task);
  var task_json = JSON.stringify(task);
  this._client.submitJobBg(task.controller, task_json);
};

module.exports = function(conf) {
  return new Runner(conf);
};

module.exports.Runner = Runner;
<|endoftext|>


<commit_before>
module.exports = function(_dbus, _bus) {
	var self = this;
	var dbus = _dbus;
	var bus = _bus;
	var registerObject = {};

	var InternalEventDispatcher = function(interface, member, args) {
		for (var path in registerObject) {
			if (!(interface in registerObject[path]))
				continue;

			if (member in registerObject[path][interface]['Methods']) {
				// args is null or undefined cause error on Array.prototype.slice.call

				if (! args)
					return registerObject[path][interface]['Methods'][member].apply(this, [ args ]);

				return registerObject[path][interface]['Methods'][member].apply(this, Array.prototype.slice.call(args));
			}
		}
	};

	this.createObjectPath = function(path) {
		/* Initializing */
		if (!(path in registerObject)) {
			registerObject[path] = {};

			/* Register a new object */
			dbus.registerObjectPath(bus, path, InternalEventDispatcher);
		}
	};

	this.createInterface = function(path, interface) {
		self.createObjectPath(path);

		if (!(interface in registerObject[path])) {
			registerObject[path][interface] = {
				Methods: {},
				Properties: {},
				Signals: {}
			};
		}
	};

	this.addMethod = function(path, interface, method, handler) {
		self.createInterface(path, interface);

		registerObject[path][interface]['Methods'][method] = handler;
	};

	this.addMethods = function(path, interface, methods) {
		for (var key in methods) {
			self.addMethod(path, interface, key, methods[key]);
		}
	};

  this.setIntrospectable = function(path, source) {
    self.addMethods(path, 'org.freedesktop.DBus.Introspectable',
      {
        Introspect: function() {
          return source;
        }
      });
  }

  this.emitSignal = function(path, interface, signal, args) {
    dbus.emitSignal(bus, path, interface, signal, args);
  }
}
<commit_msg>Fixed receiving arguments on services<commit_after>module.exports = function(_dbus, _bus) {
	var self = this;
	var dbus = _dbus;
	var bus = _bus;
	var registerObject = {};

	var InternalEventDispatcher = function(interface, member, args) {
		for (var path in registerObject) {
			if (!(interface in registerObject[path]))
				continue;

			if (member in registerObject[path][interface]['Methods']) {
				// args is null or undefined cause error on Array.prototype.slice.call

				if (! args)
					return registerObject[path][interface]['Methods'][member].apply(this, [ args ]);

				return registerObject[path][interface]['Methods'][member].apply(this, Array.prototype.slice.call(Array.isArray(args) ? args : [ args ] ));
			}
		}
	};

	this.createObjectPath = function(path) {
		/* Initializing */
		if (!(path in registerObject)) {
			registerObject[path] = {};

			/* Register a new object */
			dbus.registerObjectPath(bus, path, InternalEventDispatcher);
		}
	};

	this.createInterface = function(path, interface) {
		self.createObjectPath(path);

		if (!(interface in registerObject[path])) {
			registerObject[path][interface] = {
				Methods: {},
				Properties: {},
				Signals: {}
			};
		}
	};

	this.addMethod = function(path, interface, method, handler) {
		self.createInterface(path, interface);

		registerObject[path][interface]['Methods'][method] = handler;
	};

	this.addMethods = function(path, interface, methods) {
		for (var key in methods) {
			self.addMethod(path, interface, key, methods[key]);
		}
	};

  this.setIntrospectable = function(path, source) {
    self.addMethods(path, 'org.freedesktop.DBus.Introspectable',
      {
        Introspect: function() {
          return source;
        }
      });
  }

  this.emitSignal = function(path, interface, signal, args) {
    dbus.emitSignal(bus, path, interface, signal, args);
  }
}
<|endoftext|>


<commit_before>module.exports = {
  "GIVE_X11_TIME_TIMEOUT": 80,
  "POLL_ALL_APPS_STARTED_INTERVAL": 2000,
  "POLL_ALL_MAX_TIMEOUT": 120000,
  "SAVE_SESSION_IN_PRETTY_FORMAT": true,
  "WM_CLASS_AND_EXECUTABLE_FILE_MAP": {
    "gnome-terminal-server.Gnome-terminal": "gnome-terminal",
    "google-chrome.Google-chrome": "google-chrome.desktop",
    "Mail.Thunderbird": "thunderbird.desktop",
    "nautilus.Nautilus": "nautilus",
    "Navigator.Firefox": "firefox.desktop",
    "Navigator.Pale": "palemoon.desktop",
    "skype.Skype":"skypeforlinux.desktop",
    "sun-awt-X11-XFramePeer.jetbrains-idea": "jetbrains-idea.desktop",
    "VirtualBox.VirtualBox": "virtualbox.desktop"
  },
  "WM_CLASS_EXCLUSIONS": [
    "N/A",
    "tilda.Tilda",
    "Popup.desktop",
    "update-manager.Update-manager",
    "desktop_window.Nautilus",
    "electron.Electron",
    "guake.Main.py",
    'gnome-software.Gnome-software'
  ],
  "WM_META_MAP": {
    "WM_CLASS(STRING)": "wmClassName",
    "_NET_WM_STATE(ATOM)": "states",
    "_NET_WM_DESKTOP(CARDINAL)": "wmCurrentDesktopNr",
    "WM_NAME(UTF8_STRING)": "wmTitle",
    "_NET_WM_PID(CARDINAL)": "wmPid",
    "_NET_WM_WINDOW_TYPE(ATOM)": "wmType",
    "_BAMF_DESKTOP_FILE(STRING)": "executableFile"
  },
  "WM_META_MAP_NUMBER_TYPES": [
    "_NET_WM_PID(CARDINAL)",
    "_NET_WM_DESKTOP(CARDINAL)"
  ],
  "DESKTOP_FILE_LOCATIONS": [
    "{home}/.local/share/applications",
    "{home}/.gnome/apps/",
    "/usr/share/applications",
    "/usr/local/share/applications",
    "/usr/share/app-install",
    "{home}/.config/autostart/"
  ],
  "CMD": {
    "GET_DISPLAY_ID": "xrandr --query | grep '[^s]connected '",
    "XPROP_ID": "xprop -id"
  }
};
<commit_msg>feat: update default config #22<commit_after>module.exports = {
  "GIVE_X11_TIME_TIMEOUT": 80,
  "POLL_ALL_APPS_STARTED_INTERVAL": 2000,
  "POLL_ALL_MAX_TIMEOUT": 120000,
  "SAVE_SESSION_IN_PRETTY_FORMAT": true,
  "WM_CLASS_AND_EXECUTABLE_FILE_MAP": {
    "gnome-terminal-server.Gnome-terminal": "gnome-terminal",
    "google-chrome.Google-chrome": "google-chrome.desktop",
    "Mail.Thunderbird": "thunderbird.desktop",
    "nautilus.Nautilus": "nautilus",
    "Navigator.Firefox": "firefox.desktop",
    "Navigator.Pale": "palemoon.desktop",
    "skype.Skype":"skypeforlinux.desktop",
    "sun-awt-X11-XFramePeer.jetbrains-idea": "jetbrains-idea.desktop",
    "VirtualBox.VirtualBox": "virtualbox.desktop",
    "Telegram.TelegramDesktop": "telegram-desktop_telegramdesktop.desktop",
    "keepassxc.keepassxc": "keepassxc_keepassxc.desktop"
  },
  "WM_CLASS_EXCLUSIONS": [
    "N/A",
    "tilda.Tilda",
    "Popup.desktop",
    "update-manager.Update-manager",
    "desktop_window.Nautilus",
    "electron.Electron",
    "guake.Main.py",
    'gnome-software.Gnome-software'
  ],
  "WM_META_MAP": {
    "WM_CLASS(STRING)": "wmClassName",
    "_NET_WM_STATE(ATOM)": "states",
    "_NET_WM_DESKTOP(CARDINAL)": "wmCurrentDesktopNr",
    "WM_NAME(UTF8_STRING)": "wmTitle",
    "_NET_WM_PID(CARDINAL)": "wmPid",
    "_NET_WM_WINDOW_TYPE(ATOM)": "wmType",
    "_BAMF_DESKTOP_FILE(STRING)": "executableFile"
  },
  "WM_META_MAP_NUMBER_TYPES": [
    "_NET_WM_PID(CARDINAL)",
    "_NET_WM_DESKTOP(CARDINAL)"
  ],
  "DESKTOP_FILE_LOCATIONS": [
    "{home}/.local/share/applications",
    "{home}/.gnome/apps/",
    "/usr/share/applications",
    "/usr/local/share/applications",
    "/usr/share/app-install",
    "{home}/.config/autostart/",
    "/var/lib/snapd/desktop/applications"
  ],
  "CMD": {
    "GET_DISPLAY_ID": "xrandr --query | grep '[^s]connected '",
    "XPROP_ID": "xprop -id"
  }
};
<|endoftext|>


<commit_before>#!/usr/bin/env node
var Docker = require('dockerode')
var docker = new Docker()
var minimatch = require('minimatch')
var dockerInfo = {}
var tagIds = []
if (process.env.TAGGING_LABELS) {
  tagIds = process.env.TAGGING_LABELS.split(',')
}

docker.info(function dockerInfoHandler (err, data) {
  if (err) {
    console.error(err)
  }
  if (data) {
    dockerInfo = data
    // SPM_MONITOR_TAGS is evaluated by spm-sender before it sends metrics to SPM
    if (!process.env.SPM_MONITOR_TAGS) {
      if (data.Labels && data.Labels.length > 0) {
        process.env.SPM_MONITOR_TAGS = data.Labels.join(',')
      }
    } else {
      if (data.Labels && data.Labels.length > 0) {
        process.env.SPM_MONITOR_TAGS = process.env.SPM_MONITOR_TAGS + ',' + data.Labels.join(',')
      }
    }
  }
})

function getEnvVar (name, list) {
  if (!list) {
    return null
  }
  if (!(list instanceof Array)) {
    var keys = Object.keys(list)
    for (var k = 0; k < keys.length; k++) {
      if (keys[k].indexOf(name) > -1) {
        return list[keys[k]].trim()
      }
    }
  } else {
    for (var i = 0; i < list.length; i++) {
      if (list[i].indexOf(name) > -1) {
        var token = list[i].split('=')
        if (token && token.length > 1 && token[1]) {
          token = token[1]
          if (token.length > 35) {
            return token.trim()
          }
        }
      }
    }
  }
  return null
}

function getValue (name, list, info) {
  if (!list) {
    return null
  }
  if (!(list instanceof Array)) {
    var keys = Object.keys(list)
    for (var k = 0; k < keys.length; k++) {
      if (minimatch(keys[k], name)) {
        if (!info.tags) {
          info.tags = {}
        }
        info.tags[keys[k]] = list[keys[k]]
        info.tags
      }
    }
  } else {
    for (var i = 0; i < list.length; i++) {
      if (minimatch(list[i], name)) {
        var value = list[i].split('=')
        if (value.length > 1) {
          if (!info.tags) {
            info.tags = {}
          }
          info.tags[value[0]] = value[1]
        }
      }
    }
  }

  return null
}

function extractLoggingTags (labels, env, info) {
  // console.log(info)
  if (tagIds.length > 0) {
    for (var i = 0; i < tagIds.length; i++) {
      getValue(tagIds[i] + '*', labels, info)
      getValue(tagIds[i] + '*', env, info)
      getValue(tagIds[i] + '*', dockerInfo.Labels, info)
    }
  }
}

function getLogseneToken (err, info) {
  var token = null
  if (!err) {
    extractLoggingTags(info.Config.Labels, info.Config.Env, info)
    if (info.Config && info.Config.Labels && info.Config.Labels.LOGSENE_ENABLED !== undefined) {
      info.LOGSENE_ENABLED = info.Config.Labels.LOGSENE_ENABLED
    } else {
      info.LOGSENE_ENABLED = getEnvVar('LOGSENE_ENABLED', info.Config.Env)
    }
    if (info.LOGSENE_ENABLED === '0' || info.LOGSENE_ENABLED === 'false' || info.LOGSENE_ENABLED === 'no') {
      console.log('Container ' + info.Id + ' ' + info.Name + ' setting LOGSENE_ENABLED=false')
      info.LOGSENE_ENABLED = false
    } else {
      info.LOGSENE_ENABLED = true
    }
    if (info.Config && info.Config.Labels && info.Config.Labels.LOGSENE_TOKEN) {
      token = info.Config.Labels.LOGSENE_TOKEN
      info.LOGSENE_TOKEN = token
    } else {
      token = getEnvVar('LOGSENE_TOKEN', info.Config.Env)
    }
  }
  if (info) {
    info.LOGSENE_TOKEN = token || process.env.LOGSENE_TOKEN
    info.dockerInfo = dockerInfo
    this.callback(null, info)
  } else {
    this.callback(null, {
      LOGSENE_TOKEN: process.env.LOGSENE_TOKEN,
      id: this.container
    })
  }
}

function getLogseneTokenForContainer (id, cb) {
  docker.getContainer(id).inspect(getLogseneToken.bind({
    callback: cb,
    container: id
  }))
}

module.exports = getLogseneTokenForContainer
<commit_msg>fix #21<commit_after>#!/usr/bin/env node
var Docker = require('dockerode')
var docker = new Docker()
var minimatch = require('minimatch')
var dockerInfo = {}
var tagIds = []
if (process.env.TAGGING_LABELS) {
  tagIds = process.env.TAGGING_LABELS.split(',')
}

docker.info(function dockerInfoHandler (err, data) {
  if (err) {
    console.error(err)
  }
  if (data) {
    dockerInfo = data
    // SPM_MONITOR_TAGS is evaluated by spm-sender before it sends metrics to SPM
    if (!process.env.SPM_MONITOR_TAGS) {
      if (data.Labels && data.Labels.length > 0) {
        process.env.SPM_MONITOR_TAGS = data.Labels.join(',')
      }
    } else {
      if (data.Labels && data.Labels.length > 0) {
        process.env.SPM_MONITOR_TAGS = process.env.SPM_MONITOR_TAGS + ',' + data.Labels.join(',')
      }
    }
  }
})

function getEnvVar (name, list) {
  if (!list) {
    return null
  }
  if (!(list instanceof Array)) {
    var keys = Object.keys(list)
    for (var k = 0; k < keys.length; k++) {
      if (keys[k].indexOf(name) > -1) {
        return list[keys[k]].trim()
      }
    }
  } else {
    for (var i = 0; i < list.length; i++) {
      if (list[i].indexOf(name) > -1) {
        var rv = list[i].split('=')
        if (rv && rv.length > 1 && rv[1]) {
          rv = rv[1]
          return rv
        }
      }
    }
  }
  return null
}

function getValue (name, list, info) {
  if (!list) {
    return null
  }
  if (!(list instanceof Array)) {
    var keys = Object.keys(list)
    for (var k = 0; k < keys.length; k++) {
      if (minimatch(keys[k], name)) {
        if (!info.tags) {
          info.tags = {}
        }
        info.tags[keys[k]] = list[keys[k]]
        info.tags
      }
    }
  } else {
    for (var i = 0; i < list.length; i++) {
      if (minimatch(list[i], name)) {
        var value = list[i].split('=')
        if (value.length > 1) {
          if (!info.tags) {
            info.tags = {}
          }
          info.tags[value[0]] = value[1]
        }
      }
    }
  }

  return null
}

function extractLoggingTags (labels, env, info) {
  // console.log(info)
  if (tagIds.length > 0) {
    for (var i = 0; i < tagIds.length; i++) {
      getValue(tagIds[i] + '*', labels, info)
      getValue(tagIds[i] + '*', env, info)
      getValue(tagIds[i] + '*', dockerInfo.Labels, info)
    }
  }
}

function getLogseneToken (err, info) {
  var token = null
  if (!err) {
    extractLoggingTags(info.Config.Labels, info.Config.Env, info)
    if (info.Config && info.Config.Labels && info.Config.Labels.LOGSENE_ENABLED !== undefined) {
      info.LOGSENE_ENABLED = info.Config.Labels.LOGSENE_ENABLED
    } else {
      info.LOGSENE_ENABLED = getEnvVar('LOGSENE_ENABLED', info.Config.Env)
    }
    if (info.LOGSENE_ENABLED === null || info.LOGSENE_ENABLED === '0' || info.LOGSENE_ENABLED === 'false' || info.LOGSENE_ENABLED === 'no') {
      console.log('Container ' + info.Id + ' ' + info.Name + ' setting LOGSENE_ENABLED=false')
      info.LOGSENE_ENABLED = false
    } else {
      info.LOGSENE_ENABLED = true
      console.log('Container ' + info.Id + ' ' + info.Name + ' setting LOGSENE_ENABLED=true')
    }
    if (info.Config && info.Config.Labels && info.Config.Labels.LOGSENE_TOKEN) {
      token = info.Config.Labels.LOGSENE_TOKEN
      info.LOGSENE_TOKEN = token
    } else {
      token = getEnvVar('LOGSENE_TOKEN', info.Config.Env)
    }
  }
  if (info) {
    info.LOGSENE_TOKEN = token || process.env.LOGSENE_TOKEN
    info.dockerInfo = dockerInfo
    this.callback(null, info)
  } else {
    this.callback(null, {
      LOGSENE_TOKEN: process.env.LOGSENE_TOKEN,
      id: this.container
    })
  }
}

function getLogseneTokenForContainer (id, cb) {
  docker.getContainer(id).inspect(getLogseneToken.bind({
    callback: cb,
    container: id
  }))
}

module.exports = getLogseneTokenForContainer
<|endoftext|>


<commit_before>var loghose = require('docker-loghose')
var through = require('through2')
var Logsene = require('logsene-js')
var SpmAgent = require('spm-agent')
var Logagent = require('logagent-js')
var fs = require('fs')

function createLogAgent () {
  var patternFile = '/etc/logagent/patterns.yml'
  if (fs.existsSync(patternFile)) {
    return new Logagent(patternFile)
  } else {
    console.log('No pattern file for log parsing found ' + patternFile + ' -> using default patterns')
    console.log('Use -v /mypattern/patterns.yml:' + patternFile + ' for custom log parser definitions.')
    return new Logagent() // use default patterns
  }
}
function extend (origin, add) {
  // Don't do anything if add isn't an object
  if (!add || typeof add !== 'object') return origin
  var keys = Object.keys(add)
  var i = keys.length
  while (i--) {
    origin[keys[i]] = add[keys[i]]
  }
  return origin
}

function DockerLogsene () {
  this.imageNameRegEx = /sematext\/spm-agent-docker/
  this.logagent = createLogAgent()
  this.logger = new Logsene(process.env.LOGSENE_TOKEN, 'docker')
  this.logger.on('error', function (err) {
    SpmAgent.Logger.log('error', 'Error in logsene-js: ' + err, err)
  })
  this.opts = {
    json: false,
    docker: null,
    events: null,
    // the following options limit the containers being matched
    // so we can avoid catching logs for unwanted containers
    matchByName: process.env.MATCH_BY_NAME,
    matchByImage: process.env.MATCH_BY_IMAGE,
    skipByName: process.env.SKIP_BY_NAME,
    skipByImage: process.env.SKIP_BY_IMAGE
  }
  var self = this
  setTimeout(function () {
    self.connect()
  }, 200)
}

DockerLogsene.prototype.debugLog = function (message)
{
  if (process.env.DEBUG_SPM_LOGGING === 'enabled') {
    SpmAgent.Logger.log('debug', message)
  }
}
DockerLogsene.prototype.getLogObject = function (line, container, cbf) {
  if (process.env.DEBUG_SPM_LOGGING==='enabled' && this.imageNameRegEx.test(container))
  {
    cbf('Disabled logging for image sematext/spm-agent-docker to avoid endless logging loop', null)
    return
  }
  var lines = line.split('\n')
  this.debugLog('Logs from Docker API (' + lines.length + ' lines) ' + container)
  this.debugLog('Logs from Docker API (content) for : ' + container + '\n' + lines)
  var self = this
  for (var i = 0; i < lines.length; i++) {
    this.logagent.parseLine(line, container, function (err, parsed) {
      self.debugLog('Log parser result: ' + JSON.stringify(parsed))
      return cbf(err, parsed)
    })
  }
}

DockerLogsene.prototype.connect = function () {
  var self = this
  var options = self.opts
  SpmAgent.Logger.log('debug', 'connect logStream to docker.sock')
  self.logStream = through.obj(function (chunk, enc, cb) {
    try {
      if (!chunk.line) {
        cb()
        return
      }
      var lines = chunk.line.split('\n') // We can get multiple lines per chunk
      lines.forEach(function (line) {
        self.logLine(line, chunk)
      })
      cb()
    } catch (ex) {
      console.log(ex)
      cb()
    }
  })
  // self.logStream.once('error', this.reconnect)
  var lh = loghose(options)
  lh.pipe(self.logStream)
  lh.on('error', this.reconnect)
}

DockerLogsene.prototype.logLine = function (logline, data, next) {
  var self = this
  var lineParser = {
    chunk: data,
    line: (process.env.REMOVE_ANSI_ESCAPE_SEQ === 'enabled') ? logline.replace(
            /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
            '') : logline,
    parse: function () {
      var chunk = this.chunk
      self.getLogObject(
      this.line,
      chunk.image + '_' + chunk.name + '_' + chunk.id,
      function (err, logObject) {
        if (err && !logObject) {
          return
        }
        if (logObject) {
          logObject.container_id = chunk.id
          logObject.image_name = chunk.image
          logObject.container_name = chunk.name
          logObject['@source'] = chunk.id + '/' + chunk.image
          var level = 'info'
          if (/error|fail|exception/i.test(this.line || '')) {
            level = 'error'
          }
          self.logger.log(String((logObject.level || logObject.lvl || logObject.severity || level)), logObject.message, logObject)
          self.debugLog('Stored Log: ' + JSON.stringify(logObject))
        }
      })
    }
  }
  lineParser.parse()
}

DockerLogsene.prototype.reconnect = function (err) {
  var self = this
  SpmAgent.Logger.log('error', 'Error in log stream: ' + err)
  try {
    self.logStream = null
    self.connect()
    SpmAgent.Logger.log('debug', 'reconnect to docker.sock ')
  } catch (ex) {
    SpmAgent.Logger.log('error', ex)
    setTimeout(function () {
      self.reconnect()
      SpmAgent.Logger.log('debug', 'reconnect to docker.sock ')
    }, 1000)
  }
}

module.exports = new DockerLogsene()
<commit_msg>more detailed debug output<commit_after>var loghose = require('docker-loghose')
var through = require('through2')
var Logsene = require('logsene-js')
var SpmAgent = require('spm-agent')
var Logagent = require('logagent-js')
var fs = require('fs')

function createLogAgent () {
  var patternFile = '/etc/logagent/patterns.yml'
  if (fs.existsSync(patternFile)) {
    return new Logagent(patternFile)
  } else {
    console.log('No pattern file for log parsing found ' + patternFile + ' -> using default patterns')
    console.log('Use -v /mypattern/patterns.yml:' + patternFile + ' for custom log parser definitions.')
    return new Logagent() // use default patterns
  }
}
function extend (origin, add) {
  // Don't do anything if add isn't an object
  if (!add || typeof add !== 'object') return origin
  var keys = Object.keys(add)
  var i = keys.length
  while (i--) {
    origin[keys[i]] = add[keys[i]]
  }
  return origin
}

function DockerLogsene () {
  this.imageNameRegEx = /sematext\/spm-agent-docker/
  this.logagent = createLogAgent()
  this.logger = new Logsene(process.env.LOGSENE_TOKEN, 'docker')
  this.logger.on('error', function (err) {
    SpmAgent.Logger.log('error', 'Error in logsene-js: ' + err, err)
  })
  var self = this
  this.logger.on('log', function (req) {
    self.debugLog(req.url)
    self.debugLog(req.request)
  })
  this.opts = {
    json: false,
    docker: null,
    events: null,
    // the following options limit the containers being matched
    // so we can avoid catching logs for unwanted containers
    matchByName: process.env.MATCH_BY_NAME,
    matchByImage: process.env.MATCH_BY_IMAGE,
    skipByName: process.env.SKIP_BY_NAME,
    skipByImage: process.env.SKIP_BY_IMAGE
  }
  var self = this
  setTimeout(function () {
    self.connect()
  }, 200)
}

DockerLogsene.prototype.debugLog = function (message)
{
  if (process.env.DEBUG_SPM_LOGGING === 'enabled') {
    SpmAgent.Logger.log('debug', message)
  }
}
DockerLogsene.prototype.getLogObject = function (line, container, cbf) {
  if (process.env.DEBUG_SPM_LOGGING==='enabled' && this.imageNameRegEx.test(container))
  {
    cbf('Disabled logging for image sematext/spm-agent-docker to avoid endless logging loop', null)
    return
  }
  var lines = line.split('\n')
  this.debugLog('Logs from Docker API (' + lines.length + ' lines) ' + container)
  this.debugLog('Logs from Docker API (content) for : ' + container + '\n' + lines)
  var self = this
  for (var i = 0; i < lines.length; i++) {
    this.logagent.parseLine(line, container, function (err, parsed) {
      self.debugLog('Log parser result: ' + JSON.stringify(parsed))
      return cbf(err, parsed)
    })
  }
}

DockerLogsene.prototype.connect = function () {
  var self = this
  var options = self.opts
  SpmAgent.Logger.log('debug', 'connect logStream to docker.sock')
  self.logStream = through.obj(function (chunk, enc, cb) {
    try {
      if (!chunk.line) {
        cb()
        return
      }
      var lines = chunk.line.split('\n') // We can get multiple lines per chunk
      lines.forEach(function (line) {
        self.logLine(line, chunk)
      })
      cb()
    } catch (ex) {
      console.log(ex)
      cb()
    }
  })
  // self.logStream.once('error', this.reconnect)
  var lh = loghose(options)
  lh.pipe(self.logStream)
  lh.on('error', this.reconnect)
}

DockerLogsene.prototype.logLine = function (logline, data, next) {
  var self = this
  var lineParser = {
    chunk: data,
    line: (process.env.REMOVE_ANSI_ESCAPE_SEQ === 'enabled') ? logline.replace(
            /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
            '') : logline,
    parse: function () {
      var chunk = this.chunk
      self.getLogObject(
      this.line,
      chunk.image + '_' + chunk.name + '_' + chunk.id,
      function (err, logObject) {
        if (err && !logObject) {
          return
        }
        if (logObject) {
          logObject.container_id = chunk.id
          logObject.image_name = chunk.image
          logObject.container_name = chunk.name
          logObject['@source'] = chunk.id + '/' + chunk.image
          var level = 'info'
          if (/error|fail|exception/i.test(this.line || '')) {
            level = 'error'
          }
          self.logger.log(String((logObject.level || logObject.lvl || logObject.severity || level)),
            logObject.message || logObject.msg, logObject,
            function (err, msg) {
            if (err)
            {
              self.debugLog('Error to sending log:' + err)
            }
            if (msg) {
              self.debugLog('Stored Log: ' + JSON.stringify(msg))
            }
          })
        }
      })
    }
  }
  lineParser.parse()
}

DockerLogsene.prototype.reconnect = function (err) {
  var self = this
  SpmAgent.Logger.log('error', 'Error in log stream: ' + err)
  try {
    self.logStream = null
    self.connect()
    SpmAgent.Logger.log('debug', 'reconnect to docker.sock ')
  } catch (ex) {
    SpmAgent.Logger.log('error', ex)
    setTimeout(function () {
      self.reconnect()
      SpmAgent.Logger.log('debug', 'reconnect to docker.sock ')
    }, 1000)
  }
}

module.exports = new DockerLogsene()
<|endoftext|>


<commit_before>/*
 * @copyright Copyright (c) Sematext Group, Inc. - All Rights Reserved
 *
 * @licence SPM for Docker is free-to-use, proprietary software.
 * THIS IS PROPRIETARY SOURCE CODE OF Sematext Group, Inc. (Sematext)
 * This source code may not be copied, reverse engineered, or altered for any purpose.
 * This source code is to be used exclusively by users and customers of Sematext.
 * Please see the full license (found in LICENSE in this distribution) for details on its license and the licenses of its dependencies.
 */
var stats = require('docker-stats')
var flatten = require('flat')
var through = require('through2')
var Aggregator = require('./aggregator.js')
var SpmAgent = require('spm-agent')
var config = SpmAgent.Config
var statsCallBackFuntion = null

var opts = {
  docker: null, //  here goes options for Dockerode
  events: null,  //  an instance of docker-allcontainers,
  statsinterval: 10,
  matchByName: process.env.SPM_MATCH_BY_NAME,
  matchByImage: process.env.SPM_MATCH_BY_IMAGE,
  skipByName: process.env.SPM_SKIP_BY_NAME,
  skipByImage: process.env.SPM_SKIP_BY_IMAGE
}
// holds aggregation object for each container
var cMetrics = {}
var counter = require('measured').createCollection()

function transformStatsApi1_21 (stats) {
  if (!stats.networks) {
    return stats
  }
  var result = {
      rx_bytes: 0,
      rx_packets: 0,
      rx_errors: 0,
      rx_dropped: 0,
      tx_bytes: 0,
      tx_packets: 0,
      tx_errors: 0,
      tx_dropped: 0
  }
  Object.keys(stats.networks).forEach(function aggNetworkMetrics (key) {
    Object.keys(result).forEach(function (metric) {
      result[metric] = result[metric] + getValueAsNum(stats.networks[key][metric])
    })
  })
  stats.network = result
  if (process.env.spmagent_logger__level === 'debug') {
    console.log('docker stats transformStatsApi1_21: ' + JSON.stringify(stats))
  }
  return stats
}

// handling different structures of docker stats
function transformStats (stats) {
  if (process.env.spmagent_logger__level === 'debug') {
    console.log('docker stats: ' + JSON.stringify(stats))
  }
  if (stats.network && stats.network.rx_bytes) {
    // API < 1.21
    return stats
  } else {
    return transformStatsApi1_21(stats)
  }
}

function processStats (chunk, enc, cb) {
  var stats = flatten(transformStats(chunk.stats))
  var metric = {}
  if (process.env.spmagent_logger__level === 'silly') {
    console.log(stats)
  }
  counter.meter('requestsPerSecond').mark()
  if (cMetrics[chunk.id] === undefined) {
    cMetrics[chunk.id] = new Aggregator()
  }
  metric.name = chunk.name
  metric.dockerId = chunk.id
  metric.image = chunk.image
  var agg = cMetrics[chunk.id]
  agg.info = metric
  metrics.forEach(function (key) {
    var value = stats[key] || 0
    if (/service_time_/i.test(key) || /wait_time_/i.test(key)) {
      // micro seconds to ms
      value = value / (1000 * 1000)
    }
    agg.update(stats['read'], key, getValueAsNum(value), useDivForAggregation[key] || false)
  })
  // setTimeout (cb, 10)
  cb()
}

function getDockerStats (dockerStatsCallback) {
  var dockerStatsStream = stats(opts)
  dockerStatsStream.pipe(through.obj(processStats))
  dockerStatsStream.on('closed', function () {
    console.log('docker stats stream closed')
    // process.exit()
  })
  dockerStatsStream.once('error', function (err) {
    console.log('docker stats stream error:' + err)
    // try to reconnect
    setTimeout(function () {
      getDockerStats(dockerStatsCallback)
    }, 15000)
    try {
      dockerStatsStream.close()
    } catch (ex) {
      console.log(ex)
    }
  })
}

function getStatsForContainer (container, aggStats) {
  var agg = cMetrics[container]
  if (!agg) {
    return null
  }
  var values = []
  aggStats.forEach(function (key) {
    var metricStats = agg.get(key)
    metricStats.id = container
    values.push(metricStats)
  })
  agg.reset()
  return values
}

var cpuMetrics = [
  // CPU
  // 'cpu_stats.cpu_usage.total_usage',
  // 'cpu_stats.cpu_usage.percpu_usage.0',
  // 'cpu_stats.cpu_usage.usage_in_kernelmode',
  // 'cpu_stats.cpu_usage.usage_in_usermode',
  'cpu_stats.cpu_usage.cpu_percent',
  // 'cpu_stats.system_cpu_usage',
  // 'cpu_stats.throttling_data.periods',
  // 'cpu_stats.throttling_data.throttled_periods',
  'cpu_stats.throttling_data.throttled_time'
]

var networkMetrics = [
  // NETWORK
  'network.rx_bytes',
  'network.rx_packets',
  'network.rx_errors',
  'network.rx_dropped',
  'network.tx_bytes',
  'network.tx_packets',
  'network.tx_errors',
  'network.tx_dropped'
]

var memoryMetrics = [
  // MEMORY
  'memory_stats.usage',
  'memory_stats.limit',
  'memory_stats.failcnt',
  // 'memory_stats.max_usage',
  // 'memory_stats.active_anon',
  // 'memory_stats.active_file',
  // 'memory_stats.cache',
  // 'memory_stats.hierarchical_memory_limit',
  // 'memory_stats.inactive_anon',
  // 'memory_stats.inactive_file',
  // 'memory_stats.mapped_file',
  'memory_stats.stats.pgfault',
  // 'memory_stats.pgmajfault',
  'memory_stats.stats.pgpgin',
  'memory_stats.stats.pgpgout'
  // 'memory_stats.ss',
  // 'memory_stats.rss_huge',
  // MEMORY FAIL COUNTER

  // MEMORY LIMIT

// MEMORY TOTAL
// 'memory_stats.total_active_anon',
// 'memory_stats.total_active_file',
// 'memory_stats.total_cache',
// 'memory_stats.total_inactive_anon',
// 'memory_total_inactive_file',
// 'memory_stats.total_mapped_file',
// 'memory_stats.total_pgfault',
// 'memory_stats.total_pgmajfault',
// 'memory_stats.total_pgpgin',
// 'memory_stats.total_pgpgout',
// 'memory_stats.total_rss',
// 'memory_stats.total_rss_huge',
// 'memory_stats.total_unevictable',
// 'memory_stats.stats.total_writeback',
// 'memory_stats.unevictable',
// 'memory_stats.writeback'
]

var ioMetrics = [
  // IO STATS
  // READ
  'blkio_stats.io_service_bytes_recursive.0.value',
  // READ time
  'blkio_stats.io_service_time_recursive.0.value',
  // READ wait time
  'blkio_stats.io_wait_time_recursive.0.value',
  // WRITE
  'blkio_stats.io_service_bytes_recursive.1.value',
  // Write time
  'blkio_stats.io_service_time_recursive.1.value',
  // Write wait time
  'blkio_stats.io_wait_time_recursive.1.value'
// 'blkio_io_service_bytes_recursive',
// 'blkio_io_serviced_recursive',
// 'blkio_io_queue_recursive',
// 'blkio_io_service_time_recursive',
// 'blkio_io_wait_time_recursive',
// 'blkio_io_merged_recursive',
// 'blkio_io_time_recursive',
// 'blkio_sectors_recursive'
]
var metrics = []
metrics = metrics.concat(cpuMetrics).concat(networkMetrics).concat(memoryMetrics).concat(ioMetrics)
var avgAggregation = {
  'cpu_stats.cpu_usage.cpu_percent': true,
  'cpu_stats.throttling_data.throttled_time': true,
  'memory_stats.usage': true,
  'memory_stats.limit': true,
  // READ time
  'blkio_stats.io_service_time_recursive.0.value': true,
  // READ wait time
  'blkio_stats.io_wait_time_recursive.0.value': true,
  // Write time
  'blkio_stats.io_service_time_recursive.1.value': true,
  // Write wait time
  'blkio_stats.io_wait_time_recursive.1.value': true
}
var useDivForAggregation = {
  'network.rx_bytes': true,
  'network.rx_packets': true,
  'network.rx_errors': true,
  'network.rx_dropped': true,
  'network.tx_bytes': true,
  'network.tx_packets': true,
  'network.tx_errors': true,
  'network.tx_dropped': true,
  'memory_stats.failcnt': true,
  'memory_stats.stats.pgfault': true,
  'memory_stats.stats.pgpgin': true,
  'memory_stats.stats.pgpgout': true,
  'blkio_stats.io_service_bytes_recursive.0.value': true,
  // READ time
  'blkio_stats.io_service_time_recursive.0.value': true,
  // READ wait time
  'blkio_stats.io_wait_time_recursive.0.value': true,
  // WRITE
  'blkio_stats.io_service_bytes_recursive.1.value': true,
  // Write time
  'blkio_stats.io_service_time_recursive.1.value': true,
  // Write wait time
  'blkio_stats.io_wait_time_recursive.1.value': true
}

function getValueAsNum (value) {
  var rv = 0
  if (!isNaN(value)) {
    rv = value
  }
  return rv
}

function getStatsValue (metric) {
  if (avgAggregation[metric.name]) {
    return getValueAsNum(metric.mean)
  } else {
    return getValueAsNum(metric.sum)
  }
}
function dockerStatsCollector (statsCallback) {
  // start streaming docker stats
  getDockerStats()
  setInterval(function () {
    var containerCount = 0
    var cpuPercentTotal = 0
    for (var container in cMetrics) {
      containerCount++
      var values = getStatsForContainer(container, metrics)
      if (!values) {
        return statsCallback(null)
      }
      values = values.map(function (v) {
        var rv = getStatsValue(v)
        return rv
      })
      var stats = {dockerId: container, image: cMetrics[container].info.image, name: cMetrics[container].info.name, value: values}
      cpuPercentTotal += values[0]
      statsCallback(stats)
    }
    statsCallback({type: 'container', name: 'count', value: containerCount})
    statsCallback({type: 'container', name: 'totalcpu', value: cpuPercentTotal})
    cMetrics = {}
  }, config.collectionInterval)
}

module.exports = dockerStatsCollector
<commit_msg>number conversion and check for NaN improved<commit_after>/*
 * @copyright Copyright (c) Sematext Group, Inc. - All Rights Reserved
 *
 * @licence SPM for Docker is free-to-use, proprietary software.
 * THIS IS PROPRIETARY SOURCE CODE OF Sematext Group, Inc. (Sematext)
 * This source code may not be copied, reverse engineered, or altered for any purpose.
 * This source code is to be used exclusively by users and customers of Sematext.
 * Please see the full license (found in LICENSE in this distribution) for details on its license and the licenses of its dependencies.
 */
var stats = require('docker-stats')
var flatten = require('flat')
var through = require('through2')
var Aggregator = require('./aggregator.js')
var SpmAgent = require('spm-agent')
var config = SpmAgent.Config
var statsCallBackFuntion = null

var opts = {
  docker: null, //  here goes options for Dockerode
  events: null,  //  an instance of docker-allcontainers,
  statsinterval: 10,
  matchByName: process.env.SPM_MATCH_BY_NAME,
  matchByImage: process.env.SPM_MATCH_BY_IMAGE,
  skipByName: process.env.SPM_SKIP_BY_NAME,
  skipByImage: process.env.SPM_SKIP_BY_IMAGE
}
// holds aggregation object for each container
var cMetrics = {}
var counter = require('measured').createCollection()

function transformStatsApi1_21 (stats) {
  if (!stats.networks) {
    return stats
  }
  var result = {
      rx_bytes: 0,
      rx_packets: 0,
      rx_errors: 0,
      rx_dropped: 0,
      tx_bytes: 0,
      tx_packets: 0,
      tx_errors: 0,
      tx_dropped: 0
  }
  Object.keys(stats.networks).forEach(function aggNetworkMetrics (key) {
    Object.keys(result).forEach(function (metric) {
      result[metric] = result[metric] + getValueAsNum(stats.networks[key][metric])
    })
  })
  stats.network = result
  if (process.env.spmagent_logger__level === 'debug') {
    console.log('docker stats transformStatsApi1_21: ' + JSON.stringify(stats))
  }
  return stats
}

// handling different structures of docker stats
function transformStats (stats) {
  if (process.env.spmagent_logger__level === 'debug') {
    console.log('docker stats: ' + JSON.stringify(stats))
  }
  if (stats.network && stats.network.rx_bytes) {
    // API < 1.21
    return stats
  } else {
    return transformStatsApi1_21(stats)
  }
}

function processStats (chunk, enc, cb) {
  var stats = flatten(transformStats(chunk.stats))
  var metric = {}
  if (process.env.spmagent_logger__level === 'silly') {
    console.log(stats)
  }
  counter.meter('requestsPerSecond').mark()
  if (cMetrics[chunk.id] === undefined) {
    cMetrics[chunk.id] = new Aggregator()
  }
  metric.name = chunk.name
  metric.dockerId = chunk.id
  metric.image = chunk.image
  var agg = cMetrics[chunk.id]
  agg.info = metric
  metrics.forEach(function (key) {
    var value = stats[key] || 0
    if (/service_time_/i.test(key) || /wait_time_/i.test(key)) {
      // micro seconds to ms
      value = value / (1000 * 1000)
    }
    agg.update(stats['read'], key, getValueAsNum(value), useDivForAggregation[key] || false)
  })
  // setTimeout (cb, 10)
  cb()
}

function getDockerStats (dockerStatsCallback) {
  var dockerStatsStream = stats(opts)
  dockerStatsStream.pipe(through.obj(processStats))
  dockerStatsStream.on('closed', function () {
    console.log('docker stats stream closed')
    // process.exit()
  })
  dockerStatsStream.once('error', function (err) {
    console.log('docker stats stream error:' + err)
    // try to reconnect
    setTimeout(function () {
      getDockerStats(dockerStatsCallback)
    }, 15000)
    try {
      dockerStatsStream.close()
    } catch (ex) {
      console.log(ex)
    }
  })
}

function getStatsForContainer (container, aggStats) {
  var agg = cMetrics[container]
  if (!agg) {
    return null
  }
  var values = []
  aggStats.forEach(function (key) {
    var metricStats = agg.get(key)
    metricStats.id = container
    values.push(metricStats)
  })
  agg.reset()
  return values
}

var cpuMetrics = [
  // CPU
  // 'cpu_stats.cpu_usage.total_usage',
  // 'cpu_stats.cpu_usage.percpu_usage.0',
  // 'cpu_stats.cpu_usage.usage_in_kernelmode',
  // 'cpu_stats.cpu_usage.usage_in_usermode',
  'cpu_stats.cpu_usage.cpu_percent',
  // 'cpu_stats.system_cpu_usage',
  // 'cpu_stats.throttling_data.periods',
  // 'cpu_stats.throttling_data.throttled_periods',
  'cpu_stats.throttling_data.throttled_time'
]

var networkMetrics = [
  // NETWORK
  'network.rx_bytes',
  'network.rx_packets',
  'network.rx_errors',
  'network.rx_dropped',
  'network.tx_bytes',
  'network.tx_packets',
  'network.tx_errors',
  'network.tx_dropped'
]

var memoryMetrics = [
  // MEMORY
  'memory_stats.usage',
  'memory_stats.limit',
  'memory_stats.failcnt',
  // 'memory_stats.max_usage',
  // 'memory_stats.active_anon',
  // 'memory_stats.active_file',
  // 'memory_stats.cache',
  // 'memory_stats.hierarchical_memory_limit',
  // 'memory_stats.inactive_anon',
  // 'memory_stats.inactive_file',
  // 'memory_stats.mapped_file',
  'memory_stats.stats.pgfault',
  // 'memory_stats.pgmajfault',
  'memory_stats.stats.pgpgin',
  'memory_stats.stats.pgpgout'
  // 'memory_stats.ss',
  // 'memory_stats.rss_huge',
  // MEMORY FAIL COUNTER

  // MEMORY LIMIT

// MEMORY TOTAL
// 'memory_stats.total_active_anon',
// 'memory_stats.total_active_file',
// 'memory_stats.total_cache',
// 'memory_stats.total_inactive_anon',
// 'memory_total_inactive_file',
// 'memory_stats.total_mapped_file',
// 'memory_stats.total_pgfault',
// 'memory_stats.total_pgmajfault',
// 'memory_stats.total_pgpgin',
// 'memory_stats.total_pgpgout',
// 'memory_stats.total_rss',
// 'memory_stats.total_rss_huge',
// 'memory_stats.total_unevictable',
// 'memory_stats.stats.total_writeback',
// 'memory_stats.unevictable',
// 'memory_stats.writeback'
]

var ioMetrics = [
  // IO STATS
  // READ
  'blkio_stats.io_service_bytes_recursive.0.value',
  // READ time
  'blkio_stats.io_service_time_recursive.0.value',
  // READ wait time
  'blkio_stats.io_wait_time_recursive.0.value',
  // WRITE
  'blkio_stats.io_service_bytes_recursive.1.value',
  // Write time
  'blkio_stats.io_service_time_recursive.1.value',
  // Write wait time
  'blkio_stats.io_wait_time_recursive.1.value'
// 'blkio_io_service_bytes_recursive',
// 'blkio_io_serviced_recursive',
// 'blkio_io_queue_recursive',
// 'blkio_io_service_time_recursive',
// 'blkio_io_wait_time_recursive',
// 'blkio_io_merged_recursive',
// 'blkio_io_time_recursive',
// 'blkio_sectors_recursive'
]
var metrics = []
metrics = metrics.concat(cpuMetrics).concat(networkMetrics).concat(memoryMetrics).concat(ioMetrics)
var avgAggregation = {
  'cpu_stats.cpu_usage.cpu_percent': true,
  'cpu_stats.throttling_data.throttled_time': true,
  'memory_stats.usage': true,
  'memory_stats.limit': true,
  // READ time
  'blkio_stats.io_service_time_recursive.0.value': true,
  // READ wait time
  'blkio_stats.io_wait_time_recursive.0.value': true,
  // Write time
  'blkio_stats.io_service_time_recursive.1.value': true,
  // Write wait time
  'blkio_stats.io_wait_time_recursive.1.value': true
}
var useDivForAggregation = {
  'network.rx_bytes': true,
  'network.rx_packets': true,
  'network.rx_errors': true,
  'network.rx_dropped': true,
  'network.tx_bytes': true,
  'network.tx_packets': true,
  'network.tx_errors': true,
  'network.tx_dropped': true,
  'memory_stats.failcnt': true,
  'memory_stats.stats.pgfault': true,
  'memory_stats.stats.pgpgin': true,
  'memory_stats.stats.pgpgout': true,
  'blkio_stats.io_service_bytes_recursive.0.value': true,
  // READ time
  'blkio_stats.io_service_time_recursive.0.value': true,
  // READ wait time
  'blkio_stats.io_wait_time_recursive.0.value': true,
  // WRITE
  'blkio_stats.io_service_bytes_recursive.1.value': true,
  // Write time
  'blkio_stats.io_service_time_recursive.1.value': true,
  // Write wait time
  'blkio_stats.io_wait_time_recursive.1.value': true
}

function getValueAsNum (value) {
  var rv = 0
  var nuVal = Number(value)
  if (!isNaN(nuVal) {
    rv = nuVal
  }
  return rv
}

function getStatsValue (metric) {
  if (avgAggregation[metric.name]) {
    return getValueAsNum(metric.mean)
  } else {
    return getValueAsNum(metric.sum)
  }
}
function dockerStatsCollector (statsCallback) {
  // start streaming docker stats
  getDockerStats()
  setInterval(function () {
    var containerCount = 0
    var cpuPercentTotal = 0
    for (var container in cMetrics) {
      containerCount++
      var values = getStatsForContainer(container, metrics)
      if (!values) {
        return statsCallback(null)
      }
      values = values.map(function (v) {
        var rv = getStatsValue(v)
        return rv
      })
      var stats = {dockerId: container, image: cMetrics[container].info.image, name: cMetrics[container].info.name, value: values}
      cpuPercentTotal += values[0]
      statsCallback(stats)
    }
    statsCallback({type: 'container', name: 'count', value: containerCount})
    statsCallback({type: 'container', name: 'totalcpu', value: cpuPercentTotal})
    cMetrics = {}
  }, config.collectionInterval)
}

module.exports = dockerStatsCollector
<|endoftext|>


<commit_before>/* See license.txt for terms of usage */

"use strict";

module.metadata = {
  "stability": "experimental"
};

var self = require("sdk/self");
var main = require("../main.js");

const { Cu, Ci } = require("chrome");
const { BasePanel } = require("../chrome/base-panel.js");
const { Class } = require("sdk/core/heritage");
const { Trace, TraceError } = require("../core/trace.js").get(module.id);
const { Tool } = require("dev/toolbox");
const { Locale } = require("../core/locale.js");
const { DomTree } = require("../dom/dom-tree.js");
const { DomProvider } = require("../dom/dom-provider.js");
const { loadSheet, removeSheet } = require("sdk/stylesheet/utils");
const { defer } = require("sdk/core/promise");
const { getFrameElement } = require("sdk/window/utils");
const { Dom } = require("../core/dom.js");
const { target } = require("../target.js");
const { Theme } = require("../chrome/theme.js");
const { setTimeout } = require("sdk/timers");

const { gDevTools } = Cu.import("resource:///modules/devtools/gDevTools.jsm", {});

// xxxHonza: part of the workaround for 1075490 (see the bottom of the file).
// Use the same weird panel ID that is generated by the SDK.
// There are places in the code (test, unregistering) that use the ID.
const panelId = "dev-panel-firebugsoftware-joehewitt-com-DOM";

/**
 * @panel This object implements a main DOM panel. It displays structure
 * of the current document. The panel might be converted into DevTools SDK
 * example showing how to render remote objects (grips) by using repository
 * of registered templates {@Reps}.
 */
const DomPanel = Class(
/** @lends DomPanel */
{
  extends: BasePanel,

  label: Locale.$STR("dom.panel.title"),
  tooltip: Locale.$STR("dom.panel.tip"),
  icon: "./icon-16.png",
  url: "./dom.html",
  searchable: true,

  // Initialization
  initialize: function(options) {
    BasePanel.prototype.initialize.apply(this, arguments);

    Trace.sysout("DomPanel.initialize;", options);
  },

  setup: function({debuggee, frame, toolbox}) {
    BasePanel.prototype.setup.apply(this, arguments);

    Trace.sysout("DomPanel.setup;", frame);
  },

  open: function(window, toolbox) {
    let parentDoc = toolbox.doc;
    let iframe = parentDoc.getElementById("toolbox-panel-iframe-" + panelId);

    let self = this;

    // Make sure the iframe content window is ready.
    let deferredIframe = defer();
    let win = iframe.contentWindow;
    if (win && win.document && win.document.readyState === "complete") {
      Trace.sysout("domPanel.complete;");
      deferredIframe.resolve(self);
    }
    else {
      iframe.addEventListener("load", function onIframeLoad() {
        iframe.removeEventListener("load", onIframeLoad, true);
        Trace.sysout("domPanel.load;");
        deferredIframe.resolve(self);
      }, true);
    }

    this.chrome.getPanelWhenReady(panelId).then(panel => {
      Trace.sysout("ready ", panel)
      panel.onReady();
    });

    return deferredIframe.promise;
  },

  onReady: function() {
    BasePanel.prototype.onReady.apply(this, arguments);

    Trace.sysout("DomPanel.onReady;", this);

    // xxxHonza: dom.html doesn't include the theme-switching.js file
    // and so, theme events are not fired for it FIX ME
    // We might want to change dom.html to dom.xul and put it into
    // chrome directory to also fix:
    // issue #15 - DomTree items are not expandable
    // In such case 'onStateChange' event that causes onReady method
    // to be executed doesn't have to be called.
    let win = this.getPanelWindow();
    loadSheet(win, "chrome://firebug/skin/domTree.css", "author");

    this.update();
  },

  initContext: function(context) {
    BasePanel.prototype.initContext.apply(this, arguments);

    this.update();
  },

  update: function() {
    Trace.sysout("domPanel.update;");

    // Get the current {@ThreadClient}. It might cause tab attach
    // (happens asynchronously) if the toolbox isn't attached yet.
    let target = this.toolbox.target;
    target.activeTab.attachThread({}, (response, threadClient) => {
      Trace.sysout("domPanel.update; threadClient", arguments);

      // Attach Console. It might involve RDP communication, so wait
      // asynchronously for the result
      this.attachConsole(threadClient).then(consoleClient => {
        Trace.sysout("domPanel.update; consoleClient", arguments);

        // Evaluate an expression to get the top level actor.
        // The goal is to render that actor in the panel content.
        consoleClient.evaluateJS("document", (response) => {
          Trace.sysout("domPanel.update; document", arguments);

          this.refresh(response.result);

          // xxxHonza: this is needed? Shouldn't it resume automatically?
          threadClient.resume();
        });

        // An example of getting node info (e.g. isDisplayed)
        /*this.toolbox.initInspector().then(() => {
          let expr = "document.getElementById('hidden')";
          consoleClient.evaluateJS(expr, (response) => {
            let objectActor = response.result;
            let walker = this.toolbox.walker;
            walker.getNodeActorFromObjectActor(objectActor.actor).
              then((nodeFront) => {
                Trace.sysout("nodeFront", nodeFront);
              });
          });
        })*/
      });
    });
  },

  attachConsole: function(threadClient) {
    let deferred = defer();
    let debuggerClient = threadClient.client;
    let consoleActor = this.toolbox.target.form.consoleActor;

    debuggerClient.attachConsole(consoleActor, ["ConsoleAPI"],
      (response, webConsoleClient) => {

      Trace.sysout("domPanel.attachConsole; ", arguments);

      if (response.error) {
        deferred.reject(response);
      } else {
        deferred.resolve(webConsoleClient);
      }
    });

    return deferred.promise;
  },

  // Selection
  select: function(object) {
    Trace.sysout("domPanel.select; object:", object);

    this.refresh(object);
  },

  refresh: function(actor) {
    Trace.sysout("domPanel.refresh; actor:", actor);

    let target = this.toolbox.target;
    if (!target) {
      return;
    }

    let context = this.getContext();
    context.getCache().then(cache => {
      let provider = new DomProvider(cache);
      this.tree = new DomTree(provider);
      this.tree.replace(this.panelNode, {object: actor});

      this.emit("refreshed", this.tree);
    });
  },

  // Theme

  onApplyTheme: function(iframeWin, oldTheme) {
    Trace.sysout("domPanel.onApplyTheme;");
  },

  onUnapplyTheme: function(iframeWin, newTheme) {
    Trace.sysout("domPanel.onUnapplyTheme;");
  },

  // Toolbar

  getPanelToolbarButtons: function() {
    let buttons = [];

    buttons.push({
      label: "dom.refresh.label",
      tooltiptext: "dom.refresh.tip",
      command: this.onRefresh.bind(this)
    });

    return buttons;
  },

  // Commands

  onRefresh: function() {
    this.update();
  },

  // Search

  onSearch: function(text, reverse) {
    BasePanel.prototype.onSearch.apply(this, arguments);

    Trace.sysout("domPanel.onSearch; " + text);
  },

  findSearchResultContainer: function(node) {
    return Dom.getAncestorByClass(node, "memberRow");
  },
});

// DOM Panel Registration

function registerDomPanel() {
  if (gDevTools.getToolDefinition(panelId)) {
    return;
  }

  // xxxHonza: workaround for SDK issue: Use of type=content iframes in
  // toolbox breaks dock to side feature
  // https://bugzilla.mozilla.org/show_bug.cgi?id=1075490
  // Let's create the pane using the platform API instead of SDK.
  // The SDK replaces the original iframe with new iframe that
  // uses type="content".
  gDevTools.registerTool({
    id: panelId,
    url: self.data.url("dom.html"),
    label: Locale.$STR("dom.panel.title"),
    tooltip: Locale.$STR("dom.panel.tip"),
    isTargetSupported: target => target.isLocalTab,
    build: (window, toolbox) => {
      let frame = getFrameElement(window);
      let doc = frame.ownerDocument;
      let panel = new DomPanel();
      frame = panel.createView(doc, frame);
      panel.setup({window: window, toolbox: toolbox, frame: frame});
      return panel.open(window, toolbox);
    }
  });

  /*const domTool = new Tool({
    name: "DOM Tool",
    panels: {
      domPanel: DomPanel
    }
  });*/
}

function unregisterDomPanel() {
  if (!gDevTools.getToolDefinition(panelId)) {
    return;
  }

  gDevTools.unregisterTool({
    id: panelId
  });
}

// Global Theme Events

/**
 * The DOM panel is available only when the Firebug theme is activated.
 * So, make sure it's registered and unregistered properly according
 * to the theme activation.
 */
function onApplyTheme(win) {
  let toolbox = main.Firebug.getToolbox(win);
  if (!toolbox) {
    return;
  }

  // Registering a panel in 'onThemeApply' event handler happens
  // at a special moment where the Toolbox instance already exists,
  // receives "tool-registered" events, but the "toolbox-tabs"
  // XUL element isn't available yet. The result is an exception saying
  // TypeError: tabs is null: toolbox.js:864 (within _buildTabForTool)
  // This can't happen when the panel is registered during module load
  // since the Toolbox doesn't exist yet and can't receive "tool-registered"
  // event. The toolbox auto-creates tabs for registered panels when opened.
  // See also: https://github.com/firebug/firebug.next/issues/213
  let tabs = toolbox.doc.getElementById("toolbox-tabs");
  if (tabs) {
    setTimeout(() => {
      registerDomPanel();
    });
  }
}

function onUnapplyTheme() {
  unregisterDomPanel();
}

// Initialization and Shutdown

target.on("initialize", Firebug => {
  Trace.sysout("domPanel.module.initialize;");

  Theme.addThemeListeners(onApplyTheme, onUnapplyTheme);
});

target.on("shutdown", Firebug => {
  Trace.sysout("domPanel.module.shutdown;");

  Theme.removeThemeListeners(onApplyTheme, onUnapplyTheme);

  onUnapplyTheme();
});

// Exports from this module
exports.DomPanel = DomPanel;
exports.domPanelId = panelId;
<commit_msg>Use panel registrar events for DOM panel registration<commit_after>/* See license.txt for terms of usage */

"use strict";

module.metadata = {
  "stability": "experimental"
};

var self = require("sdk/self");
var main = require("../main.js");

const { Cu, Ci } = require("chrome");
const { BasePanel } = require("../chrome/base-panel.js");
const { Class } = require("sdk/core/heritage");
const { Trace, TraceError } = require("../core/trace.js").get(module.id);
const { Tool } = require("dev/toolbox");
const { Locale } = require("../core/locale.js");
const { DomTree } = require("../dom/dom-tree.js");
const { DomProvider } = require("../dom/dom-provider.js");
const { loadSheet, removeSheet } = require("sdk/stylesheet/utils");
const { defer } = require("sdk/core/promise");
const { getFrameElement } = require("sdk/window/utils");
const { Dom } = require("../core/dom.js");
const { target } = require("../target.js");
const { Theme } = require("../chrome/theme.js");
const { setTimeout } = require("sdk/timers");

const { gDevTools } = Cu.import("resource:///modules/devtools/gDevTools.jsm", {});

// xxxHonza: part of the workaround for 1075490 (see the bottom of the file).
// Use the same weird panel ID that is generated by the SDK.
// There are places in the code (test, unregistering) that use the ID.
const panelId = "dev-panel-firebugsoftware-joehewitt-com-DOM";

/**
 * @panel This object implements a main DOM panel. It displays structure
 * of the current document. The panel might be converted into DevTools SDK
 * example showing how to render remote objects (grips) by using repository
 * of registered templates {@Reps}.
 */
const DomPanel = Class(
/** @lends DomPanel */
{
  extends: BasePanel,

  label: Locale.$STR("dom.panel.title"),
  tooltip: Locale.$STR("dom.panel.tip"),
  icon: "./icon-16.png",
  url: "./dom.html",
  searchable: true,

  // Initialization
  initialize: function(options) {
    BasePanel.prototype.initialize.apply(this, arguments);

    Trace.sysout("DomPanel.initialize;", options);
  },

  setup: function({debuggee, frame, toolbox}) {
    BasePanel.prototype.setup.apply(this, arguments);

    Trace.sysout("DomPanel.setup;", frame);
  },

  open: function(window, toolbox) {
    let parentDoc = toolbox.doc;
    let iframe = parentDoc.getElementById("toolbox-panel-iframe-" + panelId);

    let self = this;

    // Make sure the iframe content window is ready.
    let deferredIframe = defer();
    let win = iframe.contentWindow;
    if (win && win.document && win.document.readyState === "complete") {
      Trace.sysout("domPanel.complete;");
      deferredIframe.resolve(self);
    }
    else {
      iframe.addEventListener("load", function onIframeLoad() {
        iframe.removeEventListener("load", onIframeLoad, true);
        Trace.sysout("domPanel.load;");
        deferredIframe.resolve(self);
      }, true);
    }

    this.chrome.getPanelWhenReady(panelId).then(panel => {
      Trace.sysout("ready ", panel)
      panel.onReady();
    });

    return deferredIframe.promise;
  },

  onReady: function() {
    BasePanel.prototype.onReady.apply(this, arguments);

    Trace.sysout("DomPanel.onReady;", this);

    // xxxHonza: dom.html doesn't include the theme-switching.js file
    // and so, theme events are not fired for it FIX ME
    // We might want to change dom.html to dom.xul and put it into
    // chrome directory to also fix:
    // issue #15 - DomTree items are not expandable
    // In such case 'onStateChange' event that causes onReady method
    // to be executed doesn't have to be called.
    let win = this.getPanelWindow();
    loadSheet(win, "chrome://firebug/skin/domTree.css", "author");

    this.update();
  },

  initContext: function(context) {
    BasePanel.prototype.initContext.apply(this, arguments);

    this.update();
  },

  update: function() {
    Trace.sysout("domPanel.update;");

    // Get the current {@ThreadClient}. It might cause tab attach
    // (happens asynchronously) if the toolbox isn't attached yet.
    let target = this.toolbox.target;
    target.activeTab.attachThread({}, (response, threadClient) => {
      Trace.sysout("domPanel.update; threadClient", arguments);

      // Attach Console. It might involve RDP communication, so wait
      // asynchronously for the result
      this.attachConsole(threadClient).then(consoleClient => {
        Trace.sysout("domPanel.update; consoleClient", arguments);

        // Evaluate an expression to get the top level actor.
        // The goal is to render that actor in the panel content.
        consoleClient.evaluateJS("document", (response) => {
          Trace.sysout("domPanel.update; document", arguments);

          this.refresh(response.result);

          // xxxHonza: this is needed? Shouldn't it resume automatically?
          threadClient.resume();
        });

        // An example of getting node info (e.g. isDisplayed)
        /*this.toolbox.initInspector().then(() => {
          let expr = "document.getElementById('hidden')";
          consoleClient.evaluateJS(expr, (response) => {
            let objectActor = response.result;
            let walker = this.toolbox.walker;
            walker.getNodeActorFromObjectActor(objectActor.actor).
              then((nodeFront) => {
                Trace.sysout("nodeFront", nodeFront);
              });
          });
        })*/
      });
    });
  },

  attachConsole: function(threadClient) {
    let deferred = defer();
    let debuggerClient = threadClient.client;
    let consoleActor = this.toolbox.target.form.consoleActor;

    debuggerClient.attachConsole(consoleActor, ["ConsoleAPI"],
      (response, webConsoleClient) => {

      Trace.sysout("domPanel.attachConsole; ", arguments);

      if (response.error) {
        deferred.reject(response);
      } else {
        deferred.resolve(webConsoleClient);
      }
    });

    return deferred.promise;
  },

  // Selection
  select: function(object) {
    Trace.sysout("domPanel.select; object:", object);

    this.refresh(object);
  },

  refresh: function(actor) {
    Trace.sysout("domPanel.refresh; actor:", actor);

    let target = this.toolbox.target;
    if (!target) {
      return;
    }

    let context = this.getContext();
    context.getCache().then(cache => {
      let provider = new DomProvider(cache);
      this.tree = new DomTree(provider);
      this.tree.replace(this.panelNode, {object: actor});

      this.emit("refreshed", this.tree);
    });
  },

  // Theme

  onApplyTheme: function(iframeWin, oldTheme) {
    Trace.sysout("domPanel.onApplyTheme;");
  },

  onUnapplyTheme: function(iframeWin, newTheme) {
    Trace.sysout("domPanel.onUnapplyTheme;");
  },

  // Toolbar

  getPanelToolbarButtons: function() {
    let buttons = [];

    buttons.push({
      label: "dom.refresh.label",
      tooltiptext: "dom.refresh.tip",
      command: this.onRefresh.bind(this)
    });

    return buttons;
  },

  // Commands

  onRefresh: function() {
    this.update();
  },

  // Search

  onSearch: function(text, reverse) {
    BasePanel.prototype.onSearch.apply(this, arguments);

    Trace.sysout("domPanel.onSearch; " + text);
  },

  findSearchResultContainer: function(node) {
    return Dom.getAncestorByClass(node, "memberRow");
  },
});

/**
 * The DOM panel is only available if the Firebug theme
 * is active.
 */
target.on("onRegisterPanels", registrar => {
  registrar.registerPanel(panelId, DomPanel);
});

target.on("onUnregisterPanels", registrar => {
  registrar.unregisterPanel(panelId);
});

// Exports from this module
exports.DomPanel = DomPanel;
exports.domPanelId = panelId;
<|endoftext|>


<commit_before>const verboseLog = require('./logger.js');
const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
const process = require('process');
const addSkipped = require('./rules-util.js').addSkipped;
const processRules = require('./rules-util.js').processRules;

const rulesConf = {
    name: 'bower.json',
    rules: [
        {
            desc: 'should not have "version" property',
            errorMsg: 'remove "version" from bower.json, bower 1.7.0 and upwards doesn\'t use it anyway',
            fn: notUsingVersio
        }
    ]
};

module.exports = function(options) {
    'use strict';
    const filename = 'bower.json';
    const packagePath = path.join(process.cwd(), filename);
    verboseLog(chalk.underline(`Examining ${filename}`), options);

    verboseLog(`using ${packagePath}`, options);

    if (!fs.existsSync(packagePath)) {
        console.error(
            chalk.red(`ABORTING: No ${filename} found in ${process.cwd()}`)
        );
        return addSkipped(rulesConf);
    }

    const data = fs.readFileSync(packagePath);

    const json = JSON.parse(data);

    // work through all rules, setting passed = true/false
    rulesConf.rules = processRules(rulesConf, json, options);
    return rulesConf;
};

// add more test functions below, should return true if passing the test

function notUsingVersio(json) {
    if (json && !json.version) {
        return true;
    }
}
<commit_msg>fix typo<commit_after>const verboseLog = require('./logger.js');
const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
const process = require('process');
const addSkipped = require('./rules-util.js').addSkipped;
const processRules = require('./rules-util.js').processRules;

const rulesConf = {
    name: 'bower.json',
    rules: [
        {
            desc: 'should not have "version" property',
            errorMsg: 'remove "version" from bower.json, bower 1.7.0 and upwards doesn\'t use it anyway',
            fn: notUsingVersion
        }
    ]
};

module.exports = function(options) {
    'use strict';
    const filename = 'bower.json';
    const packagePath = path.join(process.cwd(), filename);
    verboseLog(chalk.underline(`Examining ${filename}`), options);

    verboseLog(`using ${packagePath}`, options);

    if (!fs.existsSync(packagePath)) {
        console.error(
            chalk.red(`ABORTING: No ${filename} found in ${process.cwd()}`)
        );
        return addSkipped(rulesConf);
    }

    const data = fs.readFileSync(packagePath);

    const json = JSON.parse(data);

    // work through all rules, setting passed = true/false
    rulesConf.rules = processRules(rulesConf, json, options);
    return rulesConf;
};

// add more test functions below, should return true if passing the test

function notUsingVersion(json) {
    if (json && !json.version) {
        return true;
    }
}
<|endoftext|>


<commit_before>var fs = require('fs');
var path = require('path');
var configure = require('basisjs-tools-config');
var Promise = require('es6-promise-polyfill').Promise;
var Flow = require('../common/flow');
var command = require('./command');
var isChildProcess = typeof process.send == 'function'; // child process has send method

if (isChildProcess)
  process.on('uncaughtException', function(error){
    process.send({
      errorType: 'Exception',
      error: String(error)
    });
    process.exit(2);
  });

//
// export
//
//exports.handler = extractHandler;
exports.handlers = extractHandlers;

//
// launched by another module
//
exports.extract = function(config){
  if (this === command)
    return extract(config);

  if (this === exports)
    return extract(command.normalize(config));
};

//
// run command in child process
//
exports.fork = function(args, options){
  return require('child_process').fork(__filename, args, options);
};

//
// launched directly (i.e. node index.js ..)
//
if (process.mainModule === module)
  configure(command).run();

//
// main function
//
function extract(config){
  //
  // init
  //

  var options = command.norm(config);
  var inputFilename = options.file;
  var flow = new Flow(options, 'extract');
  var fconsole = flow.console;

  fconsole.enabled = !options.silent && options.target == 'log';

  fconsole.start('Extract settings');
  fconsole.log('Base path: ', options.base);
  fconsole.log('Index file:', options.file);
  fconsole.log('Plugins:   ', flow.plugins ? flow.plugins.join(', ') : 'NONE');
  fconsole.log('Target:    ', options.target);
  fconsole.endl();

  //
  // process input
  //

  // check input file exists
  if (!fs.existsSync(inputFilename) || !fs.statSync(inputFilename).isFile())
    flow.exit('Input file ' + inputFilename + ' not found');

  fconsole.start('\nInit\n====\n');

  // add input file in queue
  flow.indexFile = flow.files.add({
    filename: path.basename(inputFilename)
  });

  //
  // Main part
  //

  var handlers = extractHandlers(flow.options).concat([
    require('./misc/stat'),

    // target
    {
      'app-profile': require('./target/appProfile'),
      'input-graph': require('./target/inputGraph'),
      'css-usage': require('./target/cssUsage')
    }[flow.options.target],

    flow.options.target == 'log' ? require('./misc/summary') : null
  ]).filter(Boolean);

  var taskCount = 0;
  var timing = flow.timing = [];
  var time;
  var resolve = function(){};
  var result = new Promise(function(resolve_){
    resolve = resolve_;
  });

  function repeat(str, count){
    return new Array(count + 1).join(str);
  }

  function asyncTaskStart(){
    taskCount++;
  }
  function asyncTaskDone(){
    taskCount--;
    nextHandler();
  }

  function nextHandler(){
    if (!taskCount)
    {
      if (handlers.length)
      {
        var timeDiff = process.hrtime(time.time);
        time.time = parseInt(timeDiff[0] * 1e3 + timeDiff[1] / 1e6, 10);
        timing.push(time);
      }

      process.nextTick(runHandler);
    }
  }

  function runHandler(){
    if (!handlers.length)
      return resolve(flow);

    var handler = handlers.shift();
    var title = handler.handlerName || 'Untitled handler';
    var skipped = typeof handler.skip == 'function' ? handler.skip(flow) : false;

    fconsole.resetDeep();

    if (title)
      fconsole.log('\n' + title + '\n' + repeat('=', title.length) + '\n');

    fconsole.incDeep();

    if (skipped)
    {
      fconsole.log('Skipped.');
      fconsole.log(skipped);
      process.nextTick(runHandler);
    }
    else
    {
      time = {
        name: title,
        time: process.hrtime()
      };

      handler(flow, asyncTaskStart, asyncTaskDone);
      nextHandler();
    }
  }

  process.nextTick(runHandler);

  if (isChildProcess)
  {
    result.then(function(flow){
      process.send({
        data: flow.result
      });
    });
  }
  else
  {
    if (!options.silent && options.target != 'log')
      result.then(function(flow){
        console.log(flow.result);
      });

    return result;
  }
}

function extractHandlers(options){
  return [
    require('./html'),
    require('./js'),
    require('./tmpl'),
    require('./css'),
    require('./res'),
    require('./l10n'),

    options && options.jsInfo ? require('./js/collectInfo') : null,
    options && options.l10nInfo ? require('./l10n/collectInfo') : null,
    options && options.cssInfo ? require('./css/collectInfo') : null
  ].filter(Boolean);
}

// function extractHandler(flow){
//   extractHandlers().forEach(function(handler){
//     handler(flow);
//   });
// }
// extractHandler.handlerName = 'Extract file graph';
<commit_msg>extract: add tools version to banner<commit_after>var fs = require('fs');
var path = require('path');
var configure = require('basisjs-tools-config');
var Promise = require('es6-promise-polyfill').Promise;
var utils = require('../common/utils');
var Flow = require('../common/flow');
var command = require('./command');
var isChildProcess = typeof process.send == 'function'; // child process has send method

if (isChildProcess)
  process.on('uncaughtException', function(error){
    process.send({
      errorType: 'Exception',
      error: String(error)
    });
    process.exit(2);
  });

//
// export
//
//exports.handler = extractHandler;
exports.handlers = extractHandlers;

//
// launched by another module
//
exports.extract = function(config){
  if (this === command)
    return extract(config);

  if (this === exports)
    return extract(command.normalize(config));
};

//
// run command in child process
//
exports.fork = function(args, options){
  return require('child_process').fork(__filename, args, options);
};

//
// launched directly (i.e. node index.js ..)
//
if (process.mainModule === module)
  configure(command).run();

//
// main function
//
function extract(config){
  //
  // init
  //

  var options = command.norm(config);
  var inputFilename = options.file;
  var flow = new Flow(options, 'extract');
  var fconsole = flow.console;

  fconsole.enabled = !options.silent && options.target == 'log';

  fconsole.start('Extract with ' + utils.getToolsId());
  fconsole.log('Base path: ', options.base);
  fconsole.log('Index file:', options.file);
  fconsole.log('Plugins:   ', flow.plugins ? flow.plugins.join(', ') : 'NONE');
  fconsole.log('Target:    ', options.target);
  fconsole.endl();

  //
  // process input
  //

  // check input file exists
  if (!fs.existsSync(inputFilename) || !fs.statSync(inputFilename).isFile())
    flow.exit('Input file ' + inputFilename + ' not found');

  fconsole.start('\nInit\n====\n');

  // add input file in queue
  flow.indexFile = flow.files.add({
    filename: path.basename(inputFilename)
  });

  //
  // Main part
  //

  var handlers = extractHandlers(flow.options).concat([
    require('./misc/stat'),

    // target
    {
      'app-profile': require('./target/appProfile'),
      'input-graph': require('./target/inputGraph'),
      'css-usage': require('./target/cssUsage')
    }[flow.options.target],

    flow.options.target == 'log' ? require('./misc/summary') : null
  ]).filter(Boolean);

  var taskCount = 0;
  var timing = flow.timing = [];
  var time;
  var resolve = function(){};
  var result = new Promise(function(resolve_){
    resolve = resolve_;
  });

  function repeat(str, count){
    return new Array(count + 1).join(str);
  }

  function asyncTaskStart(){
    taskCount++;
  }
  function asyncTaskDone(){
    taskCount--;
    nextHandler();
  }

  function nextHandler(){
    if (!taskCount)
    {
      if (handlers.length)
      {
        var timeDiff = process.hrtime(time.time);
        time.time = parseInt(timeDiff[0] * 1e3 + timeDiff[1] / 1e6, 10);
        timing.push(time);
      }

      process.nextTick(runHandler);
    }
  }

  function runHandler(){
    if (!handlers.length)
      return resolve(flow);

    var handler = handlers.shift();
    var title = handler.handlerName || 'Untitled handler';
    var skipped = typeof handler.skip == 'function' ? handler.skip(flow) : false;

    fconsole.resetDeep();

    if (title)
      fconsole.log('\n' + title + '\n' + repeat('=', title.length) + '\n');

    fconsole.incDeep();

    if (skipped)
    {
      fconsole.log('Skipped.');
      fconsole.log(skipped);
      process.nextTick(runHandler);
    }
    else
    {
      time = {
        name: title,
        time: process.hrtime()
      };

      handler(flow, asyncTaskStart, asyncTaskDone);
      nextHandler();
    }
  }

  process.nextTick(runHandler);

  if (isChildProcess)
  {
    result.then(function(flow){
      process.send({
        data: flow.result
      });
    });
  }
  else
  {
    if (!options.silent && options.target != 'log')
      result.then(function(flow){
        console.log(flow.result);
      });

    return result;
  }
}

function extractHandlers(options){
  return [
    require('./html'),
    require('./js'),
    require('./tmpl'),
    require('./css'),
    require('./res'),
    require('./l10n'),

    options && options.jsInfo ? require('./js/collectInfo') : null,
    options && options.l10nInfo ? require('./l10n/collectInfo') : null,
    options && options.cssInfo ? require('./css/collectInfo') : null
  ].filter(Boolean);
}

// function extractHandler(flow){
//   extractHandlers().forEach(function(handler){
//     handler(flow);
//   });
// }
// extractHandler.handlerName = 'Extract file graph';
<|endoftext|>


<commit_before>
var shell = require( 'shelljs' );

function gitPull( root, options )
{
    return function( req, res, next ) {
        var cmd = 'git pull' + ( options.rebase ? ' --rebase' : '' );

        shell.cd( root );
        shell.exec( cmd, function( code, output ) {
            console.log( cmd + ' exited with code ' + code );
        } );

        next();
    };
};

module.exports = function( req, res, done )
{
  var root = '/var/www/hackersatepfl.com';

  gitPull( root, { rebase: true } )( req, res, next );

  function next( req, res ) {
    shell.exec( 'npm install' );
    shell.exec( 'compass compile -e production --force public/' );
    done();
  }
};
<commit_msg>Minor update.<commit_after>
var shell = require( 'shelljs' );

function gitPull( root, options )
{
    return function( req, res, next ) {
        var cmd = 'git pull' + ( options.rebase ? ' --rebase' : '' );
        shell.cd( root );
        shell.exec( cmd );
        next();
    };
};

module.exports = function( req, res, done )
{
  console.log( '-- Received a GitHub WebHook notification.' );

  var root = '/var/www/hackersatepfl.com';

  gitPull( root, { rebase: true } )( req, res, next );

  function next( req, res ) {
    shell.cd( root );
    shell.exec( 'npm install' );
    shell.exec( 'compass compile -e production --force public/' );
    done();
  }
};
<|endoftext|>