You are here

MetricService.php in Drupalmonitor 8

File

src/MetricService.php
View source
<?php

namespace Drupal\drupalmonitor;

use Drupal\Core\Database\Driver\mysql\Connection;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\Yaml\Yaml;

/**
 * Class MetricService.
 *
 * @package Drupal\drupalmonitor
 */
class MetricService {

  /**
   * Database.
   *
   * @var \Drupal\Core\Database\Driver\mysql\Connection
   */
  protected $database;

  /**
   * Module Handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs a new MetricService object.
   */
  public function __construct(Connection $database, ModuleHandlerInterface $module_handler) {
    $this->database = $database;
    $this->moduleHandler = $module_handler;
  }

  /**
   * Get User Count.
   *
   * @return int
   *   User Count.
   */
  public function getUserCount() {
    $query = "SELECT count(*) FROM {users}";
    return (int) $this->database
      ->query($query)
      ->fetchField();
  }

  /**
   * Get Active Sessions Count.
   *
   * @return int
   *   Active Sessions Count.
   */
  public function getActiveSessionsCount() {
    $query = "SELECT count(*) FROM {users_field_data} u WHERE NOW()-300 > u.access";
    return (int) $this->database
      ->query($query)
      ->fetchField();
  }

  /**
   * Get Logged in Sessions Count.
   *
   * @return int
   *   Logged in Sessions Count.
   */
  public function getLoggedInSessionsCount() {
    $query = "SELECT count(*) FROM {users_field_data} u WHERE NOW()-300 > u.access AND u.uid > 0";
    return (int) $this->database
      ->query($query)
      ->fetchField();
  }

  /**
   * Get Files Count.
   *
   * @return int
   *   Files Count.
   */
  public function getFilesCount() {
    $query = "SELECT count(*) FROM {file_managed}";
    return (int) $this->database
      ->query($query)
      ->fetchField();
  }

  /**
   * Get Total Files Size.
   *
   * @return int
   *   Total Files Size.
   */
  public function getTotalFilesSize() {
    $query = "SELECT sum(filesize) FROM {file_managed}";
    return (int) $this->database
      ->query($query)
      ->fetchField();
  }

  /**
   * Get Node Content Types.
   *
   * @return array
   *   Node Content Types.
   */
  public function getNodeContentTypes() {
    $query = "SELECT n.type, count(*) as counter FROM {node} n GROUP BY n.type";
    return $this->database
      ->query($query)
      ->fetchAllKeyed();
  }

  /**
   * Get Module List.
   *
   * @return array
   *   Array with Modules and their info from info.yml file.
   */
  public function getModuleList() {
    $modules_list = [];

    /** @var \Drupal\Core\Extension\Extension $module_info */
    foreach ($this->moduleHandler
      ->getModuleList() as $module_name => $module_info) {
      $modules_list[$module_name] = Yaml::parseFile($module_info
        ->getPathname());
    }
    return $modules_list;
  }

  /**
   * Get Config List.
   *
   * @return array
   *   Config List.
   */
  public function getConfigList() {
    $query = "SELECT * FROM {config}";
    $result = $this->database
      ->query($query);
    $data = [];
    foreach ($result as $record) {
      $data[$record->name] = unserialize($record->data);
    }
    return $data;
  }

  /**
   * Get Server Data.
   *
   * @return array
   *   Server Data.
   */
  public function getServerData() {
    $info = [];
    $info['php_uname'] = php_uname('s');
    $info['system_load'] = sys_getloadavg();
    $info['php_version'] = phpversion();
    $info['getrusage'] = getrusage();
    $info['sys_get_temp_dir'] = sys_get_temp_dir();
    $info['PHP_SERVER'] = $_SERVER;
    $info['phpinfo'] = $this
      ->getPhpInfoAsArray();
    return $info;
  }

  /**
   * Get PHP Info as Array.
   *
   * @see https://www.php.net/manual/de/function.phpinfo.php#106862
   *
   * @return array
   *   PHP Info as Array.
   */
  public function getPhpInfoAsArray() {
    ob_start();
    phpinfo();
    $info_arr = [];
    $info_lines = explode("\n", strip_tags(ob_get_clean(), "<tr><td><h2>"));
    $cat = "General";
    foreach ($info_lines as $line) {

      // Check if a new category is shown on line.
      preg_match("~<h2>(.*)</h2>~", $line, $title) ? $cat = $title[1] : NULL;
      if (preg_match("~<tr><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td></tr>~", $line, $val)) {
        $info_arr[$cat][$val[1]] = $val[2];
      }
      elseif (preg_match("~<tr><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td></tr>~", $line, $val)) {
        $info_arr[$cat][$val[1]] = [
          "local" => $val[2],
          "master" => $val[3],
        ];
      }
    }
    return $info_arr;
  }

}

Classes

Namesort descending Description
MetricService Class MetricService.