require 'active_support/core_ext/numeric/bytes'
require 'thor'
require 'debci/package'

module Debci
  class StorageLimit
    def storage_limit
      @storage_limit ||= Debci.config.disk_storage_limit.megabytes
    end

    def run
      expiration = Time.now - Debci.config.data_retention.days
      packages = Debci::Package.find_by_sql("
        SELECT packages.*
        FROM packages
        JOIN jobs ON packages.id = jobs.package_id
        WHERE date > '#{expiration}'
        GROUP BY packages.id
        HAVING sum(jobs.log_size + jobs.artifacts_size) >= #{storage_limit}
      ")
      packages.each do |package|
        self.cleanup_package(package)
      end
    end

    def cleanup_package(package)
      keep = true
      storage = 0
      package.jobs.order("date DESC").in_batches.each do |subset|
        subset.each do |job|
          storage += job.disk_usage
          keep = false if storage > storage_limit
          unless keep
            job.cleanup
            Debci.log('Cleaned up files for job %<job_id>s (%<job>s)' % { job_id: job.run_id, job: job })
          end
        end
      end
    end

    class CLI < Thor
      desc 'start', 'keeps storage limit by package/suite/architecture'
      def start
        ::Debci::StorageLimit.new.run
      end
      default_task :start
    end
  end
end
