<commit_before>V1::ServicesApi.route('service_stats') do |r|

  # GET /v1/services/:grid_name/:service_name/stats
  r.get do
    r.is do
      containers = @grid_service.containers.asc(:name)
      @stats = containers.map do |container|
        stat = container.container_stats.last
        if stat
          sum = ContainerStat.collection.aggregate([
          {
            '$match' => {
              'grid_service_id' => @grid_service.id
            }
          },
          {
            '$group' => {
              '_id' => {name: '$name'},
              'rx_bytes' => { '$sum' => '$network.rx_bytes'},
              'tx_bytes' => { '$sum' => '$network.tx_bytes'}
            }
          }])
          if stat.spec['memory']['limit'] == 1.8446744073709552e+19
            stat.spec['memory']['limit'] = container.host_node.mem_total
          end
          stat.network['rx_bytes'] = sum[0]['rx_bytes']
          stat.network['tx_bytes'] = sum[0]['tx_bytes']
        end
        {
            container: container,
            stats: stat
        }<commit_msg>check that aggregated sum exist<commit_after>V1::ServicesApi.route('service_stats') do |r|

  # GET /v1/services/:grid_name/:service_name/stats
  r.get do
    r.is do
      containers = @grid_service.containers.asc(:name)
      @stats = containers.map do |container|
        stat = container.container_stats.last
        if stat
          sum = ContainerStat.collection.aggregate([
          {
            '$match' => {
              'grid_service_id' => @grid_service.id
            }
          },
          {
            '$group' => {
              '_id' => {name: '$name'},
              'rx_bytes' => { '$sum' => '$network.rx_bytes'},
              'tx_bytes' => { '$sum' => '$network.tx_bytes'}
            }
          }])
          if stat.spec['memory']['limit'] == 1.8446744073709552e+19
            stat.spec['memory']['limit'] = container.host_node.mem_total
          end
          if sum[0]
            stat.network['rx_bytes'] = sum[0]['rx_bytes']
            stat.network['tx_bytes'] = sum[0]['tx_bytes']
          end
        end
        {
            container: container,
            stats: stat
        }
{'fraction_non_alphanumeric': 0.15173674588665448, 'fraction_numerical': 0.021480804387568556, 'mean_word_length': 1.676039119804401, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '4221274', 'n_tokens_mistral': 674, 'n_tokens_neox': 651, 'n_words': 105}