Skip to content

Commit

Permalink
Refactor/simplify statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
fisharebest committed Jan 29, 2025
1 parent 408e187 commit bed3d34
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 68 deletions.
7 changes: 2 additions & 5 deletions app/Statistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,9 @@ public function totalGivennames(...$params): string
return $this->individual_repository->totalGivennames(...$params);
}

/**
* @param array<string> $events
*/
public function totalEvents(array $events = []): string
public function totalEvents(): string
{
return $this->event_repository->totalEvents($events);
return $this->event_repository->totalEvents();
}

public function totalEventsBirth(): string
Expand Down
99 changes: 36 additions & 63 deletions app/Statistics/Repository/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,14 @@
use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\Gedcom;
use Fisharebest\Webtrees\GedcomRecord;
use Fisharebest\Webtrees\Header;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Tree;
use Illuminate\Database\Query\JoinClause;

use function abs;
use function array_map;
use function array_merge;
use function e;
use function strncmp;
use function substr;

class EventRepository
{
Expand All @@ -64,51 +59,35 @@ public function __construct(Tree $tree)
}

/**
* Returns the total number of a given list of events (with dates).
*
* @param array<string> $events The list of events to count (e.g. BIRT, DEAT, ...)
* @param array<string> $events
*/
private function getEventCount(array $events): int
private function countEvents(array $events): int
{
$query = DB::table('dates')
->where('d_file', '=', $this->tree->id());

$no_types = [
'HEAD',
'CHAN',
];

if ($events !== []) {
$types = [];

foreach ($events as $type) {
if (strncmp($type, '!', 1) === 0) {
$no_types[] = substr($type, 1);
} else {
$types[] = $type;
}
}

if ($types !== []) {
$query->whereIn('d_fact', $types);
}
}

return $query->whereNotIn('d_fact', $no_types)
return DB::table('dates')
->where('d_file', '=', $this->tree->id())
->whereIn('d_fact', $events)
->count();
}

/**
* @param array<string> $events
*/
public function totalEvents(array $events = []): string
private function countOtherEvents(array $events): int
{
return I18N::number($this->getEventCount($events));
return DB::table('dates')
->where('d_file', '=', $this->tree->id())
->whereNotIn('d_fact', $events)
->count();
}

public function totalEvents(): string
{
return I18N::number($this->countOtherEvents(['CHAN']));
}

public function totalEventsBirth(): string
{
return $this->totalEvents(Gedcom::BIRTH_EVENTS);
return I18N::number($this->countEvents(Gedcom::BIRTH_EVENTS));
}

public function totalBirths(): string
Expand All @@ -118,7 +97,7 @@ public function totalBirths(): string

public function totalEventsDeath(): string
{
return $this->totalEvents(Gedcom::DEATH_EVENTS);
return I18N::number($this->countEvents(Gedcom::DEATH_EVENTS));
}

public function totalDeaths(): string
Expand All @@ -128,7 +107,7 @@ public function totalDeaths(): string

public function totalEventsMarriage(): string
{
return $this->totalEvents(Gedcom::MARRIAGE_EVENTS);
return I18N::number($this->countEvents(Gedcom::MARRIAGE_EVENTS));
}

public function totalMarriages(): string
Expand All @@ -138,38 +117,25 @@ public function totalMarriages(): string

public function totalEventsDivorce(): string
{
return $this->totalEvents(Gedcom::DIVORCE_EVENTS);
return I18N::number($this->countEvents(Gedcom::DIVORCE_EVENTS));
}

public function totalDivorces(): string
{
return I18N::number($this->countFamiliesWithEvents([self::EVENT_DIVORCE]));
}

/**
* Returns the list of common facts used query the data.
*
* @return array<string>
*/
private function getCommonFacts(): array
{
// The list of facts used to limit the query result
return array_merge(
Gedcom::BIRTH_EVENTS,
Gedcom::MARRIAGE_EVENTS,
Gedcom::DIVORCE_EVENTS,
Gedcom::DEATH_EVENTS
);
}

public function totalEventsOther(): string
{
$no_facts = array_map(
static fn (string $fact): string => '!' . $fact,
$this->getCommonFacts()
);
$events = [
'CHAN',
...Gedcom::BIRTH_EVENTS,
...Gedcom::DEATH_EVENTS,
...Gedcom::MARRIAGE_EVENTS,
...Gedcom::DIVORCE_EVENTS,
];

return $this->totalEvents($no_facts);
return I18N::number($this->countOtherEvents($events));
}

/**
Expand All @@ -181,11 +147,18 @@ public function totalEventsOther(): string
*/
private function eventQuery(string $direction): object|null
{
$events = [
...Gedcom::BIRTH_EVENTS,
...Gedcom::DEATH_EVENTS,
...Gedcom::MARRIAGE_EVENTS,
...Gedcom::DIVORCE_EVENTS,
];


return DB::table('dates')
->select(['d_gid as id', 'd_year as year', 'd_fact AS fact', 'd_type AS type'])
->where('d_file', '=', $this->tree->id())
->where('d_gid', '<>', Header::RECORD_TYPE)
->whereIn('d_fact', $this->getCommonFacts())
->whereIn('d_fact', $events)
->where('d_julianday1', '<>', 0)
->orderBy('d_julianday1', $direction)
->orderBy('d_type')
Expand Down

0 comments on commit bed3d34

Please sign in to comment.