PHP code example of radiatecode / dastats

1. Go to this page and download the library: Download radiatecode/dastats 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/ */

    

radiatecode / dastats example snippets


use RadiateCode\DaStats\Facades\Stats;
.......

public function storeUser(Request $request){
    // stores statements
    ............

    Stats::title('User count')->key('total-user')->increase();
}

public function deleteUser($id){
    // delete statements
    ............

    Stats::key('total-user')->decrease();
}
 
use RadiateCode\DaStats\Facades\Stats;
.......

public function orderStore(Request $request){
    // pending order placement statements
    ............

    Stats::title('Pending order count')->key('total-pending-order')->increase();
}

public function orderDeliver(Request $request){
    // order delivery statements
    ............
    
    Stats::key('total-pending-order')->decrease();
} 
 
use RadiateCode\DaStats\Facades\Stats;
.......

public function purchaseStore(Request $request){
    // other statements
    ............

    foreach($purchaseProducts as $product){ // multiple products
        // purchase products save statements
        ..............
        
        // increase stock for a product
        Stats::title('Live stock')->key($product->id)->increase($product->quantity);
    }
}
 
use RadiateCode\DaStats\Facades\Stats;
.......

public function purchaseUpdate(Request $request){
    // other statement
    ............

    foreach($purchaseProducts as $product){
        // purchase products update statements
        ..............
        ..............


        //live stock
        $variation = (int) $newQty - $oldQty;

        Stats::when($variation > 0,function ($stats) use ($product,$variation){
            $stats->title('Live stock')->key($product->id)->increase($variation);
        });

        Stats::when($variation < 0,function ($stats) use ($product,$variation){
            $stats->title('Live stock')->key($product->id)->decrease(abs($variation));
        });
    }
}
 
use RadiateCode\DaStats\Facades\Stats;
.......

public function productDelete($id){
    // other statement
    ............

    // find product
    $purchaseProduct = PurchaseProduct::findOrFail($id);

    // decrese value for this product
    Stats::title('Live stock')->key($purchaseProduct->id)->decrease($purchaseProduct->quantity);

    $purchaseProduct->delete(); // purchase product delete
}
 
$stats = Stats::key('total-pending-order')->find();

$stats = Stats::key('total-user')->find();

$stock = Stats::title('Live stock')->get();
 
$stats = Stats::inKeys('total-pending-order','total-user')->get();

// get stock by product ids
$stocks = Stats::inKeys(1,22,55,66)->get(); 
 
$stats = Stats::contains('pending')->get();
 
Stats::title('Your Title')->key('your-key')->increase(); 
 
Stats::key('your-key')->decrease();
 
Stats::title('Your Title')->key('your-key')->decrease();
 
Stats::key('your-key')->replace($value);
 
Stats::title('Your Title')->key('your-key')->replace($value);
 
Stats::inKeys('key-1','key-2')->get();
 
Stats::inKeys('key-1','key-2')->paginate(10);
 
Stats::title('Title 1')->key('key-1')->find();
 
Stats::key('key-1')->find();
 
Stats::title('Title 1')->key('key-1')->exists(); // return true/false
 
// based on existence do stats operation

Stats::title('Title')->key('key-1')->exists(
    function ($stats){ // if exists
        $stats->replace(300);
    },function ($stats){ // if not exists
        $stats->increase(25);
    }
);
 
Stats::contain('partOfkey')->get();

example:
// key name is `total-registered-user`
// we can get stats by the key part `registered`
Stats::contain('registered')->get();
 
$stats = Stats::key('key-1')->remove();
 
Stats::inKeys('key-1','key-2','key-3')->remove();

Stats::joinWith('products', 'id') // joining products table
    ->title('inventory')
    ->get(['products.product_name', 'products.product_unit']);

Stats::joinWith('products', 'id') // joining products table
    ->join('categories','categories.id','=','products.category_id') // join categories table with products
    ->join('units','units.id','=','products.unit_id') // join units table with products
    ->title('inventory')
    ->get(['products.product_name', 'products.product_unit','categories.category_name','units.unit_name']);
 
Stats::isolate('Tenant',101)->title('Total Order')->key('order-count')->increment();

Stats::isolate('Tenant',101)->key('order-count')->find();
 
Stats::isolate('User',202)->title('Complete Project')->key('project-completion')->increment();

Stats::isolate('User',202)->key('project-completion')->find();
 
Stats::when($some_condition,function($stats){
    return $stats->title('Title 1')->key('key-1')->increase(8000);
});
 
Stats::title('Title 1')->key('key-1')
->when($some_condition,
    function($stats){ // when condition true
        return $stats->decrease(2500);
    },function(){ // when condition false
        return $stats->increase(500); 
    }
);
 
Stats::when($has_tenant,function($stats) use ($tenantId){
    return $stats->isolate('Tenant', $tenantId);
})->all();
   
use RadiateCode\DaStats\Enum\StatsAction;
...........


// data format
$data =  [
    ['key' => 'key-1','value' = 40],
    ['key' => 'key-2','value' = 25],
    ['key' => 'key-3','value' = 35],
]

// action (ex: StatsAction::INCREASE, StatsAction::DECREASE, StatsAction::REPLACE)
$action = StatsAction::INCREASE

Stats::title('Live stock')->doMany($action,$data);
 
// data format
$data =  [
    ['key' => 'key-1','value' = 40],
    ['key' => 'key-2','value' = 25],
    ['key' => 'key-3','value' = 35],
]

Stats::isolate('Organisation',$organisaiton->id)->title('Live stock')->doMany(StatsAction::INCREASE,$data);
 
Stats::title('Live stock')->doMany(
    StatsAction::INCREASE,
    [
        ['key' => 1,'value' = 500], // key => product id and value => quantity
        ['key' => 5,'value' = 152],
        ['key' => 35,'value' = 7569],
        ['key' => 7,'value' = 900],
        ['key' => 9,'value' = 25],
    ]
);
 
use RadiateCode\DaStats\Jobs\SingleStatsJob;
use RadiateCode\DaStats\Enum\StatsAction;
..........

// dispatch the job to increase a stats
dispatch(new SingleStatsJob(StatsAction::INCREASE,'Title','key',$value));

or

// dispatch the job to decrease a stats
dispatch(new SingleStatsJob(StatsAction::DECREASE,'Title','key',$value));

// dispatch the job to replace a stats
dispatch(new SingleStatsJob(StatsAction::REPLACE,'Title','key',$value));
 
use RadiateCode\DaStats\Jobs\MultiStatsJob;
use RadiateCode\DaStats\Enum\StatsAction;
..........

$data = [ 
            ['key' => 1,'value' = 500],
            ['key' => 5,'value' = 152],
        ];

// dispatch the job to decrease multiple stats value
dispatch(new MultiStatsJob(StatsAction::DECREASE,'Title',$data));
 
// dispatch the job to increase multiple stats value
dispatch(new MultiStatsJob(StatsAction::INCREASE,'Title',$data));
 
use RadiateCode\DaStats\Jobs\MultiStatsJob;
use RadiateCode\DaStats\Enum\StatsAction;
..........

$data = [ 
            ['key' => 1,'value' = 500],
            ['key' => 5,'value' = 152],
        ];

$job = new MultiStatsJob(StatsAction::DECREASE,'Title',$data);

$job->withIsolation('Tenant',1001);

dispatch($job);