1. Go to this page and download the library: Download finller/laravel-kpi 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/ */
namespace App\Models;
use Finller\Kpi\HasKpi;
class User extends Model
{
use HasKpi;
/*
* The date represent the date of the KPI
* It is usefull when capturing a Kpi from the past
*/
public static function registerKpis(Carbon $date = null): Collection
{
$query = static::query()
->when($date, fn (Builder $q) => $q->whereDate('created_at', '<=', $date->clone()));
// The model count Kpi is always snapshoted, you don't need to register it
return collect()
// When using `put`, the kpi namespace will be automatially
->put('active:count', new Kpi([
'number_value' => $query->clone()->active()->count(),
'created_at' => $date->clone(),
]))
// You can also manually define the key
->push( new Kpi([
'key' =>
'number_value' => $query->clone()->subscribed()->count(),
'created_at' => $date->clone(),
]));
}
}
namespace App\Console\Commands;
use App\Models\User;
class SnapshotKpisCommand extends Command
{
protected $signature = 'kpis:snapshot';
protected $description = 'Snapshot KPI';
public function handle()
{
User::snapshotKpis();
}
}
namespace App\Console;
use App\Console\Commands\SnapshotKpiCommand;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected function schedule(Schedule $schedule)
{
$schedule->command(SnapshotKpiCommand::class)->dailyAt('00:00');
}
}
// With Kpi model
use Finller\Kpi\Kpi;
Kpi::where('key', "users:count")->get();
// With HasKpi trait
use App\Models\User;
User::kpi('count')->get();
// With KpiBuilder
use Finller\Kpi\KpiBuilder;
KpiBuilder::query("users:count")->get();
KpiBuilder::query(Kpi::query()->where("key", "users:count"))->get();
use Finller\Kpi\Enums\KpiInterval;
Kpi::query()
->where('key', 'users:blocked:count')
->between(now()->subWeek(), now())
->perDay()
->get()
->fillGaps( // optional parameters
start: now()->subWeek(),
end: now(),
interval: KpiInterval::Day,
default: ['number_value' => 0]
);
KpiBuilder::query('users:blocked:count')
->perDay()
->between(now()->subWeek(), now())
->fillGaps()
->get();
Kpi::query()
->where('key', 'users:blocked:count')
->between(now()->subWeek(), now())
->perDay()
->get()
->fillGaps(); // if you do not specify anything when using KpiCollection, the start, end and the interval values will be guessed from your dataset