<commit_before>module Backend
  module Cells
    class WeatherCellsController < Backend::Cells::BaseController
      def show
        openweathermap_api_key = Identifier.find_by(nature: :openweathermap_api_key)
        weather_client = OpenWeatherMapClient.from_identifier openweathermap_api_key

        coordinates = params[:centroid]

        # We try to get weather from cultivable zones
        coordinates ||= CultivableZone.geom_union_centroid(:shape)

        # We use the 5days forecast free from openwheathermap
        if coordinates.present?
          json = weather_client.fetch_forecast([coordinates.y, coordinates.x])

          @forecast = json.fmap { |j| build_forecast j }.or_nil
        end
      rescue Net::OpenTimeout => e
        @forecast = nil
        logger.warn "Net::OpenTimeout: Cannot open service OpenWeatherMap in time (#{e.message})"
      rescue Net::ReadTimeout => e
        @forecast = nil
        logger.warn "Net::ReadTimeout: Cannot read service OpenWeatherMap in time (#{e.message})"
      end

      private

        def build_forecast(json)
          forecast = json.deep_symbolize_keys

          if forecast[:cod] == '200'
            forecast[:list] = forecast[:list].collect do |day|
              day = day.deep_symbolize_keys

              {
                at: Time.zone.at(day[:dt]),<commit_msg>Fixed weather cell when passing a centroid<commit_after>module Backend
  module Cells
    class WeatherCellsController < Backend::Cells::BaseController
      def show
        openweathermap_api_key = Identifier.find_by(nature: :openweathermap_api_key)
        weather_client = OpenWeatherMapClient.from_identifier openweathermap_api_key

        coordinates = if params[:centroid]
          params[:centroid]
        else
          geom_centroid = CultivableZone.geom_union_centroid(:shape)
          geom_centroid.present? ? [geom_centroid.y, geom_centroid.x] : nil
        end

        # We use the 5days forecast free from openwheathermap
        if coordinates.present?
          json = weather_client.fetch_forecast(coordinates)

          @forecast = json.fmap { |j| build_forecast j }.or_nil
        end
      rescue Net::OpenTimeout => e
        @forecast = nil
        logger.warn "Net::OpenTimeout: Cannot open service OpenWeatherMap in time (#{e.message})"
      rescue Net::ReadTimeout => e
        @forecast = nil
        logger.warn "Net::ReadTimeout: Cannot read service OpenWeatherMap in time (#{e.message})"
      end

      private

        def build_forecast(json)
          forecast = json.deep_symbolize_keys

          if forecast[:cod] == '200'
            forecast[:list] = forecast[:list].collect do |day|
              day = day.deep_symbolize_keys

              {
                at: Time.zone.at(day[:dt]),
{'fraction_non_alphanumeric': 0.08558072635742539, 'fraction_numerical': 0.002876663070837828, 'mean_word_length': 2.562099871959027, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3312037', 'n_tokens_mistral': 820, 'n_tokens_neox': 774, 'n_words': 192}