# typed: strict
module Scraper::Metrics::JobBaseMetrics
  extend T::Sig

  # a job has begun
  sig { params(job_class: T.class_of(Scraper::JobBase)).void }
  def self.observe_job_start(job_class:)
    JOB_START_COUNT.increment({ job_class: job_class.name })
  end

  # a job has finished
  sig do
    params(
      job_class: T.class_of(Scraper::JobBase),
      time_ms: Integer,
      success: T::Boolean,
    ).void
  end
  def self.observe_job_finish(job_class:, time_ms:, success:)
    keys = { job_class: job_class.name, success: success }
    JOB_FINISH_COUNT.increment(keys)
    JOB_FINISH_TIME_HISTOGRAM.observe(time_ms, keys)
    JOB_FINISH_TIME_SUMMARY.observe(time_ms, keys)
  end

  sig do
    params(
      source_class:
        T.any(T.class_of(Scraper::JobBase), T.class_of(ReduxApplicationRecord)),
      enqueued_class: T.class_of(Scraper::JobBase),
    ).void
  end
  def self.observe_job_enqueued(source_class:, enqueued_class:)
    JOB_ENQUEUED_COUNT.increment(
      { source_class: source_class.name, enqueued_class: enqueued_class.name },
    )
  end

  private

  JOB_ENQUEUED_COUNT =
    T.let(
      PrometheusExporter::Client.default.register(
        :counter,
        "job_enqueued_count",
        "count of jobs enqueued, labeled by source and enqueued classes",
      ),
      PrometheusExporter::Client::RemoteMetric,
    )

  JOB_START_COUNT =
    T.let(
      PrometheusExporter::Client.default.register(
        :counter,
        "job_start_count",
        "count of jobs started, labeled by job class",
      ),
      PrometheusExporter::Client::RemoteMetric,
    )

  JOB_FINISH_COUNT =
    T.let(
      PrometheusExporter::Client.default.register(
        :counter,
        "job_finish_count",
        "count of jobs finished, labeled by job class and success",
      ),
      PrometheusExporter::Client::RemoteMetric,
    )

  JOB_FINISH_TIME_HISTOGRAM =
    T.let(
      PrometheusExporter::Client.default.register(
        :histogram,
        "job_finish_time_histogram",
        "histogram of job finish times, labeled by job class and success  ",
        buckets: Scraper::Metrics::JOB_TIME_MS_BUCKETS,
      ),
      PrometheusExporter::Client::RemoteMetric,
    )

  JOB_FINISH_TIME_SUMMARY =
    T.let(
      PrometheusExporter::Client.default.register(
        :summary,
        "job_finish_time_summary",
        "summary of job finish times, labeled by job class and success",
        quantiles: Scraper::Metrics::QUARTILES,
      ),
      PrometheusExporter::Client::RemoteMetric,
    )
end
