<?php

  global $bdnuss;
  include $_SERVER['DOCUMENT_ROOT'] . "/include/all.php";
  $json = json_decode(PostJson("$bdnuss/Storage/SVC/SVC_INVENTORY.php", ''), true);
  $poolsData = $json['Data']['Pools'];

  function formatBytes($bytes, $precision = 2): string
  {
    if ($bytes == 0)
      return '0 octets';
    $base = 1024;
    $size = $bytes / pow($base, 4);
    return round($size, $precision) . ' Tio';
  }

  $seriesData = [
    'V5100' => [
      'physical_used' => 0,
      'physical_free' => 0,
      'physical_total_api' => 0,
      'virtual_total' => 0,
      'written_total' => 0,
      'compressed_total' => 0,
      'dedup_savings' => 0
    ],
    'V5030' => [
      'physical_used' => 0,
      'physical_free' => 0,
      'physical_total_api' => 0,
      'virtual_total' => 0,
      'written_total' => 0,
      'compressed_total' => 0,
      'dedup_savings' => 0
    ],
  ];

  foreach ($poolsData as $poolName => $poolDetails) {
    $currentSeries = null;
    if (str_contains($poolName, 'V5100')) {
      $currentSeries = 'V5100';
    } elseif (str_contains($poolName, 'V5030')) {
      $currentSeries = 'V5030';
    }
    if ($currentSeries) {
      $used_capacity = (float) $poolDetails['used_capacity'];
      $free_capacity = (float) $poolDetails['free_capacity'];
      $capacity = (float) $poolDetails['capacity'];
      $virtual_cap = (float) $poolDetails['virtual_capacity'];
      $uncomp_cap = (float) $poolDetails['compression_uncompressed_capacity'];
      $comp_cap = (float) $poolDetails['compression_compressed_capacity'];
      $dedup_sav = (float) $poolDetails['deduplication_capacity_saving'];

      $seriesData[$currentSeries]['physical_used'] += $used_capacity;
      $seriesData[$currentSeries]['physical_free'] += $free_capacity;
      $seriesData[$currentSeries]['physical_total_api'] += $capacity;
      $seriesData[$currentSeries]['virtual_total'] += $virtual_cap;
      $seriesData[$currentSeries]['written_total'] += $uncomp_cap;
      $seriesData[$currentSeries]['compressed_total'] += $comp_cap;
      $seriesData[$currentSeries]['dedup_savings'] += $dedup_sav;
    }
  }

  foreach ($seriesData as $series => &$data) {
    $data['physical_total_calc'] = ($data['physical_used'] + ($data['physical_free'] / 2));
    $data['physical_used_percent'] = ($data['physical_total_calc'] > 0) ? ($data['physical_used'] / $data['physical_total_calc']) * 100 : 0;
    $data['physical_free_percent'] = 100 - $data['physical_used_percent'];
    $data['volume_written_percent'] = ($data['virtual_total'] > 0) ? ($data['written_total'] / $data['virtual_total']) * 100 : 0;
    $data['compression_savings'] = $data['written_total'] - $data['compressed_total'];
    $data['thin_savings'] = $data['virtual_total'] - $data['written_total'];
    $data['total_savings'] = $data['compression_savings'] + $data['dedup_savings'] + $data['thin_savings'];
    $data['compression_ratio'] = ($data['compressed_total'] > 0) ? ($data['written_total'] / $data['compressed_total']) : 0;
    $data['compression_savings_percent'] = ($data['written_total'] > 0) ? ($data['compression_savings'] / $data['written_total']) * 100 : 0;
    $data['dedup_savings_percent'] = ($data['written_total'] > 0) ? ($data['dedup_savings'] / $data['written_total']) * 100 : 0;
    $data['thin_savings_percent'] = ($data['virtual_total'] > 0) ? ($data['thin_savings'] / $data['virtual_total']) * 100 : 0;
    $data['total_savings_percent'] = ($data['virtual_total'] > 0) ? ($data['total_savings'] / $data['virtual_total']) * 100 : 0;
  }
  unset($data);

?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Page Title -->
  <title>Web Infra Reports</title>
  <link rel="shortcut icon" type="image/png" href="/include/favicon-32x32.png">

  <!-- JQuery -->
  <script src="/js/jquery-3.6.1.min.js"></script>
  <!-- Bootstrap -->
  <link rel="stylesheet" href="/css/bootstrap.min.css">
  <link rel="stylesheet" href="/css/bootstrap-icons/bootstrap-icons.css">
  <script src="/js/bootstrap.bundle.min.js"></script>
  <!-- Bootstrap-tables -->
  <link rel="stylesheet" href="/css/bootstrap-table.min.css">
  <script src="/js/bootstrap-table.min.js"></script>
  <script src="/js/bootstrap-table-fr-FR.min.js"></script>
  <!-- Chart -->
  <script src="/js/chart.min.js"></script>

  <style>
      .capacity-block .value {
          font-size: 1.8rem;
          font-weight: bold;
      }

      .capacity-block .percent {
          font-size: 1rem;
          font-weight: bold;
          color: #555;
      }

      .gain-item {
          display: flex;
          justify-content: space-between;
          margin-bottom: 5px;
          font-size: 0.9rem;
      }

      .gain-item span:last-child {
          font-weight: bold;
      }
  </style>


</head>

<body class="bg-light text-dark">
<div class="container-fluid">
  <div class="row flex-nowrap">
    <!-- Left NAVBAR -->
    <div class="col-auto col-md-2 col-xl-2 px-sm-2 px-0 bg-dark vh-100 position-sticky top-0" style="-ms-flex: 0 0 230px;flex: 0 0 230px;">
      <?php include $_SERVER['DOCUMENT_ROOT'] . "/navbar.html"; ?> <!-- Left Navbar -->
    </div>
    <!-- Display -->
    <div class="col py-3">
      <!-- Page Title -->
      <h1><span class="badge text-bg-secondary font-weight-bold" style="width:100%;">SVC Storage Dashboard</span></h1>

      <!-- Main content -->
      <?php
        //Gold => 5100
        //Silver => 5030

        $GB = 1024 * 1024 * 1024;
        $TB = $GB * 1024;
        //get full data
        $json = json_decode(PostJson("$bdnuss/Storage/SVC/SVC_INVENTORY.php", ''), true);

        // Sum LUN Size by Bay
        $lunsGold = 0;
        $lunsSilver = 0;
        try {
          foreach ($json['Data']['VolumeCopies'] as $data) {
            if (str_contains($data['mdisk_grp_name'], "5100")) {
              $lunsGold += $data['capacity'];
            } else {
              $lunsSilver += $data['capacity'];
            }
          }
        } catch (Exception $e) {
        }

        // Get Full Capacity
        $goldCapacity = 0;
        $silverCapacity = 0;
        $GoldCompression = 0;
        $SilverCompression = 0;
        foreach ($json['Data']['Pools'] as $data) {
          if (str_contains($data['name'], "5100")) {
            $goldCapacity += $data['capacity'];
            if ($data['used_capacity'] > 0) {
              $GoldCompression += ($data['real_capacity'] / $data['used_capacity']);
            }
          } else {
            $silverCapacity += $data['capacity'];
            if ($data['used_capacity'] > 0) {
              $SilverCompression += ($data['real_capacity'] / $data['used_capacity']);
            }
          }
        }

        // Datas
        $goldCapacity = $goldCapacity / 2;
        $silverCapacity = $silverCapacity / 2;
        $GoldFree = $goldCapacity - $lunsGold;
        $SilverFree = $silverCapacity - $lunsSilver;
        $goldPercentUsed = round($lunsGold / $goldCapacity * 100, 2);
        $silverPercentUsed = round($lunsSilver / $silverCapacity * 100, 2);
        $GoldCompression = round($GoldCompression / 2, 2);
        $SilverCompression = round($GoldCompression / 2, 2);

        // Unnasigned Luns
        $unnasigned = 0;
        $cuc = "bg-success";
        $lLUN = "";
        foreach ($json['Data']['Volumes'] as $volume) {
          if ($volume['protocol'] != "scsi") {
            $unnasigned++;
            $cuc = "bg-warning";
            $lLUN .= $volume['name'] . "<br>";
          }
        }

        // Orphan Hosts
        $orphanHosts = 0;
        $chc = "bg-success";
        $lHOST = "";
        foreach ($json['Data']['Hosts'] as $host) {
          if ($host['protocol'] != "scsi") {
            $orphanHosts++;
            $chc = "bg-warning";
            $lHOST .= $host['name'] . "<br>";
          }
        }
        if ($json['Data']['Hostclusters']) {
          foreach ($json['Data']['Hostclusters'] as $host) {
            if ($host['protocol'] != "scsi") {
              $orphanHosts++;
              $chc = "bg-warning";
              $lHOST .= $host['name'] . "<br>";
            }
          }
        }
      ?>


      <br>
      <!-- Carte GOLD -->
      <div class="card mb-3" style="border: 3px solid black">
        <div class="card-header text-dark text-center fs-3 bg-info">
          <i class="fs-4 bi-server text-black"></i><b> Gold (V5100)</b>
        </div>
        <div class="card-body bg-dark fs-4">
          <div class="row g-4">
            <?php $data = $seriesData['V5100']; ?>
            <div class="col-lg-4">
              <div class="card h-100" style="border: none">
                <div class="card-body capacity-block bg-dark">
                  <div>
                    <h5 class="card-title">Physical Capacity</h5>
                    <div class="d-flex justify-content-between align-items-start mb-2">
                      <div>
                        <span class="percent"><?= number_format($data['physical_used_percent']) ?>%</span>
                        <div class="value"><?= formatBytes($data['physical_used']) ?></div>
                      </div>
                      <div class="text-end">
                        <span class="percent"><?= number_format($data['physical_free_percent']) ?>%</span>
                        <div class="value"><?= formatBytes($data['physical_free'] / 2) ?></div>
                      </div>
                    </div>
                    <div class="progress" style="height:30px;">
                      <div class="progress-bar" role="progressbar"
                           style="width: <?= $data['physical_used_percent'] ?>%;"
                           aria-valuenow="<?= $data['physical_used_percent'] ?>" aria-valuemin="0" aria-valuemax="100">
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>

            <div class="col-lg-4">
              <div class="card h-100" style="border: none">
                <div class="card-body capacity-block bg-dark">
                  <div>
                    <h5 class="card-title">Volume Capacity</h5>
                    <div class="d-flex justify-content-between align-items-start mb-2">
                      <div>
                        <span class="percent"><?= number_format($data['volume_written_percent']) ?>%</span>
                        <div class="value"><?= formatBytes($data['written_total']) ?></div>
                      </div>
                    </div>
                    <div class="progress" style="height: 30px;">
                      <div class="progress-bar" role="progressbar"
                           style="width: <?= $data['volume_written_percent'] ?>%;"
                           aria-valuenow="<?= $data['volume_written_percent'] ?>" aria-valuemin="0" aria-valuemax="100">
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>

            <div class="col-lg-4">
              <div class="card h-100" style="border: none">
                <div class="card-body capacity-block bg-dark">
                  <div>
                    <h5 class="card-title">Capacity gain</h5>
                    <div class="gain-item">
                      <span><?= number_format($data['compression_savings_percent']) ?>% Compression</span>
                      <span><?= formatBytes($data['compression_savings']) ?></span>
                    </div>
                    <div class="gain-item">
                      <span><?= number_format($data['thin_savings_percent']) ?>% Thin provisioning</span>
                      <span><?= formatBytes($data['thin_savings']) ?></span>
                    </div>
                    <hr>
                    <div class="gain-item">
                      <span>Compression</span>
                      <span><?= number_format($data['compression_ratio'], 1) ?>:1</span>
                    </div>
                  </div>

                </div>
              </div>
            </div>

          </div>
        </div>
      </div>
      <br>
      <!-- Carte Silver -->
      <div class="card mb-3" style="border: 3px solid black">
        <div class="card-header text-dark text-center fs-3 bg-info">
          <i class="fs-4 bi-server text-black"></i><b> Silver (V5030)</b>
        </div>
        <div class="card-body bg-dark fs-4">
          <div class="row g-4">
            <?php $data = $seriesData['V5030']; ?>
            <div class="col-lg-4">
              <div class="card h-100" style="border: none">
                <div class="card-body capacity-block bg-dark">
                  <div>
                    <h5 class="card-title">Physical Capacity</h5>
                    <div class="d-flex justify-content-between align-items-start mb-2">
                      <div>
                        <span class="percent"><?= number_format($data['physical_used_percent']) ?>%</span>
                        <div class="value"><?= formatBytes($data['physical_used']) ?></div>
                      </div>
                      <div class="text-end">
                        <span class="percent"><?= number_format($data['physical_free_percent']) ?>%</span>
                        <div class="value"><?= formatBytes($data['physical_free'] / 2) ?></div>
                      </div>
                    </div>
                    <div class="progress" style="height: 30px;">
                      <div class="progress-bar" role="progressbar"
                           style="width: <?= $data['physical_used_percent'] ?>%;"
                           aria-valuenow="<?= $data['physical_used_percent'] ?>" aria-valuemin="0" aria-valuemax="100">
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>

            <div class="col-lg-4">
              <div class="card h-100" style="border: none">
                <div class="card-body capacity-block bg-dark">
                  <div>
                    <h5 class="card-title">Volume Capacity</h5>
                    <div class="d-flex justify-content-between align-items-start mb-2">
                      <div>
                        <span class="percent"><?= number_format($data['volume_written_percent']) ?>%</span>
                        <div class="value"><?= formatBytes($data['written_total']) ?></div>
                      </div>
                    </div>
                    <div class="progress" style="height: 30px;">
                      <div class="progress-bar" role="progressbar"
                           style="width: <?= $data['volume_written_percent'] ?>%;"
                           aria-valuenow="<?= $data['volume_written_percent'] ?>" aria-valuemin="0" aria-valuemax="100">
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>

            <div class="col-lg-4">
              <div class="card h-100" style="border: none">
                <div class="card-body capacity-block bg-dark">
                  <div>
                    <h5 class="card-title">Capacity gain</h5>
                    <div class="gain-item">
                      <span><?= number_format($data['compression_savings_percent']) ?>% Compression</span>
                      <span><?= formatBytes($data['compression_savings']) ?></span>
                    </div>
                    <div class="gain-item">
                      <span><?= number_format($data['thin_savings_percent']) ?>% Thin provisioning</span>
                      <span><?= formatBytes($data['thin_savings']) ?></span>
                    </div>
                    <hr>
                    <div class="gain-item">
                      <span>Compression</span>
                      <span><?= number_format($data['compression_ratio'], 1) ?>:1</span>
                    </div>
                  </div>

                </div>
              </div>
            </div>

          </div>
        </div>
      </div>

      <hr>
      <!-- IO_grp -->
      <div class="row flex-nowrap text-center">
        <div class="col"><!-- GRP0 -->
          <div class="card border-secondary mb-3">
            <div class="card-header text-dark text-center bg-info">
              <h5><i class="fs-4 bi-hdd-network text-black"></i> IO_grp 0</h5>
            </div>
            <div class="card-body bg-dark">
              <b>Hosts : </b><?php echo $json['Data']['IOgroups']['io_grp0']['host_count']; ?><br>
              <b>Volumes : </b><?php echo $json['Data']['IOgroups']['io_grp0']['vdisk_count']; ?>
            </div>
          </div>
        </div>
        <div class="col"><!-- GRP1 -->
          <div class="card border-secondary mb-3">
            <div class="card-header text-dark text-center bg-info">
              <h5><i class="fs-4 bi-hdd-network text-black"></i> IO_grp 1</h5>
            </div>
            <div class="card-body bg-dark text-left">
              <b>Hosts : </b><?php echo $json['Data']['IOgroups']['io_grp1']['host_count']; ?><br>
              <b>Volumes : </b><?php echo $json['Data']['IOgroups']['io_grp1']['vdisk_count']; ?>
            </div>
          </div>
        </div>
        <div class="col"><!-- GRP2 -->
          <div class="card border-secondary mb-3">
            <div class="card-header text-dark text-center bg-info">
              <h5><i class="fs-4 bi-hdd-network text-black"></i> IO_grp 2</h5>
            </div>
            <div class="card-body bg-dark">
              <b>Hosts : </b><?php echo $json['Data']['IOgroups']['io_grp2']['host_count']; ?><br>
              <b>Volumes : </b><?php echo $json['Data']['IOgroups']['io_grp2']['vdisk_count']; ?>
            </div>
          </div>
        </div>
        <div class="col"><!-- Orphan LUNS -->
          <div class="card border-secondary mb-3">
            <div class="card-header text-dark text-center <?php echo $cuc; ?>">
              <h5><i class="fs-4 bi-hdd text-black"></i> Unnasigned LUNs</h5>
            </div>
            <div class="card-body bg-dark text-center fs-2">
              <button type="button" class="btn btn-secondary" data-bs-html="true" data-toggle="tooltip"
                      data-placement="top" title="<?php echo $lLUN; ?>"><b><?php echo $unnasigned; ?></b></button>
            </div>
          </div>
        </div>
        <div class="col"><!-- Orphan Hosts -->
          <div class="card border-secondary mb-3">
            <div class="card-header text-dark text-center <?php echo $chc; ?>">
              <h5><i class="fs-4 bi-hdd text-black"></i> Hosts w/o LUNs</h5>
            </div>
            <div class="card-body bg-dark text-center fs-2">
              <button type="button" class="btn btn-secondary" data-bs-html="true" data-toggle="tooltip"
                      data-placement="top" title="<?php echo $lHOST; ?>"><b><?php echo $orphanHosts; ?></b></button>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- End of main content -->
  </div>
</div>
<script src="/js/switch.js"></script>
</body>
<script>
  $(function () {
    $('[data-toggle="tooltip"]').tooltip()
  })
</script>