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

This commit is contained in:
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;
}