<commit_before>url = require 'url'
ElasticSearch = require 'elasticsearch'
async = require 'async'
_ = require 'lodash'
request = require 'request'
QUERY = require './query.json'

class Command
  constructor: ->
    sourceElasticsearchUrl       = process.env.SOURCE_ELASTICSEARCH_URL ? 'localhost:9200'
    @destinationElasticsearchUrl = process.env.DESTINATION_ELASTICSEARCH_URL ? 'localhost:9200'

    @sourceElasticsearch      = new ElasticSearch.Client host: sourceElasticsearchUrl

  run: =>
    @search QUERY, (error, result) =>
      throw error if error?

      deployments = @process @normalize result
      async.each deployments, @update, (error) =>
        throw error if error?
        console.log 'all done...?'
        process.exit 0

  update: (deployment, callback) =>
    uri = url.format
      protocol: 'http'
      host: @destinationElasticsearchUrl
      pathname: "/flow_start_history/event/#{deployment.deploymentUuid}"

    request.put uri, json: deployment, callback

  search: (body, callback=->) =>
    @sourceElasticsearch.search({
      index: 'device_status_flow'
      type:  'event'
      search_type: 'count'
      body:  body
    }, callback)

  normalize: (result) =>
    buckets = result.aggregations.flowStart.group_by_deploymentUuid.buckets
    _.map buckets, (bucket) =>
      {
        deploymentUuid: bucket.key
        beginTime: bucket.beginRecord.beginTime.value
        endTime:   bucket.endRecord.endTime.value
        workflow: 'flow-start'
      }

  process: (deployments) =>
    _.map deployments, (deployment) =>
      {beginTime, endTime} = deployment

      _.extend {}, deployment, {
        elapsedTime: endTime - beginTime<commit_msg>Be less resilient to errors<commit_after>url = require 'url'
ElasticSearch = require 'elasticsearch'
async = require 'async'
_ = require 'lodash'
request = require 'request'
QUERY = require './query.json'

class Command
  constructor: ->
    sourceElasticsearchUrl       = process.env.SOURCE_ELASTICSEARCH_URL ? 'localhost:9200'
    @destinationElasticsearchUrl = process.env.DESTINATION_ELASTICSEARCH_URL ? 'localhost:9200'

    @sourceElasticsearch      = new ElasticSearch.Client host: sourceElasticsearchUrl

  run: =>
    @search QUERY, (error, result) =>
      throw error if error?

      deployments = @process @normalize result
      async.each deployments, @update, (error) =>
        throw error if error?
        process.exit 0

  update: (deployment, callback) =>
    uri = url.format
      protocol: 'http'
      host: @destinationElasticsearchUrl
      pathname: "/flow_start_history/event/#{deployment.deploymentUuid}"

    request.put uri, json: deployment, (error, response, body) =>
      return callback error if error?
      return callback new Error(JSON.stringify body) if response.statusCode >= 300
      callback null

  search: (body, callback=->) =>
    @sourceElasticsearch.search({
      index: 'device_status_flow'
      type:  'event'
      search_type: 'count'
      body:  body
    }, callback)

  normalize: (result) =>
    buckets = result.aggregations.flowStart.group_by_deploymentUuid.buckets
    _.map buckets, (bucket) =>
      {
        deploymentUuid: bucket.key
        beginTime: bucket.beginRecord.beginTime.value
        endTime:   bucket.endRecord.endTime.value
        workflow: 'flow-start'
      }

  process: (deployments) =>
    _.map deployments, (deployment) =>
      {beginTime, endTime} = deployment

      elapsedTime = null
      elapsedTime = endTime - beginTime if endTime?

      _.extend {}, deployment, {
        elapsedTime: elapsedTime
{'fraction_non_alphanumeric': 0.10557103064066853, 'fraction_numerical': 0.00584958217270195, 'mean_word_length': 3.0993150684931505, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6046234', 'n_tokens_mistral': 1087, 'n_tokens_neox': 1055, 'n_words': 251}