61 lines
2.1 KiB
Ruby
61 lines
2.1 KiB
Ruby
module Api
|
|
module V1
|
|
module Admin
|
|
class StatsController < ApplicationController
|
|
include ApiAuthenticatable
|
|
|
|
# GET /api/v1/admin/stats
|
|
def index
|
|
today = Time.current.beginning_of_day
|
|
|
|
# Gateway stats
|
|
total_gateways = ::Gateway.count
|
|
active_gateways = ::Gateway.active.count
|
|
online_gateways = ::Gateway.online.count
|
|
|
|
# Message stats
|
|
total_messages_sent = ::Gateway.sum(:total_messages_sent)
|
|
total_messages_received = ::Gateway.sum(:total_messages_received)
|
|
|
|
messages_sent_today = ::Gateway.sum(:messages_sent_today)
|
|
messages_received_today = ::Gateway.sum(:messages_received_today)
|
|
|
|
# Pending and failed messages
|
|
pending_messages = SmsMessage.pending.count
|
|
failed_messages_today = SmsMessage.failed
|
|
.where("created_at >= ?", today)
|
|
.count
|
|
|
|
# OTP stats
|
|
otps_sent_today = OtpCode.where("created_at >= ?", today).count
|
|
otps_verified_today = OtpCode.where("verified_at >= ?", today).count
|
|
|
|
render json: {
|
|
gateways: {
|
|
total: total_gateways,
|
|
active: active_gateways,
|
|
online: online_gateways,
|
|
offline: total_gateways - online_gateways
|
|
},
|
|
messages: {
|
|
total_sent: total_messages_sent,
|
|
total_received: total_messages_received,
|
|
sent_today: messages_sent_today,
|
|
received_today: messages_received_today,
|
|
total_today: messages_sent_today + messages_received_today,
|
|
pending: pending_messages,
|
|
failed_today: failed_messages_today
|
|
},
|
|
otp: {
|
|
sent_today: otps_sent_today,
|
|
verified_today: otps_verified_today,
|
|
verification_rate: otps_sent_today > 0 ? (otps_verified_today.to_f / otps_sent_today * 100).round(2) : 0
|
|
},
|
|
timestamp: Time.current
|
|
}
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|