PHP code example of prismamedia / metrics

1. Go to this page and download the library: Download prismamedia/metrics library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

prismamedia / metrics example snippets


# config/bundles.php
return [
    // ...
    PrismaMedia\Metrics\Bundle\PrismaMediaMetricsBundle::class => ['all' => true],
    // ...
];


# src/Metrics/ArticleCountMetric.php

namespace App\Metrics;

use Doctrine\DBAL\Connection;
use PrismaMedia\Metrics\Metric;
use PrismaMedia\Metrics\MetricGenerator;

class ArticleCountMetric implements MetricGenerator
{
    private $connection;
    public function __construct(Connection $connection)
    {
        $this->connection = $connection;
    }

    /**
     * @return Metric[]
     */
    public function getMetrics(): \Traversable
    {
        // SELECT a.status, COUNT(*) as total FROM article GROUP BY a.status
        $qbd = $this->connection->createQueryBuilder();
        $qbd->select('a.status, COUNT(*) as total')
            ->from('article', 'a')
            ->groupBy('a.status');

        foreach ($qbd->execute()->fetchAll() as $row) {
            // app_article_total{status=<status>} <total>
            yield new Metric('app_article_total', (int) $row['total'], [
                'status' => $row['status'],
            ]);
        }
    }
}