<commit_before>require 'cache_lock'

class Json::StatusController < ApplicationController
  layout false

  def index
    items = {}

    icecast_status = Rails.cache.read('icecast_status')
    if icecast_status
      icecast_sources = icecast_status['icestats']['source']
      listeners = Array(icecast_sources).map { |source| source['listeners'] }.inject(0, :+)
      items[:date] = Time.now.utc
      items[:listeners] = listeners
    end

    respond_to do |format|
      format.xml { render xml: items }
      format.json { render json: items }
      format.any { head :not_found }
    end<commit_msg>Add multiple icecast_status support to status controller<commit_after>require 'cache_lock'

class Json::StatusController < ApplicationController
  layout false

  def index
    items = {}
    total_listeners = 0

    rails_cache_keys = Rails.cache.instance_variable_get(:@data).try(:keys) || []
    icecast_status_keys = rails_cache_keys.select do |key|
      key =~ /(?:\Aicecast_status|:icecast_status)\z/
    end

    icecast_status_keys.each do |icecast_status_key|
      icecast_status = Rails.cache.read(icecast_status_key)
      next unless icecast_status
      icecast_sources = icecast_status['icestats']['source']
      icecast_phateio_sources = Array(icecast_sources).select do |source|
        source['listenurl'] =~ %r{/phateio(?:\.\w+)?\z}
      end
      listeners = icecast_phateio_sources.map { |source| source['listeners'] }.inject(0, :+)
      total_listeners += listeners
    end
    items[:date] = Time.now.utc
    items[:listeners] = total_listeners

    respond_to do |format|
      format.xml { render xml: items }
      format.json { render json: items }
      format.any { head :not_found }
    end
{'fraction_non_alphanumeric': 0.11380704794916234, 'fraction_numerical': 0.0017331022530329288, 'mean_word_length': 3.4870466321243523, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '4879675', 'n_tokens_mistral': 551, 'n_tokens_neox': 537, 'n_words': 111}