<commit_before>nick = require 'app/util/nick'
kd   = require 'kd'

getIp = (url)->
  _ = global.document.createElement 'a'
  _.href = url

  return _.hostname


module.exports =

  queryKites: ->

    { generateQueryString } = require 'app/kite/kitecache'
    { computeController, kontrol } = kd.singletons

    return kontrol
      .queryKites
        query         :
          username    : nick()
          environment : 'managed'
      .timeout 5000
      .then (result)->

        if result?.kites?.length
          {kites} = result
          kites.forEach (kite)->
            kite.queryString = generateQueryString kite.kite
            kite.machine     = computeController
              .findMachineFromQueryString kite.queryString
            kite.ipAddress   = getIp kite.url
          return kites
        else
          return []


  createMachine: (kite, callback)->

    { computeController } = kd.singletons

    stack = computeController.stacks.first._id

    computeController.create {
      provider    : 'managed'
      queryString : kite.queryString
      ipAddress   : kite.ipAddress
      stack
    }, callback<commit_msg>ManagedHelpers: updateMachineData: to update queryString and ipAddress of a managed VM<commit_after>nick = require 'app/util/nick'
kd   = require 'kd'

getIp = (url)->
  _ = global.document.createElement 'a'
  _.href = url

  return _.hostname


module.exports =

  queryKites: ->

    { generateQueryString } = require 'app/kite/kitecache'
    { computeController, kontrol } = kd.singletons

    return kontrol
      .queryKites
        query         :
          username    : nick()
          environment : 'managed'
      .timeout 5000
      .then (result)->

        if result?.kites?.length
          {kites} = result
          kites.forEach (kite)->
            kite.queryString = generateQueryString kite.kite
            kite.machine     = computeController
              .findMachineFromQueryString kite.queryString
            kite.ipAddress   = getIp kite.url
          return kites
        else
          return []


  createMachine: (kite, callback)->

    { computeController } = kd.singletons

    stack = computeController.stacks.first._id

    computeController.create {
      provider    : 'managed'
      queryString : kite.queryString
      ipAddress   : kite.ipAddress
      stack
    }, callback


  updateMachineData: ({machine, kite}, callback)->

    options       =
      machineId   : machine._id
      provider    : 'managed'
      queryString : kite.queryString
      ipAddress   : kite.ipAddress

    remote = require('app/remote').getInstance()
    remote.api.ComputeProvider.update options, callback
{'fraction_non_alphanumeric': 0.09046728971962617, 'fraction_numerical': 0.0029906542056074765, 'mean_word_length': 2.0723306544202065, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '970466', 'n_tokens_mistral': 778, 'n_tokens_neox': 743, 'n_words': 167}