Compare commits

...

4 Commits

Author SHA1 Message Date
99dc13b297 Initial implementation of API access to clrghouz with a whoami endpoint
All checks were successful
Create Docker Image / Build Docker Image (x86_64) (push) Successful in 58s
Create Docker Image / Build Docker Image (arm64) (push) Successful in 2m51s
Create Docker Image / Final Docker Image Manifest (push) Successful in 8s
2025-04-17 09:41:01 +10:00
6284795e3b Logging some @todos to do 2025-04-17 09:36:39 +10:00
1461647159 Improvements to mail send job and to reduce the number of failed polls going to the failed job table and improvements to determining downlinks 2025-04-17 09:33:32 +10:00
3f0e17e20b Optimize the SQL queries that finds unsent echomail,netmail and files 2025-04-17 09:31:02 +10:00
14 changed files with 342 additions and 310 deletions

View File

@ -9,6 +9,7 @@ use Illuminate\Support\Facades\Log;
use App\Classes\Dynamic;
use App\Models\Address;
use App\Traits\HubStats as HubStatsTrait;
/**
* This method will generate the hub status for an upstream Host/RC/ZC
@ -18,6 +19,8 @@ use App\Models\Address;
*/
class HubStats extends Dynamic
{
use HubStatsTrait;
private const LOGKEY = 'DHS';
private string $name = '';
@ -32,41 +35,8 @@ class HubStats extends Dynamic
{
$date = Carbon::now()->yesterday()->endOfday();
$r = Address::select([
'a.id',
'addresses.system_id',
'addresses.zone_id',
'addresses.region_id',
'addresses.host_id',
'addresses.node_id',
'addresses.point_id',
'addresses.hub_id',
'addresses.role',
DB::raw('sum(a.uncollected_echomail) as uncollected_echomail'),
DB::raw('sum(a.uncollected_netmail) as uncollected_netmail'),
DB::raw('sum(a.uncollected_files) as uncollected_files')
])
->from(
Address::UncollectedEchomailTotal()
->where('echomails.created_at','<',$date)
->union(Address::UncollectedNetmailTotal()
->where('netmails.created_at','<',$date)
)
->union(Address::UncollectedFilesTotal()
->where('files.created_at','<',$date)
),'a')
->where('systems.active',true)
->where('addresses.active',TRUE)
->where('zones.active',TRUE)
->where('domains.active',TRUE)
->where('zones.id',$this->ao->zone_id)
->join('addresses',['addresses.id'=>'a.id'])
->join('systems',['systems.id'=>'addresses.system_id'])
->join('zones',['zones.id'=>'addresses.zone_id'])
->join('domains',['domains.id'=>'zones.domain_id'])
->ftnOrder()
->groupBy('addresses.system_id','a.id','addresses.zone_id','addresses.region_id','addresses.host_id','addresses.node_id','addresses.point_id','addresses.hub_id','addresses.role')
->with(['system','zone.domain']);
$r = $this->HubStats($date)
->where('zones.id',$this->ao->zone_id);
$header = "| %-12s | %4d | %3d | %3d | %16s | %5s | %5s |\r\n";

View File

@ -27,7 +27,7 @@ use App\Traits\ObjectIssetFix;
* = By definition we should know the author node, because it's either ours or (will be) in the nodelist (but it might not be there yet)
* = The target node may not be in the nodelist (anymore)
*
* + Echomail - only has source addresses (MUST have an AREA: tag, otherwise its netmail))
* + Echomail - only has source addresses (MUST have an AREA: tag, otherwise its netmail)
* a Origin Line " * Origin: <some text> (z:f/n.p)
* b MSGID Kludge "MSGID: z:f/n.p<@domain> <sometext>
* c net/node from msg headers (dst should be to hub to be processed)
@ -329,6 +329,7 @@ class Message extends FTNBase
switch ($key) {
// From Addresses
// @todo $this->src no longer appears to be defined
case 'fz': return (int)Arr::get($this->src,'z');
case 'fn': return (int)($x=$this->src) ? Arr::get($x,'n') : Arr::get($this->header,'onet');
case 'ff': return (int)($x=$this->src) ? Arr::get($x,'f') : Arr::get($this->header,'onode');
@ -677,6 +678,7 @@ class Message extends FTNBase
* @param Echomail|Netmail $o
* @return Echomail|Netmail
* @throws InvalidPacketException
* @todo Remove parsing $o as second object, make this private, and use $this->... instead of $o->...
*/
public function unpackMessage(string $message,Echomail|Netmail $o): Echomail|Netmail
{
@ -895,6 +897,10 @@ class Message extends FTNBase
);
$validator->after(function($validator) {
// @todo If the message has an INTL kludge, we send the message to our ZC, and if we are the ZC onto the dst ZC
// @todo So validate we can send it on
// @todo This validation below is incorrect - netmails only need to have an INTL if they are traversing zones
// @todo Without an INTL it is affecting our determination of a source zone/dst zone
if (($this->mo instanceof Netmail) && (! $this->mo->kludges->has('INTL')))
$validator->errors()->add('no-intl','Netmail message is missing INTL KLUDGE.');

View File

@ -34,6 +34,8 @@ use App\Models\Address;
* - To areafix (processed)
* - To ping (respond)
* - With trace turned on (respond)
*
* @todo Enable force processing packets when the password is wrong
*/
class PacketProcess extends Command
{

View File

@ -14,9 +14,12 @@ use App\Classes\FTN\{Message,Packet};
use App\Http\Requests\SetupRequest;
use App\Models\File as FileModel;
use App\Models\{Address,Echomail,Netmail,Setup,System};
use App\Traits\HubStats;
class HomeController extends Controller
{
use HubStats;
public function home()
{
return redirect(Auth::check() ? 'dashboard' : 'about');
@ -246,49 +249,8 @@ class HomeController extends Controller
{
$date = Carbon::now()->yesterday()->endOfday();
$r = Address::select([
'a.id',
'a.system_id',
'a.zone_id',
'addresses.region_id',
'a.host_id',
'a.node_id',
'a.point_id',
'addresses.hub_id',
'addresses.role',
DB::raw('sum(a.uncollected_echomail) as uncollected_echomail'),
DB::raw('sum(a.uncollected_netmail) as uncollected_netmail'),
DB::raw('sum(a.uncollected_files) as uncollected_files')
])
->from(
Address::UncollectedEchomailTotal()
->where('echomails.created_at','<',$this->yesterdayEOD())
->union(Address::UncollectedNetmailTotal()
->where('netmails.created_at','<',$this->yesterdayEOD())
)
->union(Address::UncollectedFilesTotal()
->where('files.created_at','<',$this->yesterdayEOD())
),'a')
->where('systems.active',TRUE)
->where('addresses.active',TRUE)
->where('zones.active',TRUE)
->where('domains.active',TRUE)
->when(! ($x=Auth::user()) || (! $x->isAdmin()),function($query) { return $query->where('domains.public',TRUE); })
->join('addresses',['addresses.id'=>'a.id'])
->join('systems',['systems.id'=>'a.system_id'])
->join('zones',['zones.id'=>'addresses.zone_id'])
->join('domains',['domains.id'=>'zones.domain_id'])
->ftnOrder()
->groupBy('a.system_id','a.id','a.zone_id','addresses.region_id','a.host_id','a.node_id','a.point_id','addresses.hub_id','addresses.role')
->with(['system','zone.domain']);
return view('status')
->with('date',$date)
->with('uncollected',$r->get());
}
private function yesterdayEOD(): Carbon
{
return Carbon::now()->yesterday()->endOfday();
->with('uncollected',$this->HubStats($date)->get());
}
}

View File

@ -72,4 +72,9 @@ class UserController extends Controller
return view('user.link');
}
public function whoami(): User
{
return Auth::user();
}
}

View File

@ -9,6 +9,7 @@ use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\ManuallyFailedException;
use Illuminate\Queue\MaxAttemptsExceededException;
use Illuminate\Queue\Middleware\Skip;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
@ -36,8 +37,10 @@ class AddressPoll implements ShouldQueue, ShouldBeUnique
private Address $ao;
private ?Mailer $mo;
// Whether to force poll even if there isnt anything waiting to send
private bool $force = FALSE;
public function __construct(Address $ao,Mailer $mo=NULL)
public function __construct(Address $ao,Mailer $mo=NULL,bool $force=FALSE)
{
$this->ao = $ao->withoutRelations();
$this->mo = $mo?->withoutRelations();
@ -77,14 +80,8 @@ class AddressPoll implements ShouldQueue, ShouldBeUnique
/**
* When calling MessageProcess - we assume that the packet is from a valid source
*/
public function handle()
public function handle(): void
{
if (! $this->ao->system->mailer_preferred->count() || ($this->mo && (! $this->ao->system->mailer_preferred->find($this->mo)))) {
$this->fail('Missing mailer details');
return;
}
Log::info(sprintf('%s:- Polling [%s] - attempt [%d]',self::LOGKEY,$this->ao->ftn,$this->attempts()));
$setup = Config::get('setup',Setup::findOrFail(config('app.id')));
@ -144,15 +141,15 @@ class AddressPoll implements ShouldQueue, ShouldBeUnique
switch (get_class($exception)) {
case ManuallyFailedException::class:
Log::error(sprintf('%s:! Address Poll failed for [%s] (%s)',self::LOGKEY,$this->ao->ftn,$exception->getMessage()));
break;
exit(0);
case MaxAttemptsExceededException::class:
Log::error(sprintf('%s:! Address Poll was tried too many times for [%s]',self::LOGKEY,$this->ao->ftn));
Notification::route('netmail',$this->ao)->notify(new PollingFailed);
Notification::route('netmail',$this->ao)
->notify(new PollingFailed);
$this->ao->system->autohold = TRUE;
$this->ao->system->save();
exit(0);
default:
@ -160,6 +157,38 @@ class AddressPoll implements ShouldQueue, ShouldBeUnique
}
}
public function middleware(): array
{
return [
// If there are no mailer details, we cant poll
Skip::when(function(): bool {
if (! $this->ao->system->mailer_preferred->count()
// If we specify a mailer, make sure it is preferred by the system
|| ($this->mo && (! $this->ao->system->mailer_preferred->find($this->mo)))) {
Log::error(sprintf('%s:! No mailer details, cannot poll [%s]',self::LOGKEY,$this->ao->ftn));
return TRUE;
}
return FALSE;
}),
// If there is no mail anymore, no need trying
Skip::unless(function(): bool {
if ($this->force
|| ($this->ao->echomailWaitingCount() > 0)
|| ($this->ao->fileWaitingCount() > 0)
|| ($this->ao->netmailWaitingCount() > 0)) {
return TRUE;
}
Log::info(sprintf('%s:/ Nothing waiting - abandoning poll [%s]',self::LOGKEY,$this->ao->ftn));
return FALSE;
}),
];
}
public function uniqueId(): string
{
return $this->ao->id;

View File

@ -2,78 +2,57 @@
namespace App\Jobs;
use Carbon\Carbon;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Repat\LaravelJobs\Job;
use App\Models\Address;
use App\Traits\HubStats;
class MailSend #implements ShouldQueue
{
use Dispatchable;
use HubStats,Dispatchable;
private const LOGKEY = 'JMS';
private bool $mode;
/**
* @param bool $crash Send crash mail only
*/
public function __construct(private ?bool $crash=NULL) {}
public function __construct(?bool $mode=NULL) {
$this->mode = $mode;
}
/**
* Execute the job.
*/
public function handle(): void
{
$u = Address::select([
'a.id',
'a.system_id',
'a.zone_id',
'addresses.region_id',
'a.host_id',
'a.node_id',
'a.point_id',
'addresses.hub_id',
'addresses.role',
DB::raw('sum(a.uncollected_echomail) as uncollected_echomail'),
DB::raw('sum(a.uncollected_netmail) as uncollected_netmail'),
DB::raw('sum(a.uncollected_files) as uncollected_files')
])
->from(Address::UncollectedEchomailTotal()->union(Address::UncollectedNetmailTotal())->union(Address::UncollectedFilesTotal()),'a')
->where('systems.active',true)
->where('addresses.active',TRUE)
->where('zones.active',TRUE)
->where('domains.active',TRUE)
->join('addresses',['addresses.id'=>'a.id'])
->join('systems',['systems.id'=>'a.system_id'])
->join('zones',['zones.id'=>'a.zone_id'])
->join('domains',['domains.id'=>'zones.domain_id'])
->groupBy('a.system_id','a.id','a.zone_id','addresses.region_id','a.host_id','a.node_id','a.point_id','addresses.hub_id','addresses.role')
->with(['system','zone.domain'])
->get();
// Return the system we poll
$u = $u->transform(function($item) {
if ($x=$item->uplink()) {
$x->uncollected_echomail = $item->uncollected_echomail;
$x->uncollected_netmail = $item->uncollected_netmail;
$x->uncollected_files = $item->uncollected_files;
$u = $this
->HubStats(Carbon::now())
->get()
->transform(function($item) {
// Add the list of queued items to the uplink
if ($x=$item->uplink()) {
$x->uncollected_echomail = $item->uncollected_echomail;
$x->uncollected_netmail = $item->uncollected_netmail;
$x->uncollected_files = $item->uncollected_files;
return $x;
return $x;
} else {
return $item;
}
})
->filter(function($item) {
if ($item->system->autohold)
return NULL;
return is_null($this->crash) || ($item->system->pollmode) || ($item->system->pollmode === $this->crash) ? $item : NULL;
});
} else {
return $item;
}
})
->filter(fn($item)=>(! $item->system->autohold) // System is not on autohold
&& (is_null($this->mode) // If we didnt specify a poll mode, poll anyway
|| ($item->system->pollmode) // If system poll mode is set to crash
|| ($item->system->pollmode === $this->mode))); // If the sytem poll mode is the same as mode calling this send
foreach ($u->groupBy('ftn') as $oo) {
if (Job::where('queue','poll')->get()->pluck('command.address.id')->search(($x=$oo->first())->id) === FALSE) {
if (! Job::where('queue','poll')->get()->pluck('command.address.id')->contains(($x=$oo->first())->id)) {
Log::info(sprintf('%s:- Polling [%s] - we have mail for [%d] links. (%d Netmail,%d Echomail,%d Files)',
self::LOGKEY,
$x->ftn,

View File

@ -450,115 +450,6 @@ class Address extends Model
->orderBy('point_id');
}
/**
* Return a list of addresses and the amount of uncollected echomail
*
* @param $query
* @return mixed
*/
public function scopeUncollectedEchomail($query)
{
return $query
->join('echomail_seenby',['echomail_seenby.address_id'=>'addresses.id'])
->join('echomails',['echomails.id'=>'echomail_seenby.echomail_id'])
->whereNotNull('export_at')
->whereNull('sent_at')
->whereNull('echomails.deleted_at')
->groupBy('addresses.id')
->dontCache();
}
public function scopeUncollectedEchomailTotal($query)
{
return $query
->select([
'addresses.id',
'zone_id',
'host_id',
'node_id',
'point_id',
'system_id',
DB::raw('count(addresses.id) as uncollected_echomail'),
DB::raw('0 as uncollected_netmail'),
DB::raw('0 as uncollected_files'),
])
->UncollectedEchomail();
}
/**
* Return a list of addresses and the amount of uncollected files
*
* @param $query
* @return mixed
*/
public function scopeUncollectedFiles($query)
{
return $query
->join('file_seenby',['file_seenby.address_id'=>'addresses.id'])
->join('files',['files.id'=>'file_seenby.file_id'])
->whereNotNull('export_at')
->whereNull('sent_at')
->whereNull('files.deleted_at')
->groupBy('addresses.id')
->dontCache();
}
public function scopeUncollectedFilesTotal($query)
{
return $query
->select([
'addresses.id',
'zone_id',
'host_id',
'node_id',
'point_id',
'system_id',
DB::raw('0 as uncollected_echomail'),
DB::raw('0 as uncollected_netmail'),
DB::raw('count(addresses.id) as uncollected_files')
])
->UncollectedFiles();
}
public function scopeUncollectedNetmail($query)
{
return $query
->join('netmails',['netmails.tftn_id'=>'addresses.id'])
->where(function($query) {
return $query->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_INTRANSIT))
->orWhereRaw(sprintf('(flags & %d) > 0',Message::FLAG_LOCAL));
})
->whereRaw(sprintf('(flags & %d) = 0',Message::FLAG_SENT))
->whereNull('sent_pkt')
->whereNull('sent_at')
->whereNull('netmails.deleted_at')
->groupBy('addresses.id')
->dontCache();
}
/**
* Return a list of addresses and the amount of uncollected netmail
*
* @param $query
* @return mixed
*/
public function scopeUncollectedNetmailTotal($query)
{
return $query
->select([
'addresses.id',
'zone_id',
'host_id',
'node_id',
'point_id',
'system_id',
DB::raw('0 as uncollected_echomail'),
DB::raw('count(addresses.id) as uncollected_netmail'),
DB::raw('0 as uncollected_files')
])
->UncollectedNetmail();
}
/* RELATIONS */
public function domain()
@ -1029,12 +920,12 @@ class Address extends Model
// If this system is not marked to default route for this address
if (! $this->is_default_route) {
$children = $this->children()
$downlinks = $this->children()
->push($this);
// We route everything for this domain
} else {
$children = self::select('addresses.*')
$downlinks = self::select('addresses.*')
->join('zones',['zones.id'=>'addresses.zone_id'])
->where('addresses.id','<>',$this->id)
->where('domain_id',$this->zone->domain_id)
@ -1045,9 +936,15 @@ class Address extends Model
}
// If there are no children
if (! $children->count())
if (! $downlinks->count())
return new Collection;
// Exclude any children of our addresses
$downlinks = $downlinks
->diff(($x=our_address($this->zone->domain,FALSE))
->flatMap(fn($item)=>$item->children())
->merge($x));
// Exclude links and their children.
$exclude = collect();
foreach (our_nodes($this->zone->domain)->diff([$this]) as $o) {
@ -1060,7 +957,7 @@ class Address extends Model
$exclude->push($o);
}
return $children->diff($exclude);
return $downlinks->diff($exclude);
}
/**
@ -1077,55 +974,55 @@ class Address extends Model
}
/**
* Echomail waiting to be sent to this system
* Echomail waiting to be sent to this address
*
* @return Builder
* @return Collection
*/
public function echomailWaiting(): Builder
public function echomailWaiting(): Collection
{
return Echomail::select('echomails.*')
->join('echomail_seenby',['echomail_seenby.echomail_id'=>'echomails.id'])
return Echomail::Uncollected()
->where('address_id',$this->id)
->whereNull('echomails.deleted_at')
->whereNotNull('export_at')
->whereNull('sent_at')
->orderby('id')
->with([
'tagline:id,value',
'tearline:id,value',
'origin:id,value',
'echoarea:id,name,domain_id',
'echoarea.domain:id,name',
'fftn:id,zone_id,region_id,host_id,node_id,point_id',
'fftn.zone:id,domain_id,zone_id',
'fftn.zone.domain:id,name',
]);
->get();
}
/**
* Files waiting to be sent to this system
* Count of echomail waiting to be sent to this address
*
* @return int
*/
public function echomailWaitingCount(): int
{
return Echomail::Uncollected()
->where('address_id',$this->id)
->count();
}
/**
* Files waiting to be sent to this address
*
* @return Collection
*/
public function filesWaiting(): Collection
{
return File::select('files.*')
->join('file_seenby',['file_seenby.file_id'=>'files.id'])
return File::Uncollected()
->where('address_id',$this->id)
->whereNull('files.deleted_at')
->whereNotNull('export_at')
->whereNull('sent_at')
->orderby('id')
->with([
'filearea:id,name,domain_id',
'filearea.domain:id,name',
'fftn:id,zone_id,region_id,host_id,node_id,point_id',
'fftn.zone:id,domain_id,zone_id',
'fftn.zone.domain:id,name',
])
->get();
}
/**
* Count of files waiting to be sent to this address
*
* @return int
*/
public function filesWaitingCount(): int
{
return File::Uncollected()
->where('address_id',$this->id)
->count();
}
/**
* Work out what role this FTN should have
*
@ -1265,43 +1162,40 @@ class Address extends Model
*/
public function netmailWaiting(): Builder
{
// Addresses that our downstream of this address, except anybody that has session details with us
$ours = our_nodes($this->zone->domain)->pluck('id');
$addresses = $this->downlinks()
->filter(fn($item)=>(! $ours->contains($item->id)))
->merge($this->system->match($this->zone,Address::NODE_ALL));
$netmails = $this
->UncollectedNetmail()
->select('netmails.id')
->whereIn('addresses.id',$addresses->pluck('id'))
->groupBy(['netmails.id'])
->get();
return Netmail::whereIn('id',$netmails->pluck('id'));
return Netmail::Uncollected()
->whereIn('tftn_id',$this->downlinks()
->add($this)
->pluck('id'));
}
/**
* Netmail alerts waiting to be sent to this system
* Count of echomail waiting to be sent to this address
*
* @return int
*/
public function netmailWaitingCount(): int
{
return Netmail::Uncollected()
->whereIn('tftn_id',$this->downlinks()
->add($this)
->pluck('id'))
->count();
}
/**
* Netmail alerts waiting to be sent to this address
*
* @return Builder
* @throws \Exception
* @note The packet password to use is on the subject line for these alerts
* @todo To Test
*/
public function netmailAlertWaiting(): Builder
{
$netmails = $this
->UncollectedNetmail()
->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_LOCAL))
->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_PKTPASSWD))
->whereRaw(sprintf('(flags & %d) = 0',Message::FLAG_SENT))
->select('netmails.id')
->whereIn('addresses.id',$this->downlinks()->add($this)->pluck('id'))
->groupBy(['netmails.id'])
->get();
return Netmail::whereIn('id',$netmails->pluck('id'));
return Netmail::UncollectedAlerts()
->whereIn('tftn_id',$this->downlinks()
->add($this)
->pluck('id'));
}
public function nodes(): Collection

View File

@ -3,6 +3,7 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
@ -279,6 +280,33 @@ final class Echomail extends Model implements Packet
});
}
/* SCOPES */
/**
* Base query to find uncollected echomails
*
* @param Builder $query
* @return Builder
*/
public function scopeUncollected(Builder $query): Builder
{
return $this
->join('echomail_seenby',['echomail_seenby.echomail_id'=>'echomails.id'])
->whereNull('echomails.deleted_at')
->whereNotNull('export_at')
->whereNull('sent_at')
->with([
'tagline:id,value',
'tearline:id,value',
'origin:id,value',
'echoarea:id,name,domain_id',
'echoarea.domain:id,name',
'fftn:id,zone_id,region_id,host_id,node_id,point_id',
'fftn.zone:id,domain_id,zone_id',
'fftn.zone.domain:id,name',
]);
}
/* RELATIONS */
public function echoarea()

View File

@ -3,6 +3,7 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Arr;
@ -174,6 +175,30 @@ class File extends Model
});
}
/* SCOPES */
/**
* Base query to find uncollected files
*
* @param Builder $query
* @return Builder
*/
public function scopeUncollected(Builder $query): Builder
{
return $query
->join('file_seenby',['file_seenby.file_id'=>'files.id'])
->whereNotNull('export_at')
->whereNull('sent_at')
->whereNull('files.deleted_at')
->with([
'filearea:id,name,domain_id',
'filearea.domain:id,name',
'fftn:id,zone_id,region_id,host_id,node_id,point_id',
'fftn.zone:id,domain_id,zone_id',
'fftn.zone.domain:id,name',
]);
}
/* RELATIONS */
public function filearea()

View File

@ -3,6 +3,7 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Arr;
@ -10,6 +11,7 @@ use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Classes\FTN\Message;
use App\Interfaces\Packet;
use App\Models\Casts\{CompressedStringOrNull,CollectionOrNull,UTF8StringOrNull};
use App\Models\Pivots\ViaPivot;
@ -237,6 +239,37 @@ final class Netmail extends Model implements Packet
});
}
/* SCOPES */
/**
* Base query to find uncollected netmails
*
* @param Builder $query
* @return Builder
*/
public function scopeUncollected(Builder $query): Builder
{
return $query
->where(fn($query)=>
$query->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_INTRANSIT))
->orWhereRaw(sprintf('(flags & %d) > 0',Message::FLAG_LOCAL)))
->whereRaw(sprintf('(flags & %d) = 0',Message::FLAG_SENT))
->whereNull('sent_pkt')
->whereNull('sent_at')
->whereNull('netmails.deleted_at');
}
public function scopeUncollectedAlerts(Builder $query): Builder
{
return $query
->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_LOCAL))
->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_PKTPASSWD))
->whereRaw(sprintf('(flags & %d) = 0',Message::FLAG_SENT))
->whereNull('sent_pkt')
->whereNull('sent_at')
->whereNull('netmails.deleted_at');
}
/* RELATIONS */
public function path()

91
app/Traits/HubStats.php Normal file
View File

@ -0,0 +1,91 @@
<?php
namespace App\Traits;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Models\{Address,Echomail,File,Netmail};
trait HubStats
{
private function HubStats(Carbon $asat): Builder
{
return Address::select([
'a.id',
'a.system_id',
'a.zone_id',
'addresses.region_id',
'a.host_id',
'a.node_id',
'a.point_id',
'addresses.hub_id',
'addresses.role',
DB::raw('sum(a.uncollected_echomail) as uncollected_echomail'),
DB::raw('sum(a.uncollected_netmail) as uncollected_netmail'),
DB::raw('sum(a.uncollected_files) as uncollected_files')
])
->from(
Echomail::Uncollected()
->select([
'addresses.id',
'zone_id',
'host_id',
'node_id',
'point_id',
'system_id',
DB::raw('count(addresses.id) as uncollected_echomail'),
DB::raw('0 as uncollected_netmail'),
DB::raw('0 as uncollected_files'),
])
->join('addresses',['addresses.id'=>'echomail_seenby.address_id'])
->where('echomails.created_at','<',$asat)
->groupBy('addresses.id')
->union(Netmail::Uncollected()
->select([
'addresses.id',
'zone_id',
'host_id',
'node_id',
'point_id',
'system_id',
DB::raw('0 as uncollected_echomail'),
DB::raw('count(addresses.id) as uncollected_netmail'),
DB::raw('0 as uncollected_files')
])
->join('addresses',['addresses.id'=>'netmails.tftn_id'])
->where('netmails.created_at','<',$asat)
->groupBy('addresses.id')
)
->union(File::Uncollected()
->select([
'addresses.id',
'zone_id',
'host_id',
'node_id',
'point_id',
'system_id',
DB::raw('0 as uncollected_echomail'),
DB::raw('0 as uncollected_netmail'),
DB::raw('count(addresses.id) as uncollected_files')
])
->join('addresses',['addresses.id'=>'file_seenby.address_id'])
->where('files.created_at','<',$asat)
->groupBy('addresses.id')
),'a')
->where('systems.active',TRUE)
->where('addresses.active',TRUE)
->where('zones.active',TRUE)
->where('domains.active',TRUE)
->when(! ($x=Auth::user()) || (! $x->isAdmin()),fn($query)=>$query->where('domains.public',TRUE))
->join('addresses',['addresses.id'=>'a.id'])
->join('systems',['systems.id'=>'a.system_id'])
->join('zones',['zones.id'=>'addresses.zone_id'])
->join('domains',['domains.id'=>'zones.domain_id'])
->ftnOrder()
->groupBy('a.system_id','a.id','a.zone_id','addresses.region_id','a.host_id','a.node_id','a.point_id','addresses.hub_id','addresses.role')
->with(['system','zone.domain']);
}
}

View File

@ -13,6 +13,11 @@ return Application::configure(basePath: dirname(__DIR__))
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->statefulApi();
})
->withMiddleware(function (Middleware $middleware) {
$middleware->appendToGroup('web', [
AddUserToView::class,

View File

@ -2,7 +2,7 @@
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\{DomainController,MatrixController};
use App\Http\Controllers\{DomainController,MatrixController,UserController};
/*
|--------------------------------------------------------------------------
@ -19,4 +19,7 @@ Route::get('/domain/daily',[DomainController::class,'api_daily_stats']);
Route::any('matrix/{item}',[MatrixController::class,'webhook'])
->where('item', '.*')
->middleware('auth:matrix');
->middleware('auth:matrix');
Route::get('/whoami',[UserController::class,'whoami'])
->middleware(['auth:sanctum']);