Skip to content

Commit

Permalink
Merge pull request #7 from achmadhadikurnia/main
Browse files Browse the repository at this point in the history
add siasn-simpeg:pull-referensi-ref-unor and siasn-simpeg:pull-pns-data-utama
  • Loading branch information
achmadhadikurnia committed Feb 27, 2024
2 parents 518f24e + e483110 commit e0a655b
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 62 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ php artisan vendor:publish --tag="siasn-simpeg-config"
## Usage

### Import pegawai

Import pegawai to database via csv file exported from SIASN Export Data ASN.

```bash
Expand All @@ -54,19 +55,26 @@ php artisan siasn-simpeg:import {filePath} --truncate
```

### Pull riwayat pegawai
Pull riwayat pegawai from API.

```bash
php artisan siasn-simpeg:pull-riwayat
```

You can also use the ```endpoint``` argument to specify only certain endpoints and or use the ```nipBaru``` argument to specify only certain employee to be pulled.
You can also use the ```endpoint``` argument to specify only certain endpoints to be pulled.

Use ```--nipBaru=``` option to select only certain pegawai.

Use ```--skip=0``` option to skip N pegawai.

or use ```--track``` option to track the data pulling process.
Use ```--track``` option to track the data pulling process.

And use ```--startOver``` option to start over the data pulling process from the begining.

and use ```--startOver``` option to start over the data pulling process from the begining.
### Pull referensi ref unor

```bash
php artisan siasn-simpeg:pull-referensi-ref-unor
```

## Testing

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public function up(): void
$table->string('tmtGolAkhir')->nullable();
$table->string('masaKerja')->nullable();
$table->string('eselon')->nullable();
$table->string('eselonId', 2)->nullable()->index('6_eselonId');
$table->string('eselonLevel')->nullable();
$table->string('tmtEselon')->nullable();
$table->string('eselonId', 10)->nullable()->index('6_eselonId');
$table->string('eselonLevel', 10)->nullable()->index('6_eselonLevel');
$table->string('tmtEselon', 10)->nullable();
$table->string('gajiPokok')->nullable();
$table->string('kpknId', 42)->nullable()->index('6_kpknId');
$table->text('kpknNama')->nullable();
Expand Down
139 changes: 139 additions & 0 deletions src/Commands/PullPnsDataUtamaCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

namespace Kanekescom\Siasn\Simpeg\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Kanekescom\Siasn\Simpeg\Facades\Simpeg;
use Kanekescom\Siasn\Simpeg\Models\Pegawai;
use Kanekescom\Siasn\Simpeg\Models\PnsDataUtama;
use Kanekescom\Siasn\Simpeg\Models\PullTracking;

class PullPnsDataUtamaCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'siasn-simpeg:pull-pns-data-utama
{--nipBaru= : NIP Baru}
{--skip=0}
{--track}
{--startOver}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Pull pns data utama pegawai to database from endpoint on SIASN Simpeg API';

/**
* Execute the console command.
*/
public function handle()
{
$nipBaru = $this->option('nipBaru');
$track = $this->option('track');
$skip = $this->option('skip');
$startOver = $this->option('startOver');

$pullTrackingCommandName = 'siasn-simpeg:pull-pns-data-utama';
$hasPullTracking = $nipBaru ? null : PullTracking::where('command', $pullTrackingCommandName)->first();
$lastTryPullTracking = $startOver ? 0 : $hasPullTracking?->last_try;
$skip = (int) ($skip > $lastTryPullTracking ? $skip : $lastTryPullTracking);
$iPegawai = $skip;

if ($startOver && $hasPullTracking) {
$hasPullTracking->delete();
$hasPullTracking = null;
$skip = 0;

$this->info(str('Start over command.')->upper());
$this->newLine();
}

$startPegawai = now();
$pullTracking = null;
$pegawais = Pegawai::get();

if ($nipBaru) {
$pegawais = Pegawai::where('nip_baru', $nipBaru)->get();
}

$pegawaiCount = $pegawais->count();

if ($skip >= $pegawaiCount) {
$this->components->error('Skip option value exceeds number of pegawai.');

return self::FAILURE;
}

if (! $nipBaru && $track && ! $startOver) {
$pullTracking = PullTracking::updateOrCreate(['command' => $pullTrackingCommandName], [
'start_from' => $skip,
'amount' => $pegawaiCount,
]);

if ($hasPullTracking && $hasPullTracking->done_at) {
$this->info('Pull command has been completed. Use --startOver to re-pull from the beginning.');

return self::SUCCESS;
}

if ($hasPullTracking) {
$pullingStartingForm = $skip + 1;

$this->info(str("Continue pulling starting from {$pullingStartingForm}")->upper());
$this->newLine();
}
}

$pegawais = $pegawais->skip($skip);

$pegawais->each(function ($pegawai) use ($pegawaiCount, &$iPegawai, $startPegawai, $pullTracking, $skip) {
$iPegawai++;

$this->info("PEGAWAI: [{$iPegawai}/{$pegawaiCount}] {$pegawai->nip_baru}");

try {
$response = Simpeg::getPnsDataUtama($pegawai->nip_baru);
} catch (\Exception $e) {
$this->error($e);
$this->newLine();

return self::FAILURE;
}

try {
$model = new PnsDataUtama;

DB::transaction(function () use ($model, $response) {
if (config('siasn-simpeg.delete_model_before_pull')) {
$model->delete();
}

$model->updateOrCreate($response->toArray());
$model->withTrashed()
->restore();
});
} catch (\Exception $e) {
$this->error($e);
$this->newLine();

return self::FAILURE;
}

$pullTracking?->update(['last_try' => $iPegawai]);
$executedItems = $iPegawai - $skip;

$this->info(str("All tasks are running for {$startPegawai->shortAbsoluteDiffForHumans(now(), 1)} and {$executedItems} items executed")->upper());
$this->newLine();
});

$pullTracking?->update(['done_at' => now()]);

return self::SUCCESS;
}
}
75 changes: 75 additions & 0 deletions src/Commands/PullReferensiRefUnorCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Kanekescom\Siasn\Simpeg\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Kanekescom\Siasn\Simpeg\Facades\Simpeg;
use Kanekescom\Siasn\Simpeg\Models\ReferensiRefUnor;

class PullReferensiRefUnorCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'siasn-simpeg:pull-referensi-ref-unor';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Pull referensi ref unor to database from endpoint on SIASN Simpeg API';

/**
* Execute the console command.
*/
public function handle()
{
try {
$response = Simpeg::getReferensiRefUnor();
} catch (\Exception $e) {
$this->error($e);
$this->newLine();

return self::FAILURE;
}

if ($response->count()) {
try {
$bar = $this->output->createProgressBar($response->count());
$bar->start();

$model = new ReferensiRefUnor;

DB::transaction(function () use ($model, $response, $bar) {
if (config('siasn-simpeg.delete_model_before_pull')) {
$model->delete();
}

$response->chunk(config('siasn-simpeg.chunk_data'))->each(function ($item) use ($model, $bar) {
$model->upsert($item->toArray(), 'id');
$model->withTrashed()
->whereIn('id', $item->pluck('id'))
->restore();

$bar->advance($item->count());
});
});

$bar->finish();

$this->newLine(2);
} catch (\Exception $e) {
$this->error($e);
$this->newLine();

return self::FAILURE;
}
}

return self::SUCCESS;
}
}

0 comments on commit e0a655b

Please sign in to comment.