<commit_before>var xmldoc = require('xmldoc')

function parseArrivalsBoardResponse (soapResponse) {
  var board = getTrainServicesBoard(soapResponse, 'GetArrivalBoardResponse')
  var trains = []
  board.eachChild(function (service) {
    trains.push(parseStandardService(service))
  })
  return {'trainServices': trains}
}

function parseArrivalsBoardWithDetails (soapResponse) {
  var board = getTrainServicesBoard(soapResponse, 'GetArrBoardWithDetailsResponse')
  var trains = []

  board.eachChild(function (service) {
    var train = parseStandardService(service)
    service.eachChild(function (element) {
      switch (element.name) {
        case 'lt5:previousCallingPoints':
          var previousCallingPoints = element.childNamed('lt4:callingPointList')
          train.previousCallingPoints = parseCallingPointList(previousCallingPoints)
          break
      }
    })
    trains.push(train)
  })

  return {'trainServices': trains}
}

function parseArrivalsDepartureBoard (soapResponse) {
  var board = getTrainServicesBoard(soapResponse, 'GetArrivalDepartureBoardResponse')
  var trains = []
  board.eachChild(function (service) {
    trains.push(parseStandardService(service))
  })
  return {'trainServices': trains}
}

function parseArrivalsDepartureBoardWithDetails (soapResponse) {
  var board = getTrainServicesBoard(soapResponse, 'GetArrDepBoardWithDetailsResponse')
  var trains = []

  board.eachChild(function (service) {
    var train = parseStandardService(service)
    service.eachChild(function (element) {
      switch (element.name) {
        case 'lt5:previousCallingPoints':
          var previousCallingPoints = element.childNamed('lt4:callingPointList')
          train.previousCallingPoints = parseCallingPointList(previousCallingPoints)
          break
        case 'lt5:subsequentCallingPoints':
          var subsequentCallingPoints = element.childNamed('lt4:callingPointList')
          train.subsequentCallingPoints = parseCallingPointList(subsequentCallingPoints)
          break
      }
    })
    trains.push(train)
  })

  return {'trainServices': trains}
}

function parseServiceIdResponse (soapResponse) {
  var serviceXml = extractResponseObject(soapResponse, 'GetServiceDetailsResponse')
        .childNamed('GetServiceDetailsResult')
  var service = parseStandardService(serviceXml)

  serviceXml.eachChild(function (element) {
    switch (element.name) {
      case 'lt4:previousCallingPoints':
        var previousCallingPoints = element.childNamed('lt4:callingPointList')
        service.previousCallingPoints = parseCallingPointList(previousCallingPoints)
        break
      case 'lt4:subsequentCallingPoints':
        var subsequentCallingPoints = element.childNamed('lt4:callingPointList')
        service.subsequentCallingPoints = parseCallingPointList(subsequentCallingPoints)
        break
    }
  })

  return {'serviceDetails': service}
}

function parseDepartureBoardResponse (soapResponse) {
  var board = getTrainServicesBoard(soapResponse, 'GetDepartureBoardResponse')
  var trains = []

  board.eachChild(function (service) {
    trains.push(parseStandardService(service))
  })
  return {'trainServices': trains}
}

function parseDepartureBoardWithDetailsResponse (soapResponse) {
  var board = getTrainServicesBoard(soapResponse, 'GetDepBoardWithDetailsResponse')
  var trains = []
  board.eachChild(function (service) {
    var train = parseStandardService(service)
    service.eachChild(function (element) {
      switch (element.name) {
        case 'lt5:subsequentCallingPoints':
          var subsequentCallingPoints = element.childNamed('lt4:callingPointList')
          service.subsequentCallingPoints = parseCallingPointList(subsequentCallingPoints)
          break
      }
    })
    trains.push(train)
  })
  return {'trainServices': trains}
}

function parseNextDestinationResponse (response) {
  var board = extractResponseObject(response, 'GetNextDeparturesResponse')
        .childNamed('DeparturesBoard')
        .childNamed('lt5:departures')
        .childNamed('lt5:destination')
  var trains = []

  board.eachChild(function (service) {
    trains.push(parseStandardService(service))
  })

  return {'trainServices': trains}
}

function parseNextDepartureWithDetailsResponse (response) {
  var board = extractResponseObject(response, 'GetNextDeparturesWithDetailsResponse')
        .childNamed('DeparturesBoard')
        .childNamed('lt5:departures')
        .childNamed('lt5:destination')
  var trains = []

  board.eachChild(function (service) {
    var train = parseStandardService(service)
    service.eachChild(function (element) {
      switch (element.name) {
        case 'lt5:subsequentCallingPoints':
          var subsequentCallingPoints = element.childNamed('lt4:callingPointList')
          service.subsequentCallingPoints = parseCallingPointList(subsequentCallingPoints)
          break
      }
    })
    trains.push(train)
  })

  return {'trainServices': trains}
}

function parseNextArrivalResponse (response) {
  var board = getTrainServicesBoard(response, 'GetArrivalBoardResponse')
  var trains = []
  board.eachChild(function (service) {
    trains.push(parseStandardService(service))
  })
  return {'trainServices': trains}
}

function parseFastestDeparture (response) {
  var board = extractResponseObject(response, 'GetFastestDeparturesResponse')
        .childNamed('DeparturesBoard')
        .childNamed('lt5:departures')
        .childNamed('lt5:destination')
  var trains = []
  board.eachChild(function (service) {
    trains.push(parseStandardService(service))
  })
  return {'trainServices': trains}
}

function parseFastestDepartureWithDetails (response) {
  var board = extractResponseObject(response, 'GetFastestDeparturesWithDetailsResponse')
        .childNamed('DeparturesBoard')
        .childNamed('lt5:departures')
        .childNamed('lt5:destination')
  var trains = []

  board.eachChild(function (service) {
    var train = parseStandardService(service)
    service.eachChild(function (element) {
      switch (element.name) {
        case 'lt5:subsequentCallingPoints':
          var subsequentCallingPoints = element.childNamed('lt4:callingPointList')
          service.subsequentCallingPoints = parseCallingPointList(subsequentCallingPoints)
          break
      }
    })
    trains.push(train)
  })
  return {'trainServices': trains}
}

function getTrainServicesBoard (response, responseType) {
  var board = extractResponseObject(response, responseType)
                  .childNamed('GetStationBoardResult')
                  .childNamed('lt5:trainServices')
  return board
}

function parseStandardService (service) {
  var train = {}
  service.eachChild(function (element) {
    switch (element.name) {
      case 'lt4:generatedAt':
        service.generatedAt = element.val
        break
      case 'lt4:std':
        train.std = element.val
        break
      case 'lt4:etd':
        train.etd = element.val
        break
      case 'lt4:sta':
        train.sta = element.val
        break
      case 'lt4:eta':
        train.eta = element.val
        break
      case 'lt4:platform':
        train.platform = element.val
        break
      case 'lt4:delayReason':
        train.delayReason = element.val
        break
      case 'lt4:serviceID':
        train.serviceId = element.val
        break
      case 'lt4:length':
        train.length = element.val
        break
      case 'lt4:operator':
        train.operator = element.val
        break
      case 'lt4:operatorCode':
        service.operatorCode = element.val
        break
      case 'lt5:rsid':
        service.rsid = element.val
        break
      case 'lt5:origin':
        var origin = element.childNamed('lt4:location')
        train.origin = parseLocation(origin)
        break
      case 'lt5:destination':
        var destin = element.childNamed('lt4:location')
        train.destination = parseLocation(destin)
        break
    }
  })
  return train
}

function parseCallingPointList (soapCallingPointList) {
  var callingPoints = []
  soapCallingPointList.eachChild(function (child) {
    var callingPoint = {}
    child.eachChild(function (element) {
      switch (element.name) {
        case 'lt4:length':
          callingPoint.length = element.val
          break
        case 'lt4:crs':
          callingPoint.crs = element.val
          break
        case 'lt4:locationName':
          callingPoint.locationName = element.val
          break
        case 'lt4:st':
          callingPoint.st = element.val
          break
        case 'lt4:et':
          callingPoint.et = element.val
          break
      }
    })
    callingPoints.push(callingPoint)
  })
  return callingPoints
}

function extractResponseObject (soapMessage, response) {
  var parsed = new xmldoc.XmlDocument(soapMessage)
  return parsed.childNamed('soap:Body').childNamed(response)
}

function parseLocation (location) {
  return {
    name: location.childNamed('lt4:locationName').val,
    crs: location.childNamed('lt4:crs').val
  }
}

module.exports.parseArrivalsBoardWithDetails = parseArrivalsBoardWithDetails
module.exports.parseArrivalsDepartureBoard = parseArrivalsDepartureBoard
module.exports.parseArrivalsDepartureBoardWithDetails = parseArrivalsDepartureBoardWithDetails
module.exports.parseDepartureBoardResponse = parseDepartureBoardResponse
module.exports.parseDepartureBoardWithDetailsResponse = parseDepartureBoardWithDetailsResponse
module.exports.parseServiceIdResponse = parseServiceIdResponse
module.exports.parseArrivalsBoardResponse = parseArrivalsBoardResponse
module.exports.parseNextDestinationResponse = parseNextDestinationResponse
module.exports.parseNextDepartureWithDetailsResponse = parseNextDepartureWithDetailsResponse
module.exports.parseNextArrivalResponse = parseNextArrivalResponse
module.exports.parseFastestDeparture = parseFastestDeparture
module.exports.parseFastestDeparturesWithDetail = parseFastestDepartureWithDetails
<commit_msg>consolidate departure board destination extraction<commit_after>var xmldoc = require('xmldoc')

function parseArrivalsBoardResponse (soapResponse) {
  var board = getTrainServicesBoard(soapResponse, 'GetArrivalBoardResponse')
  var trains = []
  board.eachChild(function (service) {
    trains.push(parseStandardService(service))
  })
  return {'trainServices': trains}
}

function parseArrivalsBoardWithDetails (soapResponse) {
  var board = getTrainServicesBoard(soapResponse, 'GetArrBoardWithDetailsResponse')
  var trains = []

  board.eachChild(function (service) {
    var train = parseStandardService(service)
    service.eachChild(function (element) {
      switch (element.name) {
        case 'lt5:previousCallingPoints':
          var previousCallingPoints = element.childNamed('lt4:callingPointList')
          train.previousCallingPoints = parseCallingPointList(previousCallingPoints)
          break
      }
    })
    trains.push(train)
  })

  return {'trainServices': trains}
}

function parseArrivalsDepartureBoard (soapResponse) {
  var board = getTrainServicesBoard(soapResponse, 'GetArrivalDepartureBoardResponse')
  var trains = []
  board.eachChild(function (service) {
    trains.push(parseStandardService(service))
  })
  return {'trainServices': trains}
}

function parseArrivalsDepartureBoardWithDetails (soapResponse) {
  var board = getTrainServicesBoard(soapResponse, 'GetArrDepBoardWithDetailsResponse')
  var trains = []

  board.eachChild(function (service) {
    var train = parseStandardService(service)
    service.eachChild(function (element) {
      switch (element.name) {
        case 'lt5:previousCallingPoints':
          var previousCallingPoints = element.childNamed('lt4:callingPointList')
          train.previousCallingPoints = parseCallingPointList(previousCallingPoints)
          break
        case 'lt5:subsequentCallingPoints':
          var subsequentCallingPoints = element.childNamed('lt4:callingPointList')
          train.subsequentCallingPoints = parseCallingPointList(subsequentCallingPoints)
          break
      }
    })
    trains.push(train)
  })

  return {'trainServices': trains}
}

function parseServiceIdResponse (soapResponse) {
  var serviceXml = extractResponseObject(soapResponse, 'GetServiceDetailsResponse')
        .childNamed('GetServiceDetailsResult')
  var service = parseStandardService(serviceXml)

  serviceXml.eachChild(function (element) {
    switch (element.name) {
      case 'lt4:previousCallingPoints':
        var previousCallingPoints = element.childNamed('lt4:callingPointList')
        service.previousCallingPoints = parseCallingPointList(previousCallingPoints)
        break
      case 'lt4:subsequentCallingPoints':
        var subsequentCallingPoints = element.childNamed('lt4:callingPointList')
        service.subsequentCallingPoints = parseCallingPointList(subsequentCallingPoints)
        break
    }
  })

  return {'serviceDetails': service}
}

function parseDepartureBoardResponse (soapResponse) {
  var board = getTrainServicesBoard(soapResponse, 'GetDepartureBoardResponse')
  var trains = []

  board.eachChild(function (service) {
    trains.push(parseStandardService(service))
  })
  return {'trainServices': trains}
}

function parseDepartureBoardWithDetailsResponse (soapResponse) {
  var board = getTrainServicesBoard(soapResponse, 'GetDepBoardWithDetailsResponse')
  var trains = []
  board.eachChild(function (service) {
    var train = parseStandardService(service)
    service.eachChild(function (element) {
      switch (element.name) {
        case 'lt5:subsequentCallingPoints':
          var subsequentCallingPoints = element.childNamed('lt4:callingPointList')
          service.subsequentCallingPoints = parseCallingPointList(subsequentCallingPoints)
          break
      }
    })
    trains.push(train)
  })
  return {'trainServices': trains}
}

function parseNextDestinationResponse (response) {
  var board = getDepartureBoardDestination(response, 'GetNextDeparturesResponse')
  var trains = []
  board.eachChild(function (service) {
    trains.push(parseStandardService(service))
  })

  return {'trainServices': trains}
}

function parseNextDepartureWithDetailsResponse (response) {
  var board = getDepartureBoardDestination(response, 'GetNextDeparturesWithDetailsResponse')
  var trains = []

  board.eachChild(function (service) {
    var train = parseStandardService(service)
    service.eachChild(function (element) {
      switch (element.name) {
        case 'lt5:subsequentCallingPoints':
          var subsequentCallingPoints = element.childNamed('lt4:callingPointList')
          service.subsequentCallingPoints = parseCallingPointList(subsequentCallingPoints)
          break
      }
    })
    trains.push(train)
  })

  return {'trainServices': trains}
}

function parseNextArrivalResponse (response) {
  var board = getTrainServicesBoard(response, 'GetArrivalBoardResponse')
  var trains = []
  board.eachChild(function (service) {
    trains.push(parseStandardService(service))
  })
  return {'trainServices': trains}
}

function parseFastestDeparture (response) {
  var board = getDepartureBoardDestination(response, 'GetFastestDeparturesResponse')
  var trains = []
  board.eachChild(function (service) {
    trains.push(parseStandardService(service))
  })
  return {'trainServices': trains}
}

function parseFastestDepartureWithDetails (response) {
  var board = getDepartureBoardDestination(response, 'GetFastestDeparturesWithDetailsResponse')
  var trains = []

  board.eachChild(function (service) {
    var train = parseStandardService(service)
    service.eachChild(function (element) {
      switch (element.name) {
        case 'lt5:subsequentCallingPoints':
          var subsequentCallingPoints = element.childNamed('lt4:callingPointList')
          service.subsequentCallingPoints = parseCallingPointList(subsequentCallingPoints)
          break
      }
    })
    trains.push(train)
  })
  return {'trainServices': trains}
}

function getTrainServicesBoard (response, responseType) {
  var board = extractResponseObject(response, responseType)
                  .childNamed('GetStationBoardResult')
                  .childNamed('lt5:trainServices')
  return board
}

function parseStandardService (service) {
  var train = {}
  service.eachChild(function (element) {
    switch (element.name) {
      case 'lt4:generatedAt':
        service.generatedAt = element.val
        break
      case 'lt4:std':
        train.std = element.val
        break
      case 'lt4:etd':
        train.etd = element.val
        break
      case 'lt4:sta':
        train.sta = element.val
        break
      case 'lt4:eta':
        train.eta = element.val
        break
      case 'lt4:platform':
        train.platform = element.val
        break
      case 'lt4:delayReason':
        train.delayReason = element.val
        break
      case 'lt4:serviceID':
        train.serviceId = element.val
        break
      case 'lt4:length':
        train.length = element.val
        break
      case 'lt4:operator':
        train.operator = element.val
        break
      case 'lt4:operatorCode':
        service.operatorCode = element.val
        break
      case 'lt5:rsid':
        service.rsid = element.val
        break
      case 'lt5:origin':
        var origin = element.childNamed('lt4:location')
        train.origin = parseLocation(origin)
        break
      case 'lt5:destination':
        var destin = element.childNamed('lt4:location')
        train.destination = parseLocation(destin)
        break
    }
  })
  return train
}

function parseCallingPointList (soapCallingPointList) {
  var callingPoints = []
  soapCallingPointList.eachChild(function (child) {
    var callingPoint = {}
    child.eachChild(function (element) {
      switch (element.name) {
        case 'lt4:length':
          callingPoint.length = element.val
          break
        case 'lt4:crs':
          callingPoint.crs = element.val
          break
        case 'lt4:locationName':
          callingPoint.locationName = element.val
          break
        case 'lt4:st':
          callingPoint.st = element.val
          break
        case 'lt4:et':
          callingPoint.et = element.val
          break
      }
    })
    callingPoints.push(callingPoint)
  })
  return callingPoints
}

function extractResponseObject (soapMessage, response) {
  var parsed = new xmldoc.XmlDocument(soapMessage)
  return parsed.childNamed('soap:Body').childNamed(response)
}

function parseLocation (location) {
  return {
    name: location.childNamed('lt4:locationName').val,
    crs: location.childNamed('lt4:crs').val
  }
}

function getDepartureBoardDestination (response, responseType) {
  var board = extractResponseObject(response, responseType)
        .childNamed('DeparturesBoard')
        .childNamed('lt5:departures')
        .childNamed('lt5:destination')
  return board
}

module.exports.parseArrivalsBoardWithDetails = parseArrivalsBoardWithDetails
module.exports.parseArrivalsDepartureBoard = parseArrivalsDepartureBoard
module.exports.parseArrivalsDepartureBoardWithDetails = parseArrivalsDepartureBoardWithDetails
module.exports.parseDepartureBoardResponse = parseDepartureBoardResponse
module.exports.parseDepartureBoardWithDetailsResponse = parseDepartureBoardWithDetailsResponse
module.exports.parseServiceIdResponse = parseServiceIdResponse
module.exports.parseArrivalsBoardResponse = parseArrivalsBoardResponse
module.exports.parseNextDestinationResponse = parseNextDestinationResponse
module.exports.parseNextDepartureWithDetailsResponse = parseNextDepartureWithDetailsResponse
module.exports.parseNextArrivalResponse = parseNextArrivalResponse
module.exports.parseFastestDeparture = parseFastestDeparture
module.exports.parseFastestDeparturesWithDetail = parseFastestDepartureWithDetails
<|endoftext|>


<commit_before>/*
 * grunt-ng-constant
 * https://github.com/werk85/grunt-ng-constant
 *
 * Copyright (c) 2013 werk85
 * Licensed under the MIT license.
 */

'use strict';

var DEFAULT_WRAP = '(function(angular, undefined) {\n\t <%= __ngModule %> \n})(angular);';

module.exports = function (grunt) {
  var _ = grunt.util._;

  function toArray(value) {
    return _.isArray(value) ? value : [value];
  }

  function stringify(value, space) {
    return _.isUndefined(value) ? 'undefined' : JSON.stringify(value, null, space);
  }

  grunt.registerMultiTask('ngconstant', 'Dynamic angular constant generator task.', function () {
    var path = require('path');

    var options = this.options({
      space: '\t',
      deps: [],
      wrap: false
    });
    var template = grunt.file.read(path.join(__dirname, 'constant.tpl.ejs'));
    var compiler = _.template(template);
    var rawOptions = grunt.config.getRaw(this.name);
    var rawData = toArray(grunt.config.getRaw(this.name + '.' + this.target));

    toArray(this.data).forEach(function (module, index) {
      var constants = _.map(module.constants, function (value, name) {
        return {
          name: name,
          value: stringify(value, options.space)
        };
      });

      // Create the module string
      var result = compiler({
        moduleName: module.name,
        deps: module.deps || options.deps,
        constants: constants
      });

      // Handle wrapping
      var wrap = rawData[index].wrap || rawOptions.wrap || '<%= __ngModule %>';
      if (wrap === true) {
        wrap = DEFAULT_WRAP;
      }
      result = grunt.template.process(wrap, {
        data: _.extend(grunt.config(), {
          '__ngModule': result
        })
      });

      grunt.file.write(module.dest, result);
      grunt.log.writeln('Module ' + module.name + ' created at ' + module.dest);
    });
    grunt.log.ok();
  });
};
<commit_msg>Code cleanups<commit_after>/*
 * grunt-ng-constant
 * https://github.com/werk85/grunt-ng-constant
 *
 * Copyright (c) 2013 werk85
 * Licensed under the MIT license.
 */

'use strict';

var path = require('path');

var DEFAULT_WRAP = '(function(angular, undefined) {\n\t <%= __ngModule %> \n})(angular);';
var TEMPLATE_PATH = path.join(__dirname, 'constant.tpl.ejs');

module.exports = function (grunt) {
  var _ = grunt.util._;

  function toArray(value) {
    return _.isArray(value) ? value : [value];
  }

  function stringify(value, space) {
    return _.isUndefined(value) ? 'undefined' : JSON.stringify(value, null, space);
  }

  grunt.registerMultiTask('ngconstant', 'Dynamic angular constant generator task.', function () {
    var options = this.options({
      space: '\t',
      deps: [],
      wrap: false
    });
    var template = grunt.file.read(TEMPLATE_PATH);
    var compiler = _.template(template);
    var rawOptions = grunt.config.getRaw(this.name);
    var rawData = toArray(grunt.config.getRaw(this.name + '.' + this.target));

    toArray(this.data).forEach(function (module, index) {
      var constants = _.map(module.constants, function (value, name) {
        return {
          name: name,
          value: stringify(value, options.space)
        };
      });

      // Create the module string
      var result = compiler({
        moduleName: module.name,
        deps: module.deps || options.deps,
        constants: constants
      });

      // Handle wrapping
      var wrap = rawData[index].wrap || rawOptions.wrap || '<%= __ngModule %>';
      if (wrap === true) {
        wrap = DEFAULT_WRAP;
      }
      result = grunt.template.process(wrap, {
        data: _.extend(grunt.config(), {
          '__ngModule': result
        })
      });

      grunt.file.write(module.dest, result);
      grunt.log.writeln('Module ' + module.name + ' created at ' + module.dest);
    });
    grunt.log.ok();
  });
};
<|endoftext|>


<commit_before>var xml = require('xml2json'),
	fs = require('fs'),
	requests = require('request')

exports.get = function (cb){

	fs.readFile('./body.xml', function (err, d){

		requests.post({

			url: 'http://130.206.80.44:1029/ngsi10/queryContext',
			headers: {

				'Content-Type':'application/xml',
				'Content-Length':d.length
			},
			body:d.toString('utf8'),
			method:'POST'

		}, function (err, res, body){

			var json = JSON.parse(xml.toJson(body))
			var shit = json['queryContextResponse']['contextResponseList']['contextElementResponse']['contextElement']['contextAttributeList']['contextAttribute']

			var arr = []
			for (s in shit){

				var ss = shit[s]
				arr.push({name:ss['name'], value:ss['contextValue']})
			}

			cb(null, arr)
		})
	})

}
this.get(console.log)
<commit_msg>Changed file<commit_after>var xml = require('xml2json'),
	fs = require('fs'),
	requests = require('request')

exports.get = function (cb){

	fs.readFile('./body.xml', function (err, d){
		requests('http://192.168.10.174:8083/ZWaveAPI/Run/devices[5].instances[0].commandClasses[49].Get()', function (err, res, body){

			setTimeout(function (){

				requests.post({

			url: 'http://130.206.80.44:1029/ngsi10/queryContext',
			headers: {

				'Content-Type':'application/xml',
				'Content-Length':d.length
			},
			body:d.toString('utf8'),
			method:'POST'

		}, function (err, res, body){

			var json = JSON.parse(xml.toJson(body))
			var shit = json['queryContextResponse']['contextResponseList']['contextElementResponse']['contextElement']['contextAttributeList']['contextAttribute']

			var arr = []
			for (s in shit){

				var ss = shit[s]
				arr.push({name:ss['name'], value:ss['contextValue']})
			}

			cb(null, arr)
		})
			}, 300)

		})

	})

}
this.get(console.log)
<|endoftext|>


<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Graph Visualization (one of 3 views: graph, clock, and list). This is way
// too heavyweight for mobile right now.

// Visualization of tracking data interconnections

(function (visualizations, global) {
"use strict";

// Bunch of utilities related to UI elements.
const graphNodeRadius = {
  "graph": 12
};

// The graph is an emitter with a default size.
var graph = new Emitter();
visualizations.graph = graph;
graph.name = "graph";
var width = 750,
  height = 750;
var force, vis;
var edges, nodes;

// There are three phases for a visualization life-cycle:
// init does initialization and receives the existing set of connections
// connection notifies of a new connection that matches existing filter
// remove lets the visualization know it is about to be switched out so it can clean up
graph.on('init', onInit);
// graph.on('connection', onConnection);
graph.on('remove', onRemove);
graph.on('reset', onReset);

/* for Highlighting and Colouring -------------------- */

var highlight = {
  visited: true,
  neverVisited: true,
  connections: true,
  cookies: true,
  watched: true,
  blocked: true
};

// Restart the simulation. This is only called when there's a new connection we
// haven't seen before.
function onUpdate() {
  // new nodes, reheat graph simulation
  if (force) {
    console.debug('restarting graph due to update');
    force.stop();
    force.nodes(filteredAggregate.nodes);
    force.links(filteredAggregate.edges);
    force.start();
    updateGraph();
    colourHighlightNodes(highlight);
  } else {
    console.log('the force is not with us');
  }
}

function onInit() {
  console.debug('graph::onInit()');
  console.debug('initializing graph from ' + filteredAggregate.nodes.length +
                ' connections');
  // Handles all of the panning and scaling.
  vis = d3.select(vizcanvas);
  // A D3 visualization has a two main components, data-shaping, and setting up the D3 callbacks
  // This binds our data to the D3 visualization and sets up the callbacks
  initGraph();
  aggregate.on('update', onUpdate);
  // Different visualizations may have different viewBoxes, so make sure we use the right one
  vizcanvas.setAttribute('viewBox', [0, 0, width, height].join(' '));
  console.debug('graph::onInit end');
  document.querySelector(".filter-display").classList.remove("hidden");
}

function onRemove() {
  var startTime = Date.now();
  if (force) {
    force.stop();
    force = null;
  }
  resetCanvas();
  document.querySelector(".filter-display").classList.add("hidden");
  console.debug('it took %s ms to remove graph view', Date.now() - startTime);
}

function onReset() {
  onRemove();
  aggregate.emit('load', global.allConnections);
}

// UTILITIES FOR CREATING POLYGONS

function point(angle, size) {
  return [Math.round(Math.cos(angle) * size), -Math.round(Math.sin(angle) * size)];
}

function polygon(points, size, debug) {
  var increment = Math.PI * 2 / points;
  var angles = [],
    i;
  for (i = 0; i < points; i++) {
    angles.push(i * increment + Math.PI / 2); // add 90 degrees so first point is up
  }
  return angles.map(function (angle) {
    return point(angle, size);
  });
}

function polygonAsString(points, size) {
  var poly = polygon(points, size);
  return poly.map(function (pair) {
    return pair.join(',');
  }).join(' ');
}

// ACCESSOR FUNCTIONS

// function scaleNode(node){ return 'translate(' + node.x + ',' + node.y + ') scale(' + (1 + .05 * node.weight) + ')'; }
function visited(node) {
  return node.nodeType === 'site' || node.nodeType === 'both';
}

function notVisited(node) {
  return node.nodeType === 'thirdparty';
}
// function timestamp(node){ return node.lastAccess.toISOString(); }
// function nodeHighlight(node){ return ( node.visitedCount > 0 ) ? highlight.highlightVisited : highlight.highlightNeverVisited; }
// function sourceX(edge){ return edge.source.x; }
// function sourceY(edge){ return edge.source.y; }
// function targetX(edge){ return edge.target.x; }
// function targetY(edge){ return edge.target.y; }
// function edgeCookie(edge){ return edge.cookieCount > 0; }
// function edgeHighlight(edge){ return highlight.connections; }
// function edgeColoured(edge){ return edge.cookieCount > 0 && highlight.cookies; }
function nodeName(node) {
  if (node) {
    return node.name;
  }
  return undefined;
}

function siteHasPref(site, pref) {
  return (userSettings.hasOwnProperty(site) &&
    userSettings[site].contains(pref));
}

function watchSite(node) {
  return siteHasPref(node.name, "watch");
}

function blockSite(node) {
  return siteHasPref(node.name, "block");
}

// SET UP D3 HANDLERS

var ticking = false;

function charge(d) {
  return -(500 + d.weight * 25);
}

function colourHighlightNodes(highlight) {
  var i;
  var watchedSites = document.querySelectorAll(".watched");
  var blockedSites = document.querySelectorAll(".blocked");
  if (highlight.watched) {
    for (i = 0; i < watchedSites.length; i++) {
      watchedSites[i].classList.add("watchedSites");
    }
  } else {
    for (i = 0; i < watchedSites.length; i++) {
      watchedSites[i].classList.remove("watchedSites");
    }
  }
  if (highlight.blocked) {
    for (i = 0; i < blockedSites.length; i++) {
      blockedSites[i].classList.add("blockedSites");
    }
  } else {
    for (i = 0; i < blockedSites.length; i++) {
      blockedSites[i].classList.remove("blockedSites");
    }
  }
}

function initGraph() {
  // Initialize D3 layout and bind data
  console.debug('initGraph()');
  force = d3.layout.force()
    .nodes(filteredAggregate.nodes)
    .links(filteredAggregate.edges)
    .charge(charge)
    .size([width, height])
    .start();
  updateGraph();
  // Terrible hack. Something about the d3 setup is wrong, and forcing onClick
  // causes d3 to redraw in a way that most of the graph is visible on screen.
	global.helpOnClick();
  global.helpOnClick();
  colourHighlightNodes(highlight);

  // update method
  var lastUpdate, lastTick;
  lastUpdate = lastTick = Date.now();
  var draws = [];
  var ticks = 0;
  const second = 1000;
  const minute = 60 * second;
  force.on('tick', function ontick(evt) {
    // find a way to report how often tick() is called, and how long it takes to run
    // without trying to console.log() every 5 milliseconds...
    if (ticking) {
      console.log('overlapping tick!');
      return;
    }
    ticking = true;
    var nextTick = Date.now();
    ticks++;
    lastTick = nextTick;
    if ((lastTick - lastUpdate) > second) {
      // console.log('%s ticks per second, each draw takes %s milliseconds', ticks, Math.floor(d3.mean(draws)));
      lastUpdate = lastTick;
      draws = [];
      ticks = 0;
    }
    edges.each(function (d, i) {
      // `this` is the DOM node
      this.setAttribute('x1', d.source.x);
      this.setAttribute('y1', d.source.y);
      this.setAttribute('x2', d.target.x);
      this.setAttribute('y2', d.target.y);
      if (d.cookieCount) {
        this.classList.add('cookieYes');
      } else {
        this.classList.remove('cookieYes');
      }
      if (highlight.connections) {
        this.classList.add('highlighted');
      } else {
        this.classList.remove('highlighted');
      }
      if (d.cookieCount && highlight.cookies) {
        this.classList.add('coloured');
      } else {
        this.classList.remove('coloured');
      }
    });
    nodes.each(function (d, i) {
      // `this` is the DOM node
      this.setAttribute('transform', 'translate(' + d.x + ',' + d.y + ') scale(' + (1 + 0.05 * d.weight) + ')');
      this.setAttribute('data-timestamp', d.lastAccess.toISOString());
      if (d.nodeType === 'site' || d.nodeType === 'both') {
        this.classList.add('visitedYes');
        this.classList.remove('visitedNo');
      } else {
        this.classList.add('visitedNo');
        this.classList.remove('visitedYes');
      }
      if ((d.nodeType === 'site' || d.nodeType === 'both') && highlight.visited) {
        this.classList.add('highlighted');
      } else if ((d.nodeType === 'thirdparty') && highlight.neverVisited) {
        this.classList.add('highlighted');
      } else {
        this.classList.remove('highlighted');
      }
    });
    var endDraw = Date.now();
    draws.push(endDraw - lastTick);
    if (force) {
      nodes.call(force.drag);
    }

    ticking = false;
  });
}

function updateGraph() {
  console.debug('updateGraph()');
  // Data binding for links
  edges = vis.selectAll('.edge')
    .data(filteredAggregate.edges, nodeName);

  edges.enter().insert('line', ':first-child')
    .classed('edge', true);

  edges.exit()
    .remove();

  nodes = vis.selectAll('.node')
    .data(filteredAggregate.nodes, nodeName);

  nodes.enter().append('g')
    .classed('visitedYes', visited)
    .classed('visitedNo', notVisited)
    .classed("watched", watchSite)
    .classed("blocked", blockSite)
    .call(addShape)
    .attr('data-name', nodeName)
    .on('mouseenter', tooltip.show)
    .on('mouseleave', tooltip.hide)
    .classed('node', true);

  nodes.exit()
    .remove();

}

function addFavicon(selection) {
  selection.append("svg:image")
    .attr("class", "favicon")
    .attr("width", "16") // move these to the favicon class in css
  .attr("height", "16")
    .attr("x", "-8") // offset to make 16x16 favicon appear centered
  .attr("y", "-8")
    .attr("xlink:href", function (node) {
      return 'http://' + node.name + '/favicon.ico';
    });
}

function addCircle(selection) {
  selection
    .append('circle')
    .attr('cx', 0)
    .attr('cy', 0)
    .attr('r', graphNodeRadius.graph)
    .classed('site', true);
}

function addShape(selection) {
  selection.filter('.visitedYes').call(addCircle).call(addFavicon);
  selection.filter('.visitedNo').call(addTriangle).call(addFavicon);
}

function addTriangle(selection) {
  selection
    .append('polygon')
    .attr('points', polygonAsString(3, 20))
    .attr('data-name', function (node) {
      return node.name;
    });
}

// FIXME: Move this out of visualization so multiple visualizations can use it.
function resetCanvas() {
  // You will still need to remove timer events
  var parent = vizcanvas.parentNode;
  var newcanvas = vizcanvas.cloneNode(false);
  var vizcanvasDefs = document.querySelector(".vizcanvas defs").cloneNode(true);
  newcanvas.appendChild(vizcanvasDefs);
  parent.replaceChild(newcanvas, vizcanvas);
  vizcanvas = newcanvas;
  aggregate.off('update', onUpdate);
}

var graphLegend = document.querySelector(".graph-footer");

function legendBtnClickHandler(legendElm) {
  legendElm.querySelector(".legend-controls").addEventListener("click", function (event) {
    if (event.target.mozMatchesSelector(".btn, .btn *")) {
      var btn = event.target;
      while (btn.mozMatchesSelector('.btn *')) {
        btn = btn.parentElement;
      }
      btn.classList.toggle("active");
    }
  });
}

legendBtnClickHandler(graphLegend);

graphLegend.querySelector(".legend-toggle-visited").addEventListener("click", function (event) {
  var visited = document.querySelectorAll(".visitedYes");
  toggleVizElements(visited, "highlighted");
  highlight.visited = !highlight.visited;
});

graphLegend.querySelector(".legend-toggle-never-visited").addEventListener("click", function (event) {
  var neverVisited = document.querySelectorAll(".visitedNo");
  toggleVizElements(neverVisited, "highlighted");
  highlight.neverVisited = !highlight.neverVisited;
});

graphLegend.querySelector(".legend-toggle-connections").addEventListener("click", function (event) {
  var cookiesConnections = document.querySelectorAll(".edge");
  toggleVizElements(cookiesConnections, "highlighted");
  highlight.connections = !highlight.connections;
});

graphLegend.querySelector(".legend-toggle-cookies").addEventListener("click", function (event) {
  var cookiesConnections = document.querySelectorAll(".cookieYes");
  toggleVizElements(cookiesConnections, "coloured");
  highlight.cookies = !highlight.cookies;
});

graphLegend.querySelector(".legend-toggle-watched").addEventListener("click", function (event) {
  highlight.watched = !highlight.watched;
  colourHighlightNodes(highlight);
});

graphLegend.querySelector(".legend-toggle-blocked").addEventListener("click", function (event) {
  highlight.blocked = !highlight.blocked;
  colourHighlightNodes(highlight);
});

graphLegend.querySelector(".legend-toggle").addEventListener("click", function (event) {
  toggleLegendSection(event.target, graphLegend);
});

})(visualizations, this);
<commit_msg>Demote a console message to "debug"<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Graph Visualization (one of 3 views: graph, clock, and list). This is way
// too heavyweight for mobile right now.

// Visualization of tracking data interconnections

(function (visualizations, global) {
"use strict";

// Bunch of utilities related to UI elements.
const graphNodeRadius = {
  "graph": 12
};

// The graph is an emitter with a default size.
var graph = new Emitter();
visualizations.graph = graph;
graph.name = "graph";
var width = 750,
  height = 750;
var force, vis;
var edges, nodes;

// There are three phases for a visualization life-cycle:
// init does initialization and receives the existing set of connections
// connection notifies of a new connection that matches existing filter
// remove lets the visualization know it is about to be switched out so it can clean up
graph.on('init', onInit);
// graph.on('connection', onConnection);
graph.on('remove', onRemove);
graph.on('reset', onReset);

/* for Highlighting and Colouring -------------------- */

var highlight = {
  visited: true,
  neverVisited: true,
  connections: true,
  cookies: true,
  watched: true,
  blocked: true
};

// Restart the simulation. This is only called when there's a new connection we
// haven't seen before.
function onUpdate() {
  // new nodes, reheat graph simulation
  if (force) {
    console.debug('restarting graph due to update');
    force.stop();
    force.nodes(filteredAggregate.nodes);
    force.links(filteredAggregate.edges);
    force.start();
    updateGraph();
    colourHighlightNodes(highlight);
  } else {
    console.debug('the force is not with us');
  }
}

function onInit() {
  console.debug('graph::onInit()');
  console.debug('initializing graph from ' + filteredAggregate.nodes.length +
                ' connections');
  // Handles all of the panning and scaling.
  vis = d3.select(vizcanvas);
  // A D3 visualization has a two main components, data-shaping, and setting up the D3 callbacks
  // This binds our data to the D3 visualization and sets up the callbacks
  initGraph();
  aggregate.on('update', onUpdate);
  // Different visualizations may have different viewBoxes, so make sure we use the right one
  vizcanvas.setAttribute('viewBox', [0, 0, width, height].join(' '));
  console.debug('graph::onInit end');
  document.querySelector(".filter-display").classList.remove("hidden");
}

function onRemove() {
  var startTime = Date.now();
  if (force) {
    force.stop();
    force = null;
  }
  resetCanvas();
  document.querySelector(".filter-display").classList.add("hidden");
  console.debug('it took %s ms to remove graph view', Date.now() - startTime);
}

function onReset() {
  onRemove();
  aggregate.emit('load', global.allConnections);
}

// UTILITIES FOR CREATING POLYGONS

function point(angle, size) {
  return [Math.round(Math.cos(angle) * size), -Math.round(Math.sin(angle) * size)];
}

function polygon(points, size, debug) {
  var increment = Math.PI * 2 / points;
  var angles = [],
    i;
  for (i = 0; i < points; i++) {
    angles.push(i * increment + Math.PI / 2); // add 90 degrees so first point is up
  }
  return angles.map(function (angle) {
    return point(angle, size);
  });
}

function polygonAsString(points, size) {
  var poly = polygon(points, size);
  return poly.map(function (pair) {
    return pair.join(',');
  }).join(' ');
}

// ACCESSOR FUNCTIONS

// function scaleNode(node){ return 'translate(' + node.x + ',' + node.y + ') scale(' + (1 + .05 * node.weight) + ')'; }
function visited(node) {
  return node.nodeType === 'site' || node.nodeType === 'both';
}

function notVisited(node) {
  return node.nodeType === 'thirdparty';
}
// function timestamp(node){ return node.lastAccess.toISOString(); }
// function nodeHighlight(node){ return ( node.visitedCount > 0 ) ? highlight.highlightVisited : highlight.highlightNeverVisited; }
// function sourceX(edge){ return edge.source.x; }
// function sourceY(edge){ return edge.source.y; }
// function targetX(edge){ return edge.target.x; }
// function targetY(edge){ return edge.target.y; }
// function edgeCookie(edge){ return edge.cookieCount > 0; }
// function edgeHighlight(edge){ return highlight.connections; }
// function edgeColoured(edge){ return edge.cookieCount > 0 && highlight.cookies; }
function nodeName(node) {
  if (node) {
    return node.name;
  }
  return undefined;
}

function siteHasPref(site, pref) {
  return (userSettings.hasOwnProperty(site) &&
    userSettings[site].contains(pref));
}

function watchSite(node) {
  return siteHasPref(node.name, "watch");
}

function blockSite(node) {
  return siteHasPref(node.name, "block");
}

// SET UP D3 HANDLERS

var ticking = false;

function charge(d) {
  return -(500 + d.weight * 25);
}

function colourHighlightNodes(highlight) {
  var i;
  var watchedSites = document.querySelectorAll(".watched");
  var blockedSites = document.querySelectorAll(".blocked");
  if (highlight.watched) {
    for (i = 0; i < watchedSites.length; i++) {
      watchedSites[i].classList.add("watchedSites");
    }
  } else {
    for (i = 0; i < watchedSites.length; i++) {
      watchedSites[i].classList.remove("watchedSites");
    }
  }
  if (highlight.blocked) {
    for (i = 0; i < blockedSites.length; i++) {
      blockedSites[i].classList.add("blockedSites");
    }
  } else {
    for (i = 0; i < blockedSites.length; i++) {
      blockedSites[i].classList.remove("blockedSites");
    }
  }
}

function initGraph() {
  // Initialize D3 layout and bind data
  console.debug('initGraph()');
  force = d3.layout.force()
    .nodes(filteredAggregate.nodes)
    .links(filteredAggregate.edges)
    .charge(charge)
    .size([width, height])
    .start();
  updateGraph();
  // Terrible hack. Something about the d3 setup is wrong, and forcing onClick
  // causes d3 to redraw in a way that most of the graph is visible on screen.
	global.helpOnClick();
  global.helpOnClick();
  colourHighlightNodes(highlight);

  // update method
  var lastUpdate, lastTick;
  lastUpdate = lastTick = Date.now();
  var draws = [];
  var ticks = 0;
  const second = 1000;
  const minute = 60 * second;
  force.on('tick', function ontick(evt) {
    // find a way to report how often tick() is called, and how long it takes to run
    // without trying to console.log() every 5 milliseconds...
    if (ticking) {
      console.log('overlapping tick!');
      return;
    }
    ticking = true;
    var nextTick = Date.now();
    ticks++;
    lastTick = nextTick;
    if ((lastTick - lastUpdate) > second) {
      // console.log('%s ticks per second, each draw takes %s milliseconds', ticks, Math.floor(d3.mean(draws)));
      lastUpdate = lastTick;
      draws = [];
      ticks = 0;
    }
    edges.each(function (d, i) {
      // `this` is the DOM node
      this.setAttribute('x1', d.source.x);
      this.setAttribute('y1', d.source.y);
      this.setAttribute('x2', d.target.x);
      this.setAttribute('y2', d.target.y);
      if (d.cookieCount) {
        this.classList.add('cookieYes');
      } else {
        this.classList.remove('cookieYes');
      }
      if (highlight.connections) {
        this.classList.add('highlighted');
      } else {
        this.classList.remove('highlighted');
      }
      if (d.cookieCount && highlight.cookies) {
        this.classList.add('coloured');
      } else {
        this.classList.remove('coloured');
      }
    });
    nodes.each(function (d, i) {
      // `this` is the DOM node
      this.setAttribute('transform', 'translate(' + d.x + ',' + d.y + ') scale(' + (1 + 0.05 * d.weight) + ')');
      this.setAttribute('data-timestamp', d.lastAccess.toISOString());
      if (d.nodeType === 'site' || d.nodeType === 'both') {
        this.classList.add('visitedYes');
        this.classList.remove('visitedNo');
      } else {
        this.classList.add('visitedNo');
        this.classList.remove('visitedYes');
      }
      if ((d.nodeType === 'site' || d.nodeType === 'both') && highlight.visited) {
        this.classList.add('highlighted');
      } else if ((d.nodeType === 'thirdparty') && highlight.neverVisited) {
        this.classList.add('highlighted');
      } else {
        this.classList.remove('highlighted');
      }
    });
    var endDraw = Date.now();
    draws.push(endDraw - lastTick);
    if (force) {
      nodes.call(force.drag);
    }

    ticking = false;
  });
}

function updateGraph() {
  console.debug('updateGraph()');
  // Data binding for links
  edges = vis.selectAll('.edge')
    .data(filteredAggregate.edges, nodeName);

  edges.enter().insert('line', ':first-child')
    .classed('edge', true);

  edges.exit()
    .remove();

  nodes = vis.selectAll('.node')
    .data(filteredAggregate.nodes, nodeName);

  nodes.enter().append('g')
    .classed('visitedYes', visited)
    .classed('visitedNo', notVisited)
    .classed("watched", watchSite)
    .classed("blocked", blockSite)
    .call(addShape)
    .attr('data-name', nodeName)
    .on('mouseenter', tooltip.show)
    .on('mouseleave', tooltip.hide)
    .classed('node', true);

  nodes.exit()
    .remove();

}

function addFavicon(selection) {
  selection.append("svg:image")
    .attr("class", "favicon")
    .attr("width", "16") // move these to the favicon class in css
  .attr("height", "16")
    .attr("x", "-8") // offset to make 16x16 favicon appear centered
  .attr("y", "-8")
    .attr("xlink:href", function (node) {
      return 'http://' + node.name + '/favicon.ico';
    });
}

function addCircle(selection) {
  selection
    .append('circle')
    .attr('cx', 0)
    .attr('cy', 0)
    .attr('r', graphNodeRadius.graph)
    .classed('site', true);
}

function addShape(selection) {
  selection.filter('.visitedYes').call(addCircle).call(addFavicon);
  selection.filter('.visitedNo').call(addTriangle).call(addFavicon);
}

function addTriangle(selection) {
  selection
    .append('polygon')
    .attr('points', polygonAsString(3, 20))
    .attr('data-name', function (node) {
      return node.name;
    });
}

// FIXME: Move this out of visualization so multiple visualizations can use it.
function resetCanvas() {
  // You will still need to remove timer events
  var parent = vizcanvas.parentNode;
  var newcanvas = vizcanvas.cloneNode(false);
  var vizcanvasDefs = document.querySelector(".vizcanvas defs").cloneNode(true);
  newcanvas.appendChild(vizcanvasDefs);
  parent.replaceChild(newcanvas, vizcanvas);
  vizcanvas = newcanvas;
  aggregate.off('update', onUpdate);
}

var graphLegend = document.querySelector(".graph-footer");

function legendBtnClickHandler(legendElm) {
  legendElm.querySelector(".legend-controls").addEventListener("click", function (event) {
    if (event.target.mozMatchesSelector(".btn, .btn *")) {
      var btn = event.target;
      while (btn.mozMatchesSelector('.btn *')) {
        btn = btn.parentElement;
      }
      btn.classList.toggle("active");
    }
  });
}

legendBtnClickHandler(graphLegend);

graphLegend.querySelector(".legend-toggle-visited").addEventListener("click", function (event) {
  var visited = document.querySelectorAll(".visitedYes");
  toggleVizElements(visited, "highlighted");
  highlight.visited = !highlight.visited;
});

graphLegend.querySelector(".legend-toggle-never-visited").addEventListener("click", function (event) {
  var neverVisited = document.querySelectorAll(".visitedNo");
  toggleVizElements(neverVisited, "highlighted");
  highlight.neverVisited = !highlight.neverVisited;
});

graphLegend.querySelector(".legend-toggle-connections").addEventListener("click", function (event) {
  var cookiesConnections = document.querySelectorAll(".edge");
  toggleVizElements(cookiesConnections, "highlighted");
  highlight.connections = !highlight.connections;
});

graphLegend.querySelector(".legend-toggle-cookies").addEventListener("click", function (event) {
  var cookiesConnections = document.querySelectorAll(".cookieYes");
  toggleVizElements(cookiesConnections, "coloured");
  highlight.cookies = !highlight.cookies;
});

graphLegend.querySelector(".legend-toggle-watched").addEventListener("click", function (event) {
  highlight.watched = !highlight.watched;
  colourHighlightNodes(highlight);
});

graphLegend.querySelector(".legend-toggle-blocked").addEventListener("click", function (event) {
  highlight.blocked = !highlight.blocked;
  colourHighlightNodes(highlight);
});

graphLegend.querySelector(".legend-toggle").addEventListener("click", function (event) {
  toggleLegendSection(event.target, graphLegend);
});

})(visualizations, this);
<|endoftext|>


<commit_before>/**
 * enb-browserify
 * =========
 *
 * Раскрывает require, используя browserify.
 *
 * **Опции**
 *
 * * *String* **source** — Исходный таргет. Обязательная опция.
 * * *String* **target** — Результирующий таргет. По умолчанию — `?.browser.js`.
 * * *Object* **opts** — Опции для browserify
 * * *Array* **plugins** — Плагины для browserify
 *
 * **Пример**
 *
 * ```javascript
 * nodeConfig.addTech([ require('enb-browserify/techs/browserify'), {
 * source: '?.node.js',
 * target: '?.browser.js',
 * opts: {
 *     detectGlobals: false,
 *     debug: true
 * },
 * plugins: [require('bundle-collapser/plugin')]
 * } ]);
 * ```
 */
var browserify = require('browserify');
var vow = require('vow');

module.exports = require('enb/lib/build-flow').create()
    .name('browserify')
    .target('target', '?.browser.js')
    .defineRequiredOption('target')
    .defineRequiredOption('source')
    .useSourceFilename('source')
    .defineOption('opts', {})
    .defineOption('plugins', [])
    .builder(function (source) {
        var b = browserify(this._opts);
        this._plugins.forEach(function(plugin) {
            b.plugin(plugin);
        });
        return new vow.Promise(function (resolve, reject) {
            b.add(source).bundle(function(err, data) {
                if (err) {
                    return reject(err);
                }
                resolve(data);
            });
        });
    })
    .createTech();
<commit_msg>support transforms<commit_after>/**
 * enb-browserify
 * =========
 *
 * Раскрывает require, используя browserify.
 *
 * **Опции**
 *
 * * *String* **source** — Исходный таргет. Обязательная опция.
 * * *String* **target** — Результирующий таргет. По умолчанию — `?.browser.js`.
 * * *Object* **opts** — Опции для browserify
 * * *Array* **plugins** — Плагины для browserify
 * * *Array* **transforms** — Трансформации для browserify
 *
 * **Пример**
 *
 * ```javascript
 * nodeConfig.addTech([ require('enb-browserify/techs/browserify'), {
 * source: '?.node.js',
 * target: '?.browser.js',
 * opts: {
 *     detectGlobals: false,
 *     debug: true
 * },
 * plugins: [require('bundle-collapser/plugin')]
 * transforms: [[globalShim, {global: true}]]
 * } ]);
 * ```
 */
var browserify = require('browserify');
var vow = require('vow');

module.exports = require('enb/lib/build-flow').create()
    .name('browserify')
    .target('target', '?.browser.js')
    .defineRequiredOption('target')
    .defineRequiredOption('source')
    .useSourceFilename('source')
    .defineOption('opts', {})
    .defineOption('plugins', [])
	.defineOption('transforms', [])
    .builder(function (source) {
        var b = browserify(this._opts);
        this._plugins.forEach(function(plugin) {
            b.plugin(plugin);
        });
		this._transforms.forEach(function(transform) {
			b.transform(transform);
		});
        return new vow.Promise(function (resolve, reject) {
            b.add(source).bundle(function(err, data) {
                if (err) {
                    return reject(err);
                }
                resolve(data);
            });
        });
    })
    .createTech();
<|endoftext|>


<commit_before>function setAvailableProducts() {

    if(!products.list()) {
        products.set(product.starterProducts);
    }

    var availableProductCard = $("#availableProductCard").html();
    availableProductCard = Handlebars.compile(availableProductCard);

    $('.availableProductCardWrapper').remove();
    $(".availableProducts").append(availableProductCard(products.list()));

};

function setProductList() {

    var productCard = $("#productCard").html();
    productCard = Handlebars.compile(productCard);

    $('.productCardWrapper').remove();
    $(".productList").append(productCard(products.selected()));

};<commit_msg>Fixed typo<commit_after>function setAvailableProducts() {

    if(!products.list()) {
        products.set(products.starterProducts);
    }

    var availableProductCard = $("#availableProductCard").html();
    availableProductCard = Handlebars.compile(availableProductCard);

    $('.availableProductCardWrapper').remove();
    $(".availableProducts").append(availableProductCard(products.list()));

};

function setProductList() {

    var productCard = $("#productCard").html();
    productCard = Handlebars.compile(productCard);

    $('.productCardWrapper').remove();
    $(".productList").append(productCard(products.selected()));

};<|endoftext|>


<commit_before>var fs = require('fs');

exports['generate'] = function(binary, next){

  //////// DOCUMENTS ////////

  var documents = {};

  documents.projectMap = "/\n\
  |- actions\n\
  |-- (your actions)\n\
  |\n\
  |- initializers\n\
  |-- (any additional initializers you want)\n\
  |\n\
  |- log\n\
  |-- (default location for logs)\n\
  |\n\
  |- node_modules\n\
  |-- (your modules, actionHero should be npm installed in here)\n\
  |\n\
  |- pids\n\
  |-- (pidfiles for your running servers)\n\
  |\n\
  |- public\n\
  |-- (your static assets to be served by /file)\n\
  |\n\
  |- servers\n\
  |-- (custom servers you may make)\n\
  |\n\
  |- tasks\n\
  |-- (your tasks)\n\
  |\n\
  readme.md\n\
  routes.js\n\
  config.js\n\
  Jakefile.js\n\
  package.json (be sure to include 'actionHero':'x')\n\
  ";

  documents.config_js = fs.readFileSync(binary.paths.actionHero_root + "/config.js");
  documents.package_json = fs.readFileSync(binary.paths.actionHero_root + "/package.json");
  documents.routes_js = fs.readFileSync(binary.paths.actionHero_root + "/routes.js");
  documents.action_status = fs.readFileSync(binary.paths.actionHero_root + "/actions/status.js");
  documents.task_runAction = fs.readFileSync(binary.paths.actionHero_root + "/tasks/runAction.js");
  documents.jakefile = fs.readFileSync(binary.paths.actionHero_root + "/Jakefile.js");
  documents.ah_jakefile = fs.readFileSync(binary.paths.actionHero_root + "/jakelib/actionHero.jake");
  documents.public_actionHeroWebSocket = fs.readFileSync(binary.paths.actionHero_root + "/public/javascript/actionHeroWebSocket.js");

  var AHversionNumber = JSON.parse(documents.package_json).version;

  documents.package_json = "{\n\
    \"author\": \"YOU <YOU@example.com>\",\n\
    \"name\": \"my_actionHero_project\",\n\
    \"description\": \"\",\n\
    \"version\": \"0.0.1\",\n\
    \"homepage\": \"\",\n\
    \"repository\": {\n\
      \"type\": \"\",\n\
      \"url\": \"\"\n\
    },\n\
    \"main\": \"app.js\",\n\
    \"keywords\": \"\",\n\
    \"engines\": {\n\
      \"node\": \">=0.8.0\"\n\
    },\n\
    \"dependencies\": {\n\
      \"actionHero\": \""+AHversionNumber+"\"\n\
    },\n\
    \"devDependencies\": {},\n\
    \"scripts\": {\n\
      \"start\": \"node ./node_modules/.bin/actionHero start\",\n\
      \"startCluster\": \"node ./node_modules/.bin/actionHero startCluster\",\n\
      \"actionHero\": \"node ./node_modules/.bin/actionHero\",\n\
      \"help\": \"node ./node_modules/.bin/actionHero help\"\n\
    }\n\
  }\n\
  ";

  documents._project_js = "exports._project = function(api, next){\n\
  // modify / append the api global variable\n\
  // I will be run as part of actionHero\'s boot process\n\
\n\
  next();\n\
}\
";

  documents.readme_md = "# My actionHero Project\nreadme";

  documents.index_html = "
Hello from actionHero!
\n\

If you are reading this, your actionHero project is working.
\n\

Good Job!
\n\
  ";

  documents.git_ignore = "log\npids\nnode_modules";

  //////// LOGIC ////////

  binary.log("Generating a new actionHero project...");

  // make directories
  binary.utils.create_dir_safely(binary.paths.project_root + "/actions");
  binary.utils.create_dir_safely(binary.paths.project_root + "/pids");
  binary.utils.create_dir_safely(binary.paths.project_root + "/certs");
  binary.utils.create_dir_safely(binary.paths.project_root + "/initializers");
  binary.utils.create_dir_safely(binary.paths.project_root + "/log");
  binary.utils.create_dir_safely(binary.paths.project_root + "/servers");
  binary.utils.create_dir_safely(binary.paths.project_root + "/public");
  binary.utils.create_dir_safely(binary.paths.project_root + "/public/javascript");
  binary.utils.create_dir_safely(binary.paths.project_root + "/tasks");
  binary.utils.create_dir_safely(binary.paths.project_root + "/jakelib");

  // make files
  binary.utils.create_file_safely(binary.paths.project_root + '/.gitignore', documents.git_ignore);
  binary.utils.create_file_safely(binary.paths.project_root + '/config.js', documents.config_js);
  binary.utils.create_file_safely(binary.paths.project_root + '/routes.js', documents.routes_js);
  binary.utils.create_file_safely(binary.paths.project_root + '/package.json', documents.package_json);
  binary.utils.create_file_safely(binary.paths.project_root + '/actions/status.js', documents.action_status);
  binary.utils.create_file_safely(binary.paths.project_root + '/tasks/runAction.js', documents.task_runAction);
  binary.utils.create_file_safely(binary.paths.project_root + '/initializers/_project.js', documents._project_js);
  binary.utils.create_file_safely(binary.paths.project_root + '/public/index.html', documents.index_html);
  binary.utils.create_file_safely(binary.paths.project_root + '/public/javascript/actionHeroWebSocket.js', documents.public_actionHeroWebSocket);
  binary.utils.create_file_safely(binary.paths.project_root + '/readme.md', documents.readme_md);
  binary.utils.create_file_safely(binary.paths.project_root + '/Jakefile.js', documents.jakefile);
  binary.utils.create_file_safely(binary.paths.project_root + '/jakelib/actionHero.jake', documents.ah_jakefile);

  binary.log("");
  binary.log("Generation Complete.  Your project directory should look like this:\n" + documents.projectMap);
  binary.log("");
  binary.log("run `./node_modules/.bin/actionHero start` to start your server");

  next();

}<commit_msg>provide the minimized js when generating the project<commit_after>var fs = require('fs');

exports['generate'] = function(binary, next){

  //////// DOCUMENTS ////////

  var documents = {};

  documents.projectMap = "/\n\
  |- actions\n\
  |-- (your actions)\n\
  |\n\
  |- initializers\n\
  |-- (any additional initializers you want)\n\
  |\n\
  |- log\n\
  |-- (default location for logs)\n\
  |\n\
  |- node_modules\n\
  |-- (your modules, actionHero should be npm installed in here)\n\
  |\n\
  |- pids\n\
  |-- (pidfiles for your running servers)\n\
  |\n\
  |- public\n\
  |-- (your static assets to be served by /file)\n\
  |\n\
  |- servers\n\
  |-- (custom servers you may make)\n\
  |\n\
  |- tasks\n\
  |-- (your tasks)\n\
  |\n\
  readme.md\n\
  routes.js\n\
  config.js\n\
  Jakefile.js\n\
  package.json (be sure to include 'actionHero':'x')\n\
  ";

  documents.config_js = fs.readFileSync(binary.paths.actionHero_root + "/config.js");
  documents.package_json = fs.readFileSync(binary.paths.actionHero_root + "/package.json");
  documents.routes_js = fs.readFileSync(binary.paths.actionHero_root + "/routes.js");
  documents.action_status = fs.readFileSync(binary.paths.actionHero_root + "/actions/status.js");
  documents.task_runAction = fs.readFileSync(binary.paths.actionHero_root + "/tasks/runAction.js");
  documents.jakefile = fs.readFileSync(binary.paths.actionHero_root + "/Jakefile.js");
  documents.ah_jakefile = fs.readFileSync(binary.paths.actionHero_root + "/jakelib/actionHero.jake");
  documents.public_actionHeroWebSocket = fs.readFileSync(binary.paths.actionHero_root + "/public/javascript/actionHeroWebSocket.js");
  documents.public_actionHeroWebSocketMin = fs.readFileSync(binary.paths.actionHero_root + "/public/javascript/actionHeroWebSocket.min.js");

  var AHversionNumber = JSON.parse(documents.package_json).version;

  documents.package_json = "{\n\
    \"author\": \"YOU <YOU@example.com>\",\n\
    \"name\": \"my_actionHero_project\",\n\
    \"description\": \"\",\n\
    \"version\": \"0.0.1\",\n\
    \"homepage\": \"\",\n\
    \"repository\": {\n\
      \"type\": \"\",\n\
      \"url\": \"\"\n\
    },\n\
    \"main\": \"app.js\",\n\
    \"keywords\": \"\",\n\
    \"engines\": {\n\
      \"node\": \">=0.8.0\"\n\
    },\n\
    \"dependencies\": {\n\
      \"actionHero\": \""+AHversionNumber+"\"\n\
    },\n\
    \"devDependencies\": {},\n\
    \"scripts\": {\n\
      \"start\": \"node ./node_modules/.bin/actionHero start\",\n\
      \"startCluster\": \"node ./node_modules/.bin/actionHero startCluster\",\n\
      \"actionHero\": \"node ./node_modules/.bin/actionHero\",\n\
      \"help\": \"node ./node_modules/.bin/actionHero help\"\n\
    }\n\
  }\n\
  ";

  documents._project_js = "exports._project = function(api, next){\n\
  // modify / append the api global variable\n\
  // I will be run as part of actionHero\'s boot process\n\
\n\
  next();\n\
}\
";

  documents.readme_md = "# My actionHero Project\nreadme";

  documents.index_html = "
Hello from actionHero!
\n\

If you are reading this, your actionHero project is working.
\n\

Good Job!
\n\
  ";

  documents.git_ignore = "log\npids\nnode_modules";

  //////// LOGIC ////////

  binary.log("Generating a new actionHero project...");

  // make directories
  binary.utils.create_dir_safely(binary.paths.project_root + "/actions");
  binary.utils.create_dir_safely(binary.paths.project_root + "/pids");
  binary.utils.create_dir_safely(binary.paths.project_root + "/certs");
  binary.utils.create_dir_safely(binary.paths.project_root + "/initializers");
  binary.utils.create_dir_safely(binary.paths.project_root + "/log");
  binary.utils.create_dir_safely(binary.paths.project_root + "/servers");
  binary.utils.create_dir_safely(binary.paths.project_root + "/public");
  binary.utils.create_dir_safely(binary.paths.project_root + "/public/javascript");
  binary.utils.create_dir_safely(binary.paths.project_root + "/tasks");
  binary.utils.create_dir_safely(binary.paths.project_root + "/jakelib");

  // make files
  binary.utils.create_file_safely(binary.paths.project_root + '/.gitignore', documents.git_ignore);
  binary.utils.create_file_safely(binary.paths.project_root + '/config.js', documents.config_js);
  binary.utils.create_file_safely(binary.paths.project_root + '/routes.js', documents.routes_js);
  binary.utils.create_file_safely(binary.paths.project_root + '/package.json', documents.package_json);
  binary.utils.create_file_safely(binary.paths.project_root + '/actions/status.js', documents.action_status);
  binary.utils.create_file_safely(binary.paths.project_root + '/tasks/runAction.js', documents.task_runAction);
  binary.utils.create_file_safely(binary.paths.project_root + '/initializers/_project.js', documents._project_js);
  binary.utils.create_file_safely(binary.paths.project_root + '/public/index.html', documents.index_html);
  binary.utils.create_file_safely(binary.paths.project_root + '/public/javascript/actionHeroWebSocket.js', documents.public_actionHeroWebSocket);
  binary.utils.create_file_safely(binary.paths.project_root + '/public/javascript/actionHeroWebSocket.min.js', documents.public_actionHeroWebSocketMin);
  binary.utils.create_file_safely(binary.paths.project_root + '/readme.md', documents.readme_md);
  binary.utils.create_file_safely(binary.paths.project_root + '/Jakefile.js', documents.jakefile);
  binary.utils.create_file_safely(binary.paths.project_root + '/jakelib/actionHero.jake', documents.ah_jakefile);

  binary.log("");
  binary.log("Generation Complete.  Your project directory should look like this:\n" + documents.projectMap);
  binary.log("");
  binary.log("run `./node_modules/.bin/actionHero start` to start your server");

  next();

}<|endoftext|>


<commit_before>#!/usr/bin/env node

var exec = require('child_process').exec;
var fs = require('fs');
var path = require('path');

var budo = require('budo');

function execCmd (cmd) {
  var p = exec(cmd);
  p.stderr.pipe(process.stderr);
  p.stdout.pipe(process.stdout);
  return p;
}

var app = budo('./index.js:build/vr-components.js', {
  debug: process.env.NODE_ENVIRONMENT !== 'production',
  verbose: true,
  stream: process.stdout,  // log to stdout
  host: '0.0.0.0',
  port: 9000,
  browserifyArgs: (
    '-s vr-components -t [browserify-shader --parameterize=true]'.split(' ')
  )
});

app
.watch('**/*.{css,js,html}')
// .live()
.on('watch', function (eventType, fn) {
  if (eventType !== 'change' && eventType !== 'add') { return; }

  if (path.extname(fn) === '.css') {
    // We want to trigger a reload of the entire document, since
    // browserify-css injects CSS into the page.
    app.reload();
  } else if (path.extname(fn) === '.js') {
    app.reload(fn);
  } else if (fn.indexOf('core/templates') === 0) {
    var templatesWriteStream = fs.createWriteStream('./build/vr-components.html');
    var p = exec('vulcanize --inline core/templates/index.html').stdout;
    p.pipe(templatesWriteStream);
    p.on('end', function () {
      app.reload('build/vr-components.html');
    });
    templatesWriteStream.on('error', function (err) {
      console.error('Could not vulcanize HTML templates: %s', err);
    });
  }
}).on('update', function (ev) {
  execCmd('semistandard -v $(git ls-files "*.js") | snazzy');
});
<commit_msg>remove browserify-shader transform call (fixes #149)<commit_after>#!/usr/bin/env node

var exec = require('child_process').exec;
var fs = require('fs');
var path = require('path');

var budo = require('budo');

function execCmd (cmd) {
  var p = exec(cmd);
  p.stderr.pipe(process.stderr);
  p.stdout.pipe(process.stdout);
  return p;
}

var app = budo('./index.js:build/vr-components.js', {
  debug: process.env.NODE_ENVIRONMENT !== 'production',
  verbose: true,
  stream: process.stdout,  // log to stdout
  host: '0.0.0.0',
  port: 9000,
  browserifyArgs: '-s vr-components'.split(' ')
});

app
.watch('**/*.{css,js,html}')
// .live()
.on('watch', function (eventType, fn) {
  if (eventType !== 'change' && eventType !== 'add') { return; }

  if (path.extname(fn) === '.css') {
    // We want to trigger a reload of the entire document, since
    // browserify-css injects CSS into the page.
    app.reload();
  } else if (path.extname(fn) === '.js') {
    app.reload(fn);
  } else if (fn.indexOf('core/templates') === 0) {
    var templatesWriteStream = fs.createWriteStream('./build/vr-components.html');
    var p = exec('vulcanize --inline core/templates/index.html').stdout;
    p.pipe(templatesWriteStream);
    p.on('end', function () {
      app.reload('build/vr-components.html');
    });
    templatesWriteStream.on('error', function (err) {
      console.error('Could not vulcanize HTML templates: %s', err);
    });
  }
}).on('update', function (ev) {
  execCmd('semistandard -v $(git ls-files "*.js") | snazzy');
});
<|endoftext|>


<commit_before>"use stric";

/**
 * Cart class/constructor
 * @param {[number]} price     [item price]
 * @param {[string]} promoCode [promo code]
 */
function Cart(price, promoCode) {
    this.hardCodedPromoCode = 'HAHA';
    this.price = price;
    this.totalPrice = price;
    this.promoCode = promoCode;

    this.checkout();
}

Cart.prototype.applyPromoValue = function (promoValue) {
    this.totalPrice = this.price - promoValue;
    this.render();
};

/**
 * Get promo code value to be applied
 * @method getPromoValue
 */
Cart.prototype.getPromoValue = function () {
    var promoValue;
    var url = 'https://private-3d52c-promocode.apiary-mock.com/promo-code';

    $.ajax(url, {
        async: false,
        success: function(data) {
            promoValue = data.value
        },
        error: function(err) {
            console.log('fetching failed', err);
            promoValue = 0;
        }
    });

    return promoValue;
};

Cart.prototype.validatePromoCode = function () {
    var that = this;
    var promoValue = 0;
    var promoCode = this.promoCode;

    if (promoCode === this.hardCodedPromoCode) {
        promoValue = this.getPromoValue();
        this.applyPromoValue(promoValue);
    } else {
        this.applyPromoValue(promoValue);
    }

};

Cart.prototype.render = function () {
    var elTotalPrice = document.querySelector('#totalPrice-view');
    elTotalPrice.innerHTML = this.totalPrice.toFixed(2);
};

Cart.prototype.checkout = function () {
    this.validatePromoCode();
};<commit_msg>remove var not in use<commit_after>"use stric";

/**
 * Cart class/constructor
 * @param {[number]} price     [item price]
 * @param {[string]} promoCode [promo code]
 */
function Cart(price, promoCode) {
    this.hardCodedPromoCode = 'HAHA';
    this.price = price;
    this.totalPrice = price;
    this.promoCode = promoCode;

    this.checkout();
}

Cart.prototype.applyPromoValue = function (promoValue) {
    this.totalPrice = this.price - promoValue;
    this.render();
};

/**
 * Get promo code value to be applied
 * @method getPromoValue
 */
Cart.prototype.getPromoValue = function () {
    var promoValue;
    var url = 'https://private-3d52c-promocode.apiary-mock.com/promo-code';

    $.ajax(url, {
        async: false,
        success: function(data) {
            promoValue = data.value
        },
        error: function(err) {
            console.log('fetching failed', err);
            promoValue = 0;
        }
    });

    return promoValue;
};

Cart.prototype.validatePromoCode = function () {
    var promoValue = 0;
    var promoCode = this.promoCode;

    if (promoCode === this.hardCodedPromoCode) {
        promoValue = this.getPromoValue();
        this.applyPromoValue(promoValue);
    } else {
        this.applyPromoValue(promoValue);
    }

};

Cart.prototype.render = function () {
    var elTotalPrice = document.querySelector('#totalPrice-view');
    elTotalPrice.innerHTML = this.totalPrice.toFixed(2);
};

Cart.prototype.checkout = function () {
    this.validatePromoCode();
};<|endoftext|>


<commit_before>var pg      = require('./pg');
var moment  = require('moment-timezone');
var shell   = require('exec-sh');
var co      = require('co');
var cosleep = require('co-sleep');

function tlog() {
    console.log();
    console.log();
    console.log.apply(this, [moment().format()].concat([].slice.call(arguments)));
}

function shellex(cmd) {
    tlog('\t-> ' + cmd);
    return new Promise(function (resolve, reject) {
        shell(cmd, true, function (err, stdout, stderr) {
            var result = {
                err   : err,
                stdout: stdout,
                stderr: stderr
            };

            if (err) {
                return reject(result);
            }

            resolve(result);
        });
    });
}

init()
    .then(function () {
        tlog('exiting');
    })
    .catch(function (err) {
        tlog('error!', err.stack);
    });

function init() {
    return co(function *() {
        var from, to, result, uid, cmd;

        var now = moment();
        var uid = now.format('YYYY/MM/DD/HH_mm_ss');
        tlog("> Task ID: ", uid);

        // If a from date is passed, use that, othwerwise
        // fetch the last record date from redshift
        if (process.env.FROM_DATE) {
            from = moment(process.env.FROM_DATE).tz('UTC');
        } else {
            result = yield pg.pquery('select max(time) as tmax from records', []);
            from = moment(result.rows[0].tmax).tz('UTC');
        }

        // If a to date is passed, use that, otherwise
        // use the current date
        if (process.env.TO_DATE) {
            to = moment(process.env.TO_DATE).tz('UTC');
        } else {
            to = moment().tz('UTC');
        }

        // Launch EMR cluster to process nginx records to csv
        cmd = [
            'aws emr create-cluster \\',
            '--release-label emr-5.3.0 \\',
            '--instance-type m4.xlarge \\',
            '--instance-count 3 \\',
            '--auto-terminate \\',
            '--log-uri s3://see-tracker-data/emr-logs/ \\',
            '--use-default-roles \\',
            '--name see-nginx-to-csv \\',
            '--applications Name=Spark \\',
            '--ec2-attributes KeyName=seevpc,SubnetId=subnet-6ada3c03 \\',
            '--steps Type=Spark,\\',
            'Name="SEE-Spark",\\',
            'ActionOnFailure=CONTINUE,\\',
            'Args=[--class,com.ramnique.projects.Task,--deploy-mode,cluster,--master,yarn,s3://see-tracker-data/seefinal.jar,s3a://see-tracker-data/nginx,s3a://see-tracker-data/csv/' + uid + ',' + from.format() + ',' + to.format() + ']'
        ].join('\n');
        result = yield shellex(cmd);

        // Wait for cluster to boot up
        parsed = JSON.parse(result.stdout);
        cmd = 'aws emr wait cluster-running --cluster-id ' + parsed.ClusterId;
        yield shellex(cmd);

        // Wait for cluster to terminate
        cmd = 'until aws emr wait cluster-terminated --cluster-id ' + parsed.ClusterId + '; do echo "wait timed out.. trying again"; sleep 2; done';
        yield shellex(cmd);

        // Clear redshift
        yield pg.pquery('delete from records where time >= $1 AND time <= $2', [from.format('YYYY-MM-DD HH:mm:ss'), to.format('YYYY-MM-DD HH:mm:ss')]);

        // Reload data
        yield pg.pquery([
            "copy records from 's3://see-tracker-data/csv/" + uid + "/part'",
            "region 'ap-southeast-1'",
            "credentials 'aws_access_key_id=" + process.env.AWS_ACCESS_KEY + ";aws_secret_access_key=" + process.env.AWS_SECRET_KEY + "'",
            "csv",
            "dateformat 'auto'",
            "timeformat 'auto';"
        ].join("\n"), []);
    });
}
<commit_msg>updatr jar path<commit_after>var pg      = require('./pg');
var moment  = require('moment-timezone');
var shell   = require('exec-sh');
var co      = require('co');
var cosleep = require('co-sleep');

function tlog() {
    console.log();
    console.log();
    console.log.apply(this, [moment().format()].concat([].slice.call(arguments)));
}

function shellex(cmd) {
    tlog('\t-> ' + cmd);
    return new Promise(function (resolve, reject) {
        shell(cmd, true, function (err, stdout, stderr) {
            var result = {
                err   : err,
                stdout: stdout,
                stderr: stderr
            };

            if (err) {
                return reject(result);
            }

            resolve(result);
        });
    });
}

init()
    .then(function () {
        tlog('exiting');
    })
    .catch(function (err) {
        tlog('error!', err.stack);
    });

function init() {
    return co(function *() {
        var from, to, result, uid, cmd;

        var now = moment();
        var uid = now.format('YYYY/MM/DD/HH_mm_ss');
        tlog("> Task ID: ", uid);

        // If a from date is passed, use that, othwerwise
        // fetch the last record date from redshift
        if (process.env.FROM_DATE) {
            from = moment(process.env.FROM_DATE).tz('UTC');
        } else {
            result = yield pg.pquery('select max(time) as tmax from records', []);
            from = moment(result.rows[0].tmax).tz('UTC');
        }

        // If a to date is passed, use that, otherwise
        // use the current date
        if (process.env.TO_DATE) {
            to = moment(process.env.TO_DATE).tz('UTC');
        } else {
            to = moment().tz('UTC');
        }

        // Launch EMR cluster to process nginx records to csv
        cmd = [
            'aws emr create-cluster \\',
            '--release-label emr-5.3.0 \\',
            '--instance-type m4.xlarge \\',
            '--instance-count 3 \\',
            '--auto-terminate \\',
            '--log-uri s3://see-tracker-data/emr-logs/ \\',
            '--use-default-roles \\',
            '--name see-nginx-to-csv \\',
            '--applications Name=Spark \\',
            '--ec2-attributes KeyName=seevpc,SubnetId=subnet-6ada3c03 \\',
            '--steps Type=Spark,\\',
            'Name="SEE-Spark",\\',
            'ActionOnFailure=CONTINUE,\\',
            'Args=[--class,com.ramnique.projects.Task,--deploy-mode,cluster,--master,yarn,s3://see-tracker-data/see-final-2.jar,s3a://see-tracker-data/nginx,s3a://see-tracker-data/csv/' + uid + ',' + from.format() + ',' + to.format() + ']'
        ].join('\n');
        result = yield shellex(cmd);

        // Wait for cluster to boot up
        parsed = JSON.parse(result.stdout);
        cmd = 'aws emr wait cluster-running --cluster-id ' + parsed.ClusterId;
        yield shellex(cmd);

        // Wait for cluster to terminate
        cmd = 'until aws emr wait cluster-terminated --cluster-id ' + parsed.ClusterId + '; do echo "wait timed out.. trying again"; sleep 2; done';
        yield shellex(cmd);

        // Clear redshift
        yield pg.pquery('delete from records where time >= $1 AND time <= $2', [from.format('YYYY-MM-DD HH:mm:ss'), to.format('YYYY-MM-DD HH:mm:ss')]);

        // Reload data
        yield pg.pquery([
            "copy records from 's3://see-tracker-data/csv/" + uid + "/part'",
            "region 'ap-southeast-1'",
            "credentials 'aws_access_key_id=" + process.env.AWS_ACCESS_KEY + ";aws_secret_access_key=" + process.env.AWS_SECRET_KEY + "'",
            "csv",
            "dateformat 'auto'",
            "timeformat 'auto';"
        ].join("\n"), []);
    });
}
<|endoftext|>


<commit_before>$(document).ready(function() {
//  var map = L.map('map', {
//	center: [38.9049,-77.0128],
//	zoom: 11,
//	minZoom: 12,
//	maxZoom: 13,
//	zoomControl: false
//  });
//  L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map);
//  var zoomControl = new L.Control.Zoom({ position: 'topright'} );
//  zoomControl.addTo(map);
  var map = L.mapbox.map('map', 'mapbox.dc-bright')
	.setView([38.9049,-77.0128], 11);
	map.addLayer(L.mapbox.tileLayer('newamerica.dc-action-neighborhoods'));

//	$.get('data/combined_nick.json', function(schools){
//
//		var allocs_kg_5 = _.pluck(schools['elementary'], 'alloc_per_student')
//		var scale = d3.scale.linear()
//		  .domain([_.min(allocs_kg_5), _.max(allocs_kg_5)])
//		  .range([2,20])
//
//		var allocs_6_8 = _.pluck(schools['middle'], 'alloc_per_student')
//		var scale2 = d3.scale.linear()
//		  .domain([_.min(allocs_6_8), _.max(allocs_6_8)])
//		  .range([2,20])
//
//		var allocs_9_12 = _.pluck(schools['high'], 'alloc_per_student')
//		var scale3 = d3.scale.linear()
//		  .domain([_.min(allocs_9_12), _.max(allocs_9_12)])
//		  .range([2,20])
//
//		schools.elementary.forEach(function(school){
//			if ( school.lat !== undefined && school.long !== undefined){
//				var marker = new L.CircleMarker([school.lat, school.long], {fillColor: '#FF0000',color: 'black'}).setRadius(scale(school.alloc_per_student)).addTo(map)
//			}
//		});
//
//		schools.middle.forEach(function(school){
//			if ( school.lat !== undefined && school.long !== undefined){
//				var marker = new L.CircleMarker([school.lat, school.long], {fillColor: '#FF0000',color: 'black'}).setRadius(scale2(school.alloc_per_student)).addTo(map)
//			}
//		});
//
//		schools.high.forEach(function(school){
//			if ( school.lat !== undefined && school.long !== undefined){
//				var marker = new L.CircleMarker([school.lat, school.long], {fillColor: '#FF0000',color: 'black'}).setRadius(scale3(school.alloc_per_student)).addTo(map)
//			}
//		});
//
//	})

	$.get('data/combined_nick.json', function(schools){

		var allocs_kg_5 = _.pluck(schools['elementary'], 'alloc_per_student')
		var scale = d3.scale.linear()
		  .domain([_.min(allocs_kg_5), _.max(allocs_kg_5)])
		  .range([2,20])

		var allocs_6_8 = _.pluck(schools['middle'], 'alloc_per_student')
		var scale2 = d3.scale.linear()
		  .domain([_.min(allocs_6_8), _.max(allocs_6_8)])
		  .range([2,20])

		var allocs_9_12 = _.pluck(schools['high'], 'alloc_per_student')
		var scale3 = d3.scale.linear()
		  .domain([_.min(allocs_9_12), _.max(allocs_9_12)])
		  .range([2,20])

		schools.elementary.forEach(function(school){
			if ( school.lat !== undefined && school.long !== undefined){
				var marker = new L.CircleMarker([school.lat, school.long], {fillColor: '#FF0000',color: 'black'}).setRadius(scale(school.alloc_per_student)).bindPopup(school.name).addTo(map)
			}
		});

		schools.middle.forEach(function(school){
			if ( school.lat !== undefined && school.long !== undefined){
				var marker = new L.CircleMarker([school.lat, school.long], {fillColor: '#00FF00',color: 'black'}).setRadius(scale2(school.alloc_per_student)).bindPopup(school.name).addTo(map)
			}
		});

		schools.high.forEach(function(school){
			if ( school.lat !== undefined && school.long !== undefined){
				var marker = new L.CircleMarker([school.lat, school.long], {fillColor: '#0000FF',color: 'black'}).setRadius(scale3(school.alloc_per_student)).bindPopup(school.name).addTo(map)
			}
		});

	})

//	$.get('data/combined_charter.json', function(charters){
//
//		var ch_allocs_kg_5 = _.pluck(charters['chelementary'], 'alloc_per_student')
//		var ch_scale = d3.scale.linear()
//		  .domain([_.min(ch_allocs_kg_5), _.max(ch_allocs_kg_5)])
//		  .range([2,20])
//
//		var ch_allocs_6_8 = _.pluck(charters['ch_middle'], 'alloc_per_student')
//		var ch_scale2 = d3.scale.linear()
//		  .domain([_.min(ch_allocs_6_8), _.max(ch_allocs_6_8)])
//		  .range([2,20])
//
//		var ch_allocs_9_12 = _.pluck(charters['ch_high'], 'alloc_per_student')
//		var ch_scale3 = d3.scale.linear()
//		  .domain([_.min(ch_allocs_9_12), _.max(ch_allocs_9_12)])
//		  .range([2,20])
//
//		charters.chelementary.forEach(function(charter){
//			if ( charter.lat !== undefined && charter.long !== undefined){
//				var marker = new L.CircleMarker([school.lat, school.long], {fillColor: '#000000',color: 'black'}).setRadius(ch_scale(charter.alloc_per_student)).addTo(map)
//			}
//		});
//
//		charters.ch_middle.forEach(function(charter){
//			if ( charter.lat !== undefined && charter.long !== undefined){
//				var marker = new L.CircleMarker([school.lat, school.long], {fillColor: '#000000',color: 'black'}).setRadius(ch_scale2(charter.alloc_per_student)).addTo(map)
//			}
//		});
//
//		charters.ch_high.forEach(function(charter){
//			if ( charter.lat !== undefined && charter.long !== undefined){
//				var marker = new L.CircleMarker([school.lat, school.long], {fillColor: '#000000',color: 'black'}).setRadius(ch_scale3(charter.alloc_per_student)).addTo(map)
//			}
//		});
//
//	})

//  var layer = L.mapbox.tileLayer('newamerica.dc-action-neighborhoods')
//  .on('load', function() {
//    // get TileJSON data from the loaded layer
//    var TileJSON = layer.getTileJSON();
//});
});<commit_msg>Update dcps.js<commit_after>$(document).ready(function() {
//  var map = L.map('map', {
//	center: [38.9049,-77.0128],
//	zoom: 11,
//	minZoom: 12,
//	maxZoom: 13,
//	zoomControl: false
//  });
//  L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map);
//  var zoomControl = new L.Control.Zoom({ position: 'topright'} );
//  zoomControl.addTo(map);
  var map = L.mapbox.map('map', 'mapbox.dc-bright')
	.setView([37.4669, 122.1397], 11);
	map.addLayer(L.mapbox.tileLayer('newamerica.dc-action-neighborhoods'));

//	$.get('data/combined_nick.json', function(schools){
//
//		var allocs_kg_5 = _.pluck(schools['elementary'], 'alloc_per_student')
//		var scale = d3.scale.linear()
//		  .domain([_.min(allocs_kg_5), _.max(allocs_kg_5)])
//		  .range([2,20])
//
//		var allocs_6_8 = _.pluck(schools['middle'], 'alloc_per_student')
//		var scale2 = d3.scale.linear()
//		  .domain([_.min(allocs_6_8), _.max(allocs_6_8)])
//		  .range([2,20])
//
//		var allocs_9_12 = _.pluck(schools['high'], 'alloc_per_student')
//		var scale3 = d3.scale.linear()
//		  .domain([_.min(allocs_9_12), _.max(allocs_9_12)])
//		  .range([2,20])
//
//		schools.elementary.forEach(function(school){
//			if ( school.lat !== undefined && school.long !== undefined){
//				var marker = new L.CircleMarker([school.lat, school.long], {fillColor: '#FF0000',color: 'black'}).setRadius(scale(school.alloc_per_student)).addTo(map)
//			}
//		});
//
//		schools.middle.forEach(function(school){
//			if ( school.lat !== undefined && school.long !== undefined){
//				var marker = new L.CircleMarker([school.lat, school.long], {fillColor: '#FF0000',color: 'black'}).setRadius(scale2(school.alloc_per_student)).addTo(map)
//			}
//		});
//
//		schools.high.forEach(function(school){
//			if ( school.lat !== undefined && school.long !== undefined){
//				var marker = new L.CircleMarker([school.lat, school.long], {fillColor: '#FF0000',color: 'black'}).setRadius(scale3(school.alloc_per_student)).addTo(map)
//			}
//		});
//
//	})

	$.get('data/combined_nick.json', function(schools){

		var allocs_kg_5 = _.pluck(schools['elementary'], 'alloc_per_student')
		var scale = d3.scale.linear()
		  .domain([_.min(allocs_kg_5), _.max(allocs_kg_5)])
		  .range([2,20])

		var allocs_6_8 = _.pluck(schools['middle'], 'alloc_per_student')
		var scale2 = d3.scale.linear()
		  .domain([_.min(allocs_6_8), _.max(allocs_6_8)])
		  .range([2,20])

		var allocs_9_12 = _.pluck(schools['high'], 'alloc_per_student')
		var scale3 = d3.scale.linear()
		  .domain([_.min(allocs_9_12), _.max(allocs_9_12)])
		  .range([2,20])

		schools.elementary.forEach(function(school){
			if ( school.lat !== undefined && school.long !== undefined){
				var marker = new L.CircleMarker([school.lat, school.long], {fillColor: '#FF0000',color: 'black'}).setRadius(scale(school.alloc_per_student)).bindPopup(school.name).addTo(map)
			}
		});

		schools.middle.forEach(function(school){
			if ( school.lat !== undefined && school.long !== undefined){
				var marker = new L.CircleMarker([school.lat, school.long], {fillColor: '#00FF00',color: 'black'}).setRadius(scale2(school.alloc_per_student)).bindPopup(school.name).addTo(map)
			}
		});

		schools.high.forEach(function(school){
			if ( school.lat !== undefined && school.long !== undefined){
				var marker = new L.CircleMarker([school.lat, school.long], {fillColor: '#0000FF',color: 'black'}).setRadius(scale3(school.alloc_per_student)).bindPopup(school.name).addTo(map)
			}
		});

	})

//	$.get('data/combined_charter.json', function(charters){
//
//		var ch_allocs_kg_5 = _.pluck(charters['chelementary'], 'alloc_per_student')
//		var ch_scale = d3.scale.linear()
//		  .domain([_.min(ch_allocs_kg_5), _.max(ch_allocs_kg_5)])
//		  .range([2,20])
//
//		var ch_allocs_6_8 = _.pluck(charters['ch_middle'], 'alloc_per_student')
//		var ch_scale2 = d3.scale.linear()
//		  .domain([_.min(ch_allocs_6_8), _.max(ch_allocs_6_8)])
//		  .range([2,20])
//
//		var ch_allocs_9_12 = _.pluck(charters['ch_high'], 'alloc_per_student')
//		var ch_scale3 = d3.scale.linear()
//		  .domain([_.min(ch_allocs_9_12), _.max(ch_allocs_9_12)])
//		  .range([2,20])
//
//		charters.chelementary.forEach(function(charter){
//			if ( charter.lat !== undefined && charter.long !== undefined){
//				var marker = new L.CircleMarker([school.lat, school.long], {fillColor: '#000000',color: 'black'}).setRadius(ch_scale(charter.alloc_per_student)).addTo(map)
//			}
//		});
//
//		charters.ch_middle.forEach(function(charter){
//			if ( charter.lat !== undefined && charter.long !== undefined){
//				var marker = new L.CircleMarker([school.lat, school.long], {fillColor: '#000000',color: 'black'}).setRadius(ch_scale2(charter.alloc_per_student)).addTo(map)
//			}
//		});
//
//		charters.ch_high.forEach(function(charter){
//			if ( charter.lat !== undefined && charter.long !== undefined){
//				var marker = new L.CircleMarker([school.lat, school.long], {fillColor: '#000000',color: 'black'}).setRadius(ch_scale3(charter.alloc_per_student)).addTo(map)
//			}
//		});
//
//	})

//  var layer = L.mapbox.tileLayer('newamerica.dc-action-neighborhoods')
//  .on('load', function() {
//    // get TileJSON data from the loaded layer
//    var TileJSON = layer.getTileJSON();
//});
});
<|endoftext|>