Skip to content

Commit ec8c0da

Browse files
authored
Merge pull request #577 from turtle0x1/software-asset-management
Draft: Software asset management
2 parents 720df01 + 6541b33 commit ec8c0da

16 files changed

+1008
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Phinx\Migration\AbstractMigration;
6+
7+
final class SoftwareInventory extends AbstractMigration
8+
{
9+
public function change(): void
10+
{
11+
if ($this->isMigratingUp()) {
12+
// Add new settings
13+
$this->execute("INSERT INTO `Instance_Settings`(`IS_ID`, `IS_Name`) VALUES (14, 'Software Assets Snapshot')");
14+
$this->execute("INSERT INTO `Instance_Settings`(`IS_ID`, `IS_Name`) VALUES (15, 'Software Assets Snapshot Days Duration')");
15+
16+
// Dont enable software asset monitoring by default & clear software asset management monitoring logs after 7 days
17+
$this->execute("INSERT INTO `Instance_Settings_Values`(`ISV_IS_ID`, `ISV_Value`) VALUES (14, '0')");
18+
$this->execute("INSERT INTO `Instance_Settings_Values`(`ISV_IS_ID`, `ISV_Value`) VALUES (15, '7')");
19+
20+
// Create table to store software assets monitor results
21+
$this->table('Software_Assets_Snapshots', ['id' => "SAS_ID", 'primary_key' => ["SAS_ID"]])
22+
->addColumn('SAS_Last_Updated', 'datetime', ['default' => 'CURRENT_TIMESTAMP'])
23+
->addColumn('SAS_Date', 'date', ['null' => false])
24+
->addColumn('SAS_Data', 'json', ['null' => false])
25+
->addIndex(['SAS_Date'], ['unique' => true, 'name' => 'unique_software_assets_monitor'])
26+
->create();
27+
}
28+
}
29+
}

src/classes/Constants/InstanceSettingsKeys.php

+2
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ class InstanceSettingsKeys
1616
const TIMEZONE = 11;
1717
const BACKUP_HISTORY = 12;
1818
const INSTANCE_METRIC_HISTORY = 13;
19+
const SOFTWARE_INVENTORY_MONITOR = 14;
20+
const SOFTWARE_INVENTORY_MONITOR_DAYS_DURATION = 15;
1921
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace dhope0000\LXDClient\Controllers\Hosts\SoftwareAssets;
4+
5+
use dhope0000\LXDClient\Model\Users\FetchUserDetails;
6+
use dhope0000\LXDClient\Tools\Hosts\SoftwareAssets\GetSnapshotSoftwareList;
7+
8+
class GetSnapshotSoftwareListController
9+
{
10+
private $fetchUserDetails;
11+
private $getSnapshotSoftwareList;
12+
13+
public function __construct(
14+
FetchUserDetails $fetchUserDetails,
15+
GetSnapshotSoftwareList $getSnapshotSoftwareList
16+
) {
17+
$this->fetchUserDetails = $fetchUserDetails;
18+
$this->getSnapshotSoftwareList = $getSnapshotSoftwareList;
19+
}
20+
21+
public function get(int $userId, string $date)
22+
{
23+
$isAdmin = $this->fetchUserDetails->isAdmin($userId) === '1';
24+
if (!$isAdmin) {
25+
throw new \Exception("No access", 1);
26+
}
27+
$date = new \DateTimeImmutable($date);
28+
return $this->getSnapshotSoftwareList->get($date);
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace dhope0000\LXDClient\Controllers\Hosts\SoftwareAssets;
4+
5+
use dhope0000\LXDClient\Model\Users\FetchUserDetails;
6+
use dhope0000\LXDClient\Tools\Hosts\SoftwareAssets\GetSoftwareSnapshotOverview;
7+
8+
class GetSoftwareAssetsOverviewController
9+
{
10+
private $fetchUserDetails;
11+
private $getSoftwareSnapshotOverview;
12+
13+
public function __construct(
14+
FetchUserDetails $fetchUserDetails,
15+
GetSoftwareSnapshotOverview $getSoftwareSnapshotOverview
16+
) {
17+
$this->fetchUserDetails = $fetchUserDetails;
18+
$this->getSoftwareSnapshotOverview = $getSoftwareSnapshotOverview;
19+
}
20+
21+
public function get(int $userId, string $date)
22+
{
23+
$isAdmin = $this->fetchUserDetails->isAdmin($userId) === '1';
24+
if (!$isAdmin) {
25+
throw new \Exception("No access", 1);
26+
}
27+
$date = new \DateTimeImmutable($date);
28+
return $this->getSoftwareSnapshotOverview->get($date);
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace dhope0000\LXDClient\Controllers\Hosts\SoftwareAssets;
4+
5+
use dhope0000\LXDClient\Model\Users\FetchUserDetails;
6+
use dhope0000\LXDClient\Model\Hosts\SoftwareAssets\FetchSoftwareAssetSnapshots;
7+
8+
class GetSoftwareAsssetsHeadersController
9+
{
10+
private $fetchUserDetails;
11+
private $fetchSoftwareAssetSnapshots;
12+
13+
public function __construct(
14+
FetchUserDetails $fetchUserDetails,
15+
FetchSoftwareAssetSnapshots $fetchSoftwareAssetSnapshots
16+
) {
17+
$this->fetchUserDetails = $fetchUserDetails;
18+
$this->fetchSoftwareAssetSnapshots = $fetchSoftwareAssetSnapshots;
19+
}
20+
21+
public function get(int $userId)
22+
{
23+
$isAdmin = $this->fetchUserDetails->isAdmin($userId) === '1';
24+
if (!$isAdmin) {
25+
throw new \Exception("No access", 1);
26+
}
27+
return $this->fetchSoftwareAssetSnapshots->fetchLastSevenHeaders();
28+
}
29+
}

src/classes/Model/Hosts/GetDetails.php

+16
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ public function fetchAlias(int $hostId)
4848
return $do->fetchColumn();
4949
}
5050

51+
public function fetchAliases(array $hostIds)
52+
{
53+
$qMarks = join(',', array_fill(0, count($hostIds), '?'));
54+
$sql = "SELECT
55+
`Host_ID`,
56+
COALESCE(`Host_Alias`, `Host_Url_And_Port`)
57+
FROM
58+
`Hosts`
59+
WHERE
60+
`Host_ID` IN ($qMarks)
61+
";
62+
$do = $this->database->prepare($sql);
63+
$do->execute($hostIds);
64+
return $do->fetchAll(\PDO::FETCH_KEY_PAIR);
65+
}
66+
5167
public function fetchHost($hostId)
5268
{
5369
$sql = "SELECT
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace dhope0000\LXDClient\Model\Hosts\SoftwareAssets;
4+
5+
use dhope0000\LXDClient\Model\Database\Database;
6+
7+
class FetchSoftwareAssetSnapshots
8+
{
9+
private $database;
10+
11+
public function __construct(Database $database)
12+
{
13+
$this->database = $database->dbObject;
14+
}
15+
16+
public function fetchForDate(\DateTimeImmutable $date)
17+
{
18+
$sql = "SELECT
19+
JSON_UNQUOTE(`SAS_Data`) as `data`
20+
FROM
21+
`Software_Assets_Snapshots`
22+
WHERE
23+
`SAS_Date` = :date
24+
";
25+
$do = $this->database->prepare($sql);
26+
$do->execute([
27+
":date" => $date->format("Y-m-d")
28+
]);
29+
return $do->fetch(\PDO::FETCH_ASSOC);
30+
}
31+
32+
public function fetchLastSevenHeaders()
33+
{
34+
$sql = "SELECT
35+
`SAS_ID` as `id`,
36+
`SAS_Date` as `date`,
37+
`SAS_Last_Updated` as `lastUpdate`
38+
FROM
39+
`Software_Assets_Snapshots`
40+
ORDER BY
41+
`SAS_Date` DESC
42+
LIMIT 7
43+
";
44+
return $this->database->query($sql)->fetchAll(\PDO::FETCH_ASSOC);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace dhope0000\LXDClient\Model\Hosts\SoftwareAssets;
4+
5+
use dhope0000\LXDClient\Model\Database\Database;
6+
7+
class InsertSoftwareAssetsSnapshot
8+
{
9+
private $database;
10+
11+
public function __construct(Database $database)
12+
{
13+
$this->database = $database->dbObject;
14+
}
15+
16+
public function insert(\DateTimeImmutable $date, array $data)
17+
{
18+
$sql = "INSERT INTO `Software_Assets_Snapshots`
19+
(
20+
`SAS_Last_Updated`,
21+
`SAS_Date`,
22+
`SAS_Data`
23+
) VALUES (
24+
CURRENT_TIMESTAMP(),
25+
:date,
26+
:data
27+
) ON DUPLICATE KEY UPDATE
28+
`SAS_Last_Updated` = CURRENT_TIMESTAMP(),
29+
`SAS_Data` = :data;
30+
";
31+
$do = $this->database->prepare($sql);
32+
$do->execute([
33+
":date"=>$date->format("Y-m-d"),
34+
":data"=>json_encode($data),
35+
]);
36+
return $do->rowCount() ? true : false;
37+
}
38+
}
39+
40+
41+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace dhope0000\LXDClient\Tools\Hosts\SoftwareAssets;
4+
5+
use dhope0000\LXDClient\Model\Hosts\SoftwareAssets\FetchSoftwareAssetSnapshots;
6+
use dhope0000\LXDClient\Model\Hosts\GetDetails;
7+
8+
class GetSnapshotSoftwareList
9+
{
10+
private $fetchSoftwareAssetSnapshots;
11+
private $getDetails;
12+
13+
public function __construct(
14+
FetchSoftwareAssetSnapshots $fetchSoftwareAssetSnapshots,
15+
GetDetails $getDetails
16+
) {
17+
$this->fetchSoftwareAssetSnapshots = $fetchSoftwareAssetSnapshots;
18+
$this->getDetails = $getDetails;
19+
}
20+
21+
public function get(\DateTimeImmutable $date)
22+
{
23+
$snapshot = $this->fetchSoftwareAssetSnapshots->fetchForDate($date);
24+
25+
if (empty($snapshot)) {
26+
return [];
27+
}
28+
29+
$snapshot = json_decode($snapshot["data"], true);
30+
31+
$output = [];
32+
33+
if (empty($snapshot)) {
34+
return $output;
35+
}
36+
37+
$hostAliases = $this->getDetails->fetchAliases(array_keys($snapshot));
38+
39+
foreach ($snapshot as $hostId => $projects) {
40+
foreach ($projects as $project => $instances) {
41+
foreach ($instances as $instance => $packages) {
42+
foreach ($packages as $package) {
43+
$output[] = array_merge([
44+
"hostName" => $hostAliases[$hostId],
45+
"project" => $project,
46+
"instance" => $instance
47+
], $package);
48+
}
49+
}
50+
}
51+
}
52+
return $output;
53+
}
54+
}

0 commit comments

Comments
 (0)