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; namespace App\Models;
use AgliPanci\LaravelCase\Facades\CaseBuilder;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
@ -113,20 +114,22 @@ class Domain extends Model
->values(); ->values();
} }
/** public function echoarea_stats(): Collection
* Get the latest message in each echomail area
*
* @return Collection
*/
public function latest_echomail_message(): 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) return Echoarea::cacheFor(self::CACHE_TIME)
->select([ ->select(['echoareas.id','name','description','active',DB::raw('count(echoareas.id) AS count'),DB::raw('max(datetime) as last_message')])
'echoareas.*',DB::raw('max(datetime) as last_message') ->selectRaw($case->toRaw().' AS stats')
]) ->join('echomails',['echomails.echoarea_id'=>'echoareas.id'])
->leftJoin('echomails',['echomails.echoarea_id'=>'echoareas.id'])
->where('domain_id',$this->id) ->where('domain_id',$this->id)
->groupBy('echoareas.id') ->groupBy('echoareas.id')
->groupBy('echoareas.name')
->groupBy('stats')
->orderBy('echoareas.name') ->orderBy('echoareas.name')
->get(); ->get();
} }

View File

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

View File

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