Adding color code dynamically to device indicators
This commit is contained in:
@@ -19,6 +19,7 @@ use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Tymon\JWTAuth\Facades\JWTAuth;
|
||||
|
||||
class UsersController extends Controller
|
||||
{
|
||||
@@ -667,11 +668,14 @@ class UsersController extends Controller
|
||||
$user = User::where('email', $email)->first();
|
||||
|
||||
if ($user) {
|
||||
$token = JWTAuth::fromUser($user);
|
||||
|
||||
$localResponse = [
|
||||
'status' => true,
|
||||
'message' => 'Login successful (Local). Redirecting to Local dashboard...',
|
||||
'user_id' => $user->id,
|
||||
'email' => $email,
|
||||
'access_token' => $token,
|
||||
'dashboard_url' => url('/dashboard')
|
||||
];
|
||||
} else {
|
||||
|
||||
@@ -22,6 +22,7 @@ use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class TelemetryController extends Controller
|
||||
{
|
||||
@@ -955,72 +956,139 @@ class TelemetryController extends Controller
|
||||
return 'green';
|
||||
}
|
||||
|
||||
// public function userAssetsNew(){
|
||||
// try {
|
||||
// $token = readHeaderToken();
|
||||
|
||||
// // Retrieve devices of user
|
||||
// $assetDeviceListing = UserAssetLink::with('asset.devices')
|
||||
// ->where(['user_id' => $token['sub'], 'active' => 1])
|
||||
// ->get();
|
||||
|
||||
// $formattedData = $assetDeviceListing->map(function ($link) {
|
||||
// $asset = $link->asset;
|
||||
// $deviceHealthStatuses = [];
|
||||
|
||||
// $devicesData = $asset->devices->map(function ($device) use (&$deviceHealthStatuses) {
|
||||
|
||||
// $timeseriesKeys = TimeseriesKeyMaster::where('display_on_health_condition', 1)
|
||||
// ->where('device_profile_xid', $device->device_profile_id)
|
||||
// ->pluck('key_name')
|
||||
// ->implode(',');
|
||||
|
||||
// // \Log::info("Keys for device {$device->name}", [$timeseriesKeys]);
|
||||
|
||||
// $telemetryValue = $this->customerInfoService->fetchTelemetryData($device->id, $timeseriesKeys);
|
||||
// $data = $telemetryValue instanceof \Illuminate\Http\JsonResponse ? $telemetryValue->getData(true) : $telemetryValue;
|
||||
|
||||
// // \Log::info("Telemetry data for device {$device->name}", $data);
|
||||
|
||||
// $transformedTelemetry = [];
|
||||
// // dd($data);
|
||||
// if (!empty($data) && is_array($data)) {
|
||||
// foreach ($data as $key => $items) {
|
||||
|
||||
// foreach ($items as $item) {
|
||||
// $transformedTelemetry[] = [
|
||||
// 'key_name' => $key,
|
||||
// 'value' => $item['value']
|
||||
// ];
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // \Log::info("Transformed data for device {$device->name}", $transformedTelemetry);
|
||||
// $deviceHealth = $this->getDeviceHealth($transformedTelemetry);
|
||||
// $deviceHealthStatuses[] = $deviceHealth;
|
||||
|
||||
// return [
|
||||
// 'deviceId' => $device->id,
|
||||
// 'deviceName' => $device->name,
|
||||
// 'deviceHealth' => $deviceHealth
|
||||
// ];
|
||||
// })->values();
|
||||
|
||||
// return [
|
||||
// 'assetId' => $asset->id,
|
||||
// 'assetName' => $asset->name,
|
||||
// 'assetHealth' => $deviceHealthStatuses ? $this->getAssetHealth($deviceHealthStatuses) : null,
|
||||
// 'devices' => $devicesData
|
||||
// ];
|
||||
|
||||
// });
|
||||
|
||||
// return response()->json(['success' => true,'data' => $formattedData]);
|
||||
|
||||
// } catch(Exception $e){
|
||||
// return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
|
||||
// }
|
||||
// }
|
||||
|
||||
public function userAssetsNew(){
|
||||
try {
|
||||
$token = readHeaderToken();
|
||||
$userId = $token['sub'];
|
||||
|
||||
// Retrieve devices of user
|
||||
$assetDeviceListing = UserAssetLink::with('asset.devices')
|
||||
->where(['user_id' => $token['sub'], 'active' => 1])
|
||||
->get();
|
||||
// Set cache key per user
|
||||
$cacheKey = "user_assets_health_{$userId}";
|
||||
|
||||
$formattedData = $assetDeviceListing->map(function ($link) {
|
||||
$asset = $link->asset;
|
||||
$deviceHealthStatuses = [];
|
||||
// Cache for 5 minutes
|
||||
$formattedData = Cache::remember($cacheKey, now()->addMinutes(5), function () use ($userId) {
|
||||
$assetDeviceListing = UserAssetLink::with('asset.devices')
|
||||
->where(['user_id' => $userId, 'active' => 1])
|
||||
->get();
|
||||
|
||||
$devicesData = $asset->devices->map(function ($device) use (&$deviceHealthStatuses) {
|
||||
return $assetDeviceListing->map(function ($link) {
|
||||
$asset = $link->asset;
|
||||
$deviceHealthStatuses = [];
|
||||
|
||||
$timeseriesKeys = TimeseriesKeyMaster::where('display_on_health_condition', 1)
|
||||
->where('device_profile_xid', $device->device_profile_id)
|
||||
->pluck('key_name')
|
||||
->implode(',');
|
||||
$devicesData = $asset->devices->map(function ($device) use (&$deviceHealthStatuses) {
|
||||
|
||||
// \Log::info("Keys for device {$device->name}", [$timeseriesKeys]);
|
||||
$timeseriesKeys = TimeseriesKeyMaster::where('display_on_health_condition', 1)
|
||||
->where('device_profile_xid', $device->device_profile_id)
|
||||
->pluck('key_name')
|
||||
->implode(',');
|
||||
|
||||
$telemetryValue = $this->customerInfoService->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;
|
||||
|
||||
// \Log::info("Telemetry data for device {$device->name}", $data);
|
||||
|
||||
$transformedTelemetry = [];
|
||||
// dd($data);
|
||||
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']
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// \Log::info("Transformed data for device {$device->name}", $transformedTelemetry);
|
||||
$deviceHealth = $this->getDeviceHealth($transformedTelemetry);
|
||||
$deviceHealthStatuses[] = $deviceHealth;
|
||||
|
||||
$deviceHealth = $this->getDeviceHealth($transformedTelemetry);
|
||||
$deviceHealthStatuses[] = $deviceHealth;
|
||||
|
||||
return [
|
||||
'deviceId' => $device->id,
|
||||
'deviceName' => $device->name,
|
||||
'deviceHealth' => $deviceHealth
|
||||
];
|
||||
})->values();
|
||||
|
||||
return [
|
||||
'deviceId' => $device->id,
|
||||
'deviceName' => $device->name,
|
||||
'deviceHealth' => $deviceHealth
|
||||
'assetId' => $asset->id,
|
||||
'assetName' => $asset->name,
|
||||
'assetHealth' => $deviceHealthStatuses ? $this->getAssetHealth($deviceHealthStatuses) : null,
|
||||
'devices' => $devicesData
|
||||
];
|
||||
})->values();
|
||||
|
||||
return [
|
||||
'assetId' => $asset->id,
|
||||
'assetName' => $asset->name,
|
||||
'assetHealth' => $deviceHealthStatuses ? $this->getAssetHealth($deviceHealthStatuses) : null,
|
||||
'devices' => $devicesData
|
||||
];
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
return response()->json(['success' => true,'data' => $formattedData]);
|
||||
return response()->json(['success' => true, 'data' => $formattedData]);
|
||||
|
||||
} catch(Exception $e){
|
||||
return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
|
||||
return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function customerDeviceInfoNew(){
|
||||
try {
|
||||
$token = readHeaderToken();
|
||||
@@ -1123,12 +1191,19 @@ class TelemetryController extends Controller
|
||||
$data = $telemetryValue instanceof \Illuminate\Http\JsonResponse ? $telemetryValue->getData(true) : $telemetryValue;
|
||||
|
||||
$transformedTelemetry = [];
|
||||
$color = '';
|
||||
|
||||
if (!empty($data) && is_array($data)) {
|
||||
foreach ($data as $key => $items) {
|
||||
|
||||
foreach ($items as $item) {
|
||||
|
||||
if($key=="PowerLoss_value"){
|
||||
$color = $item['value'] > 5 ? 'red' : ($item['value'] > 30 ? 'orange' : 'green');
|
||||
} else {
|
||||
$color = $item['value'] > 70 ? 'green' : ($item['value'] > 30 ? 'orange' : 'red');
|
||||
}
|
||||
|
||||
// Get average of values from past 6 hours
|
||||
$ts['startTs'] = Carbon::now()->subHours(6)->timestamp * 1000;
|
||||
$ts['endTs'] = Carbon::now()->timestamp * 1000;
|
||||
@@ -1152,9 +1227,10 @@ class TelemetryController extends Controller
|
||||
}
|
||||
|
||||
$transformedTelemetry[] = [
|
||||
'display_name' => $displayNameMap[$key] ?? $key,
|
||||
'value' => $item['value'],
|
||||
'averageVal' => (string)$averageVal
|
||||
'display_name' => $displayNameMap[$key] ?? $key,
|
||||
'value' => $item['value'],
|
||||
'averageVal' => (string)$averageVal,
|
||||
'color' => $color
|
||||
];
|
||||
}
|
||||
|
||||
@@ -1248,11 +1324,12 @@ class TelemetryController extends Controller
|
||||
try{
|
||||
$token = readHeaderToken();
|
||||
|
||||
$deviceParams = TimeseriesKeyMaster::select('key_name', 'display_name')
|
||||
$deviceParams = TimeseriesKeyMaster::select('key_name', 'display_name','display_on_popup_sequence')
|
||||
->where('display_on_popup', 1)
|
||||
->whereHas('device', function ($query) use ($deviceId) {
|
||||
$query->where('id', $deviceId);
|
||||
})
|
||||
->orderBy('display_on_popup_sequence')
|
||||
->get();
|
||||
|
||||
$keyNameList = $deviceParams->pluck('key_name')->implode(',');
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('timeseries_key_master', function (Blueprint $table) {
|
||||
$table->integer('display_on_popup_sequence')->default(0)->after('display_on_popup');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('timeseries_key_master', function (Blueprint $table) {
|
||||
$table->dropColumn('display_on_popup_sequence');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user