Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reminders: cache elapsed time #784

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions app/Jobs/CalendarsClearElapsed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Jobs;

use App\Models\Calendar;
use App\Models\EntityEvent;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;

class CalendarsClearElapsed implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public int $id;
/**
* Create a new job instance.
*/
public function __construct(Calendar $calendar)
{
$this->id = $calendar->id;
}

/**
* Execute the job.
*/
public function handle(): void
{
$calendar = Calendar::find($this->id);
if (!$calendar) {
return;
}

$model = new EntityEvent();
DB::update('UPDATE ' . $model->getTable() . ' SET elapsed = NULL WHERE calendar_id = \'' . $calendar->id . '\'');
}
}
24 changes: 20 additions & 4 deletions app/Models/EntityEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @property int|null $recurring_until
* @property string $recurring_periodicity
* @property int $type_id
* @property int $elapsed
* @property int|null $elapsed
*
* @property Calendar|null $calendar
* @property EntityEvent|null $death
Expand Down Expand Up @@ -244,6 +244,11 @@ public function isPastDate(int $year, int $month, int $day): bool
*/
public function calcElapsed(EntityEvent $event = null): int
{
// Have the value cached? Don't bother with more work
if (empty($event) && !empty($this->elapsed)) {
return $this->elapsed;
}

if (!empty($event)) {
$year = $event->year;
$month = $event->month;
Expand All @@ -262,14 +267,25 @@ public function calcElapsed(EntityEvent $event = null): int
$years = $year - $baseYear;

if ($month < $this->month) {
return $years - 1;
return $this->saveElapsed($years - 1, empty($event));
}
if ($month > $this->month) {
return $years;
return $this->saveElapsed($years, empty($event));
}

// Same month
return $years - ($day < $this->day ? 1 : 0);
return $this->saveElapsed($years - ($day < $this->day ? 1 : 0), empty($event));
}

protected function saveElapsed(int $number, bool $save): int
{
// If comparing two days, don't save the "elapsed" part, we need to re-calc those one each page load
if (!$save) {
return $number;
}
$this->elapsed = $number;
$this->saveQuietly();
return $this->elapsed;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions app/Observers/CalendarObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

namespace App\Observers;

use App\Jobs\CalendarsClearElapsed;
use App\Models\MiscModel;

class CalendarObserver extends MiscObserver
{
public function updated(MiscModel $model)
{
if ($model->isDirty(['date'])) {
CalendarsClearElapsed::dispatch($model);
}
}
}
8 changes: 8 additions & 0 deletions app/Observers/EntityEventObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ public function saving(EntityEvent $entityEvent)
$entityEvent->recurring_until = null;
}
}

public function updating(EntityEvent $entityEvent)
{
// When updating and elapsed isn't dirty (calculated on the overview), reset it
if ($entityEvent->isDirty(['year', 'month', 'day', 'calendar_id'])) {
$entityEvent->elapsed = null;
}
}
}
6 changes: 4 additions & 2 deletions app/Services/Calendars/AdvancerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Services\Calendars;

use App\Jobs\CalendarsClearElapsed;
use App\Models\Calendar;

class AdvancerService
Expand Down Expand Up @@ -42,7 +43,7 @@ public function advance(): self
$year++;
}
$this->calendar->date = $year . '-' . $month . ($day !== false ? '-' . $day : null);
$this->calendar->saveQuietly();
$this->calendar->save();
return $this;
}

Expand All @@ -68,7 +69,8 @@ public function retreat(): self
$year--;
}
$this->calendar->date = $year . '-' . $month . '-' . $day;
$this->calendar->saveQuietly();
$this->calendar->save();
CalendarsClearElapsed::dispatch($this->calendar);
return $this;
}
}
1 change: 1 addition & 0 deletions public/build/assets/app-b968c210.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion public/build/assets/app-c41b2a06.js

This file was deleted.

2 changes: 1 addition & 1 deletion public/build/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"src": "resources/js/api.js"
},
"resources/js/app.js": {
"file": "assets/app-c41b2a06.js",
"file": "assets/app-b968c210.js",
"imports": [
"_tippy.esm-24ef6cb2.js",
"_mention-6dbb4e38.js",
Expand Down
2 changes: 0 additions & 2 deletions resources/js/crud.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,6 @@ const loadCalendarDates = (calendarID) => {
if (data.length === 1) {
entityCalendarMonthField.val(data[0].id);
}

initSpectrum();
});
};

Expand Down
4 changes: 2 additions & 2 deletions resources/views/calendars/events/_entity_form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
</x-grid>


<div class="entity-calendar-subform" style="{{ $onlyOneCalendar ? '' : 'display: none;' }}">
@include('calendars.events._subform', ['colourAppendTo' => $dropdownParent ?? '#primary-dialog'])
<div class="entity-calendar-subform" style="{{ $onlyOneCalendar ? '' : 'display: none' }}">
@include('calendars.events._subform')
</div>

<div class="entity-calendar-loading" style="display: none">
Expand Down
3 changes: 1 addition & 2 deletions resources/views/calendars/events/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

@section('content')
{!! Form::open([
'method' => 'POST',
'route' => ['calendars.event.store', $campaign, $calendar->id],
'method'=>'POST',
'data-shortcut' => 1,
'class' => 'ajax-subform entity-calendar-subform',
'data-maintenance' => 1
Expand All @@ -24,7 +24,6 @@
'content' => 'calendars.events._form',
'dialog' => true,
'dropdownParent' => '#primary-dialog',
'colourAppendTo' => '#primary-dialog',
])

@if (request()->has('layout'))
Expand Down
1 change: 0 additions & 1 deletion resources/views/calendars/events/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
'deleteID' => '#delete-reminder-' . $entityEvent->id,
'dialog' => true,
'dropdownParent' => '#primary-dialog',
'colourAppendTo' => '#primary-dialog',
])


Expand Down
14 changes: 10 additions & 4 deletions resources/views/entities/components/elasped_events.blade.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* @var \App\Models\EntityEvent $event
* @var \App\Models\EntityEvent $birth
* @var \App\Models\EntityEvent $death
* @var \App\Models\EntityEvent|null $birth
* @var \App\Models\EntityEvent|null $death
* @var \App\Models\EntityEvent[] $elapsed
* @var \App\Models\MiscModel $model
*/
Expand Down Expand Up @@ -47,9 +47,15 @@
}
?>
@foreach ($distinctCalendars as $calendarId => $calendarEvents)
@php $birth = $calendarEvents['birth'] ?? null; $death = $calendarEvents['death'] ?? null; @endphp
@php
/**
* @var \App\Models\EntityEvent|null $birth
* @var \App\Models\EntityEvent|null $death
*/
$birth = $calendarEvents['birth'] ?? null;
$death = $calendarEvents['death'] ?? null;
@endphp
@if (!empty($birth) && !empty($death))

<li class="flex">
<div class="flex-grow font-bold">{{ __('characters.fields.life') }}</div>
<div class="flex-grow text-right">
Expand Down
4 changes: 2 additions & 2 deletions resources/views/entities/pages/reminders/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
@can('events', $entity->child)
<div class="header-buttons flex flex-wrap gap-2 items-center justify-end">
<a href="https://docs.kanka.io/en/latest/features/reminders.html" target="_blank" class="btn2 btn-ghost btn-sm">
<x-icon class="question"></x-icon> {{ __('crud.actions.help') }}
<x-icon class="question" /> {{ __('crud.actions.help') }}
</a>
<a href="{{ route('entities.entity_events.create', [$campaign, $entity, 'next' => 'entity.events']) }}" id="entity-calendar-modal-add"
class="btn2 btn-sm" data-toggle="dialog" data-target="primary-dialog"
data-url="{{ route('entities.entity_events.create', [$campaign, $entity, 'next' => 'entity.events']) }}">
<x-icon class="plus"></x-icon> {{ __('entities/events.show.actions.add') }}
<x-icon class="plus" /> {{ __('entities/events.show.actions.add') }}
</a>
</div>
@endcan
Expand Down
Loading