# app/controllers/api/v1/dashboard_controller.rb module Api module V1 class DashboardController < ApplicationController # GET /api/v1/dashboard/metricas def metricas authorize :dashboard, :metricas? ultimo = HistoricoEstimado.recentes.first vivo = HistoricoEstimado.calcular_ao_vivo render json: { ultimo_registro: ultimo && { data_hora: ultimo.data_hora, valor_estimado: ultimo.valor_total_estimado.to_f, entregas: ultimo.entregas_contadas }, ao_vivo: { valor_mes: vivo[:valor], entregas_mes: vivo[:entregas] }, historico_24h: HistoricoEstimado .where(data_hora: 24.hours.ago..Time.current) .order(:data_hora) .map { |h| { hora: h.data_hora.strftime('%H:%M'), valor: h.valor_total_estimado.to_f } } } end end end end