Skip to content

Commit

Permalink
Merge pull request Silvanite#72 from SGS-Optimisation/feature/69-pm-r…
Browse files Browse the repository at this point in the history
…ule-metrics

added rule activity stat
  • Loading branch information
yayann authored Jul 30, 2021
2 parents 95bcce5 + 3c52129 commit b061295
Show file tree
Hide file tree
Showing 18 changed files with 558 additions and 12 deletions.
10 changes: 10 additions & 0 deletions app/Features/BaseFeature.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php


namespace App\Features;


abstract class BaseFeature
{
abstract public function handle();
}
76 changes: 76 additions & 0 deletions app/Features/Stats/RuleCreationPerClientAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php


namespace App\Features\Stats;


use App\Features\BaseFeature;
use App\Models\ClientAccount;
use App\Models\Rule;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Expression;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Laravel\Nova\Metrics\Trend;
use Laravel\Nova\Metrics\TrendDateExpressionFactory;

class RuleCreationPerClientAccount extends Trend
{
public $dataset = [];

/**
* RuleCreationPerClientAccount constructor.
* @param int $range
* @param string $count
*/
public function __construct(
public ?string $count = self::BY_WEEKS,
public ?int $range = 24,
public ?string $column = 'created_at'
) {
parent::__construct();
}


public function handle()
{
foreach (ClientAccount::withCount('rules')->get() as $client_account) {
$this->dataset[$client_account->name] = [
'client_id' => $client_account->id,
'rules_count' => $client_account->rules_count,
'created_at' => $client_account->created_at->format('Y-m-d H:i:s'),
'trend' => $this->processClientAccount($client_account)->trend,
];
}

return $this->dataset;
}

public function processClientAccount($client_account)
{
$query = Rule::forClient($client_account);

$request = new Request();
$request->merge(['range' => $this->range, 'twelveHourTime' => false, 'timezone' => 'UTC']);

return $this->{'countBy'.Str::title(Str::plural($this->count))}($request, $query, $this->column);
}

public function ranges()
{
return [
5 => __('5 Weeks'),
10 => __('10 Weeks'),
15 => __('15 Weeks'),
24 => __('24 Weeks'),
];
}

public function name()
{
return 'Client Account Rules Per Week';
}

}
28 changes: 28 additions & 0 deletions app/Http/Controllers/Stats/StatsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Controllers\Stats;

use App\Features\Stats\RuleCreationPerClientAccount;
use App\Http\Controllers\Controller;
use App\Models\ClientAccount;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Laravel\Jetstream\Jetstream;

class StatsController extends Controller
{
public function index(Request $request)
{
$count = $request->get('count', 'week');
$range = $request->get('range', 24);
$column = $request->get('column', 'created_at');


return Jetstream::inertia()->render($request, 'Stats/ClientAccountStats', [
'stats' => (new RuleCreationPerClientAccount($count, $range, $column))->handle(),
'count' => Str::title($count),
'range' => $range,
'column' => $column,
]);
}
}
6 changes: 6 additions & 0 deletions app/Models/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* @method static Builder|Rule isPublished()
* @method static Builder|Rule whereNotState(string $column, $states)
* @method static Builder|Rule whereState($value)
* @method static Builder|Rule forClient(\App\Models\ClientAccount $clientAccount)
*/
class Rule extends Model implements Auditable
{
Expand Down Expand Up @@ -98,6 +99,11 @@ public function scopeIsOmnipresent(Builder $query)
});
}

public function scopeForClient(Builder $query, ClientAccount $clientAccount)
{
return $query->where('client_account_id', $clientAccount->id);
}

public function scopeIsFlagged(Builder $query)
{
return $query->where('flagged', true);
Expand Down
44 changes: 44 additions & 0 deletions app/Nova/Dashboards/ClientDashboard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Nova\Dashboards;

use App\Models\ClientAccount;
use App\Nova\Metrics\RulesPerWeek;
use Laravel\Nova\Dashboard;

class ClientDashboard extends Dashboard
{
public ClientAccount $client_account;

/**
* ClientDashboard constructor.
* @param ClientAccount $client_account
*/
public function __construct(ClientAccount $client_account)
{
$this->client_account = $client_account;
parent::__construct();
}

/**
* Get the cards for the dashboard.
*
* @return array
*/
public function cards()
{
return [
new RulesPerWeek($this->client_account),
];
}

/**
* Get the URI key for the dashboard.
*
* @return string
*/
public static function uriKey()
{
return 'client-dashboard';
}
}
23 changes: 22 additions & 1 deletion app/Nova/Metrics/RulesPerWeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,32 @@

namespace App\Nova\Metrics;

use App\Models\ClientAccount;
use App\Models\Rule;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Metrics\Trend;

class RulesPerWeek extends Trend
{

public ClientAccount $client_account;

/**
* RulesPerWeek constructor.
* @param ClientAccount $client_account
*/
public function __construct(ClientAccount $client_account)
{
$this->client_account = $client_account;
parent::__construct();
}


public function name()
{
return $this->client_account->name . ' Rules Per Week';
}

/**
* Calculate the value of the metric.
*
Expand All @@ -16,7 +36,8 @@ class RulesPerWeek extends Trend
*/
public function calculate(NovaRequest $request)
{
return $this->countByWeeks($request, Rule::class);
logger(print_r($request->all(), true));
return $this->countByWeeks($request, Rule::forClient($this->client_account));
}

/**
Expand Down
1 change: 1 addition & 0 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function boot()
'createRules', 'updateRules', 'deleteRules', 'publishRules',
'createClientAccounts', 'updateClientAccounts', 'deleteClientAccounts',
'accessPM',
'accessStats',
'accessBackend',
'viewTaxonomies', 'manageTaxonomies',
'viewTerms', 'manageTerms',
Expand Down
21 changes: 17 additions & 4 deletions app/Providers/NovaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Providers;

use Anaseqal\NovaImport\NovaImport;
use App\Models\ClientAccount;
use App\Nova\Dashboards\ClientDashboard;
use App\Nova\Metrics\FlaggedRules;
use App\Nova\Metrics\NewUsers;
use App\Nova\Metrics\PublishedRules;
Expand Down Expand Up @@ -85,13 +87,19 @@ protected function gate()
*/
protected function cards()
{
return [
$client_cards = [];

foreach (ClientAccount::all() as $clientAccount) {
$client_cards[] = new RulesPerWeek($clientAccount);
}

return array_merge([
(new PublishedRules())->width('1/6'),
(new FlaggedRules)->width('1/6'),
new RulesPerWeek,
new RulesPerAccount,
new NewUsers,
];
],
$client_cards);
}

/**
Expand All @@ -101,7 +109,12 @@ protected function cards()
*/
protected function dashboards()
{
return [];
$dashboards = [];
/*foreach(ClientAccount::all() as $clientAccount) {
$dashboards[] = new ClientDashboard($clientAccount);
}*/

return $dashboards;
}

/**
Expand Down
Loading

0 comments on commit b061295

Please sign in to comment.