<commit_before>(function() {

  cartodb.createVis = function(el, vizjson, options, callback) {
    if(!el) {
      throw new TypeError("an dom element should be provided");
    }
    var args = arguments,
    fn = args[args.length -1];
    if(_.isFunction(fn)) {
      callback = fn;
    }
    el = (typeof el === 'string' ? document.getElementById(el) : el);
    var vis = new cartodb.vis.Vis({ el: el });
    if(vizjson) {
      vis.load(vizjson, options);
      if(callback) {
        vis.done(callback);
      }
    }
    return vis;
  };

})();
<commit_msg>Fixes misspelling<commit_after>(function() {

  cartodb.createVis = function(el, vizjson, options, callback) {

    if (!el) {
      throw new TypeError("a dom element should be provided");
    }

    var
    args = arguments,
    fn   = args[args.length -1];

    if (_.isFunction(fn)) {
      callback = fn;
    }

    el = (typeof el === 'string' ? document.getElementById(el) : el);

    var vis = new cartodb.vis.Vis({ el: el });

    if (vizjson) {

      vis.load(vizjson, options);

      if (callback) {
        vis.done(callback);
      }

    }

    return vis;

  };

})();
<|endoftext|>


<commit_before>describe('a grapher instance with target', function () {
  var network, grapher;
  var index = 1;
  const MAX_PERFORMANCE_NODES = 10000;
  const MAX_PERFORMANCE_LINKS = 10000;
  const MAX_X = 2000;
  const MAX_Y = 2000;
  const MAX_R = 50;

  function getRandomInt(max) {
    return Math.floor(Math.random() * Math.floor(max));
  }

  function generateNodes(max) {
    let nodes = [max];
    for (let i = 0; i < max; i++) {
      nodes[i] = { x: getRandomInt(MAX_X),
                   y: getRandomInt(MAX_Y),
                   r: getRandomInt(MAX_R)
                 }
    }
    return nodes;
  }

  it('trying to find why PhantomJS breaks in Travis', function() {
    expect(0).toBe(0);
  })

});
<commit_msg>Changing let to var. Will that work?<commit_after>describe('a grapher instance with target', function () {
  var network, grapher;
  var index = 1;
  const MAX_PERFORMANCE_NODES = 10000;
  const MAX_PERFORMANCE_LINKS = 10000;
  const MAX_X = 2000;
  const MAX_Y = 2000;
  const MAX_R = 50;

  function getRandomInt(max) {
    return Math.floor(Math.random() * Math.floor(max));
  }

  function generateNodes(max) {
    var nodes = [max];
    for (let i = 0; i < max; i++) {
      nodes[i] = { x: getRandomInt(MAX_X),
                   y: getRandomInt(MAX_Y),
                   r: getRandomInt(MAX_R)
                 }
    }
    return nodes;
  }

  it('trying to find why PhantomJS breaks in Travis', function() {
    expect(0).toBe(0);
  })

});
<|endoftext|>


<commit_before>var App = function(appKey, tag){
    var key = appKey;
    var wykopTag = tag;
    var apiSrc = 'http://a.wykop.pl/';
    var gfycatApiUrl = 'http://gfycat.com/cajax/get/';
    var apiUri = apiSrc + 'tag/' + 'Entries/' + wykopTag + '/appkey,' + key + ',page,';
    var gfycatRegex = /http\:\/\/gfycat\.com\/[a-zA-Z]*/g;
    var gfycatNameRegex = /gfycat\.com\/([a-zA-Z]*)/g;
    var gifs = [];
    var maxApiPages = 4;
    var processedPages = 0;
    var iframe = {};
    var repeatFrequency = 20 * 1000;
    var appSheduledJob;
    var isStarted = false;

    (function prepareIframe(){
        iframe = document.createElement('iframe');
        iframe.id = 'gfyiframe';
        iframe.style.width = '100%';
        iframe.style.height = '100%';
    })();

    var doubleClickGfy =  function (argument) {
        var iframeDocument = iframe.contentDocument;
        var video = iframeDocument.getElementById('gfyVid1');
        var event = new MouseEvent('dblclick', {
            'view': iframe.contentWindow,
            'bubbles': true,
            'cancelable': true
        });
        video.dispatchEvent(event);
    }

    function startApp(index) {
        isStarted = true;
        iframe.src = gifs[index]['url'];
        var length = gifs[index]['length'];

        document.replaceChild(iframe, document.documentElement);
        iframe.onload = doubleClickGfy;
        var timeout = length > 0 ? length : repeatFrequency;
        if (index == gifs.length) {
            index = -1;
        }
        index++;
        appSheduledJob = setTimeout(function(){startApp(index)}, timeout)
    }

    this.isStarted = function(){
        return isStarted;
    }

    this.stopApp = function(){
        isStarted = false;
        clearTimeout(appSheduledJob);
    }

    function parseGfycatResponse(response) {
        if(this.readyState == XMLHttpRequest.DONE && this.status==200) {
            response = JSON.parse(this.responseText);
            if(response.items !== null && response.items !== undefined){
                for(var i = 0; i < response.items.length; i++){
                    prepareEntry(response.items[i]);
                }
            }
        }
    }

    function fetchGfyInfo(url, gfyInfo) {
        var gfycatNameRegex = /gfycat\.com\/([a-zA-Z]*)/g;
        var gfyName = gfycatNameRegex.exec(url);
        gfyName = gfyName[1];

        //doAjax(gfycatApiUrl + gfyName, parseGfycatResponse);
    }

    function addGfy(url) {
        gfyInfo = {
            'url': url,
            'length': 0
        };
        fetchGfyInfo(url, gfyInfo);
        gifs.push(gfyInfo);
    }

    function doAjax(url, responseHandler) {
        var url = url || "";
        var method =  "GET";
        var async =  true;

        if (url != "") {
            var xmlHttpRequst = new XMLHttpRequest();
            xmlHttpRequst.open(method, url, async);
            xmlHttpRequst.onreadystatechange = responseHandler;
            xmlHttpRequst.send();
        }
    }

    function prepareEntry(entry) {
        var gfysInEntrysBody = gfycatRegex.exec(entry.body);
        if(gfysInEntrysBody && gfysInEntrysBody.length > 0){
            gfysInEntrysBody.every(function(oneMatch){
                addGfy(oneMatch);
            });
        }

        var embedSrc = "";
        if(entry.embed !== null) {
            embedSrc = entry.embed.source;
            var gfysInEntrysEmbed = gfycatRegex.exec(embedSrc);
            if(gfysInEntrysEmbed && gfysInEntrysEmbed.length > 0) {
                addGfy(gfysInEntrysEmbed[0]);
            }
        }
    }

    function parseWykopResponse(response) {
        if(this.readyState == XMLHttpRequest.DONE && this.status==200) {
            processedPages++;
            response = JSON.parse(this.responseText);
            if(response.items !== null && response.items !== undefined){
                for(var i = 0; i < response.items.length; i++){
                    prepareEntry(response.items[i]);
                }
            }
            if(processedPages >= maxApiPages) {
                return startApp(0);
            }
        }
    }

    function loadData(page, maxPages){
        for(; page <= maxPages; page++) {
            doAjax(apiUri + page, parseWykopResponse);
        }
    };

    loadData(1, maxApiPages);
}

if(GolgifApp === undefined || GolgifApp.isStarted() === false) {
    var GolgifApp = new App('wTUUF5swmD', 'golgif');
}
else {
    GolgifApp.stopApp();
}
<commit_msg>fixed looper<commit_after>var App = function(appKey, tag){
    var key = appKey;
    var wykopTag = tag;
    var apiSrc = 'http://a.wykop.pl/';
    var gfycatApiUrl = 'http://gfycat.com/cajax/get/';
    var apiUri = apiSrc + 'tag/' + 'Entries/' + wykopTag + '/appkey,' + key + ',page,';
    var gfycatRegex = /http\:\/\/gfycat\.com\/[a-zA-Z]*/g;
    var gfycatNameRegex = /gfycat\.com\/([a-zA-Z]*)/g;
    var gifs = [];
    var maxApiPages = 6;
    var processedPages = 0;
    var iframe = {};
    var repeatFrequency = 20 * 1000;
    var appSheduledJob;
    var isStarted = false;

    (function prepareIframe(){
        iframe = document.createElement('iframe');
        iframe.id = 'gfyiframe';
        iframe.style.width = '100%';
        iframe.style.height = '100%';
    })();

    var doubleClickGfy =  function (argument) {
        var iframeDocument = iframe.contentDocument;
        var video = iframeDocument.getElementById('gfyVid1');
        var event = new MouseEvent('dblclick', {
            'view': iframe.contentWindow,
            'bubbles': true,
            'cancelable': true
        });
        video.dispatchEvent(event);
    }

    function startApp(index) {
        isStarted = true;
        if (index == gifs.length) {
            index = 0;
        }

        iframe.src = gifs[index]['url'];
        var length = gifs[index]['length'];

        document.replaceChild(iframe, document.documentElement);
        iframe.onload = doubleClickGfy;
        var timeout = length > 0 ? length : repeatFrequency;
        index++;
        appSheduledJob = setTimeout(function(){startApp(index)}, timeout);
    }

    this.isStarted = function(){
        return isStarted;
    }

    this.stopApp = function(){
        isStarted = false;
        clearTimeout(appSheduledJob);
    }

    function parseGfycatResponse(response) {
        if(this.readyState == XMLHttpRequest.DONE && this.status==200) {
            response = JSON.parse(this.responseText);
            if(response.items !== null && response.items !== undefined){
                for(var i = 0; i < response.items.length; i++){
                    prepareEntry(response.items[i]);
                }
            }
        }
    }

    function fetchGfyInfo(url, gfyInfo) {
        var gfycatNameRegex = /gfycat\.com\/([a-zA-Z]*)/g;
        var gfyName = gfycatNameRegex.exec(url);
        gfyName = gfyName[1];

        //doAjax(gfycatApiUrl + gfyName, parseGfycatResponse);
    }

    function addGfy(url) {
        gfyInfo = {
            'url': url,
            'length': 0
        };
        fetchGfyInfo(url, gfyInfo);
        gifs.push(gfyInfo);
    }

    function doAjax(url, responseHandler) {
        var url = url || "";
        var method =  "GET";
        var async =  true;

        if (url != "") {
            var xmlHttpRequst = new XMLHttpRequest();
            xmlHttpRequst.open(method, url, async);
            xmlHttpRequst.onreadystatechange = responseHandler;
            xmlHttpRequst.send();
        }
    }

    function prepareEntry(entry) {
        var gfysInEntrysBody = gfycatRegex.exec(entry.body);
        if(gfysInEntrysBody && gfysInEntrysBody.length > 0){
            gfysInEntrysBody.every(function(oneMatch){
                addGfy(oneMatch);
            });
        }

        var embedSrc = "";
        if(entry.embed !== null) {
            embedSrc = entry.embed.source;
            var gfysInEntrysEmbed = gfycatRegex.exec(embedSrc);
            if(gfysInEntrysEmbed && gfysInEntrysEmbed.length > 0) {
                addGfy(gfysInEntrysEmbed[0]);
            }
        }
    }

    function parseWykopResponse(response) {
        if(this.readyState == XMLHttpRequest.DONE && this.status==200) {
            processedPages++;
            response = JSON.parse(this.responseText);
            if(response.items !== null && response.items !== undefined){
                for(var i = 0; i < response.items.length; i++){
                    prepareEntry(response.items[i]);
                }
            }
            if(processedPages >= maxApiPages) {
                return startApp(0);
            }
        }
    }

    function loadData(page, maxPages){
        for(; page <= maxPages; page++) {
            doAjax(apiUri + page, parseWykopResponse);
        }
    };

    loadData(1, maxApiPages);
}

if(GolgifApp === undefined || GolgifApp.isStarted() === false) {
    var GolgifApp = new App('wTUUF5swmD', 'golgif');
}
else {
    GolgifApp.stopApp();
}
<|endoftext|>


<commit_before>'use strict';

const Token = require('markdown-it/lib/token');
const sluggo = require('sluggo');

const renderPermalink = function(slug, opts, tokens, idx) {
  return tokens[idx + 1].children.unshift(Object.assign(new Token('link_open', 'a', 1), {
    attrs: [['class', opts.permalinkClass], ['href', '#' + slug]]
  }), Object.assign(new Token('text', '', 0), {
    content: opts.permalinkSymbol
  }), new Token('link_close', 'a', -1), Object.assign(new Token('text', '', 0), {
    content: ''
  }));
};

const anchor = function(md, opts) {
 Object.assign(opts, { renderPermalink });

  const titleStore = {};
  const originalHeadingOpen = md.renderer.rules.heading_open;

  md.renderer.rules.heading_open = function(...args) {
    const [tokens, idx, something, somethingelse, self] = args; // eslint-disable-line no-unused-vars

    if (tokens[idx].tag.substr(1) >= opts.level) {
      let _tokens$idx;

      const title = tokens[idx + 1].children.reduce((acc, t) => {
        return acc + t.content;
      }, '');

      let slug = sluggo(title);

      if (Object.prototype.hasOwnProperty.call(titleStore, slug)) {
        titleStore[slug] = titleStore[slug] + 1;
        slug = slug + '-' + opts.collisionSuffix + titleStore[slug].toString();
      } else {
        titleStore[slug] = 1;
      }

      (_tokens$idx = tokens[idx], !_tokens$idx.attrs && (_tokens$idx.attrs = []), _tokens$idx.attrs)
        .push(['id', slug]);

      if (opts.permalink) {
        opts.renderPermalink.apply(opts, [slug, opts].concat(args));
      }
    }

    return originalHeadingOpen
      ? originalHeadingOpen.apply(this, args)
      : self.renderToken.apply(self, args);
  };
};

module.exports = anchor;
<commit_msg>style: indentation<commit_after>'use strict';

const Token = require('markdown-it/lib/token');
const sluggo = require('sluggo');

const renderPermalink = function(slug, opts, tokens, idx) {
  return tokens[idx + 1].children.unshift(Object.assign(new Token('link_open', 'a', 1), {
    attrs: [['class', opts.permalinkClass], ['href', '#' + slug]]
  }), Object.assign(new Token('text', '', 0), {
    content: opts.permalinkSymbol
  }), new Token('link_close', 'a', -1), Object.assign(new Token('text', '', 0), {
    content: ''
  }));
};

const anchor = function(md, opts) {
  Object.assign(opts, { renderPermalink });

  const titleStore = {};
  const originalHeadingOpen = md.renderer.rules.heading_open;

  md.renderer.rules.heading_open = function(...args) {
    const [tokens, idx, something, somethingelse, self] = args; // eslint-disable-line no-unused-vars

    if (tokens[idx].tag.substr(1) >= opts.level) {
      let _tokens$idx;

      const title = tokens[idx + 1].children.reduce((acc, t) => {
        return acc + t.content;
      }, '');

      let slug = sluggo(title);

      if (Object.prototype.hasOwnProperty.call(titleStore, slug)) {
        titleStore[slug] = titleStore[slug] + 1;
        slug = slug + '-' + opts.collisionSuffix + titleStore[slug].toString();
      } else {
        titleStore[slug] = 1;
      }

      (_tokens$idx = tokens[idx], !_tokens$idx.attrs && (_tokens$idx.attrs = []), _tokens$idx.attrs)
        .push(['id', slug]);

      if (opts.permalink) {
        opts.renderPermalink.apply(opts, [slug, opts].concat(args));
      }
    }

    return originalHeadingOpen
      ? originalHeadingOpen.apply(this, args)
      : self.renderToken.apply(self, args);
  };
};

module.exports = anchor;
<|endoftext|>


<commit_before>'use strict';

var app = angular.module('licker', ['ngRoute'])
    .config(function ($routeProvider) {
      $routeProvider
          .when('/', {
            templateUrl: 'views/table.html',
            controller: 'MainCtrl'
          })
          .otherwise('/');
    });

app.controller('mainCtrl', ['$scope', 'githubApiConnector', function ($scope, githubApiConnector) {
  $scope.githubHandle = '';
  $scope.repositories = [];
  $scope.fetched_repositories = [];

  $scope.checkLicenses = function () {

    var page_parameter = '&page=';

    githubApiConnector.fetchReposForUser($scope.githubHandle).
        success(function (data) {
          $scope.fetched_repositories = data;
        }).
        error(function () {
          githubApiConnector.fetchReposForOrganisation($scope.githubHandle).
              success(function (data) {
                $scope.fetched_repositories = data;
              }).
              error(function () {
                $scope.fetched_repositories = $scope.fetched_repositories.push({
                  'name': 'test-repo',
                  'link': 'http://www.google.de',
                  'license': 'Apache License v2'
                });
              })
        });

  };
  $scope.$watch('fetched_repositories', function () {
    angular.forEach($scope.fetched_repositories, function (repository) {
      githubApiConnector.getLicenseForRepo(repository).
          success(function (data) {
            $scope.repositories.push(data);
          }).
          error(function () {
            alert('error.')
          });
    });
  });
}]);

app.factory('githubApiConnector', ['$http', function ($http) {
  var api = 'https://api.github.com';

  return {
    fetchReposForUser: function (githubHandle) {
      var user_repo_endpoint = api + '/users/' + githubHandle + '/repos';
      var request = {
        method: 'GET',
        url: user_repo_endpoint
      };
      return $http(request);
    },
    fetchReposForOrganisation: function (githubHandle) {
      var organisation_repo_endpoint = api + '/orgs/' + githubHandle + '/repos';
      var request = {
        method: 'GET',
        url: organisation_repo_endpoint
      };
      return $http(request);
    },
    getLicenseForRepo: function (repo) {
      var license_endpoint = api + '/repos/' + repo['full_name']
      var request = {
        method: 'GET',
        url: license_endpoint,
        headers: {
          'Accept': 'application/vnd.github.drax-preview+json'
        }
      };
      return $http(request);
    }
  };
}]);<commit_msg>now also handling api pages.<commit_after>'use strict';

var app = angular.module('licker', ['ngRoute'])
    .config(function ($routeProvider) {
      $routeProvider
          .when('/', {
            templateUrl: 'views/table.html',
            controller: 'MainCtrl'
          })
          .otherwise('/');
    });

app.controller('mainCtrl', ['$scope', 'githubApiConnector', function ($scope, githubApiConnector) {
  $scope.githubHandle = '';
  $scope.repositories = [];
  $scope.fetched_repositories = [];

  $scope.checkLicenses = function () {
    $scope.fetched_repositories = [];

    var page = 1;

    do {
      githubApiConnector.fetchReposForUser($scope.githubHandle, page).
          success(function (data) {
            $scope.fetched_repositories = data;
          }).
          error(function () {
            $scope.fetched_repositories = $scope.fetched_repositories;
          });
      page++;
    } while ($scope.fetched_repositories.length !== $scope.fetched_repositories.length);

    page = 1;
    if ($scope.fetched_repositories.length < 1) {
      do {
        githubApiConnector.fetchReposForOrganisation($scope.githubHandle, page).
            success(function (data) {
              $scope.fetched_repositories = data;
            }).
            error(function () {
              $scope.fetched_repositories = $scope.fetched_repositories;
            });
        page++;
      } while ($scope.fetched_repositories.length !== $scope.fetched_repositories.length);
    }
  };

  $scope.$watch('fetched_repositories', function () {
    angular.forEach($scope.fetched_repositories, function (repository) {
      githubApiConnector.getLicenseForRepo(repository).
          success(function (data) {
            $scope.repositories.push(data);
          }).
          error(function () {
            alert('error.')
          });
    });
  });
}]);

app.factory('githubApiConnector', ['$http', function ($http) {
  var api = 'https://api.github.com';
  var page_parameter = '?page=';

  return {
    fetchReposForUser: function (githubHandle, page) {
      var user_repo_endpoint = api + '/users/' + githubHandle + '/repos' + page_parameter + page;
      var request = {
        method: 'GET',
        url: user_repo_endpoint
      };
      return $http(request);
    },
    fetchReposForOrganisation: function (githubHandle, page) {
      var organisation_repo_endpoint = api + '/orgs/' + githubHandle + '/repos' + page_parameter + page;
      var request = {
        method: 'GET',
        url: organisation_repo_endpoint
      };
      return $http(request);
    },
    getLicenseForRepo: function (repo) {
      var license_endpoint = api + '/repos/' + repo['full_name']
      var request = {
        method: 'GET',
        url: license_endpoint,
        headers: {
          'Accept': 'application/vnd.github.drax-preview+json'
        }
      };
      return $http(request);
    }
  };
}]);<|endoftext|>


<commit_before>var aggregate           = require('./aggregate');
var http                = require('http');
var os                  = require('os');
var version             = require('../package.json').version;
var util                = require('./util');
var AWS                 = require('aws-sdk');
var EventEmitter        = require('events').EventEmitter;
var archiveTickled      = false;
var archiveInterval     = 60000;
var archiveTimeoutId    = null;

var s3BucketName        = undefined;
var s3KeyPrefix         = undefined;
var s3Handler           = undefined;

var snsArn              = undefined;
var snsHandler          = undefined;

var snapshotBucket      = undefined;
var snapshotArn         = undefined;

var config              = undefined;

var emitter             = new EventEmitter();

var initAWS             = [];
var gettingCredentials  = false;

function initAwsCredentials(){
  if( gettingCredentials ){
    return;
  } else {
    acquireAwsCredentials();
  }
}

function refreshAwsCredentials() {
  acquireAwsCredentials();
}

// Invoke a Concurix web-api to obtain AWS credentials
function acquireAwsCredentials() {
  gettingCredentials = true;
  function requestCallback(response) {
    var str = '';

    // This is an 'OK' response.  Set up handlers to be informed about the content
    if (response.statusCode === 200) {
      response.on('data',  function(chunk) { str += chunk; });
      response.on('end',   function()      { start(str);   });

      response.on('error', function(err)   { console.log('\n\nErr:  ' + err); });
    } else {
      console.error('Failed to get Trace Session Data');
      console.log(require('util').inspect(response));
    }
  }

  function start(str) {
    var response = JSON.parse(str);

    s3BucketName = response.s3Bucket;
    s3KeyPrefix  = response.s3Prefix;
    snsArn       = response.snsArn;
    snapshotArn  = response.snapshotArn;
    snapshotBucket = response.snapshotBucket;

    AWS.config   = new AWS.Config({
                                    accessKeyId:     response.AccessKeyId,
                                    sessionToken:    response.SessionToken,
                                    secretAccessKey: response.SecretAccessKey,
                                    region:          response.Region
                                 });

    s3Handler    = new AWS.S3();
    snsHandler   = new AWS.SNS();

    // Schedule an event to refresh the credentials about 1 minute
    // before the new credentials expire
    setTimeout(refreshAwsCredentials, response.DurationMS - 60 * 1000);

    // we might have queued up a few packages to archive before AWS is initialized.  If so, go ahead and send them...
    gettingCredentials = false;
    if( initAWS.length > 0 ){
      initAWS.forEach(doArchive);
      initAWS = [];
    }

  }

  var options     = {
                      host:   config.archiveHost,
                      port:   config.archivePort,
                      path:   '/v1/' + config.accountKey + '/new_run/' + config.hostname,
                      method: 'POST'
                    };

  initAWS = [];
  var stream = require('http').request(options, requestCallback);
  stream.on('error', function(err){
    gettingCredentials = false;
    emitter.emit('archiveOffline', err);
  });
  stream.end();
}

function doArchive(json) {
  if (!s3Handler){
    initAWS.push (json);
    return;
  };
  var unixMsec = (new Date()).getTime();
  var unixSec  = Math.floor(unixMsec / 1000);
  var key      = s3KeyPrefix + '/' + unixSec + '.json';

  // Write the data to S3
  aggregate.stopTracing();
  var body = JSON.stringify(json);

  //once we have pickled our data, go ahead and clear the aggregate cache.
  global.concurix.traceAggregate.resetTimes();

  s3Handler.putObject({Bucket: s3BucketName, Key: key, Body: body}, function(putErr, putData) {
    if (!putErr) {
      var message = JSON.stringify({ type: 'stream', key: key, accountKey: config.accountKey});
      var params  = { TopicArn: snsArn, Message: message };

      // Publish a notification for the new trace
      aggregate.stopTracing();
      snsHandler.client.publish(params, function(err, data) {
        if (err) {
          console.log('concurix.archive: trace notification error ', err, data);
        }
        //once it's done, send the event.
        emitter.emit('sendArchive', json);
      });
      aggregate.startTracing();
    } else {
      console.log('concurix.archive: failed to write trace archive: ' + putErr);
    }
  });
  aggregate.startTracing();
}

function configure(options){
  config = options;
  if( options.archiveListener ){
    emitter.on('sendArchive', options.archiveListener);
  }
  if( options.archiveOffline ){
    emitter.on('archiveOffline', options.archiveOffline);
  }

  if (options.accountKey && options.hostname && options.archiveHost && options.archivePort) {
    initAwsCredentials();
  }
}
function sendArchive() {
  var data = computePackage();
  doArchive(data);
  archiveTickled = false;
}

function tickle() {
  if( archiveTickled ){
    return;
  } else {
    archiveTimeoutId = setTimeout(module.exports.sendArchive, config.archiveInterval);
    archiveTickled = true;
  }
}

function computePackage() {
  //quick validation
  var links = global.concurix.traceAggregate.linkCache;
  var nodes = global.concurix.traceAggregate.nodeCache;
  for( key in links){
    if( !nodes[links[key].source] || !nodes[links[key].target]){
      console.log('yikes, missing node for link ', links[key]);
    } else {
      //console.log('got it');
    }
  }
  var msg = {
    type: "nodejs",
    version: version,
    // run_id: 'to be set',
    load_avg: os.loadavg(), //array of 1, 5, and 15 minute load averages
    cpus: os.cpus(),
    timestamp: util.unixTimeSec(),
    data: {
      nodes:   util.values(global.concurix.traceAggregate.nodeCache),
      links:   util.values(global.concurix.traceAggregate.linkCache)
    }
  };
  return msg;
}

function mergeConfig(prev){
  if( prev && prev.config && prev.config.archiveListener){
    emmitter.on('sendArchive', prev.config.archiveListener);
  }
}

function reset(){
  if( archiveTimeoutId ){
    clearTimeout(archiveTimeoutId);
  }
  archiveTickled = false;
}

function stop(){
  reset();
}

var archive = {
  configure:  configure,
  sendArchive: sendArchive,
  tickle: tickle,
  config: config,
  version: version,
  mergeConfig: mergeConfig,
  reset: reset,
  stop: stop
}

if( !global.concurix ){
  global.concurix = {};
}

// run this at instantiation time to setup globals properly.  There should only be one instance of the archiveInterval
if( !global.concurix.archive || global.concurix.archive.version < version ){
  var prev = global.concurix.archive;
  global.concurix.archive = archive;
  archive.mergeConfig(prev);
}

module.exports.configure = function configure(options){
  global.concurix.archive.configure(options);
}

module.exports.sendArchive = function sendArchive(){
  global.concurix.archive.sendArchive();
}

module.exports.tickle = function tickle(){
  global.concurix.archive.tickle();
}

module.exports.reset = function reset(){
  global.concurix.archive.reset();
}<commit_msg>reduce the frequency of grabbing credentials<commit_after>var aggregate           = require('./aggregate');
var http                = require('http');
var os                  = require('os');
var version             = require('../package.json').version;
var util                = require('./util');
var AWS                 = require('aws-sdk');
var EventEmitter        = require('events').EventEmitter;
var archiveTickled      = false;
var archiveInterval     = 60000;
var archiveTimeoutId    = null;

var s3BucketName        = undefined;
var s3KeyPrefix         = undefined;
var s3Handler           = undefined;

var snsArn              = undefined;
var snsHandler          = undefined;

var snapshotBucket      = undefined;
var snapshotArn         = undefined;

var config              = undefined;

var emitter             = new EventEmitter();

var initAWS             = [];
var gettingCredentials  = false;

function initAwsCredentials(){
  if( gettingCredentials ){
    return;
  } else {
    acquireAwsCredentials();
  }
}

function refreshAwsCredentials() {
  acquireAwsCredentials();
}

// Invoke a Concurix web-api to obtain AWS credentials
function acquireAwsCredentials() {
  gettingCredentials = true;  // note that we will never reset this to false once we've started an init process
  function requestCallback(response) {
    var str = '';

    // This is an 'OK' response.  Set up handlers to be informed about the content
    if (response.statusCode === 200) {
      response.on('data',  function(chunk) { str += chunk; });
      response.on('end',   function()      { start(str);   });

      response.on('error', function(err)   { console.log('\n\nErr:  ' + err); });
    } else {
      console.error('Failed to get Trace Session Data');
      console.log(require('util').inspect(response));
    }
  }

  function start(str) {
    var response = JSON.parse(str);

    s3BucketName = response.s3Bucket;
    s3KeyPrefix  = response.s3Prefix;
    snsArn       = response.snsArn;
    snapshotArn  = response.snapshotArn;
    snapshotBucket = response.snapshotBucket;

    AWS.config   = new AWS.Config({
                                    accessKeyId:     response.AccessKeyId,
                                    sessionToken:    response.SessionToken,
                                    secretAccessKey: response.SecretAccessKey,
                                    region:          response.Region
                                 });

    s3Handler    = new AWS.S3();
    snsHandler   = new AWS.SNS();

    // Schedule an event to refresh the credentials about 1 minute
    // before the new credentials expire
    setTimeout(refreshAwsCredentials, response.DurationMS - 60 * 1000);

    // we might have queued up a few packages to archive before AWS is initialized.  If so, go ahead and send them...
    if( initAWS.length > 0 ){
      initAWS.forEach(doArchive);
      initAWS = [];
    }

  }

  var options     = {
                      host:   config.archiveHost,
                      port:   config.archivePort,
                      path:   '/v1/' + config.accountKey + '/new_run/' + config.hostname,
                      method: 'POST'
                    };

  initAWS = [];
  var stream = require('http').request(options, requestCallback);
  stream.on('error', function(err){
    gettingCredentials = false;
    emitter.emit('archiveOffline', err);
  });
  stream.end();
}

function doArchive(json) {
  if (!s3Handler){
    initAWS.push (json);
    return;
  };
  var unixMsec = (new Date()).getTime();
  var unixSec  = Math.floor(unixMsec / 1000);
  var key      = s3KeyPrefix + '/' + unixSec + '.json';

  // Write the data to S3
  aggregate.stopTracing();
  var body = JSON.stringify(json);

  //once we have pickled our data, go ahead and clear the aggregate cache.
  global.concurix.traceAggregate.resetTimes();

  s3Handler.putObject({Bucket: s3BucketName, Key: key, Body: body}, function(putErr, putData) {
    if (!putErr) {
      var message = JSON.stringify({ type: 'stream', key: key, accountKey: config.accountKey});
      var params  = { TopicArn: snsArn, Message: message };

      // Publish a notification for the new trace
      aggregate.stopTracing();
      snsHandler.client.publish(params, function(err, data) {
        if (err) {
          console.log('concurix.archive: trace notification error ', err, data);
        }
        //once it's done, send the event.
        emitter.emit('sendArchive', json);
      });
      aggregate.startTracing();
    } else {
      console.log('concurix.archive: failed to write trace archive: ' + putErr);
    }
  });
  aggregate.startTracing();
}

function configure(options){
  config = options;
  if( options.archiveListener ){
    emitter.on('sendArchive', options.archiveListener);
  }
  if( options.archiveOffline ){
    emitter.on('archiveOffline', options.archiveOffline);
  }

  if (options.accountKey && options.hostname && options.archiveHost && options.archivePort) {
    initAwsCredentials();
  }
}
function sendArchive() {
  var data = computePackage();
  doArchive(data);
  archiveTickled = false;
}

function tickle() {
  if( archiveTickled ){
    return;
  } else {
    archiveTimeoutId = setTimeout(module.exports.sendArchive, config.archiveInterval);
    archiveTickled = true;
  }
}

function computePackage() {
  //quick validation
  var links = global.concurix.traceAggregate.linkCache;
  var nodes = global.concurix.traceAggregate.nodeCache;
  for( key in links){
    if( !nodes[links[key].source] || !nodes[links[key].target]){
      console.log('yikes, missing node for link ', links[key]);
    } else {
      //console.log('got it');
    }
  }
  var msg = {
    type: "nodejs",
    version: version,
    // run_id: 'to be set',
    load_avg: os.loadavg(), //array of 1, 5, and 15 minute load averages
    cpus: os.cpus(),
    timestamp: util.unixTimeSec(),
    data: {
      nodes:   util.values(global.concurix.traceAggregate.nodeCache),
      links:   util.values(global.concurix.traceAggregate.linkCache)
    }
  };
  return msg;
}

function mergeConfig(prev){
  if( prev && prev.config && prev.config.archiveListener){
    emmitter.on('sendArchive', prev.config.archiveListener);
  }
}

function reset(){
  if( archiveTimeoutId ){
    clearTimeout(archiveTimeoutId);
  }
  archiveTickled = false;
}

function stop(){
  reset();
}

var archive = {
  configure:  configure,
  sendArchive: sendArchive,
  tickle: tickle,
  config: config,
  version: version,
  mergeConfig: mergeConfig,
  reset: reset,
  stop: stop
}

if( !global.concurix ){
  global.concurix = {};
}

// run this at instantiation time to setup globals properly.  There should only be one instance of the archiveInterval
if( !global.concurix.archive || global.concurix.archive.version < version ){
  var prev = global.concurix.archive;
  global.concurix.archive = archive;
  archive.mergeConfig(prev);
}

module.exports.configure = function configure(options){
  global.concurix.archive.configure(options);
}

module.exports.sendArchive = function sendArchive(){
  global.concurix.archive.sendArchive();
}

module.exports.tickle = function tickle(){
  global.concurix.archive.tickle();
}

module.exports.reset = function reset(){
  global.concurix.archive.reset();
}<|endoftext|>


<commit_before>require("./spec_helper.js")

describe("Jaml.Node", function() {
  it("renders nothing inside if there's nothing in the node", function(){
    assert.equal("<fooBar></fooBar>\n", new Jaml.Node("fooBar").render())
  })
})

<commit_msg>test padding<commit_after>require("./spec_helper.js")

describe("Jaml.Node", function() {
  it("renders nothing inside if there's nothing in the node", function(){
    assert.equal("<fooBar></fooBar>\n", new Jaml.Node("fooBar").render())
  })

  it("properly prepends the specified amount of left padding", function(){
    assert.equal("     <fooBar></fooBar>\n", new Jaml.Node("fooBar").render(5))
  })
})

<|endoftext|>


<commit_before>var xl = require('../lib/libxl');

describe('The font class', function() {

    var book = new xl.Book(xl.BOOK_TYPE_XLS),
        font = book.addFont(),
        wrongBook = new xl.Book(xl.BOOK_TYPE_XLS),
        wrongFont = book.addFont();

    it('The font constructor can not be called directly', function() {
        expect(function() {new font.constructor();}).toThrow();
    });

});

describe('The sheet class', function() {

    var book = new xl.Book(xl.BOOK_TYPE_XLS),
        sheet = book.addSheet('foo'),
        format = book.addFormat(),
        wrongBook = new xl.Book(xl.BOOK_TYPE_XLS),
        wrongSheet = wrongBook.addSheet('bar'),
        wrongFormat = wrongBook.addFormat(),
        row = 1;

    it('sheet.cellType determines cell type', function() {
        sheet
            .writeStr(row, 0, 'foo')
            .writeNum(row, 1, 10);

        expect(function() {sheet.cellType();}).toThrow();
        expect(function() {sheet.cellType.call({}, row, 0);}).toThrow();

        expect(sheet.cellType(row, 0)).toBe(xl.CELLTYPE_STRING);
        expect(sheet.cellType(row, 1)).toBe(xl.CELLTYPE_NUMBER);

        row++;
    });

    it('sheet.isFormula checks whether a cell contains a formula', function() {
        sheet
            .writeStr(row, 0, 'foo')
            .writeFormula(row, 1, '=1');

        expect(function() {sheet.isFormula();}).toThrow();
        expect(function() {sheet.isFormula.call({}, row, 0)}).toThrow();

        expect(sheet.isFormula(row, 0)).toBe(false);
        expect(sheet.isFormula(row, 1)).toBe(true);

        row++;
    });

    it ('sheet.cellFormat retrieves the cell format', function() {
        expect(function() {sheet.cellFormat();}).toThrow();
        expect(function() {sheet.cellFormat.call({}, row, 0);}).toThrow();

        var cellFormat = sheet.cellFormat(row, 0);
        expect(format instanceof format.constructor).toBe(true);
    });

    it('sheet.setCellFormat sets the cell format', function() {
        expect(function() {sheet.setCellFormat();}).toThrow();
        expect(function() {sheet.setCellFormat.call({}, row, 0, format);}).toThrow();

        expect(function() {sheet.setCellFormat(row, 0, wrongFormat);}).toThrow();
        expect(sheet.setCellFormat(row, 0, format)).toBe(sheet);
    });

    it('sheet.readStr reads a string', function() {
        sheet.writeStr(row, 0, 'foo');
        sheet.writeNum(row, 1, 10);

        expect(function() {sheet.readStr();}).toThrow();
        expect(function() {sheet.readStr.call({}, row, 0);}).toThrow();

        var formatRef = {};
        expect(function() {sheet.readStr(row, 1);}).toThrow();
        expect(sheet.readStr(row, 0)).toBe('foo');
        expect(sheet.readStr(row, 0, formatRef)).toBe('foo');
        expect(formatRef.format instanceof format.constructor).toBe(true);

        row++;
    });

    it('sheet.writeStr writes a string', function() {
        expect(function() {sheet.writeStr();}).toThrow();
        expect(function() {sheet.writeStr.call({}, row, 0, 'foo');}).toThrow();

        expect(function() {sheet.writeStr(row, 0, 'foo', wrongFormat);}).toThrow();
        expect(sheet.writeStr(row, 0, 'foo')).toBe(sheet);
        expect(sheet.writeStr(row, 0, 'foo', format)).toBe(sheet);

        row++;
    });

    it('sheet.readNum reads a number', function() {
        sheet.writeNum(row, 0, 10);

        expect(function() {sheet.readNum();}).toThrow();
        expect(function() {sheet.readNum.call({}, row, 0);}).toThrow();

        var formatRef = {};
        expect(sheet.readNum(row, 0)).toEqual(10);
        expect(sheet.readNum(row, 0, formatRef)).toEqual(10);
        expect(formatRef.format instanceof format.constructor).toBe(true);

        row++;
    });

    it('sheet.writeNum writes a Number', function() {
        expect(function() {sheet.writeNum();}).toThrow();
        expect(function() {sheet.writeNum.call({}, row, 0, 10);}).toThrow();

        expect(function() {sheet.writeNum(row, 0, 10, wrongFormat);}).toThrow();
        expect(sheet.writeNum(row, 0, 10)).toBe(sheet);
        expect(sheet.writeNum(row, 0, 10, format)).toBe(sheet);

        row++;
    });

    it('sheet.readBool reads a bool', function() {
        sheet.writeBool(row, 0, true);

        expect(function() {sheet.readBool();}).toThrow();
        expect(function() {sheet.readBool.call({}, row, 0);}).toThrow();

        var formatRef = {};
        expect(sheet.readBool(row, 0)).toEqual(true);
        expect(sheet.readBool(row, 0, formatRef)).toEqual(true);
        expect(formatRef.format instanceof format.constructor).toBe(true);

        row++;
    });

    it('sheet.writeBool writes a bool', function() {
        expect(function() {sheet.writeBool();}).toThrow();
        expect(function() {sheet.writeBool.call({}, row, 0, true);}).toThrow();

        expect(function() {sheet.writeBool(row, 0, true, wrongFormat);}).toThrow();
        expect(sheet.writeBool(row, 0, true)).toBe(sheet);
        expect(sheet.writeBool(row, 0, true, format)).toBe(sheet);

        row++;
    });

    it('sheet.readBlank reads a blank cell', function() {
        sheet.writeBlank(row, 0, format);
        sheet.writeNum(row, 1, 10);

        expect(function() {sheet.readBlank();}).toThrow();
        expect(function() {sheet.readBlank.call({}, row, 0);}).toThrow();

        var formatRef = {};
        expect(sheet.readBlank(row, 0) instanceof format.constructor).toBe(true);
        expect(sheet.readBlank(row, 0, formatRef) instanceof format.constructor).toBe(true);
        expect(formatRef.format instanceof format.constructor).toBe(true);
        expect(function() {sheet.readBlank(row, 1);}).toThrow();

        row++;
    });

    it('sheet.writeBlank writes a blank cell', function() {
        expect(function() {sheet.writeBlank();}).toThrow();
        expect(function() {sheet.writeBlank.call({}, row, 0, format);}).toThrow();

        expect(function() {sheet.writeBlank(row, 0, wrongFormat);}).toThrow();
        expect(function() {sheet.writeBlank(row, 0);}).toThrow();
        expect(sheet.writeBlank(row, 0, format)).toBe(sheet);

        row++;
    });

    it('sheet.readFormula reads a formula', function() {
        sheet.writeFormula(row, 0,'=SUM(A1:A10)');
        sheet.writeNum(row, 1, 10);

        expect(function() {sheet.readFormula();}).toThrow();
        expect(function() {sheet.readFormula.call({}, row, 0);}).toThrow();

        var formatRef = {};
        expect(function() {sheet.readFormula(row, 1);}).toThrow();
        expect(sheet.readFormula(row, 0)).toBe('SUM(A1:A10)');
        expect(sheet.readFormula(row, 0, formatRef)).toBe('SUM(A1:A10)');
        expect(formatRef.format instanceof format.constructor).toBe(true);

        row++;
    });

    it('sheet.writeComment writes a comment', function() {
        expect(function() {sheet.writeComment();}).toThrow();
        expect(function() {sheet.writeComment.call({}, row, 0, 'comment');}).toThrow();

        expect(sheet.writeComment(row, 0, 'comment')).toBe(sheet);

        row++;
    });

    it('sheet.readComment reads a comment', function() {
        sheet.writeString(row, 0, 'foo');
        sheet.writeComment(row, 0,'comment');

        expect(function() {sheet.readComment();}).toThrow();
        expect(function() {sheet.readComment.call({}, row, 0);}).toThrow();

        var formatRef = {};
        expect(function() {sheet.readComment(row, 1);}).toThrow();
        expect(sheet.readComment(row, 0)).toBe('comment');

        row++;
    });

    it('sheet.readError reads an error', function() {
        sheet.writeStr(row, 0, '');

        expect(function() {sheet.readError();}).toThrow();
        expect(function() {sheet.readError.call({}, row, 0);}).toThrow();

        expect(sheet.readError(row, 0)).toBe(xl.ERRORTYPE_NOERROR);

        row++;
    });

    it('sheet.colWidth reads colum width', function() {
        sheet.setCol(0, 0, 42);

        expect(function() {sheet.colWidth();}).toThrow();
        expect(function() {sheet.colWidth.call({}, 0);}).toThrow();

        expect(sheet.colWidth(0)).toEqual(42);
    });

    it('sheet.rowHeight reads row Height', function() {
        sheet.setRow(1, 42);

        expect(function() {sheet.rowHeight();}).toThrow();
        expect(function() {sheet.rowHeight.call({}, 1);}).toThrow();

        expect(sheet.rowHeight(1)).toEqual(42);
    });

    it('sheet.setCol configures columns', function() {
        expect(function() {sheet.setCol();}).toThrow();
        expect(function() {sheet.setCol.call({}, 0, 0, 42);}).toThrow();

        expect(function() {sheet.setCol(0, 0, 42, wrongFormat);}).toThrow();

        expect(sheet.setCol(0, 0, 42)).toBe(sheet);
        expect(sheet.setCol(0, 0, 42, format, false)).toBe(sheet);
    });

    it('sheet.setRow configures rows', function() {
        expect(function() {sheet.setRow();}).toThrow();
        expect(function() {sheet.setRow.call({}, 1, 42);}).toThrow();

        expect(function() {sheet.setRow(1, 42, wrongFormat);}).toThrow();

        expect(sheet.setRow(1, 42)).toBe(sheet);
        expect(sheet.setRow(1, 42, format, false)).toBe(sheet);
    });

    it('sheet.rowHidden checks whether a row is hidden', function() {
        expect(function() {sheet.rowHidden();}).toThrow();
        expect(function() {sheet.rowHidden.call({}, row);}).toThrow();

        expect(sheet.rowHidden(row)).toBe(false);
    });

    it('sheet.setRowHidden hides or shows a row', function() {
        expect(function() {sheet.setRowHidden();}).toThrow();
        expect(function() {sheet.setRowHidden.call({}, row, true)}).toThrow();

        expect(sheet.setRowHidden(row, true)).toBe(sheet);
        expect(sheet.rowHidden(row)).toBe(true);
        expect(sheet.setRowHidden(row, false)).toBe(sheet);
        expect(sheet.rowHidden(row)).toBe(false);
    });

    it('sheet.colHidden checks whether a column is hidden', function() {
        expect(function() {sheet.colHidden();}).toThrow();
        expect(function() {sheet.colHidden.call({}, 0);}).toThrow();

        expect(sheet.colHidden(0)).toBe(false);
    });

    it('sheet.setColHidden hides or shows a column', function() {
        expect(function() {sheet.setColHidden();}).toThrow();
        expect(function() {sheet.setColHidden.call({}, 0, true)}).toThrow();

        expect(sheet.setColHidden(0, true)).toBe(sheet);
        expect(sheet.colHidden(0)).toBe(true);
        expect(sheet.setColHidden(0, false)).toBe(sheet);
        expect(sheet.colHidden(0)).toBe(false);
    })

});
<commit_msg>Font tests.<commit_after>var xl = require('../lib/libxl'),
    util = require('util');

function shouldThrow(fun, scope) {
    var args = Array.prototype.slice.call(arguments, 2);

    expect(function() {fun.apply(scope, args);}).toThrow();
}

describe('The font class', function() {

    var book = new xl.Book(xl.BOOK_TYPE_XLS),
        font = book.addFont();

    it('The font constructor can not be called directly', function() {
        expect(function() {new font.constructor();}).toThrow();
    });

    it('font.size reads font size', function() {
        shouldThrow(font.size, book);
        font.setSize(10);
        expect(font.size()).toBe(10);
        font.setSize(20);
        expect(font.size()).toBe(20);
    });

    it('font.setSize sets font size', function() {
        shouldThrow(font.setSize, font, '');
        shouldThrow(font.setSize, {}, 10);
        expect(font.setSize(10)).toBe(font);
    });

    it('font.italic checks wether a font is italic', function() {
        shouldThrow(font.italic, book);
        font.setItalic(false);
        expect(font.italic()).toBe(false);
        font.setItalic(true);
        expect(font.italic()).toBe(true);
    });

    it('font.setItalic toggles a font italic', function() {
        shouldThrow(font.setItalic, font, 10);
        shouldThrow(font.setItalic, book, true);
        expect(font.setItalic(true)).toBe(font);
    });

    it('font.strikeOut checks wether a font is striked out', function() {
        shouldThrow(font.strikeOut, book);
        font.setStrikeOut(false);
        expect(font.strikeOut()).toBe(false);
        font.setStrikeOut(true);
        expect(font.strikeOut()).toBe(true);
    });

    it('font.setStrikeOut toggles a font striked out', function() {
        shouldThrow(font.setStrikeOut, font, 10);
        shouldThrow(font.setStrikeOut, book, true);
        expect(font.setStrikeOut(true)).toBe(font);
    });

    it('font.color gets font color', function() {
        shouldThrow(font.color, book);
        font.setColor(xl.COLOR_BLACK);
        expect(font.color()).toBe(xl.COLOR_BLACK);
        font.setColor(xl.COLOR_WHITE);
        expect(font.color()).toBe(xl.COLOR_WHITE);
    });

    it('font.setColor sets font color', function() {
        shouldThrow(font.setColor, font, true);
        shouldThrow(font.setColor, book, xl.COLOR_BLACK);
        expect(font.setColor(xl.COLOR_BLACK)).toBe(font);
    });

    it('font.bold checks whether a font is bold', function() {
        shouldThrow(font.bold, book);
        font.setBold(false);
        expect(font.bold()).toBe(false);
        font.setBold(true);
        expect(font.bold()).toBe(true);
    });

    it('font.setBold toggles a font bold', function() {
        shouldThrow(font.setBold, font, 10);
        shouldThrow(font.setBold, book, true);
        expect(font.setBold(true)).toBe(font);
    });

    it('font.script gets a fonts script style', function() {
        shouldThrow(font.script, book);
        font.setScript(xl.SCRIPT_NORMAL);
        expect(font.script()).toBe(xl.SCRIPT_NORMAL);
        font.setScript(xl.SCRIPT_SUPER);
        expect(font.script()).toBe(xl.SCRIPT_SUPER);
    });

    it('font.setScript sets a fonts script style', function() {
        shouldThrow(font.setScript, font, '');
        shouldThrow(font.setScript, book, xl.SCRIPT_NORMAL);
        expect(font.setScript(xl.SCRIPT_NORMAL)).toBe(font);
    });

    it('font.underline gets a fonts underline mode', function() {
        shouldThrow(font.underline, book);
        font.setUnderline(xl.UNDERLINE_NONE);
        expect(font.underline()).toBe(xl.UNDERLINE_NONE);
        font.setUnderline(xl.UNDERLINE_SINGLE);
        expect(font.underline()).toBe(xl.UNDERLINE_SINGLE);
    });

    it('font.setUnderline sets a fonts underline mode', function() {
        shouldThrow(font.setUnderline, font, true);
        shouldThrow(font.setUnderline, book, xl.UNDERLINE_NONE);
        expect(font.setUnderline(xl.UNDERLINE_NONE)).toBe(font);
    });

    it('font.name gets the font name', function() {
        shouldThrow(font.name, book);
        font.setName('arial');
        expect(font.name()).toBe('arial');
        font.setName('times');
        expect(font.name()).toBe('times');
    });

    it('font.setName sets the font name', function() {
        shouldThrow(font.setName, font, 10);
        shouldThrow(font.setName, book, 'arial');
        expect(font.setName('arial')).toBe(font);
    });

});

describe('The sheet class', function() {

    var book = new xl.Book(xl.BOOK_TYPE_XLS),
        sheet = book.addSheet('foo'),
        format = book.addFormat(),
        wrongBook = new xl.Book(xl.BOOK_TYPE_XLS),
        wrongSheet = wrongBook.addSheet('bar'),
        wrongFormat = wrongBook.addFormat(),
        row = 1;

    it('sheet.cellType determines cell type', function() {
        sheet
            .writeStr(row, 0, 'foo')
            .writeNum(row, 1, 10);

        expect(function() {sheet.cellType();}).toThrow();
        expect(function() {sheet.cellType.call({}, row, 0);}).toThrow();

        expect(sheet.cellType(row, 0)).toBe(xl.CELLTYPE_STRING);
        expect(sheet.cellType(row, 1)).toBe(xl.CELLTYPE_NUMBER);

        row++;
    });

    it('sheet.isFormula checks whether a cell contains a formula', function() {
        sheet
            .writeStr(row, 0, 'foo')
            .writeFormula(row, 1, '=1');

        expect(function() {sheet.isFormula();}).toThrow();
        expect(function() {sheet.isFormula.call({}, row, 0);}).toThrow();

        expect(sheet.isFormula(row, 0)).toBe(false);
        expect(sheet.isFormula(row, 1)).toBe(true);

        row++;
    });

    it ('sheet.cellFormat retrieves the cell format', function() {
        expect(function() {sheet.cellFormat();}).toThrow();
        expect(function() {sheet.cellFormat.call({}, row, 0);}).toThrow();

        var cellFormat = sheet.cellFormat(row, 0);
        expect(format instanceof format.constructor).toBe(true);
    });

    it('sheet.setCellFormat sets the cell format', function() {
        expect(function() {sheet.setCellFormat();}).toThrow();
        expect(function() {sheet.setCellFormat.call({}, row, 0, format);}).toThrow();

        expect(function() {sheet.setCellFormat(row, 0, wrongFormat);}).toThrow();
        expect(sheet.setCellFormat(row, 0, format)).toBe(sheet);
    });

    it('sheet.readStr reads a string', function() {
        sheet.writeStr(row, 0, 'foo');
        sheet.writeNum(row, 1, 10);

        expect(function() {sheet.readStr();}).toThrow();
        expect(function() {sheet.readStr.call({}, row, 0);}).toThrow();

        var formatRef = {};
        expect(function() {sheet.readStr(row, 1);}).toThrow();
        expect(sheet.readStr(row, 0)).toBe('foo');
        expect(sheet.readStr(row, 0, formatRef)).toBe('foo');
        expect(formatRef.format instanceof format.constructor).toBe(true);

        row++;
    });

    it('sheet.writeStr writes a string', function() {
        expect(function() {sheet.writeStr();}).toThrow();
        expect(function() {sheet.writeStr.call({}, row, 0, 'foo');}).toThrow();

        expect(function() {sheet.writeStr(row, 0, 'foo', wrongFormat);}).toThrow();
        expect(sheet.writeStr(row, 0, 'foo')).toBe(sheet);
        expect(sheet.writeStr(row, 0, 'foo', format)).toBe(sheet);

        row++;
    });

    it('sheet.readNum reads a number', function() {
        sheet.writeNum(row, 0, 10);

        expect(function() {sheet.readNum();}).toThrow();
        expect(function() {sheet.readNum.call({}, row, 0);}).toThrow();

        var formatRef = {};
        expect(sheet.readNum(row, 0)).toEqual(10);
        expect(sheet.readNum(row, 0, formatRef)).toEqual(10);
        expect(formatRef.format instanceof format.constructor).toBe(true);

        row++;
    });

    it('sheet.writeNum writes a Number', function() {
        expect(function() {sheet.writeNum();}).toThrow();
        expect(function() {sheet.writeNum.call({}, row, 0, 10);}).toThrow();

        expect(function() {sheet.writeNum(row, 0, 10, wrongFormat);}).toThrow();
        expect(sheet.writeNum(row, 0, 10)).toBe(sheet);
        expect(sheet.writeNum(row, 0, 10, format)).toBe(sheet);

        row++;
    });

    it('sheet.readBool reads a bool', function() {
        sheet.writeBool(row, 0, true);

        expect(function() {sheet.readBool();}).toThrow();
        expect(function() {sheet.readBool.call({}, row, 0);}).toThrow();

        var formatRef = {};
        expect(sheet.readBool(row, 0)).toEqual(true);
        expect(sheet.readBool(row, 0, formatRef)).toEqual(true);
        expect(formatRef.format instanceof format.constructor).toBe(true);

        row++;
    });

    it('sheet.writeBool writes a bool', function() {
        expect(function() {sheet.writeBool();}).toThrow();
        expect(function() {sheet.writeBool.call({}, row, 0, true);}).toThrow();

        expect(function() {sheet.writeBool(row, 0, true, wrongFormat);}).toThrow();
        expect(sheet.writeBool(row, 0, true)).toBe(sheet);
        expect(sheet.writeBool(row, 0, true, format)).toBe(sheet);

        row++;
    });

    it('sheet.readBlank reads a blank cell', function() {
        sheet.writeBlank(row, 0, format);
        sheet.writeNum(row, 1, 10);

        expect(function() {sheet.readBlank();}).toThrow();
        expect(function() {sheet.readBlank.call({}, row, 0);}).toThrow();

        var formatRef = {};
        expect(sheet.readBlank(row, 0) instanceof format.constructor).toBe(true);
        expect(sheet.readBlank(row, 0, formatRef) instanceof format.constructor).toBe(true);
        expect(formatRef.format instanceof format.constructor).toBe(true);
        expect(function() {sheet.readBlank(row, 1);}).toThrow();

        row++;
    });

    it('sheet.writeBlank writes a blank cell', function() {
        expect(function() {sheet.writeBlank();}).toThrow();
        expect(function() {sheet.writeBlank.call({}, row, 0, format);}).toThrow();

        expect(function() {sheet.writeBlank(row, 0, wrongFormat);}).toThrow();
        expect(function() {sheet.writeBlank(row, 0);}).toThrow();
        expect(sheet.writeBlank(row, 0, format)).toBe(sheet);

        row++;
    });

    it('sheet.readFormula reads a formula', function() {
        sheet.writeFormula(row, 0,'=SUM(A1:A10)');
        sheet.writeNum(row, 1, 10);

        expect(function() {sheet.readFormula();}).toThrow();
        expect(function() {sheet.readFormula.call({}, row, 0);}).toThrow();

        var formatRef = {};
        expect(function() {sheet.readFormula(row, 1);}).toThrow();
        expect(sheet.readFormula(row, 0)).toBe('SUM(A1:A10)');
        expect(sheet.readFormula(row, 0, formatRef)).toBe('SUM(A1:A10)');
        expect(formatRef.format instanceof format.constructor).toBe(true);

        row++;
    });

    it('sheet.writeComment writes a comment', function() {
        expect(function() {sheet.writeComment();}).toThrow();
        expect(function() {sheet.writeComment.call({}, row, 0, 'comment');}).toThrow();

        expect(sheet.writeComment(row, 0, 'comment')).toBe(sheet);

        row++;
    });

    it('sheet.readComment reads a comment', function() {
        sheet.writeString(row, 0, 'foo');
        sheet.writeComment(row, 0,'comment');

        expect(function() {sheet.readComment();}).toThrow();
        expect(function() {sheet.readComment.call({}, row, 0);}).toThrow();

        var formatRef = {};
        expect(function() {sheet.readComment(row, 1);}).toThrow();
        expect(sheet.readComment(row, 0)).toBe('comment');

        row++;
    });

    it('sheet.readError reads an error', function() {
        sheet.writeStr(row, 0, '');

        expect(function() {sheet.readError();}).toThrow();
        expect(function() {sheet.readError.call({}, row, 0);}).toThrow();

        expect(sheet.readError(row, 0)).toBe(xl.ERRORTYPE_NOERROR);

        row++;
    });

    it('sheet.colWidth reads colum width', function() {
        sheet.setCol(0, 0, 42);

        expect(function() {sheet.colWidth();}).toThrow();
        expect(function() {sheet.colWidth.call({}, 0);}).toThrow();

        expect(sheet.colWidth(0)).toEqual(42);
    });

    it('sheet.rowHeight reads row Height', function() {
        sheet.setRow(1, 42);

        expect(function() {sheet.rowHeight();}).toThrow();
        expect(function() {sheet.rowHeight.call({}, 1);}).toThrow();

        expect(sheet.rowHeight(1)).toEqual(42);
    });

    it('sheet.setCol configures columns', function() {
        expect(function() {sheet.setCol();}).toThrow();
        expect(function() {sheet.setCol.call({}, 0, 0, 42);}).toThrow();

        expect(function() {sheet.setCol(0, 0, 42, wrongFormat);}).toThrow();

        expect(sheet.setCol(0, 0, 42)).toBe(sheet);
        expect(sheet.setCol(0, 0, 42, format, false)).toBe(sheet);
    });

    it('sheet.setRow configures rows', function() {
        expect(function() {sheet.setRow();}).toThrow();
        expect(function() {sheet.setRow.call({}, 1, 42);}).toThrow();

        expect(function() {sheet.setRow(1, 42, wrongFormat);}).toThrow();

        expect(sheet.setRow(1, 42)).toBe(sheet);
        expect(sheet.setRow(1, 42, format, false)).toBe(sheet);
    });

    it('sheet.rowHidden checks whether a row is hidden', function() {
        expect(function() {sheet.rowHidden();}).toThrow();
        expect(function() {sheet.rowHidden.call({}, row);}).toThrow();

        expect(sheet.rowHidden(row)).toBe(false);
    });

    it('sheet.setRowHidden hides or shows a row', function() {
        expect(function() {sheet.setRowHidden();}).toThrow();
        expect(function() {sheet.setRowHidden.call({}, row, true);}).toThrow();

        expect(sheet.setRowHidden(row, true)).toBe(sheet);
        expect(sheet.rowHidden(row)).toBe(true);
        expect(sheet.setRowHidden(row, false)).toBe(sheet);
        expect(sheet.rowHidden(row)).toBe(false);
    });

    it('sheet.colHidden checks whether a column is hidden', function() {
        expect(function() {sheet.colHidden();}).toThrow();
        expect(function() {sheet.colHidden.call({}, 0);}).toThrow();

        expect(sheet.colHidden(0)).toBe(false);
    });

    it('sheet.setColHidden hides or shows a column', function() {
        expect(function() {sheet.setColHidden();}).toThrow();
        expect(function() {sheet.setColHidden.call({}, 0, true);}).toThrow();

        expect(sheet.setColHidden(0, true)).toBe(sheet);
        expect(sheet.colHidden(0)).toBe(true);
        expect(sheet.setColHidden(0, false)).toBe(sheet);
        expect(sheet.colHidden(0)).toBe(false);
    });

});
<|endoftext|>


<commit_before>var app = require('./app.js'),
	$ = require('jquery'),
	socket = require('socket.io-client')(3000),
	user;

socket.on('connect', function () {

	socket.on('event', function (data) {

	});

	socket.on('disconnect', function () {

	});

});

function start() {

	// Start the app here

	select();

	$(window).on('pageshow', function () {
		if (user) {
			$('#wait').hide();
			$('#content').addClass('devices');
			$('#container').removeClass('centerized');
			$('#devices').show();
		}
	});

}

function initializeUser(cb) {
	$.getJSON('/user', function (res) {
		if (res.user) {
			user = res.user;
			cb();
		} else {
			login();
		}
	});
}

function login() {

	$('#login').show();
	$('#login-button').click(function () {
		location = '/auth/google';
	});

}

function selectAll() {
	$('input[name="labels[]"]').not(':disabled').prop('checked', true);
}

function selectNone() {
	$('input[name="labels[]"]').not(':disabled').removeAttr('checked');
}

function select() {

	if (!user) {
		initializeUser(select);
		return;
	}

	$('#content').addClass('devices');

	$('#devices-form').submit(selectSubmit);

	$('#address').focus(function (event) {
		if (!event.target.value) {
			event.target.value = 'http://www.';
		}
	});

	setTimeout(function () {
		$('#devices').show();
		$('#container').removeClass('centerized');
	}, 300);

	var address = localStorage.getItem('address');

	if (address) {
		$('#address').val(address);
	}

	$('#select-all').click(selectAll);
	$('#select-none').click(selectNone);

	$('#stop-testing').click(stopTesting);

	var devicesList = $('#devices-list');
	$.getJSON('/devices', function (data) {

		$.each(data, function (key, value) {

			var rowElement = $('

');

			rowElement.append(
				'' + value.label + '	' +
				'' + (value.name || '') + '	' +
				'' + (value.model || '') + '	' +
				'' + (value.os || '') + '	' +
				'' + (value.serial || '') + '	' +
				'' + (value.imei || '') + '	' +
				'' + (value.location || '') + '	' +
				'' + (value.owner || '') + '	'
			);

			if (user.id == value.userId) {

				var
					cellElement = $('	'),
					spanElement = $('' + (value.userName || '') + '');

				cellElement.click(function (event) {

					$.post('/stop', {label: value.label});

					spanElement.remove();
					cellElement.removeClass('emphasize');
					$('input[type="checkbox"][value="' + value.label + '"]').removeAttr('disabled');
					return false;

				});

				cellElement.append(spanElement);
				rowElement.append(cellElement);

			} else {
				rowElement.append('' + (value.userName || '') + '	');
			}

			rowElement.append(
				'<time>' + (value.lastUsed ? moment(new Date(value.lastUsed)).fromNow() : '') + '</time>	' +
				'<input type="checkbox" name="labels[]" value="' + value.label + '" ' + (value.userId ? 'disabled' : '') + '>	'
			);

			devicesList.append(rowElement);

		});

		$('#devices-list [contenteditable]').blur(function (event) {

			var
				element = $(event.target),
				label = element.parent().attr('data-label'),
				key = element.attr('data-key'),
				value = element.text();

			$.post('/save', {label: label, key: key, value: value});

		});

		$('#devices-list [contenteditable]').keypress(function (event) {
			if (event.which == 13) {
				event.target.blur();
				return false;
			}
		});

	});

}

function selectSubmit(event) {

	var address = $('#address').val();

	localStorage.setItem('address', address);

	$.post('/start', $('#devices-form').serialize());

	var interval = setInterval(function () {
		$.getJSON('/ping', {user_id: user.id}, function (data) {
			if (data.address) {
				clearInterval(interval);
				setTimeout(function () {
					location = data.address;
				}, 1000);
			}
		});
	}, 1000);

	$('#container').addClass('centerized');
	$('#stop-testing').show();
	$('#go').hide();

	setTimeout(function () {

		$('#devices').hide();
		$('#content').removeClass('devices');

		setTimeout(function () {
			$('#wait').show();
		}, 300);

	}, 0);

	return false;

}

function stopTesting() {
	$.post('/stop', {userId: user.id}, function () {
		$('#stop-testing').hide();
		$('#go').show();
	});
}

exports = module.exports = {
	start: start
};
<commit_msg>Replace tabs with 2 spaces<commit_after>var app = require('./app.js'),
  $ = require('jquery'),
  socket = require('socket.io-client')(3000),
  user;

socket.on('connect', function () {

  socket.on('event', function (data) {

  });

  socket.on('disconnect', function () {

  });

});

function start() {

  // Start the app here

  select();

  $(window).on('pageshow', function () {
    if (user) {
      $('#wait').hide();
      $('#content').addClass('devices');
      $('#container').removeClass('centerized');
      $('#devices').show();
    }
  });

}

function initializeUser(cb) {
  $.getJSON('/user', function (res) {
    if (res.user) {
      user = res.user;
      cb();
    } else {
      login();
    }
  });
}

function login() {

  $('#login').show();
  $('#login-button').click(function () {
    location = '/auth/google';
  });

}

function selectAll() {
  $('input[name="labels[]"]').not(':disabled').prop('checked', true);
}

function selectNone() {
  $('input[name="labels[]"]').not(':disabled').removeAttr('checked');
}

function select() {

  if (!user) {
    initializeUser(select);
    return;
  }

  $('#content').addClass('devices');

  $('#devices-form').submit(selectSubmit);

  $('#address').focus(function (event) {
    if (!event.target.value) {
      event.target.value = 'http://www.';
    }
  });

  setTimeout(function () {
    $('#devices').show();
    $('#container').removeClass('centerized');
  }, 300);

  var address = localStorage.getItem('address');

  if (address) {
    $('#address').val(address);
  }

  $('#select-all').click(selectAll);
  $('#select-none').click(selectNone);

  $('#stop-testing').click(stopTesting);

  var devicesList = $('#devices-list');
  $.getJSON('/devices', function (data) {

    $.each(data, function (key, value) {

      var rowElement = $('

');

      rowElement.append(
        '' + value.label + '	' +
        '' + (value.name || '') + '	' +
        '' + (value.model || '') + '	' +
        '' + (value.os || '') + '	' +
        '' + (value.serial || '') + '	' +
        '' + (value.imei || '') + '	' +
        '' + (value.location || '') + '	' +
        '' + (value.owner || '') + '	'
      );

      if (user.id == value.userId) {

        var
          cellElement = $('	'),
          spanElement = $('' + (value.userName || '') + '');

        cellElement.click(function (event) {

          $.post('/stop', {label: value.label});

          spanElement.remove();
          cellElement.removeClass('emphasize');
          $('input[type="checkbox"][value="' + value.label + '"]').removeAttr('disabled');
          return false;

        });

        cellElement.append(spanElement);
        rowElement.append(cellElement);

      } else {
        rowElement.append('' + (value.userName || '') + '	');
      }

      rowElement.append(
        '<time>' + (value.lastUsed ? moment(new Date(value.lastUsed)).fromNow() : '') + '</time>	' +
        '<input type="checkbox" name="labels[]" value="' + value.label + '" ' + (value.userId ? 'disabled' : '') + '>	'
      );

      devicesList.append(rowElement);

    });

    $('#devices-list [contenteditable]').blur(function (event) {

      var
        element = $(event.target),
        label = element.parent().attr('data-label'),
        key = element.attr('data-key'),
        value = element.text();

      $.post('/save', {label: label, key: key, value: value});

    });

    $('#devices-list [contenteditable]').keypress(function (event) {
      if (event.which == 13) {
        event.target.blur();
        return false;
      }
    });

  });

}

function selectSubmit(event) {

  var address = $('#address').val();

  localStorage.setItem('address', address);

  $.post('/start', $('#devices-form').serialize());

  var interval = setInterval(function () {
    $.getJSON('/ping', {user_id: user.id}, function (data) {
      if (data.address) {
        clearInterval(interval);
        setTimeout(function () {
          location = data.address;
        }, 1000);
      }
    });
  }, 1000);

  $('#container').addClass('centerized');
  $('#stop-testing').show();
  $('#go').hide();

  setTimeout(function () {

    $('#devices').hide();
    $('#content').removeClass('devices');

    setTimeout(function () {
      $('#wait').show();
    }, 300);

  }, 0);

  return false;

}

function stopTesting() {
  $.post('/stop', {userId: user.id}, function () {
    $('#stop-testing').hide();
    $('#go').show();
  });
}

exports = module.exports = {
  start: start
};
<|endoftext|>


<commit_before>/**
 * Created by james on 13-11-22.
 * pipeline middleware
 */
var crypto = require('crypto');
var redis = require("redis");
var hbase = require('hbase');
var os = require("os");

var pipeline = function(spiderCore){
    this.spiderCore = spiderCore;
    logger = spiderCore.settings.logger;
}

////report to spidercore standby////////////////////////
pipeline.prototype.assembly = function(){
    this.hbase_cli = hbase({
        host:this.spiderCore.settings['crawled_hbase_db'][0],
        port:this.spiderCore.settings['crawled_hbase_db'][1]
    });
    this.hbase_table = this.hbase_cli.getTable('crawled');
    this.redis_cli0 = redis.createClient(this.spiderCore.settings['driller_info_redis_db'][1],this.spiderCore.settings['driller_info_redis_db'][0]);
    this.redis_cli1 = redis.createClient(this.spiderCore.settings['url_info_redis_db'][1],this.spiderCore.settings['url_info_redis_db'][0]);
    var spider = this;
    var spiderCore = this.spiderCore;
    this.redis_cli0.select(this.spiderCore.settings['driller_info_redis_db'][2], function(err,value) {
        spider.redis_cli1.select(spiderCore.settings['url_info_redis_db'][2], function(err,value) {
            spiderCore.emit('standby','pipeline');
        });
    });
}

pipeline.prototype.save_links = function(page_url,linkobjs){
    var spiderCore = this.spiderCore;
    var redis_cli0 = this.redis_cli0;
    var redis_cli1 = this.redis_cli1;
            for(alias in linkobjs){
                var links = linkobjs[alias];
                for(i=0;i<links.length;i++){
                    var link = links[i];
                    (function(pageurl,alias,link){
                        //--push url to queue--begin--
                        redis_cli0.rpush(alias,link,function(err, value){
                            if(err)throw(err);
                            logger.debug('push url: '+link+' to urllib: '+alias);
                            var kk = crypto.createHash('md5').update(link+'').digest('hex');
                            var vv = {
                                'url':link,
                                'trace':alias,
                                'referer':pageurl,
                                'create':(new Date()).getTime(),
                                'records':JSON.stringify([]),
                                'last':(new Date()).getTime(),
                                'status':'hit'
                            }
                            redis_cli1.hmset(kk,vv,function(err, value){
                                if (err) throw(err);
                                logger.debug(' save url info: '+link);
                                if(spiderCore.settings['test'])spiderCore.emit('append_link',link);
                                //redis_cli1.quit();
                            });
                                //redis_cli0.quit();
                        });
                        //--push url to queue--end--
                    })(page_url,alias,link);
                }
            }
}

pipeline.prototype.save_content = function(pageurl,content,referer,pattern){
    var url_hash = crypto.createHash('md5').update(pageurl+'').digest('hex');
    var spider = os.hostname()+'-'+process.pid;

//    var val_cells = [
//                { "column":"basic:spider","timestamp":Date.now(),"$":spider},
//                { "column":"basic:url","timestamp":Date.now(),"$":pageurl},
//                { "column":"basic:content","timestamp":Date.now(),"$":content}
//                ];
        var keylist = ['basic:spider','basic:url','basic:content','basic:referer','basic:url_pattern'];
        var valuelist = [spider,pageurl,content,referer,pattern];
        var row = this.hbase_table.getRow(url_hash);
    try{
        row.put(keylist,valuelist,function(err,success){
                      logger.debug('insert content extracted from '+pageurl);
                  });
//        row.put(['basic:spider','basic:url','basic:content'],[spider,pageurl,content],function(err,success){
//            logger.debug('insert content extracted from '+pageurl);
//        });
    }catch(e){
        logger.error('use bench mode insert data , err: '+e);
        row.put('basic:spider',spider,function(err, success){
            logger.debug(pageurl+' update basic:spider ');
        });
        row.put('basic:url',pageurl,function(err, success){
            logger.debug(pageurl+' update basic:url ');
        });
        row.put('basic:content',content,function(err, success){
            logger.debug(pageurl+' update basic:content ');
        });
        row.put('basic:referer',referer,function(err, success){
            logger.debug(pageurl+' update basic:referer ');
        });
        row.put('basic:url_pattern',pattern,function(err, success){
            logger.debug(pageurl+' update basic:url_pattern ');
        });
    }
}

pipeline.prototype.save_jsresult = function(pageurl,content,referer,pattern){
    var url_hash = crypto.createHash('md5').update(pageurl+'').digest('hex');
    var spider = os.hostname()+'-'+process.pid;

    var keylist = ['basic:spider','basic:url','basic:jsresult','basic:referer','basic:url_pattern'];
    var valuelist = [spider,pageurl,content,referer,pattern];

    var row = this.hbase_table.getRow(url_hash);
    try{
        row.put(keylist,valuelist,function(err,success){
            logger.debug('insert js result extracted from '+pageurl);
        });
    }catch(e){
        logger.error('use bench mode insert jsresult , err: '+e);

        row.put('basic:spider',spider,function(err, success){
            logger.debug(pageurl+' update basic:spider ');
        });
        row.put('basic:url',pageurl,function(err, success){
            logger.debug(pageurl+' update basic:url ');
        });
        row.put('basic:jsresult',content,function(err, success){
            logger.debug(pageurl+' update basic:jsresult ');
        });
        row.put('basic:referer',referer,function(err, success){
            logger.debug(pageurl+' update basic:referer ');
        });
        row.put('basic:url_pattern',pattern,function(err, success){
            logger.debug(pageurl+' update basic:url_pattern ');
        });
    }
}

pipeline.prototype.save =function(extracted_info){
    if(this.spiderCore.settings['test']){
        var fs = require('fs');
        var path = require('path');
        var htmlfile = path.join(__dirname,'..', 'instance',this.spiderCore.settings['instance'],'logs','debug-page.html');
        var resultfile = path.join(__dirname,'..', 'instance',this.spiderCore.settings['instance'],'logs','debug-result.json');
        fs.writeFile(htmlfile,extracted_info['content'],'utf8', function (err) {
            if (err)throw err;
            logger.debug('Content saved, '+htmlfile);
        });
        fs.writeFile(resultfile,JSON.stringify(extracted_info),'utf8',function(err){
            if (err)throw err;
            logger.debug('Crawling result saved, '+resultfile);
        });
    }else{
        if(extracted_info['drill_link'])this.save_links(extracted_info['url'],extracted_info['drill_link']);
        if(extracted_info['origin']['save_page'])this.save_content(extracted_info['url'],extracted_info['content'],extracted_info['origin']['referer'],extracted_info['origin']['url_pattern']);
        if(extracted_info['js_result'])this.save_jsresult(extracted_info['url'],extracted_info['js_result'],extracted_info['origin']['url_pattern']);
    }
}

module.exports = pipeline;<commit_msg>fix save pattern bug<commit_after>/**
 * Created by james on 13-11-22.
 * pipeline middleware
 */
var crypto = require('crypto');
var redis = require("redis");
var hbase = require('hbase');
var os = require("os");

var pipeline = function(spiderCore){
    this.spiderCore = spiderCore;
    logger = spiderCore.settings.logger;
}

////report to spidercore standby////////////////////////
pipeline.prototype.assembly = function(){
    this.hbase_cli = hbase({
        host:this.spiderCore.settings['crawled_hbase_db'][0],
        port:this.spiderCore.settings['crawled_hbase_db'][1]
    });
    this.hbase_table = this.hbase_cli.getTable('crawled');
    this.redis_cli0 = redis.createClient(this.spiderCore.settings['driller_info_redis_db'][1],this.spiderCore.settings['driller_info_redis_db'][0]);
    this.redis_cli1 = redis.createClient(this.spiderCore.settings['url_info_redis_db'][1],this.spiderCore.settings['url_info_redis_db'][0]);
    var spider = this;
    var spiderCore = this.spiderCore;
    this.redis_cli0.select(this.spiderCore.settings['driller_info_redis_db'][2], function(err,value) {
        spider.redis_cli1.select(spiderCore.settings['url_info_redis_db'][2], function(err,value) {
            spiderCore.emit('standby','pipeline');
        });
    });
}

pipeline.prototype.save_links = function(page_url,linkobjs){
    var spiderCore = this.spiderCore;
    var redis_cli0 = this.redis_cli0;
    var redis_cli1 = this.redis_cli1;
            for(alias in linkobjs){
                var links = linkobjs[alias];
                for(i=0;i<links.length;i++){
                    var link = links[i];
                    (function(pageurl,alias,link){
                        //--push url to queue--begin--
                        redis_cli0.rpush(alias,link,function(err, value){
                            if(err)throw(err);
                            logger.debug('push url: '+link+' to urllib: '+alias);
                            var kk = crypto.createHash('md5').update(link+'').digest('hex');
                            var vv = {
                                'url':link,
                                'trace':alias,
                                'referer':pageurl,
                                'create':(new Date()).getTime(),
                                'records':JSON.stringify([]),
                                'last':(new Date()).getTime(),
                                'status':'hit'
                            }
                            redis_cli1.hmset(kk,vv,function(err, value){
                                if (err) throw(err);
                                logger.debug(' save url info: '+link);
                                if(spiderCore.settings['test'])spiderCore.emit('append_link',link);
                                //redis_cli1.quit();
                            });
                                //redis_cli0.quit();
                        });
                        //--push url to queue--end--
                    })(page_url,alias,link);
                }
            }
}

pipeline.prototype.save_content = function(pageurl,content,referer,pattern){
    var url_hash = crypto.createHash('md5').update(pageurl+'').digest('hex');
    var spider = os.hostname()+'-'+process.pid;

//    var val_cells = [
//                { "column":"basic:spider","timestamp":Date.now(),"$":spider},
//                { "column":"basic:url","timestamp":Date.now(),"$":pageurl},
//                { "column":"basic:content","timestamp":Date.now(),"$":content}
//                ];
        var keylist = ['basic:spider','basic:url','basic:content','basic:referer','basic:url_pattern'];
        var valuelist = [spider,pageurl,content,referer,pattern];
        var row = this.hbase_table.getRow(url_hash);
    try{
        row.put(keylist,valuelist,function(err,success){
                      logger.debug('insert content extracted from '+pageurl);
                  });
//        row.put(['basic:spider','basic:url','basic:content'],[spider,pageurl,content],function(err,success){
//            logger.debug('insert content extracted from '+pageurl);
//        });
    }catch(e){
        logger.error('use bench mode insert data , err: '+e);
        row.put('basic:spider',spider,function(err, success){
            logger.debug(pageurl+' update basic:spider ');
        });
        row.put('basic:url',pageurl,function(err, success){
            logger.debug(pageurl+' update basic:url ');
        });
        row.put('basic:content',content,function(err, success){
            logger.debug(pageurl+' update basic:content ');
        });
        row.put('basic:referer',referer,function(err, success){
            logger.debug(pageurl+' update basic:referer ');
        });
        row.put('basic:url_pattern',pattern,function(err, success){
            logger.debug(pageurl+' update basic:url_pattern ');
        });
    }
}

pipeline.prototype.save_jsresult = function(pageurl,content,referer,pattern){
    var url_hash = crypto.createHash('md5').update(pageurl+'').digest('hex');
    var spider = os.hostname()+'-'+process.pid;

    var keylist = ['basic:spider','basic:url','basic:jsresult','basic:referer','basic:url_pattern'];
    var valuelist = [spider,pageurl,content,referer,pattern];

    var row = this.hbase_table.getRow(url_hash);
    try{
        row.put(keylist,valuelist,function(err,success){
            logger.debug('insert js result extracted from '+pageurl);
        });
    }catch(e){
        logger.error('use bench mode insert jsresult , err: '+e);

        row.put('basic:spider',spider,function(err, success){
            logger.debug(pageurl+' update basic:spider ');
        });
        row.put('basic:url',pageurl,function(err, success){
            logger.debug(pageurl+' update basic:url ');
        });
        row.put('basic:jsresult',content,function(err, success){
            logger.debug(pageurl+' update basic:jsresult ');
        });
        row.put('basic:referer',referer,function(err, success){
            logger.debug(pageurl+' update basic:referer ');
        });
        row.put('basic:url_pattern',pattern,function(err, success){
            logger.debug(pageurl+' update basic:url_pattern ');
        });
    }
}

pipeline.prototype.save =function(extracted_info){
    if(!this.spiderCore.settings['test']){
        var fs = require('fs');
        var path = require('path');
        var htmlfile = path.join(__dirname,'..', 'instance',this.spiderCore.settings['instance'],'logs','debug-page.html');
        var resultfile = path.join(__dirname,'..', 'instance',this.spiderCore.settings['instance'],'logs','debug-result.json');
        fs.writeFile(htmlfile,extracted_info['content'],'utf8', function (err) {
            if (err)throw err;
            logger.debug('Content saved, '+htmlfile);
        });
        fs.writeFile(resultfile,JSON.stringify(extracted_info),'utf8',function(err){
            if (err)throw err;
            logger.debug('Crawling result saved, '+resultfile);
        });
    }else{
        if(extracted_info['drill_link'])this.save_links(extracted_info['url'],extracted_info['drill_link']);
        if(extracted_info['origin']['save_page'])this.save_content(extracted_info['url'],extracted_info['content'],extracted_info['origin']['referer'],extracted_info['origin']['url_pattern']);
        if(extracted_info['js_result'])this.save_jsresult(extracted_info['url'],extracted_info['js_result'],extracted_info['origin']['referer'],extracted_info['origin']['url_pattern']);
    }
}

module.exports = pipeline;<|endoftext|>


<commit_before>$(document).ready(function() {
  $.getJSON("https://api.meetup.com/Barcelona-free-software?photo-host=public&sig_id=103310792&sig=9cf16f880e9f7b358f584792d1cc83b8f44ff6c0&callback=?", function (response) {
    // Update membership count
    $(".js-members").text(response.data.members)
  });

  $.getJSON("https://api.meetup.com/barcelona-free-software/events?desc=true&photo-host=public&page=20&sig_id=103310792&status=upcoming%2Cpast&sig=6753a3f52681dd68a724d452abda5a3292fdb2ea&callback=?", function(response) {
    var upcomingMeetups = response.data.filter(function(d) { return d.status  === 'upcoming'});
    var pastMeetups = response.data.filter(function(d) { return d.status  === 'past'});

    if (!$.isEmptyObject(upcomingMeetups)) {
      $('.upcomingMeetups')
        .append('

');

      printMeetups(upcomingMeetups, '.upcomingMeetups');
    } else {
      $('.upcomingMeetups')
        .append('
None planned for the moment. Do you have something interesting to present? Contact us!
');
    }

    printMeetups(pastMeetups, '.lastMeetups');
  });

  function printMeetups(data, selector) {
    data.forEach(function(d) {
      var date = new Date(d.time);
      var day = date.getUTCDate();
      var month = date.getUTCMonth() + 1; //months from 1-12
      var year = date.getUTCFullYear();

      $(selector + ' ul')
        .append('
' + d.name + '' + day + '/' + month + '/' + year  + ' @' + d.venue.name + '
')
    });
  }
});
<commit_msg>Update email, again<commit_after>$(document).ready(function() {
  $.getJSON("https://api.meetup.com/Barcelona-free-software?photo-host=public&sig_id=103310792&sig=9cf16f880e9f7b358f584792d1cc83b8f44ff6c0&callback=?", function (response) {
    // Update membership count
    $(".js-members").text(response.data.members)
  });

  $.getJSON("https://api.meetup.com/barcelona-free-software/events?desc=true&photo-host=public&page=20&sig_id=103310792&status=upcoming%2Cpast&sig=6753a3f52681dd68a724d452abda5a3292fdb2ea&callback=?", function(response) {
    var upcomingMeetups = response.data.filter(function(d) { return d.status  === 'upcoming'});
    var pastMeetups = response.data.filter(function(d) { return d.status  === 'past'});

    if (!$.isEmptyObject(upcomingMeetups)) {
      $('.upcomingMeetups')
        .append('

');

      printMeetups(upcomingMeetups, '.upcomingMeetups');
    } else {
      $('.upcomingMeetups')
        .append('
None planned for the moment. Do you have something interesting to present? Contact us!
');
    }

    printMeetups(pastMeetups, '.lastMeetups');
  });

  function printMeetups(data, selector) {
    data.forEach(function(d) {
      var date = new Date(d.time);
      var day = date.getUTCDate();
      var month = date.getUTCMonth() + 1; //months from 1-12
      var year = date.getUTCFullYear();

      $(selector + ' ul')
        .append('
' + d.name + '' + day + '/' + month + '/' + year  + ' @' + d.venue.name + '
')
    });
  }
});
<|endoftext|>


<commit_before>function luminance(r, g, b) {
    return (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255;
}

function asciify(inputWidth, inputHeight, outputWidth, outputHeight, monochrome, ctx, fontSize, fidelity) {
    // Characters from 'darkest' to 'lightest'
    var asciiLuminanceMap = '$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft\/|()1{}[]?-_+~<>i!lI;:,"^`\'. ';
    var ratio;
    var inputSampleWidth;
    var inputSampleHeight;
    var incrementX;
    var incrementY;
    ctx.font = fontSize + "pt Courier";
    var fontWidth = ctx.measureText('W').width;
    var fontHeight = fontSize;
    resize(inputWidth, inputHeight, outputWidth, outputHeight);

    function draw(imageData) {
        // For each ascii character in the output
        for (var y = 0; y < outputHeight; y += fontHeight) {
            for (var x = 0; x < outputWidth; x += fontWidth) {
                // Loop over input sample, determine average RGB
                // and luminance values
                var blockLuminanceTotal = 0;
                var redTotal = 0;
                var greenTotal = 0;
                var blueTotal = 0;
                var area = 0;
                for (var y2 = 0; y2 < inputSampleHeight; y2 += incrementY) {
                    for (var x2 = 0; x2 < inputSampleWidth; x2 += incrementX) {
                        var index = ((Math.round(x * ratio) + x2) + ((Math.round(y * ratio) + y2) * inputWidth)) * 4;
                        if (index < imageData.length) {
                            var red = imageData[index];
                            var green = imageData[index + 1];
                            var blue = imageData[index + 2];
                            redTotal += red;
                            greenTotal += green;
                            blueTotal += blue;
                            blockLuminanceTotal += luminance(red, green, blue);
                            area += 1;
                        }
                    }
                }
                var blockLuminanceAvg = blockLuminanceTotal / area;
                var idx = Math.floor((asciiLuminanceMap.length - 1) * blockLuminanceAvg);
                if (!monochrome) {
                    var r = Math.floor(redTotal / area);
                    var g = Math.floor(greenTotal / area);
                    var b = Math.floor(blueTotal / area);
                    ctx.fillStyle = "rgb(" + r + "," + g + "," + b + ")";
                }
                var character = asciiLuminanceMap[idx];
                ctx.fillText(character, x, y);
            }
        }
    }

    function resize(iWidth, iHeight, oWidth, oHeight) {
        inputWidth = iWidth;
        inputHeight = iHeight;
        outputWidth = oWidth;
        outputHeight = oHeight;
        ratio = inputWidth / outputWidth;
        inputSampleWidth = Math.floor(fontWidth * ratio);
        inputSampleHeight = Math.floor(fontHeight * ratio);
        incrementX = Math.max(1, Math.floor(inputSampleWidth * (1 - fidelity)));
        incrementY = Math.max(1, Math.floor(inputSampleHeight * (1 - fidelity)));
    }

    function toggleMonochrome(){
        monochrome = !monochrome;
    }

    return {
        toggleMonochrome: toggleMonochrome,
        draw: draw,
        resize: resize,
    };
}

module.exports = asciify;<commit_msg>Force monochrome to black<commit_after>function luminance(r, g, b) {
    return (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255;
}

function asciify(inputWidth, inputHeight, outputWidth, outputHeight, monochrome, ctx, fontSize, fidelity) {
    // Characters from 'darkest' to 'lightest'
    var asciiLuminanceMap = '$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft\/|()1{}[]?-_+~<>i!lI;:,"^`\'. ';
    var ratio;
    var inputSampleWidth;
    var inputSampleHeight;
    var incrementX;
    var incrementY;
    ctx.font = fontSize + "pt Courier";
    var fontWidth = ctx.measureText('W').width;
    var fontHeight = fontSize;
    resize(inputWidth, inputHeight, outputWidth, outputHeight);

    function draw(imageData) {
        // For each ascii character in the output
        for (var y = 0; y < outputHeight; y += fontHeight) {
            for (var x = 0; x < outputWidth; x += fontWidth) {
                // Loop over input sample, determine average RGB
                // and luminance values
                var blockLuminanceTotal = 0;
                var redTotal = 0;
                var greenTotal = 0;
                var blueTotal = 0;
                var area = 0;
                for (var y2 = 0; y2 < inputSampleHeight; y2 += incrementY) {
                    for (var x2 = 0; x2 < inputSampleWidth; x2 += incrementX) {
                        var index = ((Math.round(x * ratio) + x2) + ((Math.round(y * ratio) + y2) * inputWidth)) * 4;
                        if (index < imageData.length) {
                            var red = imageData[index];
                            var green = imageData[index + 1];
                            var blue = imageData[index + 2];
                            redTotal += red;
                            greenTotal += green;
                            blueTotal += blue;
                            blockLuminanceTotal += luminance(red, green, blue);
                            area += 1;
                        }
                    }
                }
                var blockLuminanceAvg = blockLuminanceTotal / area;
                var idx = Math.floor((asciiLuminanceMap.length - 1) * blockLuminanceAvg);
                if (!monochrome) {
                    var r = Math.floor(redTotal / area);
                    var g = Math.floor(greenTotal / area);
                    var b = Math.floor(blueTotal / area);
                    ctx.fillStyle = "rgb(" + r + "," + g + "," + b + ")";
                }
                var character = asciiLuminanceMap[idx];
                ctx.fillText(character, x, y);
            }
        }
    }

    function resize(iWidth, iHeight, oWidth, oHeight) {
        inputWidth = iWidth;
        inputHeight = iHeight;
        outputWidth = oWidth;
        outputHeight = oHeight;
        ratio = inputWidth / outputWidth;
        inputSampleWidth = Math.floor(fontWidth * ratio);
        inputSampleHeight = Math.floor(fontHeight * ratio);
        incrementX = Math.max(1, Math.floor(inputSampleWidth * (1 - fidelity)));
        incrementY = Math.max(1, Math.floor(inputSampleHeight * (1 - fidelity)));
    }

    function toggleMonochrome(){
        ctx.fillStyle = "black";
        monochrome = !monochrome;
    }

    return {
        toggleMonochrome: toggleMonochrome,
        draw: draw,
        resize: resize,
    };
}

module.exports = asciify;<|endoftext|>


<commit_before>import { axe } from "axe-core/axe.min.js";
import report from "./reporter";

export default function () {
  window.axe.configure({
    checks: [{
      id: "color-contrast",
      options: { noScroll: true }
    }]
  });

  window.axe.a11yCheck(document, (results) => {
    report(results);
  });
}
<commit_msg>Remove scroll configuration<commit_after>import { axe } from "axe-core/axe.min.js";
import report from "./reporter";

export default function () {
  window.axe.a11yCheck(document, (results) => {
    report(results);
  });
}
<|endoftext|>


<commit_before>//Checks the latex input area and updates the rendered text area
//every keyup or every time anything is changed

const textArea = document.querySelector(".latex");
const renderArea = document.querySelector(".rendered");

textArea.addEventListener("onchange", renderText);
textArea.addEventListener("keyup", renderText);

function renderText() {
  var text = textArea.value;
  text = text.replace(/\B\$\$/gm, `<span class="latex-wrap">$$$`);
  text = text.replace(/\b\$\$/gm, `$$$</span>`);

  $(".rendered").html(text);
  MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
}

const endpoint = "https://gist.githubusercontent.com/tstusr441/ea890ae6b7e9b1f67f4c8c504f395c3d/raw/12eac49825cf53e500b2c1fa6c7829a1e23c1608/.json";
const syntax = [];

var myHeaders = new Headers();

var myInit = {
  method: 'GET',
  headers: myHeaders,
  mode: 'cors'
};

/*
 *Transferring the json data to syntax array.
 *fetch() not supported by IE, we will have to think about this
 */
fetch(endpoint, myInit)
  .then(blob => blob.json())
  .then(data => syntax.push(...data));

//Returns true when search term matches keyword on json
function findLatex(wordQuery, syntax) {
  return syntax.filter(math => {
    const regex = new RegExp(wordQuery, "gi");
    return math.keyword.match(regex);
  });
}

var results = document.querySelector(".results");

function matchResults() {

  if (!this.value) {
    results.innerHTML = ""; //This is to prevent results from popping up when search is empty
    return 0;
  }

  const resultsArray = findLatex(this.value, syntax);
  console.log(this.value);
  const html = resultsArray.map(math => {
    return `

        ${math.keyword}

    `;
  }).join('');
  results.innerHTML = html;
}

const search = document.querySelector(".search");
search.addEventListener("change", matchResults);
search.addEventListener("keyup", matchResults);

$("ul").on("click", "li", function(e) {
  for (let i = 0; i < syntax.length; i++) {
    if (e.target.innerText === syntax[i].keyword) {
      document.querySelector(".doc").innerHTML = `<code>${syntax[i].syntax}</code>`;
      results.innerHTML = "";
      break;
    }
  }
});
<commit_msg>detects when user types ctrl-s in text input area<commit_after>//Checks the latex input area and updates the rendered text area
//every keyup or every time anything is changed

const textArea = document.querySelector(".latex");
const renderArea = document.querySelector(".rendered");

textArea.addEventListener("onchange", renderText);
textArea.addEventListener("keyup", renderText);
textArea.addEventListener("keydown", showSearch);

function renderText() {
  var text = textArea.value;
  text = text.replace(/\B\$\$/gm, `<span class="latex-wrap">$$$`);
  text = text.replace(/\b\$\$/gm, `$$$</span>`);

  $(".rendered").html(text);
  MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
}

function showSearch(e) {
  var x = e.key;
    if (x == "s" && e.ctrlKey) {
      console.log("Trigger search");
    }
}

const endpoint = "https://gist.githubusercontent.com/tstusr441/ea890ae6b7e9b1f67f4c8c504f395c3d/raw/12eac49825cf53e500b2c1fa6c7829a1e23c1608/.json";
const syntax = [];

var myHeaders = new Headers();

var myInit = {
  method: 'GET',
  headers: myHeaders,
  mode: 'cors'
};

/*
 *Transferring the json data to syntax array.
 *fetch() not supported by IE, we will have to think about this
 */
fetch(endpoint, myInit)
  .then(blob => blob.json())
  .then(data => syntax.push(...data));

//Returns true when search term matches keyword on json
function findLatex(wordQuery, syntax) {
  return syntax.filter(math => {
    const regex = new RegExp(wordQuery, "gi");
    return math.keyword.match(regex);
  });
}

var results = document.querySelector(".results");

function matchResults() {

  if (!this.value) {
    results.innerHTML = ""; //This is to prevent results from popping up when search is empty
    return 0;
  }

  const resultsArray = findLatex(this.value, syntax);
  console.log(this.value);
  const html = resultsArray.map(math => {
    return `

        ${math.keyword}

    `;
  }).join('');
  results.innerHTML = html;
}

const search = document.querySelector(".search");
search.addEventListener("change", matchResults);
search.addEventListener("keyup", matchResults);

$("ul").on("click", "li", function(e) {
  for (let i = 0; i < syntax.length; i++) {
    if (e.target.innerText === syntax[i].keyword) {
      document.querySelector(".doc").innerHTML = `<code>${syntax[i].syntax}</code>`;
      results.innerHTML = "";
      break;
    }
  }
});
<|endoftext|>