completed SMS gateway project
This commit is contained in:
49
app/controllers/api/v1/admin/gateways_controller.rb
Normal file
49
app/controllers/api/v1/admin/gateways_controller.rb
Normal file
@@ -0,0 +1,49 @@
|
||||
module Api
|
||||
module V1
|
||||
module Admin
|
||||
class GatewaysController < ApplicationController
|
||||
include ApiAuthenticatable
|
||||
|
||||
# GET /api/v1/admin/gateways
|
||||
def index
|
||||
gateways = ::Gateway.order(created_at: :desc)
|
||||
|
||||
render json: {
|
||||
gateways: gateways.map { |gateway|
|
||||
{
|
||||
id: gateway.id,
|
||||
device_id: gateway.device_id,
|
||||
name: gateway.name,
|
||||
status: gateway.status,
|
||||
last_heartbeat_at: gateway.last_heartbeat_at,
|
||||
messages_sent_today: gateway.messages_sent_today,
|
||||
messages_received_today: gateway.messages_received_today,
|
||||
total_messages_sent: gateway.total_messages_sent,
|
||||
total_messages_received: gateway.total_messages_received,
|
||||
active: gateway.active,
|
||||
priority: gateway.priority,
|
||||
metadata: gateway.metadata,
|
||||
created_at: gateway.created_at
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
# POST /api/v1/admin/gateways/:id/toggle
|
||||
def toggle
|
||||
gateway = ::Gateway.find(params[:id])
|
||||
gateway.update!(active: !gateway.active)
|
||||
|
||||
render json: {
|
||||
success: true,
|
||||
gateway: {
|
||||
id: gateway.id,
|
||||
device_id: gateway.device_id,
|
||||
active: gateway.active
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
60
app/controllers/api/v1/admin/stats_controller.rb
Normal file
60
app/controllers/api/v1/admin/stats_controller.rb
Normal file
@@ -0,0 +1,60 @@
|
||||
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
|
||||
Reference in New Issue
Block a user