Skip to content

Commit

Permalink
Translate attributes of one produt at once. Refactor translation process
Browse files Browse the repository at this point in the history
  • Loading branch information
drejmanMacopedia committed Jun 5, 2023
1 parent ddb011e commit e247a54
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Macopedia\OpenAiTranslator\Connector\Job\JobParameters\ConstraintCollectionProvider;

use Akeneo\Tool\Component\Batch\Job\JobInterface;
use Akeneo\Tool\Component\Batch\Job\JobParameters\ConstraintCollectionProviderInterface;
use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Type;

class TranslateAttributes implements ConstraintCollectionProviderInterface
{
/**
* @param array<string> $supportedJobNames
*/
public function __construct(
private array $supportedJobNames,
) {
}

/**
* {@inheritdoc}
*/
public function getConstraintCollection(): Collection
{
return new Collection(
[
'fields' => [
'filters' => new NotNull(),
'actions' => new NotNull(),
'realTimeVersioning' => new Type('bool'),
'users_to_notify' => [
new Type('array'),
new All(new Type('string')),
],
'is_user_authenticated' => new Type('bool'),
'open_ai_key' => new NotNull(),
]
]
);
}

/**
* {@inheritdoc}
*/
public function supports(JobInterface $job): bool
{
return in_array($job->getName(), $this->supportedJobNames);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Macopedia\OpenAiTranslator\Connector\Job\JobParameters\DefaultValueProvider;

use Akeneo\Tool\Component\Batch\Job\JobInterface;
use Akeneo\Tool\Component\Batch\Job\JobParameters\DefaultValuesProviderInterface;

class TranslateAttributes implements DefaultValuesProviderInterface
{
/**
* @param array<string> $supportedJobNames
*/
public function __construct(
private array $supportedJobNames,
private ?string $openAiKey
) {
}

/**
* {@inheritdoc}
*/
public function getDefaultValues(): array
{
return [
'filters' => [],
'actions' => [],
'realTimeVersioning' => true,
'users_to_notify' => [],
'is_user_authenticated' => false,
'open_ai_key' => $this->openAiKey ?? ''
];
}

/**
* {@inheritdoc}
*/
public function supports(JobInterface $job): bool
{
return in_array($job->getName(), $this->supportedJobNames);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Macopedia\OpenAiTranslator\Connector\Tasklet;

use Akeneo\Tool\Component\Batch\Job\BatchStatus;
use Akeneo\Tool\Component\Batch\Model\StepExecution;
use Akeneo\Tool\Component\Connector\Step\TaskletInterface;

class ValidateOpenAiKeyTasklet implements TaskletInterface
{
private StepExecution $stepExecution;

public function execute(): void
{
if (empty($this->stepExecution->getJobParameters()->get('open_ai_key'))) {
$this->stepExecution->addFailureException(new \Exception('OpenAI key is not set'));
$this->stepExecution->setStatus(new BatchStatus(BatchStatus::FAILED));
}
}

public function setStepExecution(StepExecution $stepExecution): void
{
$this->stepExecution = $stepExecution;
}
}
29 changes: 22 additions & 7 deletions src/Macopedia/OpenAiTranslator/Resources/config/connector.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
parameters:
maco.open_ai_translator.job: 'update_product_translations'
services:
Macopedia\OpenAiTranslator\Connector\Processor\MassEdit\TranslateAttributesProcessor:
arguments:
Expand All @@ -6,24 +8,27 @@ services:
macopedia.job.update_product_translations:
class: '%pim_connector.job.simple_job.class%'
arguments:
- 'update_product_translations'
- '%maco.open_ai_translator.job%'
- '@event_dispatcher'
- '@akeneo_batch.job_repository'
- [ '@macopedia.step.update_product_translations.mass_edit' ]
-
- '@macopedia.step.validate_openai_key_tasklet'
- '@macopedia.step.update_product_translations.mass_edit'
tags:
- { name: akeneo_batch.job, connector: 'Macopedia OpenAi Connector', type: '%pim_enrich.job.mass_edit_type%' }

macopedia.job.default_values_provider.translate_product:
class: Akeneo\Pim\Enrichment\Component\Product\Connector\Job\JobParameters\DefaultValueProvider\ProductMassEdit
class: Macopedia\OpenAiTranslator\Connector\Job\JobParameters\DefaultValueProvider\TranslateAttributes
arguments:
- [ 'update_product_translations' ]
- [ '%maco.open_ai_translator.job%' ]
- '%open_ai_key%'
tags:
- { name: akeneo_batch.job.job_parameters.default_values_provider }

macopedia.job.constraint_collection_provider.translate_product:
class: 'Akeneo\Pim\Enrichment\Component\Product\Connector\Job\JobParameters\ConstraintCollectionProvider\ProductMassEdit'
class: Macopedia\OpenAiTranslator\Connector\Job\JobParameters\ConstraintCollectionProvider\TranslateAttributes
arguments:
- [ 'update_product_translations' ]
- [ '%maco.open_ai_translator.job%' ]
tags:
- { name: akeneo_batch.job.job_parameters.constraint_collection_provider }

Expand All @@ -36,4 +41,14 @@ services:
- '@pim_enrich.reader.database.product_and_product_model'
- '@Macopedia\OpenAiTranslator\Connector\Processor\MassEdit\TranslateAttributesProcessor'
- '@pim_enrich.writer.database.product_and_product_model_writer'
- '%pim_job_product_batch_size%'
- '%pim_job_product_batch_size%'

Macopedia\OpenAiTranslator\Connector\Tasklet\ValidateOpenAiKeyTasklet: ~

macopedia.step.validate_openai_key_tasklet:
class: '%pim_connector.step.tasklet.class%'
arguments:
- 'validate_openai_key'
- '@event_dispatcher'
- '@akeneo_batch.job_repository'
- '@Macopedia\OpenAiTranslator\Connector\Tasklet\ValidateOpenAiKeyTasklet'

0 comments on commit e247a54

Please sign in to comment.