fix handle token login when loop and fix width column on task fix color default danger success scss, fix add timeout on php.ini, add scraping for execute from api, add check api for handle disabled button sync
This commit is contained in:
@@ -2,13 +2,17 @@
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Enums\ImportDatasourceStatus;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\ImportDatasourceResource;
|
||||
use App\Models\ImportDatasource;
|
||||
use App\Traits\GlobalApiResponse;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ImportDatasourceController extends Controller
|
||||
{
|
||||
use GlobalApiResponse;
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
@@ -23,6 +27,19 @@ class ImportDatasourceController extends Controller
|
||||
return ImportDatasourceResource::collection($query->paginate());
|
||||
}
|
||||
|
||||
public function checkImportDatasource(){
|
||||
try{
|
||||
$data = ImportDatasource::where("status",ImportDatasourceStatus::Processing->value )->count();
|
||||
$result = [
|
||||
"can_execute" => $data === 0,
|
||||
"total_processing" => $data
|
||||
];
|
||||
return response()->json( $result , 200);
|
||||
}catch(Exception $ex){
|
||||
return response()->json(["message" => $ex->getMessage(), 500]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
|
||||
61
app/Http/Controllers/Api/ScrapingController.php
Normal file
61
app/Http/Controllers/Api/ScrapingController.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Enums\ImportDatasourceStatus;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\ImportDatasource;
|
||||
use App\Traits\GlobalApiResponse;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ScrapingController extends Controller
|
||||
{
|
||||
use GlobalApiResponse;
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$check_datasource = ImportDatasource::where("status", ImportDatasourceStatus::Processing->value)->count();
|
||||
if($check_datasource > 0){
|
||||
return $this->resError("Failed to execute while processing another scraping");
|
||||
}
|
||||
|
||||
// run service artisan command
|
||||
Artisan::call("app:execute-scraping");
|
||||
return $this->resSuccess("Success execute scraping service please wait");
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,12 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ImportDatasource extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
protected $table = 'import_datasources';
|
||||
protected $fillable = [
|
||||
'id',
|
||||
|
||||
@@ -26,13 +26,15 @@ class ServiceClient
|
||||
);
|
||||
}
|
||||
|
||||
public function makeRequest($url, $method = 'GET', $body = null, $headers = []){
|
||||
public function makeRequest($url, $method = 'GET', $body = null, $headers = [], $timeout = 300){
|
||||
try {
|
||||
|
||||
$headers = array_merge($this->headers, $headers);
|
||||
|
||||
$options = [
|
||||
'headers' => $headers,
|
||||
'timeout' => $timeout,
|
||||
'connect_timeout' => 60
|
||||
];
|
||||
|
||||
if ($body) {
|
||||
|
||||
@@ -48,21 +48,13 @@ class ServiceSIMBG
|
||||
return $res;
|
||||
}
|
||||
|
||||
public function syncIndexIntegration($uuid)
|
||||
public function syncIndexIntegration($uuid, $token)
|
||||
{
|
||||
$clientHelper = new ServiceClient($this->simbg_host);
|
||||
$url = "/api/pbg/v1/detail/" . $uuid . "/retribution/indeks-terintegrasi/";
|
||||
$resToken = $this->getToken();
|
||||
|
||||
if (!isset($resToken) || empty($resToken->original['data']['token']['access'])) {
|
||||
// Log error
|
||||
Log::error("Token not retrieved for syncIndexIntegration", ['uuid' => $uuid]);
|
||||
return null;
|
||||
}
|
||||
|
||||
$apiToken = $resToken->original['data']['token']['access'];
|
||||
|
||||
$headers = [
|
||||
'Authorization' => "Bearer " . $apiToken,
|
||||
'Authorization' => "Bearer " . $token,
|
||||
];
|
||||
|
||||
$res = $clientHelper->get($url, $headers);
|
||||
@@ -70,16 +62,16 @@ class ServiceSIMBG
|
||||
if (empty($res->original['success']) || !$res->original['success']) {
|
||||
// Log error
|
||||
Log::error("API response indicates failure", ['url' => $url, 'uuid' => $uuid]);
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = $res->original['data']['data'] ?? null;
|
||||
if (!$data) {
|
||||
Log::error("No valid data returned from API", ['url' => $url, 'uuid' => $uuid]);
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
|
||||
PbgTaskIndexIntegrations::updateOrCreate(
|
||||
$resultData = PbgTaskIndexIntegrations::updateOrCreate(
|
||||
['pbg_task_uid' => $uuid],
|
||||
[
|
||||
'indeks_fungsi_bangunan' => $data['indeks_fungsi_bangunan'] ?? null,
|
||||
@@ -93,19 +85,25 @@ class ServiceSIMBG
|
||||
);
|
||||
|
||||
// Log success
|
||||
Log::info("syncIndexIntegration completed successfully", ['uuid' => $uuid]);
|
||||
if ($resultData->wasRecentlyCreated) {
|
||||
Log::info("integration created successfully", ['uuid' => $uuid]);
|
||||
} else {
|
||||
Log::info("integration updated successfully", ['uuid' => $uuid]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function syncTaskList()
|
||||
{
|
||||
$clientHelper = new ServiceClient($this->simbg_host);
|
||||
$resToken = $this->getToken();
|
||||
$initResToken = $this->getToken();
|
||||
|
||||
$importDatasource = ImportDatasource::create([
|
||||
'status' => ImportDatasourceStatus::Processing->value,
|
||||
]);
|
||||
|
||||
if (empty($resToken->original['data']['token']['access'])) {
|
||||
if (empty($initResToken->original['data']['token']['access'])) {
|
||||
$importDatasource->update([
|
||||
'status' => ImportDatasourceStatus::Failed->value,
|
||||
'message' => 'Failed to retrieve token'
|
||||
@@ -113,14 +111,14 @@ class ServiceSIMBG
|
||||
return $this->resError("Failed to retrieve token");
|
||||
}
|
||||
|
||||
$apiToken = $resToken->original['data']['token']['access'];
|
||||
$apiToken = $initResToken->original['data']['token']['access'];
|
||||
$headers = ['Authorization' => "Bearer " . $apiToken];
|
||||
|
||||
$url = "/api/pbg/v1/list/?page=1&size={$this->fetch_per_page}&sort=ASC";
|
||||
$initialResponse = $clientHelper->get($url, $headers);
|
||||
|
||||
$totalPage = $initialResponse->original['data']['total_page'] ?? 0;
|
||||
if ($totalPage === 0) {
|
||||
if ($totalPage == 0) {
|
||||
$importDatasource->update([
|
||||
'status' => ImportDatasourceStatus::Failed->value,
|
||||
'message' => 'Invalid response: no total_page'
|
||||
@@ -132,14 +130,26 @@ class ServiceSIMBG
|
||||
|
||||
for ($currentPage = 1; $currentPage <= $totalPage; $currentPage++) {
|
||||
$pageUrl = "/api/pbg/v1/list/?page={$currentPage}&size={$this->fetch_per_page}&sort=ASC";
|
||||
$token = $this->getToken();
|
||||
$getToken = $this->getToken();
|
||||
if (empty($getToken->original['data']['token']['access'])) {
|
||||
$importDatasource->update([
|
||||
'status' => ImportDatasourceStatus::Failed->value,
|
||||
'message' => 'Failed to retrieve token'
|
||||
]);
|
||||
break;
|
||||
}
|
||||
$token = $getToken->original['data']['token']['access'];
|
||||
$headers = ['Authorization' => "Bearer " . $token];
|
||||
$response = $clientHelper->get($pageUrl, $headers);
|
||||
$tasks = $response->original['data']['data'] ?? [];
|
||||
|
||||
if (empty($tasks)) {
|
||||
$importDatasource->update([
|
||||
'status' => ImportDatasourceStatus::Failed->value,
|
||||
'message' => 'No data found on page'
|
||||
]);
|
||||
Log::warning("No data found on page", ['page' => $currentPage]);
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
|
||||
Log::info("executed page", ['page' => $currentPage, 'total' => $totalPage]);
|
||||
@@ -170,15 +180,20 @@ class ServiceSIMBG
|
||||
'created_at' => now(),
|
||||
];
|
||||
|
||||
$this->syncIndexIntegration($item['uid']);
|
||||
$this->syncTaskDetailSubmit($item['uid']);
|
||||
$this->syncIndexIntegration($item['uid'], $token);
|
||||
$this->syncTaskDetailSubmit($item['uid'], $token);
|
||||
$savedCount++;
|
||||
} catch (Exception $e) {
|
||||
$failedCount++;
|
||||
$importDatasource->update([
|
||||
'status' => ImportDatasourceStatus::Failed->value,
|
||||
'message' => "Successfully processed: $savedCount, Failed: $failedCount"
|
||||
]);
|
||||
Log::error("Failed to process task", [
|
||||
'error' => $e->getMessage(),
|
||||
'task' => $item,
|
||||
]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -201,21 +216,13 @@ class ServiceSIMBG
|
||||
}
|
||||
|
||||
|
||||
public function syncTaskDetailSubmit($uuid)
|
||||
public function syncTaskDetailSubmit($uuid, $token)
|
||||
{
|
||||
$clientHelper = new ServiceClient($this->simbg_host);
|
||||
$resToken = $this->getToken();
|
||||
|
||||
if (!isset($resToken) || empty($resToken->original['data']['token']['access'])) {
|
||||
// Log error
|
||||
Log::error("Token not retrieved for syncTaskDetailSubmit");
|
||||
return null;
|
||||
}
|
||||
|
||||
$apiToken = $resToken->original['data']['token']['access'];
|
||||
$url = "/api/pbg/v1/detail/" . $uuid . "/retribution/submit/";
|
||||
$headers = [
|
||||
'Authorization' => "Bearer " . $apiToken,
|
||||
'Authorization' => "Bearer " . $token,
|
||||
];
|
||||
|
||||
$res = $clientHelper->get($url, $headers);
|
||||
@@ -223,13 +230,13 @@ class ServiceSIMBG
|
||||
if (empty($res->original['success']) || !$res->original['success']) {
|
||||
// Log error
|
||||
Log::error("API response indicates failure", ['url' => $url, 'uuid' => $uuid]);
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = $res->original['data']['data'] ?? [];
|
||||
if (empty($data)) {
|
||||
Log::error("No data returned from API", ['url' => $url, 'uuid' => $uuid]);
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
|
||||
$detailCreatedAt = isset($data['created_at'])
|
||||
@@ -281,7 +288,8 @@ class ServiceSIMBG
|
||||
PbgTaskPrasarana::upsert($insertData, ['pbg_task_uid', 'prasarana_id']);
|
||||
}
|
||||
|
||||
Log::info("syncTaskDetailSubmit completed successfully", ['uuid' => $uuid]);
|
||||
Log::info("retribution and prasarana successfully", ['uuid' => $uuid]);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,10 +12,6 @@ trait GlobalApiResponse
|
||||
'success' => true,
|
||||
'data' => $result
|
||||
];
|
||||
|
||||
if(!empty($message)){
|
||||
$response['message'] = $message;
|
||||
}
|
||||
|
||||
return response()->json($response, $code);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user