diff --git a/app/Jobs/Commands/MiningTaxes/FetchMiningTaxesLedgersJob.php b/app/Jobs/Commands/MiningTaxes/FetchMiningTaxesLedgersJob.php index 5935154a5..e89dbbe98 100644 --- a/app/Jobs/Commands/MiningTaxes/FetchMiningTaxesLedgersJob.php +++ b/app/Jobs/Commands/MiningTaxes/FetchMiningTaxesLedgersJob.php @@ -2,24 +2,74 @@ namespace App\Jobs; +//Internal Library use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use Carbon\Carbon; + +//App Library +use Seat\Eseye\Exceptions\RequestionFailedException; +use App\Library\Esi\Esi; +use App\Library\Lookups\LookupHelper; + +//App Models +use App\Models\MiningTax\Observer; +use App\Models\MiningTax\Ledger; class FetchMiningTaxesLedgersJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + /** + * Timeout in seconds + * + * @var int + */ + public $timeout = 3600; + + /** + * Number of job retries + * + * @var int + */ + public $tries = 3; + + /** + * Job Variables + */ + private $charId; + private $corpId; + private $observerId; + private $refreshToken; + private $esi; + /** * Create a new job instance. * * @return void */ - public function __construct() + public function __construct($corpId, $observerId) { - // + //Set the connection for the job + $this->connection = 'redis'; + + //Import the variables from the calling function + $this->charId = $charId; + $this->corpId = $corpId; + $this->observerId = $observerId; + + $this->$esi = new Esi; + + //Setup the private esi variables + if(!$this->esi->haveEsiScope($this->charId, 'esi-industry.read_corporation_mining.v1')) { + Log::critical('Character: ' . $this->charId . ' did not have the correct esi scope in FetchMiningTaxesLedgersJob.'); + return null; + } + $this->refreshToken = $this->esi->GetRefreshToken($this->charId); + $this->esi = $this->esi->SetupEsiAuthentication($this->refreshToken); } /** @@ -29,6 +79,50 @@ class FetchMiningTaxesLedgersJob implements ShouldQueue */ public function handle() { - // + //Declare variables + $lookup = new LookupHelper; + $final = array(); + $items = array(); + $notSorted = array(); + + //Get the ledger from ESI + try { + $ledgers = $this->esi->invoke('get', '/corporation/{corporation_id}/mining/observers/{observer_id}/', [ + 'corporation_id' => $this->corpId, + 'observer_id' => $this->observerId, + ]); + } catch(RequestFailedException $e) { + Log::warning('Failed to get the mining ledger in FetchMiningTaxesLedgersJob for observer id: ' . $this->observerId); + return null; + } + + //Sort through the array, and create the variables needed for database entries + foreach($ledgers as $ledger) { + //Get some basic information we need to work with + $charName = $lookup->CharacterIdToName($ledger->character_id); + $typeName = $lookup->ItemIdToName($ledger->ore); + $updated = $this->esi->DecodeDate($ledger->last_updated); + + //Insert or update the entry in the database + $item = Ledger::updateOrCreate([ + 'character_id' => $ledger->character_id, + 'character_name' => $charName, + 'last_updated' => $updated, + 'type_id' => $ledger->type_id, + 'ore_name' => $typeName, + 'quantity' => $ledger->quantity, + ], [ + 'character_id' => $ledger->character_id, + 'character_name' => $charName, + 'last_updated' => $updated, + 'type_id' => $ledger->type_id, + 'ore_name' => $typeName, + 'quantity' => $ledger->quantity, + ]); + } + + //Clean up old data + Ledger::where(['updated_at', '<', Carbon::now()->subDays(120)])->delete(); + } } diff --git a/app/Library/Assets/AssetHelper.php b/app/Library/Assets/AssetHelper.php index 707b66f84..2851d1537 100644 --- a/app/Library/Assets/AssetHelper.php +++ b/app/Library/Assets/AssetHelper.php @@ -64,37 +64,25 @@ class AssetHelper { * Store a new asset record in the database */ public function StoreNewAsset($asset) { - //See if we find any assets which already exist - $found = Asset::where([ + Asset::updateOrCreate([ 'item_id' => $asset->item_id, - ])->count(); - - //If nothing is found - if($found == 0) { - $item = new Asset; - if(isset($asset->is_blueprint_copy)) { - $item->is_blueprint_copy = $asset->is_blueprint_copy; - } - $item->is_singleton = $asset->is_singleton; - $item->item_id = $asset->item_id; - $item->location_flag = $asset->location_flag; - $item->location_id = $asset->location_id; - $item->location_type = $asset->location_type; - $item->quantity = $asset->quantity; - $item->type_id = $asset->type_id; - $item->save(); - } else { - $this->UpdateAsset($asset); - } + ], [ + 'is_blueprint_copy' => $asset->is_blueprint_copy, + 'is_singleton' => $asset->is_singleton, + 'item_id' => $asset->item_id, + 'location_flag' => $asset->location_flag, + 'location_id' => $asset->location_id, + 'location_type' => $asset->location_type, + 'quantity' => $asset->quantity, + 'type_id' => $asset->type_id, + ]); } /** * Purge old data, so we don't run into data issues */ public function PurgeStaleData() { - $date = Carbon::now()->subDay(1); - - Asset::where('updated_at', '<', $date)->delete(); + Asset::where('updated_at', '<', Carbon::now()->subDay())->delete(); } /** @@ -120,24 +108,6 @@ class AssetHelper { return $asset['quantity']; } } - - /** - * Update an existing asset based off the esi pull - */ - private function UpdateAsset($asset) { - - Asset::where([ - 'item_id' => $asset->item_id, - ])->update([ - 'is_singleton' => $asset->is_singleton, - 'location_flag' => $asset->location_flag, - 'location_id' => $asset->location_id, - 'location_type' => $asset->location_type, - 'quantity' => $asset->quantity, - 'type_id' => $asset->type_id, - 'updated_at' => Carbon::now(), - ]); - } } ?> \ No newline at end of file diff --git a/app/Library/Esi/Esi.php b/app/Library/Esi/Esi.php index d31481dd0..0b727bac3 100644 --- a/app/Library/Esi/Esi.php +++ b/app/Library/Esi/Esi.php @@ -78,73 +78,6 @@ class Esi { return $corporation; } - public function GetCharacterName($charId) { - $esi = $this->SetupEsiAuthentication; - - try { - $character = $esi->invoke('get', '/characters/{character_id}/', [ - 'character_id' => $charId, - ]); - } catch(RequestFailedException $e) { - return null; - } - - return $character->name; - } - - public function FindCharacterId($name) { - $config = config('esi'); - - $esi = $this->SetupEsiAuthentication(); - - try { - $character = $esi->setBody(array( - $name, - ))->invoke('post', '/universe/ids/'); - } catch(RequestFailedException $e) { - return null; - } - - if(isset($character->characters[0]->id)) { - return $character->characters[0]->id; - } else { - return null; - } - } - - public function FindCorporationId($charId) { - $esi = $this->SetupEsiAuthentication(); - - try { - $character = $esi->invoke('get', '/characters/{character_id}/', [ - 'character_id' => $charId, - ]); - } catch(RequestFailedException $e) { - return null; - } - - return $character->corporation_id; - } - - public function FindCorporationName($charId) { - $esi = $this->SetupEsiAuthentication(); - - try { - $character = $esi->invoke('get', '/characters/{character_id}/', [ - 'character_id' => $charId, - ]); - - $corporation = $esi->invoke('get', '/corporations/{corporation_id}/', [ - 'corporation_id' => $character->corporation_id, - ]); - } catch(RequestFailedException $e) { - return null; - } - - - return $corporation->name; - } - public function DecodeDate($date) { //Find the end of the date $dateEnd = strpos($date, "T"); diff --git a/app/Library/Moons/MiningLedgerHelper.php b/app/Library/Moons/MiningLedgerHelper.php index 1f91c863b..90f86e7ca 100644 --- a/app/Library/Moons/MiningLedgerHelper.php +++ b/app/Library/Moons/MiningLedgerHelper.php @@ -80,6 +80,7 @@ class MiningLedgerHelper { public function GetMiningStructureInfo($observerId) { //Declare variables $esiHelper = new Esi; + $lookup = new LookupHelper; //Check for ESI scopes if(!$esiHelper->HaveEsiScope($this->charId, 'esi-universe.read_structures.v1')) { @@ -98,7 +99,7 @@ class MiningLedgerHelper { return null; } - $system = $this->GetSolarSystemName($info->solar_system_id); + $system = $lookup->SystemIdToName($info->solar_system_id); return [ 'name' => $info->name, @@ -161,7 +162,7 @@ class MiningLedgerHelper { //Sort through the array and replace character id with name and item id with name foreach($items as $item) { $charName = $lookup->CharacterIdToName($item->character_id); - $typeName = $this->GetItemName($item->type_id); + $typeName = $lookup->ItemIdToName($item->type_id); $corpName = $lookup->CorporationIdToName($item->recorded_corporation_id); if(isset($final[$charName])) { @@ -183,59 +184,5 @@ class MiningLedgerHelper { return $final; } - - /** - * Get the type id and return the name of the ore - * - * @var typeId - * @return string - */ - private function GetItemName($typeId) { - //Setup the esi helper variable - $esiHelper = new Esi; - - //Setup the authentication container for ESI - $esi = $esiHelper->SetupEsiAuthentication(); - - try { - $item = $esi->invoke('get', '/universe/types/{type_id}/', [ - 'type_id' => $typeId, - ]); - } catch(RequestFailedException $e) { - return null; - } - - return $item->name; - } - - /** - * Get the solar system name - * - * @var systemId - * @return string - */ - private function GetSolarSystemName($systemId) { - //Setup the esi helper variable - $esiHelper = new Esi; - - //Setup the authentication container for ESI - $esi = $esiHelper->SetupEsiAuthentication(); - - //Attempt to get the solar system name from ESI - try { - $solar = $esi->invoke('get', '/universe/systems/{system_id}/', [ - 'system_id' => $systemId, - ]); - } catch(RequestFailedException $e) { - $solar = null; - } - - if($solar != null) { - return $solar->name; - } else { - return null; - } - } - } ?> \ No newline at end of file diff --git a/database/migrations/2021_01_10_164334_create_mining_tax_tables.php b/database/migrations/2021_01_10_164334_create_mining_tax_tables.php index 083429f2e..059029951 100644 --- a/database/migrations/2021_01_10_164334_create_mining_tax_tables.php +++ b/database/migrations/2021_01_10_164334_create_mining_tax_tables.php @@ -42,6 +42,7 @@ class CreateMiningTaxTables extends Migration Schema::create('alliance_mining_tax_ledgers', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('character_id'); + $table->string('character_name'); $table->dateTime('last_updated'); $table->unsignedBigInteger('type_id'); $table->string('ore_name');