Skip to content

Commit

Permalink
Merge pull request #577 from turtle0x1/software-asset-management
Browse files Browse the repository at this point in the history
Draft: Software asset management
  • Loading branch information
turtle0x1 authored Sep 28, 2024
2 parents 720df01 + 6541b33 commit ec8c0da
Show file tree
Hide file tree
Showing 16 changed files with 1,008 additions and 1 deletion.
29 changes: 29 additions & 0 deletions db/migrations/20240915221912_software_inventory.php
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();
}
}
}
2 changes: 2 additions & 0 deletions src/classes/Constants/InstanceSettingsKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ class InstanceSettingsKeys
const TIMEZONE = 11;
const BACKUP_HISTORY = 12;
const INSTANCE_METRIC_HISTORY = 13;
const SOFTWARE_INVENTORY_MONITOR = 14;
const SOFTWARE_INVENTORY_MONITOR_DAYS_DURATION = 15;
}
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);
}
}
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);
}
}
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();
}
}
16 changes: 16 additions & 0 deletions src/classes/Model/Hosts/GetDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ public function fetchAlias(int $hostId)
return $do->fetchColumn();
}

public function fetchAliases(array $hostIds)
{
$qMarks = join(',', array_fill(0, count($hostIds), '?'));
$sql = "SELECT
`Host_ID`,
COALESCE(`Host_Alias`, `Host_Url_And_Port`)
FROM
`Hosts`
WHERE
`Host_ID` IN ($qMarks)
";
$do = $this->database->prepare($sql);
$do->execute($hostIds);
return $do->fetchAll(\PDO::FETCH_KEY_PAIR);
}

public function fetchHost($hostId)
{
$sql = "SELECT
Expand Down
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);
}
}
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 src/classes/Tools/Hosts/SoftwareAssets/GetSnapshotSoftwareList.php
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;
}
}
Loading

0 comments on commit ec8c0da

Please sign in to comment.