<commit_before>local net = require('net')
local JSON = require('json')
local fixtures = require('./')
local LineEmitter = require('line-emitter').LineEmitter
local tls = require('tls')
local timer = require('timer')
local string = require('string')
local math = require('math')

local lineEmitter = LineEmitter:new()
local ports = {50041, 50051, 50061}

local opts = {}
local function set_option(options, name, default)
  options[name] = process.env[string.upper(name)] or default
end

set_option(opts, "send_schedule_changed_initial", 2000)
set_option(opts, "send_schedule_changed_interval", 60000)
set_option(opts, "destroy_connection_jitter", 60000)
set_option(opts, "destroy_connection_base", 60000)

local keyPem = [[
-----BEGIN RSA PRIVATE KEY-----
<KEY>
]]

local certPem = [[
-----BEGIN CERTIFICATE-----
<KEY>
-----END CERTIFICATE-----
]]

local options = {
  cert = certPem,
  key = keyPem
}

local respond = function(log, client, payload)

  -- skip responses to requests
  if payload.method == nil then
    return
  end

  local response_method = payload.method .. '.response'
  local response = JSON.parse(fixtures[response_method])
  local response_out = nil


  response.target = payload.source
  response.source = payload.target
  response.id = payload.id

  log("Sending response:")
  p(response)

  response_out = JSON.stringify(response)
  response_out:gsub("\n", " ")

  client:write(response_out .. '\n')
end

local send_schedule_changed = function(log, client)
  local request = fixtures['check_schedule.changed.request']

  log("Sending request:")
  p(JSON.parse(request))
  client:write(request .. '\n')
end

local function start_fixture_server(options, port)
  local log = function(...)
    print(port .. ": " .. ...)
  end

  tls.createServer(options, function (client)
    client:pipe(lineEmitter)
    lineEmitter:on('data', function(line)
      local payload = JSON.parse(line)
      log("Got payload:")
      p(payload)
      respond(log, client, payload)
    end)

    timer.setTimeout(opts.send_schedule_changed_initial, function()
      send_schedule_changed(log, client)
    end)
    timer.setInterval(opts.send_schedule_changed_interval, function()
      send_schedule_changed(log, client)
    end)

    -- Disconnect the agent after some random number of seconds
    -- to exercise reconnect logic
    local disconnect_time = opts.destroy_connection_base + math.floor(math.random() * opts.destroy_connection_jitter)
    timer.setTimeout(disconnect_time, function()
      log("Destroying connection after " .. disconnect_time .. "ms connected")
      client:destroy()
    end)
  end):listen(port)<commit_msg>monitoring: fixtures: server: add listen_ip option<commit_after>local net = require('net')
local JSON = require('json')
local fixtures = require('./')
local LineEmitter = require('line-emitter').LineEmitter
local tls = require('tls')
local timer = require('timer')
local string = require('string')
local math = require('math')

local lineEmitter = LineEmitter:new()
local ports = {50041, 50051, 50061}

local opts = {}
local function set_option(options, name, default)
  options[name] = process.env[string.upper(name)] or default
end

set_option(opts, "send_schedule_changed_initial", 2000)
set_option(opts, "send_schedule_changed_interval", 60000)
set_option(opts, "destroy_connection_jitter", 60000)
set_option(opts, "destroy_connection_base", 60000)
set_option(opts, "listen_ip", '127.0.0.1')

local keyPem = [[
-----<KEY>-----
]]

local certPem = [[
-----BEGIN CERTIFICATE-----
<KEY>
-----END CERTIFICATE-----
]]

local options = {
  cert = certPem,
  key = keyPem
}

local respond = function(log, client, payload)

  -- skip responses to requests
  if payload.method == nil then
    return
  end

  local response_method = payload.method .. '.response'
  local response = JSON.parse(fixtures[response_method])
  local response_out = nil


  response.target = payload.source
  response.source = payload.target
  response.id = payload.id

  log("Sending response:")
  p(response)

  response_out = JSON.stringify(response)
  response_out:gsub("\n", " ")

  client:write(response_out .. '\n')
end

local send_schedule_changed = function(log, client)
  local request = fixtures['check_schedule.changed.request']

  log("Sending request:")
  p(JSON.parse(request))
  client:write(request .. '\n')
end

local function start_fixture_server(options, port)
  local log = function(...)
    print(port .. ": " .. ...)
  end

  tls.createServer(options, function (client)
    client:pipe(lineEmitter)
    lineEmitter:on('data', function(line)
      local payload = JSON.parse(line)
      log("Got payload:")
      p(payload)
      respond(log, client, payload)
    end)

    timer.setTimeout(opts.send_schedule_changed_initial, function()
      send_schedule_changed(log, client)
    end)
    timer.setInterval(opts.send_schedule_changed_interval, function()
      send_schedule_changed(log, client)
    end)

    -- Disconnect the agent after some random number of seconds
    -- to exercise reconnect logic
    local disconnect_time = opts.destroy_connection_base + math.floor(math.random() * opts.destroy_connection_jitter)
    timer.setTimeout(disconnect_time, function()
      log("Destroying connection after " .. disconnect_time .. "ms connected")
      client:destroy()
    end)
  end):listen(port, opts.listen_ip)
{'fraction_non_alphanumeric': 0.1321882001493652, 'fraction_numerical': 0.013816280806572068, 'mean_word_length': 4.180851063829787, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '2136373', 'n_tokens_mistral': 1723, 'n_tokens_neox': 1619, 'n_words': 390}