Skip to content

Commit

Permalink
Merge pull request #34 from vc-dhavaljoshi/main
Browse files Browse the repository at this point in the history
Bug fixing and code Improvement
  • Loading branch information
ruchit288 authored Apr 23, 2024
2 parents b54c44f + 9cbde3a commit ff82761
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 106 deletions.
96 changes: 62 additions & 34 deletions src/Livewire/PulseActiveSessions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,48 @@
#[Lazy]
class PulseActiveSessions extends Card
{
public $webLoginCount = [];
public $time;
public $runAt;
public $session;
public $filters;
public $provider;
public array|object $webLoginCount = [];
public string $time;
public string $runAt;
public object $session;
public array $filters;
public string $provider;

/**
* @return void
*/
public function mount()
public function mount(): void
{
$this->filters = authProviders();
$this->session = collect([]);
$this->provider = authProviders()[0];
$this->session = collect();
$this->provider = $this->filters[0];
}

/**
* @return \Illuminate\Contracts\View\View
* @throws \JsonException
*/
public function render()
public function render(): \Illuminate\Contracts\View\View
{
// Get the data out of the Pulse data store.
$this->webLoginCount = Pulse::values('pulse_active_session_' . $this->provider, ['result'])->first();

$this->webLoginCount = $this->webLoginCount
? json_decode($this->webLoginCount->value, associative: true, flags: JSON_THROW_ON_ERROR)
: [];
$this->webCount($this->provider);

if (Livewire::isLivewireRequest()) {
$this->dispatch('servers-chart-update-session', servers: $this->webLoginCount);
$this->dispatch(
'servers-chart-update-session',
servers: $this->webLoginCount
);
}

// Get login count periodically
[$this->session, $this->time, $this->runAt] = $this->remember(fn () => Pulse::graph(
['login_hit', 'api_hit'],
'max',
$this->periodAsInterval(),
));
[$this->session, $this->time, $this->runAt] = $this->pulseGraph();

if (Livewire::isLivewireRequest()) {
$this->session = $this->session->filter(
function ($item, $key) {
return $key == 'active_session_' . $this->provider;
}
$this->session = $this->sessionFilter();
$this->dispatch(
'session-chart-update',
session: $this->session
);
$this->dispatch('session-chart-update', session: $this->session);
}

return View::make('pulse_active_session::livewire.pulse_active_session');
Expand All @@ -68,19 +63,52 @@ function ($item, $key) {
* @return void
* @throws \JsonException
*/
public function filterByProviders($provider)
public function filterByProviders($provider): void
{
$this->provider = $provider;
$this->webCount($this->provider);
$this->session = $this->sessionFilter();
}

$this->webLoginCount = Pulse::values('pulse_active_session_' . $this->provider, ['result'])->first();
/**
* @param string $provider
* @return void
* @throws \JsonException
*/
public function webCount(string $provider): void
{
$this->webLoginCount = json_decode(
Pulse::values(
'pulse_active_session_' . $this->provider,
['result']
)->first()?->value ?: '[]',
true,
JSON_THROW_ON_ERROR
);
}

$this->session = $this->session->filter(function ($item, $key) use ($provider) {
return $key == 'active_session_' . $provider;
});
/**
* @return array
*/
public function pulseGraph(): array
{
return $this->remember(fn() => Pulse::graph(
['login_hit', 'api_hit'],
'max',
$this->periodAsInterval(),
));
}

$this->webLoginCount = $this->webLoginCount
? json_decode($this->webLoginCount->value, associative: true, flags: JSON_THROW_ON_ERROR)
: [];
/**
* @return mixed
*/
public function sessionFilter(): mixed
{
return $this->session->filter(
function ($item, $key) {
return $key == 'active_session_' . $this->provider;
}
);
}

/**
Expand Down
Loading

0 comments on commit ff82761

Please sign in to comment.