<commit_before>{Model} = require 'bongo'

module.exports = class JVM extends Model

  {permit} = require './group/permissionset'

  KodingError = require '../error'

  @share()

  @trait __dirname, '../traits/protected'

  @bound = require 'koding-bound'

  @set
    softDelete          : yes
    permissions         :
      'sudoer'          : []
      'create vms'      : ['member','moderator']
      'list all vms'    : ['member','moderator']
      'list default vm' : ['member','moderator']
    sharedMethods       :
      static            : ['fetchVmsByContext']#,'create']
      instance          : []
    schema              :
      ip                :
        type            : String
        default         : -> null
      ldapPassword      :
        type            : String
        default         : -> null
      name              : String
      users             : Array
      groups            : Array
      isEnabled         :
        type            : Boolean
        default         : yes
      shouldDelete      :
        type            : Boolean
        default         : no

  # TODO: this needs to be rethought in terms of bundles, as per the
  # discussion between Devrim, <NAME>. and Badahir  C.T.
  # @createVm = (target, groupSlug, limit, callback)->
  #   unless 0 < limit.getValue()
  #     callback new KodingError 'Quota exceeded'
  #   else
  #     target.fetchUser (err, user)->
  #       return callback err  if err
  #       { usage } = limit
  #       vm = new JVM {
  #         name: "#{user.username}#{unless usage then '' else "~#{usage}"}"
  #         users: [{ id: user.getId(),  sudo: yes }]
  #       }
  #       vm.save (err)->
  #         return callback err  if err
  #         limit.update {$inc: usage: 1}, (err)->
  #           return callback err  if err
  #           callback null, vm

  # @create = permit 'create vms',
  #   success: (client, callback) ->

  # @initializeVmLimits =(target, callback)->
  #   JLimit = require './limit'
  #   limit = new JLimit { quota: 5 }
  #   limit.save (err)->
  #     return callback err  if err
  #     target.addLimit limit, 'vm', (err)->
  #       callback err ? null, unless err then limit

  @fetchVmsByContext = permit 'list all vms',
    success: (client, options, callback) ->
      [callback, options] = [options, callback]  unless callback

      options ?= {}
      options.limit = Math.min options.limit ? 10, 10

      JGroup = require './group'

      {context, connection:{delegate}} = client

      slug = context.group ? 'koding'

      delegate.fetchUser (err, user) ->
        return callback err  if err

        JGroup.one {slug}, (err, group) ->
          return callback err  if err

          selector =
            groups  : { $elemMatch: id: group.getId() }<commit_msg>vm: added "usage" and also "calculateUsage"<commit_after>{Model} = require 'bongo'

module.exports = class JVM extends Model

  {permit} = require './group/permissionset'

  KodingError = require '../error'

  @share()

  @trait __dirname, '../traits/protected'

  @bound = require 'koding-bound'

  @set
    softDelete          : yes
    permissions         :
      'sudoer'          : []
      'create vms'      : ['member','moderator']
      'list all vms'    : ['member','moderator']
      'list default vm' : ['member','moderator']
    sharedMethods       :
      static            : ['fetchVmsByContext','calculateUsage']#,'create']
      instance          : []
    schema              :
      ip                :
        type            : String
        default         : -> null
      ldapPassword      :
        type            : String
        default         : -> null
      name              : String
      users             : Array
      groups            : Array
      usage             :
        cpu             :
          type          : Number
          default       : 1
        ram             :
          type          : Number
          default       : 0.25
        disk            :
          type          : Number
          default       : 0.5
      isEnabled         :
        type            : Boolean
        default         : yes
      shouldDelete      :
        type            : Boolean
        default         : no

  # TODO: this needs to be rethought in terms of bundles, as per the
  # discussion between Devrim, <NAME>. and Badahir  C.T.
  # @createVm = (target, groupSlug, limit, callback)->
  #   unless 0 < limit.getValue()
  #     callback new KodingError 'Quota exceeded'
  #   else
  #     target.fetchUser (err, user)->
  #       return callback err  if err
  #       { usage } = limit
  #       vm = new JVM {
  #         name: "#{user.username}#{unless usage then '' else "~#{usage}"}"
  #         users: [{ id: user.getId(),  sudo: yes }]
  #       }
  #       vm.save (err)->
  #         return callback err  if err
  #         limit.update {$inc: usage: 1}, (err)->
  #           return callback err  if err
  #           callback null, vm

  # @create = permit 'create vms',
  #   success: (client, callback) ->

  # @initializeVmLimits =(target, callback)->
  #   JLimit = require './limit'
  #   limit = new JLimit { quota: 5 }
  #   limit.save (err)->
  #     return callback err  if err
  #     target.addLimit limit, 'vm', (err)->
  #       callback err ? null, unless err then limit

  @getUsageTemplate = -> { cpu: 0, ram: 0, disk: 0 }

  @calculateUsage = (account, groupSlug, callback)->
    nickname =
      if 'string' is typeof account then account
      else account.profile.nickname

    @all { name: ///$#{groupSlug}~#{nickname}~/// }, (err, vms) =>
      return callback err  if err?
      callback null, vms
        .map((vm) -> vm.usage)
        .reduce (acc, usage) ->
          for own field, val of usage
            acc[field] += val
            return acc
        , @getUsageTemplate()


  @calculateUsage$ = permit 'list all vms',
    success: (client, groupSlug, callback)->
      {delegate} = client.connection
      @calculateUsage delegate, groupSlug, callback

  @fetchVmsByContext = permit 'list all vms',
    success: (client, options, callback) ->
      [callback, options] = [options, callback]  unless callback

      options ?= {}
      options.limit = Math.min options.limit ? 10, 10

      JGroup = require './group'

      {context, connection:{delegate}} = client

      slug = context.group ? 'koding'

      delegate.fetchUser (err, user) ->
        return callback err  if err

        JGroup.one {slug}, (err, group) ->
          return callback err  if err

          selector =
            groups  : { $elemMatch: id: group.getId() }
{'fraction_non_alphanumeric': 0.11818044431011032, 'fraction_numerical': 0.0034758954208855978, 'mean_word_length': 1.5983510011778563, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, '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': '358666', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1872, 'n_words': 554}