From 339ffc8b58d50a26dd44b9cfe62780f47443c95b Mon Sep 17 00:00:00 2001 From: Nikhil Kadam Date: Thu, 8 May 2025 13:34:30 +0530 Subject: [PATCH] Fixing issue in Alarms api --- .../APIS/CustomerApi/TelemetryController.php | 130 ++++++++++-------- .../Controllers/AlarmControllerCommon.php | 4 +- app/Services/AlarmService.php | 2 +- 3 files changed, 78 insertions(+), 58 deletions(-) diff --git a/app/Http/Controllers/APIS/CustomerApi/TelemetryController.php b/app/Http/Controllers/APIS/CustomerApi/TelemetryController.php index 69b1ad4..2c39664 100644 --- a/app/Http/Controllers/APIS/CustomerApi/TelemetryController.php +++ b/app/Http/Controllers/APIS/CustomerApi/TelemetryController.php @@ -1031,8 +1031,8 @@ class TelemetryController extends Controller // Set cache key per user $cacheKey = "user_assets_health_{$userId}"; - // Cache for 5 minutes - $formattedData = Cache::remember($cacheKey, now()->addMinutes(5), function () use ($userId) { + // Cache for 24 hours + $formattedData = Cache::remember($cacheKey, now()->addHours(24), function () use ($userId) { $assetDeviceListing = UserAssetLink::with('asset.devices') ->where(['user_id' => $userId, 'active' => 1]) ->get(); @@ -1089,74 +1089,77 @@ class TelemetryController extends Controller } } - public function customerDeviceInfoNew(){ + public function customerDeviceInfoNew() + { try { $token = readHeaderToken(); + $cacheKey = 'device_info_' . $token['sub']; - // Fetch all devices linked to user in one go - $devices = UserAssetLink::with('asset.devices') - ->where('user_id', $token['sub']) - ->get() - ->pluck('asset.devices') - ->flatten() - ->unique('id'); // Ensure no duplicate devices + // Cache the entire response for 24 hours (1440 minutes) + $response = Cache::remember($cacheKey, 1440, function () use ($token) { + $devices = UserAssetLink::with('asset.devices') + ->where('user_id', $token['sub']) + ->get() + ->pluck('asset.devices') + ->flatten() + ->unique('id'); - $good = $moderate = $bad = 0; - $deviceIds = $devices->pluck('id')->toArray(); + $good = $moderate = $bad = 0; + $deviceIds = $devices->pluck('id')->toArray(); - foreach ($deviceIds as $deviceId) { - $device = Device::find($deviceId); // get the device row first + if (!empty($deviceIds)) { + foreach ($deviceIds as $deviceId) { + $device = Device::find($deviceId); + if ($device) { + $timeseriesKeys = TimeseriesKeyMaster::where('display_on_health_condition', 1) + ->where('device_profile_xid', $device->device_profile_id) + ->pluck('key_name') + ->implode(','); - if ($device) { - $timeseriesKeys = TimeseriesKeyMaster::where('display_on_health_condition', 1) - ->where('device_profile_xid', $device->device_profile_id) - ->pluck('key_name') - ->implode(','); + $telemetryValue = app(CustomerInfoService::class)->fetchTelemetryData($device->id, $timeseriesKeys); + $data = $telemetryValue instanceof \Illuminate\Http\JsonResponse ? $telemetryValue->getData(true) : $telemetryValue; - $telemetryValue = $this->customerInfoService->fetchTelemetryData($device->id, $timeseriesKeys); - $data = $telemetryValue instanceof \Illuminate\Http\JsonResponse ? $telemetryValue->getData(true) : $telemetryValue; - - $transformedTelemetry = []; - - if (!empty($data) && is_array($data)) { - foreach ($data as $key => $items) { - - foreach ($items as $item) { - $transformedTelemetry[] = [ - 'key_name' => $key, - 'value' => $item['value'] - ]; + $transformedTelemetry = []; + if (!empty($data) && is_array($data)) { + foreach ($data as $key => $items) { + foreach ($items as $item) { + $transformedTelemetry[] = [ + 'key_name' => $key, + 'value' => $item['value'] + ]; + } } } + + $deviceHealth = $this->getDeviceHealth($transformedTelemetry); + + match ($deviceHealth) { + 'green' => $good++, + 'orange' => $moderate++, + default => $bad++ + }; } - // \Log::info("Transformed data for device {$device->name}", $transformedTelemetry); - $deviceHealth = $this->getDeviceHealth($transformedTelemetry); + } - match ($deviceHealth) { - 'green' => $good++, - 'orange' => $moderate++, - default => $bad++ - }; + $deviceCount = app(CustomerInfoService::class)->getDevicesCount($deviceIds); + $alarms = app(CustomerInfoService::class)->fetchDeviceAlarms($deviceIds); } - } - $deviceCount = $this->customerInfoService->getDevicesCount($deviceIds); - $alarms = $this->customerInfoService->fetchDeviceAlarms($deviceIds); - - $response = [ - 'success' => true, - 'good' => $good, - 'moderate' => $moderate, - 'bad' => $bad, - 'total' => $deviceCount['totalDevices'], - 'active' => $deviceCount['activeDevices'], - 'alarm' => $alarms['count'] - ]; + return [ + 'success' => true, + 'good' => $good, + 'moderate' => $moderate, + 'bad' => $bad, + 'total' => $deviceCount['totalDevices'] ?? 0, + 'active' => $deviceCount['activeDevices'] ?? 0, + 'alarm' => $alarms['count'] ?? 0 + ]; + }); return response()->json($response); - } catch(Exception $e){ - return response()->json(['success' => false, 'message' => $e->getMessage()], 500); + } catch (Exception $e) { + return response()->json(['success' => false, 'message' => $e->getMessage()], 500); } } @@ -1387,6 +1390,21 @@ class TelemetryController extends Controller ] ]; + $thresholdLimit = [ + 'ChannelSpeed' => [ + ['min' => 0, 'color' => 5000] + ], + 'PowerLoss_value' => [ + ['min' => 0, 'color' => 10] + ], + 'GlobalLevel_value' => [ + ['min' => 0, 'color' => 10] + ], + 'default' => [ + ['min' => 0, 'color' => 100] + ] + ]; + foreach ($data as $key => $items) { foreach ($items as $item) { @@ -1397,6 +1415,7 @@ class TelemetryController extends Controller // Determine thresholds $thresholds = $thresholdMap[$key] ?? $thresholdMap['default']; + $thresholdLimits = $thresholdLimit[$key] ?? $thresholdLimit['default']; // Determine health status based on thresholds $status = 'Stable'; @@ -1416,7 +1435,8 @@ class TelemetryController extends Controller 'display_name' => $displayNameMap[$key] ?? $key, 'value' => (string)$value, 'health_status' => $status, - 'thresholds' => $thresholds + 'thresholds' => $thresholds, + 'limit' => $thresholdLimits ]; } } diff --git a/app/Http/Controllers/AlarmControllerCommon.php b/app/Http/Controllers/AlarmControllerCommon.php index 915721e..f7ce8ea 100644 --- a/app/Http/Controllers/AlarmControllerCommon.php +++ b/app/Http/Controllers/AlarmControllerCommon.php @@ -73,7 +73,7 @@ class AlarmControllerCommon extends Controller { try { $token = app('App\Services\AdminService')->getToken(); // Fetch cached token - $url = env('THINGSBOARD_URL') . "/api/alarm/{$id}"; + $url = env('THINGSBOARD_URL') . "api/alarm/{$id}"; $response = Http::withHeaders([ 'X-Authorization' => 'Bearer ' . $token, @@ -215,7 +215,7 @@ class AlarmControllerCommon extends Controller public function filterAlarm(Request $request) { try { - + // dd('alarm'); $alarmData = $request->only([ 'severity', 'pageSize', diff --git a/app/Services/AlarmService.php b/app/Services/AlarmService.php index 838e2b6..6046aa7 100644 --- a/app/Services/AlarmService.php +++ b/app/Services/AlarmService.php @@ -316,7 +316,7 @@ class AlarmService public function filterAlarm(array $data) { $token = $this->getToken(); - dd($token); + $deviceId = null; if (!empty($data['deviceId'])) {