-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #577 from turtle0x1/software-asset-management
Draft: Software asset management
- Loading branch information
Showing
16 changed files
with
1,008 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Phinx\Migration\AbstractMigration; | ||
|
||
final class SoftwareInventory extends AbstractMigration | ||
{ | ||
public function change(): void | ||
{ | ||
if ($this->isMigratingUp()) { | ||
// Add new settings | ||
$this->execute("INSERT INTO `Instance_Settings`(`IS_ID`, `IS_Name`) VALUES (14, 'Software Assets Snapshot')"); | ||
$this->execute("INSERT INTO `Instance_Settings`(`IS_ID`, `IS_Name`) VALUES (15, 'Software Assets Snapshot Days Duration')"); | ||
|
||
// Dont enable software asset monitoring by default & clear software asset management monitoring logs after 7 days | ||
$this->execute("INSERT INTO `Instance_Settings_Values`(`ISV_IS_ID`, `ISV_Value`) VALUES (14, '0')"); | ||
$this->execute("INSERT INTO `Instance_Settings_Values`(`ISV_IS_ID`, `ISV_Value`) VALUES (15, '7')"); | ||
|
||
// Create table to store software assets monitor results | ||
$this->table('Software_Assets_Snapshots', ['id' => "SAS_ID", 'primary_key' => ["SAS_ID"]]) | ||
->addColumn('SAS_Last_Updated', 'datetime', ['default' => 'CURRENT_TIMESTAMP']) | ||
->addColumn('SAS_Date', 'date', ['null' => false]) | ||
->addColumn('SAS_Data', 'json', ['null' => false]) | ||
->addIndex(['SAS_Date'], ['unique' => true, 'name' => 'unique_software_assets_monitor']) | ||
->create(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/classes/Controllers/Hosts/SoftwareAssets/GetSnapshotSoftwareListController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace dhope0000\LXDClient\Controllers\Hosts\SoftwareAssets; | ||
|
||
use dhope0000\LXDClient\Model\Users\FetchUserDetails; | ||
use dhope0000\LXDClient\Tools\Hosts\SoftwareAssets\GetSnapshotSoftwareList; | ||
|
||
class GetSnapshotSoftwareListController | ||
{ | ||
private $fetchUserDetails; | ||
private $getSnapshotSoftwareList; | ||
|
||
public function __construct( | ||
FetchUserDetails $fetchUserDetails, | ||
GetSnapshotSoftwareList $getSnapshotSoftwareList | ||
) { | ||
$this->fetchUserDetails = $fetchUserDetails; | ||
$this->getSnapshotSoftwareList = $getSnapshotSoftwareList; | ||
} | ||
|
||
public function get(int $userId, string $date) | ||
{ | ||
$isAdmin = $this->fetchUserDetails->isAdmin($userId) === '1'; | ||
if (!$isAdmin) { | ||
throw new \Exception("No access", 1); | ||
} | ||
$date = new \DateTimeImmutable($date); | ||
return $this->getSnapshotSoftwareList->get($date); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/classes/Controllers/Hosts/SoftwareAssets/GetSoftwareAssetsOverviewController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace dhope0000\LXDClient\Controllers\Hosts\SoftwareAssets; | ||
|
||
use dhope0000\LXDClient\Model\Users\FetchUserDetails; | ||
use dhope0000\LXDClient\Tools\Hosts\SoftwareAssets\GetSoftwareSnapshotOverview; | ||
|
||
class GetSoftwareAssetsOverviewController | ||
{ | ||
private $fetchUserDetails; | ||
private $getSoftwareSnapshotOverview; | ||
|
||
public function __construct( | ||
FetchUserDetails $fetchUserDetails, | ||
GetSoftwareSnapshotOverview $getSoftwareSnapshotOverview | ||
) { | ||
$this->fetchUserDetails = $fetchUserDetails; | ||
$this->getSoftwareSnapshotOverview = $getSoftwareSnapshotOverview; | ||
} | ||
|
||
public function get(int $userId, string $date) | ||
{ | ||
$isAdmin = $this->fetchUserDetails->isAdmin($userId) === '1'; | ||
if (!$isAdmin) { | ||
throw new \Exception("No access", 1); | ||
} | ||
$date = new \DateTimeImmutable($date); | ||
return $this->getSoftwareSnapshotOverview->get($date); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/classes/Controllers/Hosts/SoftwareAssets/GetSoftwareAsssetsHeadersController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace dhope0000\LXDClient\Controllers\Hosts\SoftwareAssets; | ||
|
||
use dhope0000\LXDClient\Model\Users\FetchUserDetails; | ||
use dhope0000\LXDClient\Model\Hosts\SoftwareAssets\FetchSoftwareAssetSnapshots; | ||
|
||
class GetSoftwareAsssetsHeadersController | ||
{ | ||
private $fetchUserDetails; | ||
private $fetchSoftwareAssetSnapshots; | ||
|
||
public function __construct( | ||
FetchUserDetails $fetchUserDetails, | ||
FetchSoftwareAssetSnapshots $fetchSoftwareAssetSnapshots | ||
) { | ||
$this->fetchUserDetails = $fetchUserDetails; | ||
$this->fetchSoftwareAssetSnapshots = $fetchSoftwareAssetSnapshots; | ||
} | ||
|
||
public function get(int $userId) | ||
{ | ||
$isAdmin = $this->fetchUserDetails->isAdmin($userId) === '1'; | ||
if (!$isAdmin) { | ||
throw new \Exception("No access", 1); | ||
} | ||
return $this->fetchSoftwareAssetSnapshots->fetchLastSevenHeaders(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/classes/Model/Hosts/SoftwareAssets/FetchSoftwareAssetSnapshots.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace dhope0000\LXDClient\Model\Hosts\SoftwareAssets; | ||
|
||
use dhope0000\LXDClient\Model\Database\Database; | ||
|
||
class FetchSoftwareAssetSnapshots | ||
{ | ||
private $database; | ||
|
||
public function __construct(Database $database) | ||
{ | ||
$this->database = $database->dbObject; | ||
} | ||
|
||
public function fetchForDate(\DateTimeImmutable $date) | ||
{ | ||
$sql = "SELECT | ||
JSON_UNQUOTE(`SAS_Data`) as `data` | ||
FROM | ||
`Software_Assets_Snapshots` | ||
WHERE | ||
`SAS_Date` = :date | ||
"; | ||
$do = $this->database->prepare($sql); | ||
$do->execute([ | ||
":date" => $date->format("Y-m-d") | ||
]); | ||
return $do->fetch(\PDO::FETCH_ASSOC); | ||
} | ||
|
||
public function fetchLastSevenHeaders() | ||
{ | ||
$sql = "SELECT | ||
`SAS_ID` as `id`, | ||
`SAS_Date` as `date`, | ||
`SAS_Last_Updated` as `lastUpdate` | ||
FROM | ||
`Software_Assets_Snapshots` | ||
ORDER BY | ||
`SAS_Date` DESC | ||
LIMIT 7 | ||
"; | ||
return $this->database->query($sql)->fetchAll(\PDO::FETCH_ASSOC); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/classes/Model/Hosts/SoftwareAssets/InsertSoftwareAssetsSnapshot.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace dhope0000\LXDClient\Model\Hosts\SoftwareAssets; | ||
|
||
use dhope0000\LXDClient\Model\Database\Database; | ||
|
||
class InsertSoftwareAssetsSnapshot | ||
{ | ||
private $database; | ||
|
||
public function __construct(Database $database) | ||
{ | ||
$this->database = $database->dbObject; | ||
} | ||
|
||
public function insert(\DateTimeImmutable $date, array $data) | ||
{ | ||
$sql = "INSERT INTO `Software_Assets_Snapshots` | ||
( | ||
`SAS_Last_Updated`, | ||
`SAS_Date`, | ||
`SAS_Data` | ||
) VALUES ( | ||
CURRENT_TIMESTAMP(), | ||
:date, | ||
:data | ||
) ON DUPLICATE KEY UPDATE | ||
`SAS_Last_Updated` = CURRENT_TIMESTAMP(), | ||
`SAS_Data` = :data; | ||
"; | ||
$do = $this->database->prepare($sql); | ||
$do->execute([ | ||
":date"=>$date->format("Y-m-d"), | ||
":data"=>json_encode($data), | ||
]); | ||
return $do->rowCount() ? true : false; | ||
} | ||
} | ||
|
||
|
||
|
54 changes: 54 additions & 0 deletions
54
src/classes/Tools/Hosts/SoftwareAssets/GetSnapshotSoftwareList.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace dhope0000\LXDClient\Tools\Hosts\SoftwareAssets; | ||
|
||
use dhope0000\LXDClient\Model\Hosts\SoftwareAssets\FetchSoftwareAssetSnapshots; | ||
use dhope0000\LXDClient\Model\Hosts\GetDetails; | ||
|
||
class GetSnapshotSoftwareList | ||
{ | ||
private $fetchSoftwareAssetSnapshots; | ||
private $getDetails; | ||
|
||
public function __construct( | ||
FetchSoftwareAssetSnapshots $fetchSoftwareAssetSnapshots, | ||
GetDetails $getDetails | ||
) { | ||
$this->fetchSoftwareAssetSnapshots = $fetchSoftwareAssetSnapshots; | ||
$this->getDetails = $getDetails; | ||
} | ||
|
||
public function get(\DateTimeImmutable $date) | ||
{ | ||
$snapshot = $this->fetchSoftwareAssetSnapshots->fetchForDate($date); | ||
|
||
if (empty($snapshot)) { | ||
return []; | ||
} | ||
|
||
$snapshot = json_decode($snapshot["data"], true); | ||
|
||
$output = []; | ||
|
||
if (empty($snapshot)) { | ||
return $output; | ||
} | ||
|
||
$hostAliases = $this->getDetails->fetchAliases(array_keys($snapshot)); | ||
|
||
foreach ($snapshot as $hostId => $projects) { | ||
foreach ($projects as $project => $instances) { | ||
foreach ($instances as $instance => $packages) { | ||
foreach ($packages as $package) { | ||
$output[] = array_merge([ | ||
"hostName" => $hostAliases[$hostId], | ||
"project" => $project, | ||
"instance" => $instance | ||
], $package); | ||
} | ||
} | ||
} | ||
} | ||
return $output; | ||
} | ||
} |
Oops, something went wrong.