You are here

class MetricService in Drupalmonitor 8

Class MetricService.

@package Drupal\drupalmonitor

Hierarchy

Expanded class hierarchy of MetricService

1 file declares its use of MetricService
DrupalMonitorController.php in src/Controller/DrupalMonitorController.php
1 string reference to 'MetricService'
drupalmonitor.services.yml in ./drupalmonitor.services.yml
drupalmonitor.services.yml
1 service uses MetricService
drupalmonitor.metric_service in ./drupalmonitor.services.yml
Drupal\drupalmonitor\MetricService

File

src/MetricService.php, line 14

Namespace

Drupal\drupalmonitor
View source
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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MetricService::$database protected property Database.
MetricService::$moduleHandler protected property Module Handler.
MetricService::getActiveSessionsCount public function Get Active Sessions Count.
MetricService::getConfigList public function Get Config List.
MetricService::getFilesCount public function Get Files Count.
MetricService::getLoggedInSessionsCount public function Get Logged in Sessions Count.
MetricService::getModuleList public function Get Module List.
MetricService::getNodeContentTypes public function Get Node Content Types.
MetricService::getPhpInfoAsArray public function Get PHP Info as Array.
MetricService::getServerData public function Get Server Data.
MetricService::getTotalFilesSize public function Get Total Files Size.
MetricService::getUserCount public function Get User Count.
MetricService::__construct public function Constructs a new MetricService object.