Skip to content

Commit

Permalink
Merge pull request #33 from saleem-hadad/feat/add-facts-reports-to-da…
Browse files Browse the repository at this point in the history
…shboard

💰 Add facts reports to dashboard #feature
  • Loading branch information
saleem-hadad committed Oct 22, 2022
2 parents 8ad576e + 8c23faf commit c7021b7
Show file tree
Hide file tree
Showing 40 changed files with 827 additions and 175 deletions.
11 changes: 10 additions & 1 deletion app/Domain/Metrics/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ abstract class Metric extends Element
protected $width = '1/2';
protected $ranges = [];
protected $graphqlQuery;
protected $showCurrency = true;

public function component()
{
Expand All @@ -26,6 +27,13 @@ public function width()
return $this->width;
}

public function setWidth($width)
{
$this->width = $width;

return $this;
}

public function ranges()
{
return [
Expand All @@ -47,7 +55,8 @@ public function jsonSerialize(): mixed
'component' => $this->component(),
'width' => $this->width(),
'ranges' => $this->ranges(),
'graphql_query' => $this->graphqlQuery()
'graphql_query' => $this->graphqlQuery(),
'show_currency' => $this->showCurrency,
]);
}
}
8 changes: 8 additions & 0 deletions app/Domain/Metrics/TrendMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@
abstract class TrendMetric extends Metric
{
protected $component = 'trend-metric';
protected $showStandardDeviation = false;

public function jsonSerialize(): mixed
{
return array_merge(parent::jsonSerialize(), [
'show_standard_deviation' => $this->showStandardDeviation
]);
}
}
32 changes: 32 additions & 0 deletions app/GraphQL/Queries/AverageValueTransaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\GraphQL\Queries;

use App\Models\Transaction;
use Illuminate\Support\Facades\DB;
use App\Domain\Metrics\PartitionMetric;

class AverageValueTransaction extends PartitionMetric
{
/**
* @param null $_
* @param array<string, mixed> $args
*/
public function __invoke($_, array $args)
{
$rangeData = app('findRangeByKey', ["key" => $args['range']]);

$query = Transaction::query()
->join('brands', 'brands.id', '=', 'transactions.brand_id')
->join('categories', 'categories.id', '=', 'brands.id')
->select("categories.type as label", DB::raw("avg(transactions.amount) as value"))
->groupBy("categories.type")
->orderBy('value', 'DESC');

if($rangeData) {
$query->whereBetween('transactions.created_at', [$rangeData->start(), $rangeData->end()]);
}

return $query->get();
}
}
32 changes: 32 additions & 0 deletions app/GraphQL/Queries/HighestValueTransaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\GraphQL\Queries;

use App\Models\Transaction;
use Illuminate\Support\Facades\DB;
use App\Domain\Metrics\PartitionMetric;

class HighestValueTransaction extends PartitionMetric
{
/**
* @param null $_
* @param array<string, mixed> $args
*/
public function __invoke($_, array $args)
{
$rangeData = app('findRangeByKey', ["key" => $args['range']]);

$query = Transaction::query()
->join('brands', 'brands.id', '=', 'transactions.brand_id')
->join('categories', 'categories.id', '=', 'brands.id')
->select("categories.type as label", DB::raw("max(transactions.amount) as value"))
->groupBy("categories.type")
->orderBy('value', 'DESC');

if($rangeData) {
$query->whereBetween('transactions.created_at', [$rangeData->start(), $rangeData->end()]);
}

return $query->get();
}
}
32 changes: 32 additions & 0 deletions app/GraphQL/Queries/LowestValueTransaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\GraphQL\Queries;

use App\Models\Transaction;
use Illuminate\Support\Facades\DB;
use App\Domain\Metrics\PartitionMetric;

class LowestValueTransaction extends PartitionMetric
{
/**
* @param null $_
* @param array<string, mixed> $args
*/
public function __invoke($_, array $args)
{
$rangeData = app('findRangeByKey', ["key" => $args['range']]);

$query = Transaction::query()
->join('brands', 'brands.id', '=', 'transactions.brand_id')
->join('categories', 'categories.id', '=', 'brands.id')
->select("categories.type as label", DB::raw("min(transactions.amount) as value"))
->groupBy("categories.type")
->orderBy('value', 'DESC');

if($rangeData) {
$query->whereBetween('transactions.created_at', [$rangeData->start(), $rangeData->end()]);
}

return $query->get();
}
}
34 changes: 34 additions & 0 deletions app/GraphQL/Queries/NumberOfTransactions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\GraphQL\Queries;

use App\Models\Transaction;
use Illuminate\Support\Facades\DB;
use App\Domain\Metrics\PartitionMetric;

class NumberOfTransactions extends PartitionMetric
{
protected $showCurrency = false;

/**
* @param null $_
* @param array<string, mixed> $args
*/
public function __invoke($_, array $args)
{
$rangeData = app('findRangeByKey', ["key" => $args['range']]);

$query = Transaction::query()
->join('brands', 'brands.id', '=', 'transactions.brand_id')
->join('categories', 'categories.id', '=', 'brands.id')
->select("categories.type as label", DB::raw("count(transactions.id) as value"))
->groupBy("categories.type")
->orderBy('value', 'DESC');

if($rangeData) {
$query->whereBetween('transactions.created_at', [$rangeData->start(), $rangeData->end()]);
}

return $query->get();
}
}
40 changes: 40 additions & 0 deletions app/GraphQL/Queries/NumberOfTransactionsPerBrand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\GraphQL\Queries;

use App\Models\Transaction;
use Illuminate\Support\Facades\DB;
use App\Domain\Metrics\RelationPartitionMetric;

class NumberOfTransactionsPerBrand extends RelationPartitionMetric
{
protected $showCurrency = false;
protected $relationGraphqlQuery = 'allCategories';
protected $relationDisplayUsing = 'name';
protected $relationForeignKey = 'id';

/**
* @param null $_
* @param array<string, mixed> $args
*/
public function __invoke($_, array $args)
{
$rangeData = app('findRangeByKey', ["key" => $args['range']]);
$categoryId = $args['id'];

$query = Transaction::query()
->join('brands', 'brands.id', '=', 'transactions.brand_id')
->whereHas('brand.category', function ($query) use($categoryId) {
return $query->where('id', $categoryId);
})
->select("brands.name as label", DB::raw("count(transactions.id) as value"))
->groupBy("brands.name")
->orderBy('value', 'DESC');

if($rangeData) {
$query->whereBetween('transactions.created_at', [$rangeData->start(), $rangeData->end()]);
}

return $query->get();
}
}
34 changes: 34 additions & 0 deletions app/GraphQL/Queries/NumberOfTransactionsPerCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\GraphQL\Queries;

use App\Models\Transaction;
use Illuminate\Support\Facades\DB;
use App\Domain\Metrics\PartitionMetric;

class NumberOfTransactionsPerCategory extends PartitionMetric
{
protected $showCurrency = false;

/**
* @param null $_
* @param array<string, mixed> $args
*/
public function __invoke($_, array $args)
{
$rangeData = app('findRangeByKey', ["key" => $args['range']]);

$query = Transaction::query()
->join('brands', 'brands.id', '=', 'transactions.brand_id')
->join('categories', 'categories.id', '=', 'brands.id')
->select("categories.name as label", DB::raw("count(transactions.id) as value"))
->groupBy("categories.name")
->orderBy('value', 'DESC');

if($rangeData) {
$query->whereBetween('transactions.created_at', [$rangeData->start(), $rangeData->end()]);
}

return $query->get();
}
}
43 changes: 43 additions & 0 deletions app/GraphQL/Queries/SectionDivider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\GraphQL\Queries;

use App\Domain\Element;

class SectionDivider extends Element
{
protected $component = "section-divider";
protected $width = 'full';
protected $title;

public function component()
{
return $this->component;
}

public function width()
{
return $this->width;
}

public function title()
{
return $this->title;
}

public function withTitle($title)
{
$this->title = $title;

return $this;
}

public function jsonSerialize(): mixed
{
return array_merge(parent::jsonSerialize(), [
'component' => $this->component(),
'width' => $this->width(),
'title' => $this->title(),
]);
}
}
2 changes: 1 addition & 1 deletion app/GraphQL/Queries/TotalCash.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class TotalCash extends ValueMetric
{
public function ranges()
public function ranges()
{
return null;
}
Expand Down
38 changes: 38 additions & 0 deletions app/GraphQL/Queries/TransactionsStandardDeviation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\GraphQL\Queries;

use App\Models\Transaction;
use Illuminate\Support\Facades\DB;
use App\Domain\Metrics\RelationTrendMetric;

class TransactionsStandardDeviation extends RelationTrendMetric
{
protected $relationGraphqlQuery = 'allCategories';
protected $relationDisplayUsing = 'name';
protected $relationForeignKey = 'id';
protected $showStandardDeviation = true;

/**
* @param null $_
* @param array<string, mixed> $args
*/
public function __invoke($_, array $args)
{
$rangeData = app('findRangeByKey', ["key" => $args['range']]);
$categoryId = $args['id'];

$query = Transaction::query()
->whereHas('brand.category', function ($query) use($categoryId) {
return $query->where('id', $categoryId);
})
->select(DB::raw("transactions.id as label, transactions.amount as value"))
->orderBy('transactions.created_at', 'asc');

if($rangeData) {
$query->whereBetween('transactions.created_at', [$rangeData->start(), $rangeData->end()]);
}

return $query->get();
}
}
Loading

0 comments on commit c7021b7

Please sign in to comment.