Implemented Dynamic Items for data to be sent to polled systems based on data in db, like stats/nodelists

This commit is contained in:
Deon George 2023-12-03 18:18:05 +11:00
parent 8f3d77b04d
commit 1890b66dc7
10 changed files with 366 additions and 4 deletions

12
app/Classes/Dynamic.php Normal file
View File

@ -0,0 +1,12 @@
<?php
namespace App\Classes;
/**
* Dynamic files that are sent to systems during a mailer session
*/
abstract class Dynamic
{
abstract public function __toString(): string;
abstract public function getName(): string;
}

View File

@ -0,0 +1,91 @@
<?php
namespace App\Classes\Dynamic;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Classes\Dynamic;
use App\Models\Address;
/**
* This method will generate the hub status for an upstream Host/RC/ZC
*
* Arg is a collection of arguments. We only understand
* + name - name of file to give to remote
*/
class HubStats extends Dynamic
{
private const LOGKEY = 'DHS';
private string $name = '';
public function __construct(private Address $ao,Collection $arg)
{
Log::debug(sprintf('%s:- Generating Hub Stats for [%s] with arguments',self::LOGKEY,$ao->ftn),['args'=>$arg]);
$this->name = $arg->get('name');
}
public function __toString(): string
{
$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::UncollectedEchomail()->union(Address::UncollectedNetmail())->union(Address::UncollectedFiles()),'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']);
$header = "| %-12s | %4d | %3d | %3d | %16s | %5s | %5s |\r\n";
$output = sprintf("Hub Status for [%s] as at [%s]\r\n",our_address($this->ao->zone)->where('active',TRUE)->first()->ftn,$date);
$output .= "\r";
$output .= "+--------------+------+-----+-----+------------------+-------+-------+\r\n";
$output .= "| FTN | ECHO | NET |FILES| LAST SESSION | MODE |AUTOHLD|\r\n";
$output .= "+--------------+------+-----+-----+------------------+-------+-------+\r\n";
foreach($r->get() as $o) {
$output .= sprintf($header,
$o->ftn4d,
$o->uncollected_echomail ?? 0,
$o->uncollected_netmail ?? 0,
$o->uncollected_files ?? 0,
$o->system->last_session?->format('Y-m-d H:i'),
is_null($o->system->pollmode) ? 'HOLD' : ($o->system->pollmode ? 'CRASH' : 'DAILY'),
$o->system->autohold ? 'YES' : 'NO');
}
$output .= "+--------------+------+-----+-----+------------------+-------+-------+\r\n";
return $output;
}
public function getName(): string
{
return $this->name ?: 'hubstats.txt';
}
}

View File

@ -6,6 +6,7 @@ use Exception;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use League\Flysystem\UnreadableFileEncountered; use League\Flysystem\UnreadableFileEncountered;
use App\Classes\File\Send\Dynamic;
use App\Classes\Node; use App\Classes\Node;
use App\Models\Address; use App\Models\Address;
@ -129,6 +130,31 @@ class Send extends Base
$this->index = NULL; $this->index = NULL;
} }
public function dynamic(Address $ao): bool
{
$file = FALSE;
// If the node is marked as hold - dont send any files.
if ($ao->system->hold) {
Log::info(sprintf('%s: - System [%d] is marked as hold - not checking for files.',self::LOGKEY,$ao->system_id));
return FALSE;
}
// Files
if (($x=$ao->dynamicWaiting())->count()) {
Log::debug(sprintf('%s:- [%d] Dynamic Files(s) added for sending to [%s]',self::LOGKEY,$x->count(),$ao->ftn));
// Add Files
foreach ($x as $do)
$this->list->push(new Dynamic($do,$ao,self::T_FILE));
$file = TRUE;
}
return $file;
}
/* /*
private function compress(string $comp_mode): void private function compress(string $comp_mode): void
{ {

View File

@ -0,0 +1,124 @@
<?php
namespace App\Classes\File\Send;
use Carbon\Carbon;
use App\Classes\File\Send;
use App\Classes\Node;
use App\Models\Address;
use App\Models\Dynamic as Model;
use App\Classes\Dynamic as Item;
/**
* Dynamic files that are sent to systems during a mailer session
*/
final class Dynamic extends Send
{
private const LOGKEY = 'FSD';
/** @var int Our internal position counter */
private int $readpos = 0;
private string $buffer;
private Item $item;
private Carbon $sent;
/**
* @throws \Exception
*/
public function __construct(private Model $do,Address $ao,int $type)
{
parent::__construct();
$this->ftype = ((($type&0xff)<<8)|self::IS_FILE);
$this->item = new $this->do->model($ao,$this->do->arguments);
$this->sent = Carbon::now();
}
public function __get($key) {
switch ($key) {
case 'dbids':
return collect([$this->do->id]);
case 'nameas':
return $this->item->getName();
case 'mtime':
return $this->sent->timestamp;
case 'size':
return strlen($this->buffer);
default:
return NULL;
}
}
public function close(bool $successful,Node $node): void
{
if ($successful) {
$this->complete = TRUE;
$next_at = $this->do->next_at
->startOfDay()
->addHours($this->do->start_time->hour)
->addMinutes($this->do->start_time->minute);
switch ($this->do->frequency) {
case 'ONCE':
$this->do->active = FALSE;
break;
case 'DAILY':
$this->do->next_at = $next_at
->addDay();
break;
case 'WEEKLY':
$this->do->next_at = $next_at
->addWeek();
break;
case 'MONTHLY':
$this->do->next_at = $next_at
->addMonth();
break;
default:
throw new \Exception(sprintf('%s:! Unknown frequency [%s] for [%d]',self::LOGKEY,$this->do->frequency,$this->do->id));
}
$this->do->save();
}
}
public function feof(): bool
{
return ($this->readpos === $this->size);
}
public function open(string $compress=''): bool
{
$this->buffer = (string)$this->item;
return TRUE;
}
public function read(int $length): string
{
$result = substr($this->buffer,$this->readpos,$length);
$this->readpos += strlen($result);
return $result;
}
public function seek(int $pos): bool
{
$this->readpos = ($pos < $this->size) ? $pos : $this->size;
return TRUE;
}
}

View File

@ -1489,6 +1489,7 @@ final class Binkp extends BaseProtocol
$this->send->mail($ao); $this->send->mail($ao);
$this->send->files($ao); $this->send->files($ao);
$this->send->dynamic($ao);
/* /*
* Add "dynamic files", eg: nodelist, nodelist segment, status reports. * Add "dynamic files", eg: nodelist, nodelist segment, status reports.

View File

@ -272,6 +272,7 @@ class HomeController extends Controller
->join('systems',['systems.id'=>'addresses.system_id']) ->join('systems',['systems.id'=>'addresses.system_id'])
->join('zones',['zones.id'=>'addresses.zone_id']) ->join('zones',['zones.id'=>'addresses.zone_id'])
->join('domains',['domains.id'=>'zones.domain_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') ->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']); ->with(['system','zone.domain']);

View File

@ -242,6 +242,11 @@ class Address extends Model
->with(['zone.domain']); ->with(['zone.domain']);
} }
public function dynamics()
{
return $this->hasMany(Dynamic::class);
}
/** /**
* Echoareas this address is subscribed to * Echoareas this address is subscribed to
* *
@ -691,6 +696,19 @@ class Address extends Model
} }
} }
/**
* Files waiting to be sent to this system
*
* @return Collection
*/
public function dynamicWaiting(): Collection
{
return $this->dynamics()
->where('next_at','<=',Carbon::now())
->where('active',TRUE)
->get();
}
/** /**
* Echomail waiting to be sent to this system * Echomail waiting to be sent to this system
* *

32
app/Models/Dynamic.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Rennokki\QueryCache\Traits\QueryCacheable;
use App\Casts\CollectionOrNull;
class Dynamic extends Model
{
use SoftDeletes,QueryCacheable;
protected $casts = [
'arguments' => CollectionOrNull::class,
'next_at' => 'datetime:Y-m-d H:i:s',
'start_date' => 'datetime:Y-m-d',
'start_time' => 'datetime:H:i:s',
];
/* RELATIONS */
public function address()
{
return $this->belongsTo(Address::class);
}
/* ATTRIBUTES */
/* METHODS */
}

View File

@ -1,12 +1,13 @@
<?php <?php
/**
* Calculate CCITT-CRC16 checksum
*/
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use App\Models\{Setup,Zone};
/**
* Calculate CCITT-CRC16 checksum
*/
if (! function_exists('crc16')) { if (! function_exists('crc16')) {
function crc16($data): int function crc16($data): int
{ {
@ -80,6 +81,22 @@ if (! function_exists('hexstr')) {
} }
} }
/**
* Return our addresses.
* If zone provided, limit the list to those within the zone
*
* @param Zone|NULL $zo
* @return Collection
*/
function our_address(Zone $zo=NULL): Collection
{
$our = Setup::findOrFail(config('app.id'))->system->addresses;
return $zo
? $our->filter(function($item) use ($zo) { return $item->zone_id === $zo->id; })
: $our;
}
if (! function_exists('timew')) { if (! function_exists('timew')) {
/** /**
* Convert a time into an 32 bit value. This is primarily used to create 8 character hex filenames that * Convert a time into an 32 bit value. This is primarily used to create 8 character hex filenames that

View File

@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('dynamics',function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('name')->unqiue();
$table->enum('frequency',['ONCE','DAILY','WEEKLY','MONTHLY'])->unsigned();
$table->date('start_date');
$table->time('start_time');
$table->datetime('next_at');
$table->string('model');
$table->json('arguments')->nullable();
$table->boolean('active')->nullable();
$table->bigInteger('address_id');
$table->foreign('address_id')->references('id')->on('addresses');
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('dynamics');
}
};