Add total messages to domain view, and improve echoarea stats query

This commit is contained in:
Deon George 2023-07-30 20:14:38 +10:00
parent 61f64c7c75
commit 7ca6fdc195
3 changed files with 29 additions and 20 deletions

View File

@ -2,6 +2,7 @@
namespace App\Models;
use AgliPanci\LaravelCase\Facades\CaseBuilder;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@ -113,20 +114,22 @@ class Domain extends Model
->values();
}
/**
* Get the latest message in each echomail area
*
* @return Collection
*/
public function latest_echomail_message(): Collection
public function echoarea_stats(): Collection
{
$dt = Carbon::now()->startOfday();
$case = CaseBuilder::whenRaw("datetime >= '?'",$dt->subDay()->format('Y-m-d'))->thenRaw("'day'")
->whenRaw("datetime >= '?'",$dt->subDays(7)->format('Y-m-d'))->thenRaw("'week'")
->whenRaw("datetime >= '?'",$dt->subMonth()->format('Y-m-d'))->thenRaw("'month'")
->elseRaw("'all'");
return Echoarea::cacheFor(self::CACHE_TIME)
->select([
'echoareas.*',DB::raw('max(datetime) as last_message')
])
->leftJoin('echomails',['echomails.echoarea_id'=>'echoareas.id'])
->select(['echoareas.id','name','description','active',DB::raw('count(echoareas.id) AS count'),DB::raw('max(datetime) as last_message')])
->selectRaw($case->toRaw().' AS stats')
->join('echomails',['echomails.echoarea_id'=>'echoareas.id'])
->where('domain_id',$this->id)
->groupBy('echoareas.id')
->groupBy('echoareas.name')
->groupBy('stats')
->orderBy('echoareas.name')
->get();
}

View File

@ -70,7 +70,7 @@ class Echoarea extends Model
/* METHODS */
public function messages_count(int $period): int
public function messages_count(int $period=NULL): int
{
$eo = Echomail::cacheFor(self::CACHE_TIME)
->where('echoarea_id',$this->id);
@ -87,6 +87,10 @@ class Echoarea extends Model
case 30: // month
$eo->where('datetime','>=',$dt->subMonth());
break;
case NULL: // all
break;
default:
return 0;
}

View File

@ -34,7 +34,7 @@
<thead>
<tr>
<th class="w-75" colspan="4"></th>
<th colspan="3" class="text-center">Messages</th>
<th colspan="4" class="text-center">Messages</th>
</tr>
<tr>
<th>Echoarea</th>
@ -44,19 +44,21 @@
<th class="text-end">Day</th>
<th class="text-end">Week</th>
<th class="text-end">Month</th>
<th class="text-end">Total</th>
</tr>
</thead>
<tbody>
@foreach ($o->latest_echomail_message() as $oo)
@foreach ($o->echoarea_stats()->groupBy('id') as $oo)
<tr>
<td style="width: 15%;"><a href="{{ url('ftn/echoarea/addedit',[$oo->id]) }}">{{ $oo->name }}</a></td>
<td>{{ $oo->description }}</td>
<td style="width: 15%;">{{ $oo->last_message ? $oo->last_message->format('Y-m-d H:i') : '-' }}</td>
<td>{{ $oo->active ? 'Active' : 'Archive' }}</td>
<td class="text-end">{{ number_format($oo->messages_count(1)) }}</td>
<td class="text-end">{{ number_format($oo->messages_count(7)) }}</td>
<td class="text-end">{{ number_format($oo->messages_count(30)) }}</td>
<td style="width: 15%;"><a href="{{ url('ftn/echoarea/addedit',[($x=$oo->first())->id]) }}">{{ $x->name }}</a></td>
<td>{{ $x->description }}</td>
<td style="width: 15%;">{{ $x->last_message ? $x->last_message->format('Y-m-d H:i') : '-' }}</td>
<td>{{ $x->active ? 'Active' : 'Archive' }}</td>
<td class="text-end">{{ number_format($oo->where('stats','day')->pop()?->count) }}</td>
<td class="text-end">{{ number_format($oo->where('stats','week')->pop()?->count) }}</td>
<td class="text-end">{{ number_format($oo->where('stats','month')->pop()?->count) }}</td>
<td class="text-end">{{ number_format($oo->where('stats','all')->pop()?->count) }}</td>
</tr>
@endforeach
</tbody>