mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-08 08:12:17 +01:00
Performance optimization for import
This commit is contained in:
committed by
Constantin Graf
parent
90480f3bb8
commit
0eef5ffcfa
@@ -12,6 +12,27 @@ use App\Models\TimeEntry;
|
||||
|
||||
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) {
|
||||
|
||||
@@ -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>
|
||||
*/
|
||||
@@ -148,6 +158,47 @@ class ImportDatabaseHelper
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
*
|
||||
|
||||
@@ -64,6 +64,7 @@ class ClockifyTimeEntriesImporter extends DefaultImporter
|
||||
], [
|
||||
'role' => Role::Placeholder->value,
|
||||
]);
|
||||
$member = $this->memberImportHelper->getModelById($memberId);
|
||||
$clientId = null;
|
||||
if ($record['Client'] !== '') {
|
||||
$clientId = $this->clientImportHelper->getKey([
|
||||
@@ -72,6 +73,8 @@ class ClockifyTimeEntriesImporter extends DefaultImporter
|
||||
]);
|
||||
}
|
||||
$projectId = null;
|
||||
$project = null;
|
||||
$projectMember = null;
|
||||
if ($record['Project'] !== '') {
|
||||
$projectId = $this->projectImportHelper->getKey([
|
||||
'name' => $record['Project'],
|
||||
@@ -81,6 +84,11 @@ class ClockifyTimeEntriesImporter extends DefaultImporter
|
||||
'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'] !== '') {
|
||||
@@ -137,7 +145,13 @@ class ClockifyTimeEntriesImporter extends DefaultImporter
|
||||
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++;
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ use App\Models\ProjectMember;
|
||||
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;
|
||||
@@ -62,6 +63,8 @@ abstract class DefaultImporter implements ImporterContract
|
||||
*/
|
||||
protected ImportDatabaseHelper $projectMemberImportHelper;
|
||||
|
||||
protected BillableRateService $billableRateService;
|
||||
|
||||
public function init(Organization $organization): void
|
||||
{
|
||||
$this->organization = $organization;
|
||||
@@ -141,6 +144,7 @@ abstract class DefaultImporter implements ImporterContract
|
||||
$this->timeEntriesCreated = 0;
|
||||
$this->colorService = app(ColorService::class);
|
||||
$this->timezoneService = app(TimezoneService::class);
|
||||
$this->billableRateService = app(BillableRateService::class);
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
|
||||
@@ -64,6 +64,7 @@ class TogglTimeEntriesImporter extends DefaultImporter
|
||||
], [
|
||||
'role' => Role::Placeholder->value,
|
||||
]);
|
||||
$member = $this->memberImportHelper->getModelById($memberId);
|
||||
$clientId = null;
|
||||
if ($record['Client'] !== '') {
|
||||
$clientId = $this->clientImportHelper->getKey([
|
||||
@@ -72,6 +73,8 @@ class TogglTimeEntriesImporter extends DefaultImporter
|
||||
]);
|
||||
}
|
||||
$projectId = null;
|
||||
$project = null;
|
||||
$projectMember = null;
|
||||
if ($record['Project'] !== '') {
|
||||
$projectId = $this->projectImportHelper->getKey([
|
||||
'name' => $record['Project'],
|
||||
@@ -81,6 +84,11 @@ class TogglTimeEntriesImporter extends DefaultImporter
|
||||
'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'] !== '') {
|
||||
@@ -123,7 +131,13 @@ class TogglTimeEntriesImporter extends DefaultImporter
|
||||
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++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user