<commit_before>module ArtversityServer
  class PerformancesController < Base
    NEARBY_RANGE = 0.004

    get '/all', provides: :json do
      status 200
      body Performance.core_data.to_json
    end

    get '/:id', provides: :json do
      performance = Performance.find(id: params[:id])

      if performance.nil?
        status 404
      else
        status 200
        body performance.full_data.to_json
      end
    end

    get '/nearby/:latitude/:longitude', provides: :json do
      latitude  = params[:latitude].to_f
      longitude = params[:longitude].to_f
      performances = Performance.nearby(latitude, longitude, NEARBY_RANGE)

      status 200
      body performances.map(&:full_data).to_json
    end

    get '/recent/:latitude/:longitude', provides: :json do
      latitude  = params[:latitude].to_f
      longitude = params[:longitude].to_f
      performances = Performance.recent(latitude, longitude, NEARBY_RANGE)

      status 200
      body performances.map(&:full_data).to_json
    end

    post '/', provides: :json do
      # TODO: extract this logic to helpers
      type = Type.find name: params['type']
      categories = params['categories'].map { |category| Category.find name: category }
      location_latitude = params['location_latitude'].to_f
      location_longitude = params['location_longitude'].to_f
      is_band = params['isBand'] == 'true'

      performance = Performance.new type: type,
                                    location_latitude: location_latitude,
                                    location_longitude: location_longitude,
                                    is_band: is_band

      performance.tag
      performance.save

      categories.each do |category|
        performance.add_category category
      end

      status 200
      body({id: performance.id}.to_json)
    end

    put '/', provides: :json do
      performance = Performance.find(id: params[:id])

      if performance.nil?
        status 404
      else
        performance.tag

        status 200

        id_hash = {id: performance.id}
        body id_hash.to_json
      end
    end
  end
end<commit_msg>Sort nearby performances by last seen date<commit_after>module ArtversityServer
  class PerformancesController < Base
    NEARBY_RANGE = 0.004

    get '/all', provides: :json do
      status 200
      body Performance.core_data.to_json
    end

    get '/:id', provides: :json do
      performance = Performance.find(id: params[:id])

      if performance.nil?
        status 404
      else
        status 200
        body performance.full_data.to_json
      end
    end

    get '/nearby/:latitude/:longitude', provides: :json do
      latitude  = params[:latitude].to_f
      longitude = params[:longitude].to_f
      performances = Performance.nearby(latitude, longitude, NEARBY_RANGE).sort_by(&:last_seen).reverse

      status 200
      body performances.map(&:full_data).to_json
    end

    get '/recent/:latitude/:longitude', provides: :json do
      latitude  = params[:latitude].to_f
      longitude = params[:longitude].to_f
      performances = Performance.recent(latitude, longitude, NEARBY_RANGE)

      status 200
      body performances.map(&:full_data).to_json
    end

    post '/', provides: :json do
      # TODO: extract this logic to helpers
      type = Type.find name: params['type']
      categories = params['categories'].map { |category| Category.find name: category }
      location_latitude = params['location_latitude'].to_f
      location_longitude = params['location_longitude'].to_f
      is_band = params['isBand'] == 'true'

      performance = Performance.new type: type,
                                    location_latitude: location_latitude,
                                    location_longitude: location_longitude,
                                    is_band: is_band

      performance.tag
      performance.save

      categories.each do |category|
        performance.add_category category
      end

      status 200
      body({id: performance.id}.to_json)
    end

    put '/', provides: :json do
      performance = Performance.find(id: params[:id])

      if performance.nil?
        status 404
      else
        performance.tag
        performance.save

        status 200

        id_hash = {id: performance.id}
        body id_hash.to_json
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08870043548017419, 'fraction_numerical': 0.012835205134082053, 'mean_word_length': 2.2397921306607276, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2633969', 'n_tokens_mistral': 1284, 'n_tokens_neox': 1197, 'n_words': 298}