2025-04-07 17:46:56 +05:30
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\APIS\AdminApi;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\Asset;
|
|
|
|
|
use App\Models\Customer;
|
|
|
|
|
use App\Models\Device;
|
|
|
|
|
use App\Services\AdminService;
|
|
|
|
|
use Illuminate\Container\Attributes\Log;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
|
|
|
|
|
class AdminDashboardController extends Controller
|
|
|
|
|
{
|
|
|
|
|
protected $adminService;
|
|
|
|
|
|
|
|
|
|
public function __construct(AdminService $adminService)
|
|
|
|
|
{
|
|
|
|
|
$this->adminService = $adminService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function adminDashboard()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$totalCustomers = Customer::count();
|
|
|
|
|
$totalAssets = Asset::count();
|
|
|
|
|
$totalDevices = Device::count();
|
|
|
|
|
|
|
|
|
|
$customers = Customer::select('name', 'country', 'state', 'city')->get();
|
|
|
|
|
|
|
|
|
|
$token = $this->adminService->getToken();
|
|
|
|
|
$activeDevices = 0;
|
|
|
|
|
$inactiveDevices = 0;
|
|
|
|
|
$activeAlarmsCount = 0;
|
|
|
|
|
$warningAlarmsCount = 0;
|
|
|
|
|
$criticalAlarmsCount = 0;
|
|
|
|
|
|
|
|
|
|
if ($token) {
|
|
|
|
|
$baseUrl = env('THINGSBOARD_URL');
|
|
|
|
|
$deviceIds = Device::pluck('id')->toArray();
|
|
|
|
|
|
|
|
|
|
foreach ($deviceIds as $deviceId) {
|
|
|
|
|
$response = Http::withHeaders([
|
|
|
|
|
'Authorization' => "Bearer $token",
|
|
|
|
|
'Accept' => 'application/json',
|
2025-05-15 11:55:51 +05:30
|
|
|
])->get("{$baseUrl}api/device/info/{$deviceId}");
|
2025-04-07 17:46:56 +05:30
|
|
|
|
|
|
|
|
if ($response->successful()) {
|
|
|
|
|
$deviceInfo = $response->json();
|
|
|
|
|
$deviceInfo['active'] ? $activeDevices++ : $inactiveDevices++;
|
|
|
|
|
} else {
|
|
|
|
|
$inactiveDevices++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$alarmsResponse = Http::withHeaders([
|
|
|
|
|
'Authorization' => "Bearer $token",
|
|
|
|
|
'Accept' => 'application/json',
|
2025-05-15 11:55:51 +05:30
|
|
|
])->get("{$baseUrl}api/v2/alarms", [
|
2025-04-07 17:46:56 +05:30
|
|
|
'pageSize' => 1000,
|
|
|
|
|
'page' => 0,
|
|
|
|
|
'status' => 'ACTIVE'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($alarmsResponse->successful()) {
|
|
|
|
|
$alarmsData = $alarmsResponse->json();
|
|
|
|
|
$activeAlarmsCount = $alarmsData['totalElements'] ?? 0;
|
|
|
|
|
|
|
|
|
|
if (isset($alarmsData['data'])) {
|
|
|
|
|
foreach ($alarmsData['data'] as $alarm) {
|
|
|
|
|
$severity = strtoupper($alarm['severity'] ?? '');
|
|
|
|
|
if ($severity === 'CRITICAL') {
|
|
|
|
|
$criticalAlarmsCount++;
|
|
|
|
|
} elseif ($severity === 'WARNING') {
|
|
|
|
|
$warningAlarmsCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'total_customers' => $totalCustomers,
|
|
|
|
|
'total_assets' => $totalAssets,
|
|
|
|
|
'devices' => [
|
|
|
|
|
'total_devices' => $totalDevices,
|
|
|
|
|
'active_devices' => $activeDevices,
|
|
|
|
|
'inactive_devices' => $inactiveDevices,
|
|
|
|
|
],
|
|
|
|
|
'alarms' => [
|
|
|
|
|
'total_active' => $activeAlarmsCount,
|
|
|
|
|
'warning' => $warningAlarmsCount,
|
|
|
|
|
'critical' => $criticalAlarmsCount,
|
|
|
|
|
'other' => $activeAlarmsCount - ($warningAlarmsCount + $criticalAlarmsCount)
|
|
|
|
|
],
|
|
|
|
|
'customers' => $customers,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'message' => 'Failed to fetch dashboard statistics',
|
|
|
|
|
'error' => $e->getMessage()
|
|
|
|
|
], 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|