Skip to content

Commit

Permalink
Performance optimization for import
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Jun 10, 2024
1 parent 90480f3 commit 0eef5ff
Show file tree
Hide file tree
Showing 8 changed files with 478 additions and 3 deletions.
21 changes: 21 additions & 0 deletions app/Service/BillableRateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@

class BillableRateService
{
public function getBillableRateForTimeEntryWithGivenRelations(TimeEntry $timeEntry, ?ProjectMember $projectMember, ?Project $project, ?Member $member, ?Organization $organization): ?int
{
if (! $timeEntry->billable) {
return null;
}
if ($projectMember !== null && $projectMember->billable_rate !== null) {
return $projectMember->billable_rate;
}
if ($project !== null && $project->billable_rate !== null) {
return $project->billable_rate;
}
if ($member !== null && $member->billable_rate !== null) {
return $member->billable_rate;
}
if ($organization !== null && $organization->billable_rate !== null) {
return $organization->billable_rate;
}

return null;
}

public function getBillableRateForTimeEntry(TimeEntry $timeEntry): ?int
{
if (! $timeEntry->billable) {
Expand Down
51 changes: 51 additions & 0 deletions app/Service/Import/ImportDatabaseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ class ImportDatabaseHelper
*/
private ?array $mapIdentifierToKey = null;

/**
* @var array<string, TModel|null>|null
*/
private ?array $mapKeyToModel = null;

/**
* @var array<string, TModel|null>|null
*/
private ?array $mapIdentifierToModel = null;

/**
* @var array<string, string>
*/
Expand Down Expand Up @@ -148,6 +158,47 @@ public function getKey(array $identifierData, array $createValues = [], ?string
}
}

/**
* @return TModel
*/
public function getModelById(string $id): ?Model
{
if ($this->mapKeyToModel === null) {
$this->mapKeyToModel = [];
}
if (isset($this->mapKeyToModel[$id])) {
return $this->mapKeyToModel[$id];
}
/** @var TModel|null $model */
$model = $this->getModelInstance()->find($id);
if ($model !== null) {
$this->mapKeyToModel[$id] = $model;
}

return $model;
}

/**
* @param array<string, mixed> $identifierData
* @return TModel|null
*/
public function getModel(array $identifierData): ?Model
{
if ($this->mapIdentifierToModel === null) {
$this->mapIdentifierToModel = [];
}
$hash = $this->getHash($identifierData);
if (isset($this->mapIdentifierToModel[$hash])) {
return $this->mapIdentifierToModel[$hash];
}
$model = $this->getModelInstance()->where($identifierData)->first();
if ($model !== null) {
$this->mapIdentifierToModel[$hash] = $model;
}

return $model;
}

/**
* @param array<string, mixed> $identifierData
*
Expand Down
16 changes: 15 additions & 1 deletion app/Service/Import/Importers/ClockifyTimeEntriesImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function importData(string $data, string $timezone): void
], [
'role' => Role::Placeholder->value,
]);
$member = $this->memberImportHelper->getModelById($memberId);
$clientId = null;
if ($record['Client'] !== '') {
$clientId = $this->clientImportHelper->getKey([
Expand All @@ -72,6 +73,8 @@ public function importData(string $data, string $timezone): void
]);
}
$projectId = null;
$project = null;
$projectMember = null;
if ($record['Project'] !== '') {
$projectId = $this->projectImportHelper->getKey([
'name' => $record['Project'],
Expand All @@ -81,6 +84,11 @@ public function importData(string $data, string $timezone): void
'color' => $this->colorService->getRandomColor(),
'is_billable' => false,
]);
$project = $this->projectImportHelper->getModelById($projectId);
$projectMember = $this->projectMemberImportHelper->getModel([
'project_id' => $projectId,
'member_id' => $memberId,
]);
}
$taskId = null;
if ($record['Task'] !== '') {
Expand Down Expand Up @@ -137,7 +145,13 @@ public function importData(string $data, string $timezone): void
throw new ImportException('End date ("'.$record['End Date'].'") or time ("'.$record['End Time'].'") are invalid');
}
$timeEntry->end = $end->utc();
$timeEntry->setComputedAttributeValue('billable_rate');
$timeEntry->billable_rate = $this->billableRateService->getBillableRateForTimeEntryWithGivenRelations(
$timeEntry,
$projectMember,
$project,
$member,
$this->organization
);
$timeEntry->save();
$this->timeEntriesCreated++;
}
Expand Down
4 changes: 4 additions & 0 deletions app/Service/Import/Importers/DefaultImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Models\Tag;
use App\Models\Task;
use App\Models\User;
use App\Service\BillableRateService;
use App\Service\ColorService;
use App\Service\Import\ImportDatabaseHelper;
use App\Service\TimezoneService;
Expand Down Expand Up @@ -62,6 +63,8 @@ abstract class DefaultImporter implements ImporterContract
*/
protected ImportDatabaseHelper $projectMemberImportHelper;

protected BillableRateService $billableRateService;

public function init(Organization $organization): void
{
$this->organization = $organization;
Expand Down Expand Up @@ -141,6 +144,7 @@ public function init(Organization $organization): void
$this->timeEntriesCreated = 0;
$this->colorService = app(ColorService::class);
$this->timezoneService = app(TimezoneService::class);
$this->billableRateService = app(BillableRateService::class);
}

#[\Override]
Expand Down
16 changes: 15 additions & 1 deletion app/Service/Import/Importers/TogglTimeEntriesImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function importData(string $data, string $timezone): void
], [
'role' => Role::Placeholder->value,
]);
$member = $this->memberImportHelper->getModelById($memberId);
$clientId = null;
if ($record['Client'] !== '') {
$clientId = $this->clientImportHelper->getKey([
Expand All @@ -72,6 +73,8 @@ public function importData(string $data, string $timezone): void
]);
}
$projectId = null;
$project = null;
$projectMember = null;
if ($record['Project'] !== '') {
$projectId = $this->projectImportHelper->getKey([
'name' => $record['Project'],
Expand All @@ -81,6 +84,11 @@ public function importData(string $data, string $timezone): void
'is_billable' => false,
'color' => $this->colorService->getRandomColor(),
]);
$project = $this->projectImportHelper->getModelById($projectId);
$projectMember = $this->projectMemberImportHelper->getModel([
'project_id' => $projectId,
'member_id' => $memberId,
]);
}
$taskId = null;
if ($record['Task'] !== '') {
Expand Down Expand Up @@ -123,7 +131,13 @@ public function importData(string $data, string $timezone): void
throw new ImportException('End date ("'.$record['End date'].'") or time ("'.$record['End time'].'") are invalid');
}
$timeEntry->end = $end->utc();
$timeEntry->setComputedAttributeValue('billable_rate');
$timeEntry->billable_rate = $this->billableRateService->getBillableRateForTimeEntryWithGivenRelations(
$timeEntry,
$projectMember,
$project,
$member,
$this->organization
);
$timeEntry->save();
$this->timeEntriesCreated++;
}
Expand Down
2 changes: 1 addition & 1 deletion config/octane.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
'watch' => [
'app',
'bootstrap',
'config',
'config/**/*.php',
'database/**/*.php',
'public/**/*.php',
'resources/**/*.php',
Expand Down
Loading

0 comments on commit 0eef5ff

Please sign in to comment.