Giter Site home page Giter Site logo

laravel-easy-metrics's Introduction

Easy metrics banner

Workflow status Laravel v10.x PHP 8.0

๐Ÿ”ฅ Easy metrics

Easily create metrics for your application.

โœจ Help support the maintenance of this package by sponsoring me.

Designed to work with Laravel, Filament, and more.

Preview

๐Ÿš€ Supported metrics

  • Bar metric
  • Doughnut metric
  • Line metric
  • Pie metric
  • Polar metric
  • Trend metric
  • Value metric

Table of contents

๐Ÿ“ฆ Install

composer require sakanjo/laravel-easy-metrics

๐Ÿฆ„ Usage

Value metric

use SaKanjo\EasyMetrics\Metrics\Value;
use App\Models\User;

$data = Value::make(User::class)
    ->count();

Query types

The currently supported aggregate functions to calculate a given column compared to the previous time interval / range

Min
Value::make(User::class)
    ->min('age');
Max
Value::make(User::class)
    ->max('age');
Sum
Value::make(User::class)
    ->sum('age');
Average
Value::make(User::class)
    ->average('age');
Count
Value::make(User::class)
    ->count();

Doughnut metric

use SaKanjo\EasyMetrics\Metrics\Doughnut;
use App\Models\User;
use App\Enums\Gender;

[$labels, $data] = Doughnut::make(User::class)
    ->options(Gender::class)
    ->count('gender');

It's always better to use the options method even though it's optional, since the retrieved data may not include all enum options.

Query types

The currently supported aggregate functions to calculate a given column compared to the previous time interval / range

Min
Doughnut::make(User::class)
    ->min('age', 'gender');
Max
Doughnut::make(User::class)
    ->max('age', 'gender');
Sum
Doughnut::make(User::class)
    ->sum('age', 'gender');
Average
Doughnut::make(User::class)
    ->average('age', 'gender');
Count
Doughnut::make(User::class)
    ->count('gender');

Trend metric

use SaKanjo\EasyMetrics\Metrics\Trend;
use App\Models\User;

[$labels, $data] = Trend::make(User::class)
    ->countByMonths();

Query types

The currently supported aggregate functions to calculate a given column compared to the previous time interval / range

Min
$trend->minByYears('age'); 
$trend->minByMonths('age'); 
$trend->minByWeeks('age');  
$trend->minByDays('age');  
$trend->minByHours('age');  
$trend->minByMinutes('age');  
Max
$trend->maxByYears('age'); 
$trend->maxByMonths('age'); 
$trend->maxByWeeks('age');  
$trend->maxByDays('age');  
$trend->maxByHours('age');  
$trend->maxByMinutes('age');  
Sum
$trend->sumByYears('age'); 
$trend->sumByMonths('age'); 
$trend->sumByWeeks('age');  
$trend->sumByDays('age');  
$trend->sumByHours('age');  
$trend->sumByMinutes('age');  
Average
$trend->averageByYears('age'); 
$trend->averageByMonths('age'); 
$trend->averageByWeeks('age');  
$trend->averageByDays('age');  
$trend->averageByHours('age');  
$trend->averageByMinutes('age');  
Count
$trend->countByYears(); 
$trend->countByMonths(); 
$trend->countByWeeks();  
$trend->countByDays();  
$trend->countByHours();  
$trend->countByMinutes();  

Other metrics

  • Bar extends Trend
  • Line extends Trend
  • Doughnut extends Pie
  • Polar extends Pie

Ranges

Every metric class contains a ranges method, that will determine the range of the results based on it's date column.

use SaKanjo\EasyMetrics\Metrics\Trend;
use SaKanjo\EasyMetrics\Metrics\Enums\Range;
use App\Models\User;

Value::make(User::class)
    ->range(30)
    ->ranges([
        15, 30, 365,
        Range::TODAY, // Or 'TODAY'
    ]);

Available custom ranges

  • Range::TODAY
  • Range::YESTERDAY
  • Range::MTD
  • Range::QTD
  • Range::YTD
  • Range::ALL

Growth rates

Growth rate, expressed as both a value and a percentage, measures the change in a quantity over time, showing the speed of its expansion or contraction in both absolute and relative terms.

Using Value metric

use SaKanjo\EasyMetrics\Metrics\Value;
use SaKanjo\EasyMetrics\Enums\GrowthRateType;
use App\Models\User;

[$value, $growth] = Value::make(User::class)
    ->withGrowthRate()
    ->growthRateType(GrowthRateType::Value) // default is `Percentage`
    ->count();

Using Trend metric

use SaKanjo\EasyMetrics\Metrics\Trend;
use App\Models\User;

[$labels, $data, $growth] = Trend::make(User::class)
    ->withGrowthRate()
    ->countByYears();

Using Doughnut metric

use SaKanjo\EasyMetrics\Metrics\Doughnut;
use App\Models\User;
use App\Enums\Gender;

[$labels, $data, $growth] = Doughnut::make(User::class)
    ->options(Gender::class)
    ->withGrowthRate()
    ->count('gender');

Available growth rate types

  • GrowthRateType::Value
  • GrowthRateType::Percentage

๐Ÿ”ฅ Practical examples

Filamentphp v3 widgets

<?php

namespace App\Filament\Widgets\Admin;

use App\Models\User;
use Filament\Widgets\ChartWidget;
use SaKanjo\EasyMetrics\Metrics\Trend;

class UsersCountChart extends ChartWidget
{
    protected static ?string $heading = 'Users count trend';

    protected function getData(): array
    {
        [$labels, $data] = Trend::make(User::class)
            ->range($this->filter)
            ->rangesFromOptions($this->getFilters())
            ->countByMonths();

        return [
            'datasets' => [
                [
                    'label' => 'Users',
                    'data' => $data,
                ],
            ],
            'labels' => $labels,
        ];
    }

    protected function getType(): string
    {
        return 'line';
    }

    protected function getFilters(): ?array
    {
        return [
            15 => '15 Months',
            30 => '30 Months',
            60 => '60 Months',
        ];
    }
}

๐Ÿ’– Support the development

Do you like this project? Support it by donating

Click the "๐Ÿ’– Sponsor" at the top of this repo.

ยฉ๏ธ Credits

๐Ÿ“„ License

MIT License ยฉ 2023-PRESENT Salah Kanjo

laravel-easy-metrics's People

Contributors

sakanjo avatar gajosu avatar kaulse avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.