<commit_before>--[[
Copyright 2012 Rackspace

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]

local Object = require('core').Object
local logging = require('logging')
local https = require('https')
local url = require('url')
local fs = require('fs')

local CrashReportSubmitter = Object:extend()

function CrashReportSubmitter:initialize(filename, url)
  self._path = filename
  self._url = url
end

function once(callback)
  local called = false
  return function(err)
    if called == false then
      called = true
      callback(err)
    end
  end
end

function CrashReportSubmitter:run(callback)
  local headers = {}
  local parsed = url.parse(self._url)

  callback = once(callback)

  logging.infof('Uploading %s to %s', self._path, self._url)

  headers = {}
  headers['Content-Type'] = 'application/octet-stream'

  local options = {
    host = parsed.hostname,
    port = tonumber(parsed.port),
    path = parsed.pathname,
    headers = headers,
    method = 'POST'
  }

  client = https.request(options, function(res)
    local data = ''
    res:on('data', function(chunk)
      data = data .. chunk
    end)
    res:on('end', function()
      callback()
    end)
  end)

  client:on('error', function(err)
    logging.info('Failed to upload crash report: %s', err)
    callback(err)
  end)

  local stream = fs.createReadStream(self._path)
  stream:on('data', function(chunk)
    client:write(chunk)
  end)

  stream:on('error', function(err)
    logging.info('Failed to upload crash report: %s', err)
    client:done()<commit_msg>log crash upload status<commit_after>--[[
Copyright 2012 Rackspace

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]

local Object = require('core').Object
local logging = require('logging')
local https = require('https')
local url = require('url')
local fs = require('fs')

local CrashReportSubmitter = Object:extend()

function CrashReportSubmitter:initialize(filename, url)
  self._path = filename
  self._url = url
end

function once(callback)
  local called = false
  return function(err)
    if called == false then
      called = true
      callback(err)
    end
  end
end

function CrashReportSubmitter:run(callback)
  local headers = {}
  local parsed = url.parse(self._url)

  callback = once(callback)

  logging.infof('Uploading %s to %s', self._path, self._url)

  headers = {}
  headers['Content-Type'] = 'application/octet-stream'

  local options = {
    host = parsed.hostname,
    port = tonumber(parsed.port),
    path = parsed.pathname,
    headers = headers,
    method = 'POST'
  }

  client = https.request(options, function(res)
    local data = ''
    logging.info('Crash Upload Status Code: '.. res.status_code)
    res:on('data', function(chunk)
      data = data .. chunk
    end)
    res:on('end', function()
      logging.info('Crash Upload Response: '.. data)
      callback()
    end)
  end)

  client:on('error', function(err)
    logging.info('Failed to upload crash report: %s', err)
    callback(err)
  end)

  local stream = fs.createReadStream(self._path)
  stream:on('data', function(chunk)
    client:write(chunk)
  end)

  stream:on('error', function(err)
    logging.info('Failed to upload crash report: %s', err)
    client:done()
{'fraction_non_alphanumeric': 0.11129727142173289, 'fraction_numerical': 0.003829583532790809, 'mean_word_length': 3.7705479452054793, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '811040', 'n_tokens_mistral': 1249, 'n_tokens_neox': 1216, 'n_words': 407}