Removing cache & displaying list in desc in admin

This commit is contained in:
Nikhil Kadam
2025-05-15 19:17:47 +05:30
parent 040e3cc62d
commit ce1203b5bb
5 changed files with 34 additions and 32 deletions

View File

@@ -236,7 +236,9 @@ class AssetadmintController extends Controller
$query->where('active', 1) // Only fetch active user asset links
->with(['user:id,first_name,last_name']); // Eager load user info
}
])->get()->map(function ($asset) {
])
->orderBy('created_at', 'desc')
->get()->map(function ($asset) {
$assetData = $asset->toArray();
unset($assetData['customer'], $assetData['user_asset_links']); // remove full objects

View File

@@ -125,7 +125,7 @@ class CustomerController extends Controller
public function listCustomers(Request $request)
{
try {
$customers = Customer::all();
$customers = Customer::orderBy('created_at', 'desc')->get();
return jsonResponseWithSuccessMessage('Customers fetched successfully', [
'customers' => $customers

View File

@@ -126,7 +126,7 @@ class DeviceController extends Controller
public function listDevices(Request $request)
{
try {
$devices = Device::with('deviceProfile', 'customer')->get()->map(function ($device) {
$devices = Device::with('deviceProfile', 'customer')->orderBy('created_at', 'desc')->get()->map(function ($device) {
$deviceData = $device->toArray();
unset($deviceData['device_profile'], $deviceData['customer']); // remove full relations

View File

@@ -330,12 +330,12 @@ class UsersController extends Controller
public function list()
{
try {
$users = $this->adminService->listUsers();
$users = User::orderBy('created_at', 'desc')->get();
if (!empty($users['data'])) {
if (!empty($users)) {
return response()->json([
'message' => 'Users fetched successfully',
'users' => $users['data']
'users' => $users
], 200);
}

View File

@@ -866,8 +866,8 @@ class TelemetryController extends Controller
// Set cache key per user
$cacheKey = "user_assets_health_{$userId}";
// Cache for 1 hour
// $formattedData = Cache::remember($cacheKey, now()->addHours(value: 1), function () use ($userId) {
// Cache for 15 minutes
// $formattedData = Cache::remember($cacheKey, now()->addMinutes(value: 15), function () use ($userId) {
$assetDeviceListing = UserAssetLink::with('asset.devices')
->where(['user_id' => $userId, 'active' => 1])
->get();
@@ -935,8 +935,8 @@ class TelemetryController extends Controller
$token = readHeaderToken();
$cacheKey = 'device_info_' . $token['sub'];
// Cache the entire response for 1 hour
$response = Cache::remember($cacheKey, now()->addHours(value: 1), function () use ($token) {
// Cache the entire response for 15 minutes
// $response = Cache::remember($cacheKey, now()->addMinutes(value: 15), function () use ($token) {
$devices = UserAssetLink::with('asset.devices')
->where('user_id', $token['sub'])
->get()
@@ -966,7 +966,7 @@ class TelemetryController extends Controller
$alarms = $this->customerInfoService->fetchDeviceAlarms($deviceIds);
}
return [
$response = [
'success' => true,
'good' => $good,
'moderate' => $moderate,
@@ -975,7 +975,7 @@ class TelemetryController extends Controller
'active' => $deviceCount['activeDevices'] ?? 0,
'alarm' => $alarms['count'] ?? 0
];
});
// });
return response()->json($response);
@@ -989,7 +989,7 @@ class TelemetryController extends Controller
$token = readHeaderToken();
$cacheKey = "device_indicators_{$token['sub']}_asset_{$assetId}";
$cachedData = Cache::remember($cacheKey, now()->addHours(value: 1), function () use ($token, $assetId) {
// $cachedData = Cache::remember($cacheKey, now()->addHours(value: 1), function () use ($token, $assetId) {
$assetDeviceListing = $this->getUserAssetLinkWithDevices($token['sub'], $assetId);
$assetLink = $assetDeviceListing->map(function($link){
@@ -1023,10 +1023,10 @@ class TelemetryController extends Controller
];
});
return $assetLink;
});
// return $assetLink;
// });
return response()->json($cachedData);
return response()->json($assetLink);
} catch(Exception $e) {
return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
@@ -1132,7 +1132,7 @@ class TelemetryController extends Controller
return response()->json(['success' => true, 'message' => 'Device not found'], 404);
}
$cachedData = Cache::remember($cacheKey, now()->addHours(value: 1), function () use ($deviceId) {
// $cachedData = Cache::remember($cacheKey, now()->addHours(value: 1), function () use ($deviceId) {
$deviceParams = $this->getDeviceAlertParameters($deviceId);
$keyNameList = $deviceParams->pluck('key_name')->implode(',');
@@ -1158,14 +1158,14 @@ class TelemetryController extends Controller
}
}
return [
$response = [
'success' => true,
'updated_time' => $this->telemetryService->convertToUserTimezone($dateTime),
'data' => $transformedTelemetry
];
});
// });
return response()->json($cachedData);
return response()->json($response);
} catch(Exception $e){
return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
@@ -1250,7 +1250,7 @@ class TelemetryController extends Controller
}
// Cache the response for 1 hour
$cachedData = Cache::remember($cacheKey, now()->addHours(value: 1), function () use ($deviceId) {
// $cachedData = Cache::remember($cacheKey, now()->addHours(value: 1), function () use ($deviceId) {
$deviceParams = $this->getPopupTimeseriesKeys($deviceId);
if ($deviceParams->isEmpty()) {
@@ -1269,14 +1269,14 @@ class TelemetryController extends Controller
// Transform the data using our TelemetryService
$transformed = $this->telemetryService->transformTelemetryData($data, $displayNameMap);
return [
$response = [
'success' => true,
'updated_time' => $transformed['dateTime'] ? $this->telemetryService->convertToUserTimezone($transformed['dateTime']) : null,
'data' => $transformed['telemetry']
];
});
// });
return response()->json($cachedData);
return response()->json($response);
} catch(Exception $e){
Log::error("Error fetching global indicators: " . $e->getMessage(), ['device_id' => $deviceId]);
@@ -1314,7 +1314,7 @@ class TelemetryController extends Controller
}
// Cache the response for 1 hour
$cachedData = Cache::remember($cacheKey, now()->addHours(value: 1), function () use ($deviceId) {
// $cachedData = Cache::remember($cacheKey, now()->addHours(value: 1), function () use ($deviceId) {
$dates = collect(range(0, 6))->map(function ($i) {
return Carbon::now()->subDays($i)->format('M d, Y');
})->reverse()->values()->toArray();
@@ -1372,14 +1372,14 @@ class TelemetryController extends Controller
$graph2[$date] = $value2;
}
return [
$response = [
'success' => true,
'graph1' => $graph1,
'graph2' => $graph2,
];
});
// });
return response()->json($cachedData);
return response()->json($response);
} catch (Exception $e) {
return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
@@ -1503,7 +1503,7 @@ class TelemetryController extends Controller
return response()->json(['success' => true, 'message' => 'Device not found'], 404);
}
$data = Cache::remember($cacheKey, now()->addHours(value: 1), function () use ($deviceId) {
// $data = Cache::remember($cacheKey, now()->addHours(value: 1), function () use ($deviceId) {
$device = Device::find($deviceId, ['device_profile_id', 'type']);
$profileId = $device->device_profile_id;
$pressureValKey = 'Pressure_value';
@@ -1542,10 +1542,10 @@ class TelemetryController extends Controller
];
}
return ['success' => true, 'data' => $response];
});
$response = ['success' => true, 'data' => $response];
// });
return response()->json($data);
return response()->json($response);
} catch (Exception $e) {
return response()->json(['success' => false, 'message' => $e->getMessage()], 500);