namespace :data_feeds do
  desc 'Set up data feeds'
  task :weather_underground_loader, [:start_date, :end_date] => :environment do |_t, args|
    start_date = args[:start_date].present? ? Date.parse(args[:start_date]) : Date.yesterday - 1
    end_date = args[:end_date].present? ? Date.parse(args[:end_date]) : Date.yesterday

    old_readings = DataFeedReading.where(feed_type: [:solar_irradiation, :temperature]).where('at >= ? and at <= ?', start_date.beginning_of_day, end_date.end_of_day)
    p "Clear out readings for #{start_date} - #{end_date} - records #{old_readings.count}"
    old_readings.delete_all

    p "Now import"
    DataFeeds::WeatherUndergroundLoader.new(start_date, end_date).import
    new_readings = DataFeedReading.where(feed_type: [:solar_irradiation, :temperature]).where('at >= ? and at <= ?', start_date, end_date)
    p "New readings for #{start_date} - #{end_date} - records #{new_readings.count}"
  end
end

namespace :data_feeds do
  desc 'Set up data feeds'
  task :weather_underground_loader, [:start_date, :end_date] => :environment do |_t, args|
    start_date = args[:start_date].present? ? Date.parse(args[:start_date]) : Date.yesterday - 3
    end_date = args[:end_date].present? ? Date.parse(args[:end_date]) : Date.yesterday

    old_readings = DataFeedReading.where(feed_type: [:solar_irradiation, :temperature]).where('at >= ? and at <= ?', start_date.beginning_of_day, end_date.end_of_day)
    p "Clear out readings for #{start_date} - #{end_date} - records #{old_readings.count}"
    old_readings.delete_all

    p "Now import"
    DataFeeds::WeatherUndergroundLoader.new(start_date, end_date).import
    new_readings = DataFeedReading.where(feed_type: [:solar_irradiation, :temperature]).where('at >= ? and at <= ?', start_date, end_date)
    p "New readings for #{start_date} - #{end_date} - records #{new_readings.count}"
  end
end

Change default dates - now yesterday -3 to yesterday
Change default dates - now yesterday -3 to yesterday
