refactor(values): created hierarchical 'machine' object

main
steven 2021-02-11 20:02:07 -05:00
parent 2b7caece6c
commit e517d6a82d
3 changed files with 56 additions and 50 deletions

View File

@ -1,4 +1,5 @@
<?php <?php
/* process functions */
function getByteString($bytes) { function getByteString($bytes) {
$symbol = array('B', 'KB', 'MB', 'GB', 'TB'); $symbol = array('B', 'KB', 'MB', 'GB', 'TB');
$exp = ($bytes ? floor(log($bytes)/log(1024)) : 0); $exp = ($bytes ? floor(log($bytes)/log(1024)) : 0);
@ -19,45 +20,40 @@
} }
function getDriveValues($drives) { function getDriveValues($drives) {
foreach ($drives as $drive) { foreach ($drives as $drive) {
array_push( $drive->spaceFree = disk_free_space($drive->location);
$drive, $drive->spaceTotal = disk_total_space($drive->location);
disk_total_space($drive[1]), $drive->spaceUsed = getDifference(
disk_free_space($drive[1]) $drive->spaceFree, $drive->spaceTotal);
); $drive->spacePercentageUsed = getPercentageUsed(
array_push( $drive->spaceTotal, $drive->spaceUsed);
$drive,
getDifference($drive[3], $drive[4])
);
array_push(
$drive,
getPercentageUsed($drive[3], $drive[5])
);
} }
return $drives; return $drives;
} }
/* display functions */
function printDrive($drive) { function printDrive($drive) {
echo ' echo '
<div class="drive"> <div class="drive">
<h3>' . $drive[0] . '</h3> <h3>' . $drive->name . '</h3>
<svg class="chart"> <svg class="chart">
<circle class="pie" /> <circle class="pie" />
<circle <circle
id="' . $drive[0] . '" id="' . $drive->name . '"
class="piece" class="piece"
stroke-dasharray="' . ($drive[5] ? stroke-dasharray="' . ($drive->spacePercentageUsed ?
$drive[5] : 0) * 31.4 / 100 . ' 31.4" $drive->spacePercentageUsed : 0) * 31.4 / 100 . ' 31.4"
/> />
</svg> </svg>
<h5>Free Space: <em>' . getByteString($drive[3]) . '</em></h5> <h5>Free Space: <em>' . getByteString($drive->spaceFree) . '</em></h5>
<h5>Used Space: <em>' . getByteString($drive[4]) . '</em></h5> <h5>Used Space: <em>' . getByteString($drive->spaceUsed) . '</em></h5>
</div>'; </div>';
} }
function printService($service) { function printService($service) {
echo ' echo '
<a href="' . $service[1] . '"> <a href="' . $service->location . '">
<div class="service" id="' . $service[0] . '"> <div class="service" id="' . $service->name . '">
<h3>' . $service[0] . '</h3> <h3>' . $service->name . '</h3>
<h4>' . $service[2] . '</h4> <h4>' . $service->description . '</h4>
</div> </div>
</a>'; </a>';
} }

View File

@ -6,7 +6,7 @@
require 'values.php'; require 'values.php';
require 'functions.php'; require 'functions.php';
$driveValues = getDriveValues($drives); $driveValues = getDriveValues($machine->drives);
?> ?>
<title><?php echo $machine[0] ?> - homo.casa</title> <title><?php echo $machine[0] ?> - homo.casa</title>
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"> <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
@ -24,13 +24,13 @@
<body> <body>
<header> <header>
<?php echo '<h1>' . $machine[0] . '</h1> <?php echo '<h1>' . $machine->name . '</h1>
<p id="' . $machine[0] . '">' . $machine[1] . '</p>'; ?> <p id="' . $machine->name . '">' . $machine->description . '</p>'; ?>
</header> </header>
<div id="drives"> <div id="drives">
<?php <?php
echo "<h2>" . $driveLabel . "</h2>"; echo "<h2>" . $machine->driveLabel . "</h2>";
foreach ($driveValues as $drive) { foreach ($driveValues as $drive) {
printDrive($drive); printDrive($drive);
@ -40,17 +40,17 @@
<div id="services"> <div id="services">
<?php <?php
echo "<h2>" . $serviceLabel . "</h2> echo "<h2>" . $machine->serviceLabel . "</h2>
<p>note: authentication required for access.</p>"; <p>note: authentication required for access.</p>";
foreach ($services as $service) { foreach ($machine->services as $service) {
printService($service); printService($service);
} }
?> ?>
</div> </div>
<footer> <footer>
<?php echo '<p id="' . $footer[0] . '">' . $footer[1] . ' <?php echo '<p id="' . $machine->footer->name . '">' . $machine->footer->description . '
</p>';?> </p>';?>
</footer> </footer>

View File

@ -1,22 +1,32 @@
<?php <?php
$machine = array( $machine->name = "Machine";
"Machine", $machine->description = "machine description";
"machine description"
); $disk1->name = "disk1";
$driveLabel = "Drives"; $disk1->description = "disk1 description";
$drives = array( $disk1->location = "/disk1/location/";
array("disk1", "/disk1/location/", "disk1 description"), $disk2->name = "disk2";
array("disk2", "/disk2/location/", "disk2 description"), $disk2->description = "disk2 description";
array("disk3", "/disk3/location/", "disk3 description") $disk2->location = "/disk2/location/";
); $disk3->name = "disk3";
$serviceLabel = "Services"; $disk3->description = "disk3 description";
$services = array( $disk3->location = "/disk3/location/";
array("service1", "https://service1.example.com", "service1 description"),
array("service2", "https://service2.example.com", "service2 description"), $service1->name = "service1";
array("service3", "https://service3.example.com", "service3 description") $service1->description = "service1 description";
); $service1->location = "https://service1.example.com";
$footer = array( $service2->name = "service2";
"footer", $service2->description = "service2 description";
"footer description" $service2->location = "https://service2.example.com";
); $service3->name = "service3";
$service3->description = "service3 description";
$service3->location = "https://service3.example.com";
$machine->driveLabel = "Drives";
$machine->drives = array($disk1, $disk2, $disk3);
$machine->serviceLabel = "Services";
$machine->services = array($service1, $service2, $service3);
$machine->footer->name = "footer";
$machine->footer->description = "footer description";
?> ?>