Skip to content

Commit

Permalink
rename task to metaTask everywhere for clarity, create a new table, d…
Browse files Browse the repository at this point in the history
…rop the old one

Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Jan 31, 2024
1 parent 192edc2 commit 11a9537
Show file tree
Hide file tree
Showing 16 changed files with 203 additions and 199 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ include text processing providers to:
* Get an answer from a free prompt
* Reformulate (OpenAi/LocalAi only)
]]> </description>
<version>1.0.4</version>
<version>1.0.5</version>
<licence>agpl</licence>
<author>Julien Veyssier</author>
<namespace>TpAssistant</namespace>
Expand Down
6 changes: 3 additions & 3 deletions lib/Command/CleanupAssistantTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
use Exception;
use OC\Core\Command\Base;
use OCA\TpAssistant\AppInfo\Application;
use OCA\TpAssistant\Db\TaskMapper;
use OCA\TpAssistant\Db\MetaTaskMapper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CleanupAssistantTasks extends Base {
public function __construct(
private TaskMapper $taskMapper,
private MetaTaskMapper $metaTaskMapper,
) {
parent::__construct();
}
Expand All @@ -42,7 +42,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {

$output->writeln('Cleanning up assistant tasks older than ' . $maxAge . ' seconds.');
try {
$cleanedUp = $this->taskMapper->cleanupOldTasks($maxAge);
$cleanedUp = $this->metaTaskMapper->cleanupOldMetaTasks($maxAge);
} catch (Exception $e) {
$output->writeln('Error: ' . $e->getMessage());
return 1;
Expand Down
14 changes: 6 additions & 8 deletions lib/Controller/AssistantController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace OCA\TpAssistant\Controller;

use OCA\TpAssistant\AppInfo\Application;
use OCA\TpAssistant\Db\TaskMapper;
use OCA\TpAssistant\Service\AssistantService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
Expand All @@ -17,12 +16,11 @@
class AssistantController extends Controller {

public function __construct(
string $appName,
IRequest $request,
string $appName,
IRequest $request,
private AssistantService $assistantService,
private IInitialState $initialStateService,
private ?string $userId,
private TaskMapper $taskMapper,
private IInitialState $initialStateService,
private ?string $userId,
) {
parent::__construct($appName, $request);
}
Expand All @@ -34,7 +32,7 @@ public function __construct(
#[NoAdminRequired]
#[NoCSRFRequired]
public function getTextProcessingTaskResultPage(int $taskId): TemplateResponse {

if ($this->userId !== null) {
$task = $this->assistantService->getTextProcessingTask($this->userId, $taskId);
if ($task !== null) {
Expand Down Expand Up @@ -143,7 +141,7 @@ public function parseTextFromFile(string $filePath): DataResponse {
if ($this->userId === null) {
return new DataResponse('Unknow user', Http::STATUS_BAD_REQUEST);
}

try {
$text = $this->assistantService->parseTextFromFile($filePath, $this->userId);
} catch (\Exception | \Throwable $e) {
Expand Down
30 changes: 15 additions & 15 deletions lib/Controller/SpeechToTextController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
use Exception;
use InvalidArgumentException;
use OCA\TpAssistant\AppInfo\Application;
use OCA\TpAssistant\Db\Task;
use OCA\TpAssistant\Db\TaskMapper;
use OCA\TpAssistant\Db\MetaTask;
use OCA\TpAssistant\Db\MetaTaskMapper;
use OCA\TpAssistant\Service\SpeechToText\SpeechToTextService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
Expand All @@ -48,14 +48,14 @@
class SpeechToTextController extends Controller {

public function __construct(
string $appName,
IRequest $request,
string $appName,
IRequest $request,
private SpeechToTextService $service,
private LoggerInterface $logger,
private IL10N $l10n,
private IInitialState $initialState,
private ?string $userId,
private TaskMapper $taskMapper,
private LoggerInterface $logger,
private IL10N $l10n,
private IInitialState $initialState,
private ?string $userId,
private MetaTaskMapper $metaTaskMapper,
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -101,17 +101,17 @@ public function getTranscript(int $id): DataResponse {
* Internal function to get transcription assistant tasks based on the assistant meta task id
*
* @param integer $id
* @return Task
* @return MetaTask
*/
private function internalGetTask(int $id): Task {
private function internalGetTask(int $id): MetaTask {
try {
$task = $this->taskMapper->getTaskOfUser($id, $this->userId);
if($task->getCategory() !== Application::TASK_CATEGORY_SPEECH_TO_TEXT) {
$metaTask = $this->metaTaskMapper->getMetaTaskOfUser($id, $this->userId);

if($metaTask->getCategory() !== Application::TASK_CATEGORY_SPEECH_TO_TEXT) {
throw new Exception('Task is not a speech to text task.', Http::STATUS_BAD_REQUEST);
}

return $task;
return $metaTask;
} catch (MultipleObjectsReturnedException $e) {
$this->logger->error('Multiple tasks found for one id: ' . $e->getMessage(), ['app' => Application::APP_ID]);
throw new Exception($this->l10n->t('Multiple tasks found'), Http::STATUS_BAD_REQUEST);
Expand Down
8 changes: 4 additions & 4 deletions lib/Cron/CleanupAssistantTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
namespace OCA\TpAssistant\Cron;

use Exception;
use OCA\TpAssistant\Db\TaskMapper;
use OCA\TpAssistant\Db\MetaTaskMapper;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use Psr\Log\LoggerInterface;
use RuntimeException;

class CleanupAssistantTasks extends TimedJob {
public function __construct(
ITimeFactory $time,
ITimeFactory $time,
private LoggerInterface $logger,
private TaskMapper $taskMapper,
private MetaTaskMapper $metaTaskMapper,
) {
parent::__construct($time);
$this->setInterval(60 * 60 * 24);
Expand All @@ -27,7 +27,7 @@ protected function run($argument): void {
$this->logger->debug('Run cleanup job for assistant tasks');

try {
$this->taskMapper->cleanupOldTasks();
$this->metaTaskMapper->cleanupOldMetaTasks();
} catch (\OCP\Db\Exception | RuntimeException | Exception $e) {
$this->logger->debug('Cleanup job for assistant tasks failed: ' . $e->getMessage());
}
Expand Down
50 changes: 25 additions & 25 deletions lib/Db/Task.php → lib/Db/MetaTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@
use OCP\AppFramework\Db\Entity;

/**
* @method string getUserId()
* @method void setUserId(string $userId)
* @method string getOutput()
* @method void setOutput(string $value)
* @method string getAppId()
* @method void setAppId(string $appId)
* @method int getOcpTaskId()
* @method void setOcpTaskId(int $value)
* @method int getTimestamp()
* @method void setTimestamp(int $timestamp)
* @method string getTaskType()
* @method void setTaskType(string $taskType)
* @method void setStatus(int $status)
* @method int getStatus()
* @method void setCategory(int $category)
* @method int getCategory()
* @method string getInputs()
* @method void setInputs(string $inputs)
* @method string getIndentifer()
* @method void setIndentifer(string $indentifer)
* @method \string getUserId()
* @method \void setUserId(string $userId)
* @method \string getOutput()
* @method \void setOutput(string $value)
* @method \string getAppId()
* @method \void setAppId(string $appId)
* @method \int getOcpTaskId()
* @method \void setOcpTaskId(int $value)
* @method \int getTimestamp()
* @method \void setTimestamp(int $timestamp)
* @method \string getTaskType()
* @method \void setTaskType(string $taskType)
* @method \void setStatus(int $status)
* @method \int getStatus()
* @method \void setCategory(int $category)
* @method \int getCategory()
* @method \string getInputs()
* @method \void setInputs(string $inputs)
* @method \string getIdentifier()
* @method \void setIdentifier(string $identifier)
*/
class Task extends Entity implements \JsonSerializable {
class MetaTask extends Entity implements \JsonSerializable {
/** @var string */
protected $userId;
/** @var string */
Expand All @@ -50,7 +50,7 @@ class Task extends Entity implements \JsonSerializable {
/** @var int */
protected $category;
/** @var string */
protected $indentifer;
protected $identifier;

public function __construct() {
$this->addType('user_id', 'string');
Expand All @@ -62,7 +62,7 @@ public function __construct() {
$this->addType('task_type', 'string');
$this->addType('status', 'integer');
$this->addType('category', 'integer');
$this->addType('indentifer', 'string');
$this->addType('identifier', 'string');
}

#[\ReturnTypeWillChange]
Expand All @@ -78,7 +78,7 @@ public function jsonSerialize() {
'timestamp' => $this->timestamp,
'status' => $this->status,
'category' => $this->category,
'indentifer' => $this->indentifer,
'identifier' => $this->identifier,
];
}

Expand All @@ -95,7 +95,7 @@ public function jsonSerializeCc() {
'timestamp' => $this->timestamp,
'status' => $this->status,
'category' => $this->category,
'indentifer' => $this->indentifer,
'identifier' => $this->identifier,
];
}

Expand Down
38 changes: 19 additions & 19 deletions lib/Db/TaskMapper.php → lib/Db/MetaTaskMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@
use OCP\IDBConnection;

/**
* @extends QBMapper<Task>
* @extends QBMapper<MetaTask>
*/
class TaskMapper extends QBMapper {
class MetaTaskMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'assistant_text_tasks', Task::class);
parent::__construct($db, 'assistant_meta_tasks', MetaTask::class);
}

/**
* @param int $id
* @return Task
* @return MetaTask
* @throws DoesNotExistException
* @throws Exception
* @throws MultipleObjectsReturnedException
*/
public function getTask(int $id): Task {
public function getMetaTask(int $id): MetaTask {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
Expand All @@ -46,12 +46,12 @@ public function getTask(int $id): Task {
/**
* @param int $ocpTaskId
* @param int $category
* @return Task
* @return MetaTask
* @throws DoesNotExistException
* @throws Exception
* @throws MultipleObjectsReturnedException
*/
public function getTaskByOcpTaskIdAndCategory(int $ocpTaskId, int $category): Task {
public function getMetaTaskByOcpTaskIdAndCategory(int $ocpTaskId, int $category): MetaTask {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
Expand Down Expand Up @@ -80,10 +80,10 @@ public function getTaskByOcpTaskIdAndCategory(int $ocpTaskId, int $category): Ta
/**
* @param int $ocpTaskId
* @param int $category
* @return array<Task>
* @return array<MetaTask>
* @throws Exception
*/
public function getTasksByOcpTaskIdAndCategory(int $ocpTaskId, int $category): array {
public function getMetaTasksByOcpTaskIdAndCategory(int $ocpTaskId, int $category): array {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
Expand Down Expand Up @@ -115,12 +115,12 @@ public function getTasksByOcpTaskIdAndCategory(int $ocpTaskId, int $category): a
/**
* @param int $id
* @param string $userId
* @return Task
* @return MetaTask
* @throws DoesNotExistException
* @throws Exception
* @throws MultipleObjectsReturnedException
*/
public function getTaskOfUser(int $id, string $userId): Task {
public function getMetaTaskOfUser(int $id, string $userId): MetaTask {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
Expand All @@ -139,7 +139,7 @@ public function getTaskOfUser(int $id, string $userId): Task {
* @return array
* @throws Exception
*/
public function getTasksOfUser(string $userId): array {
public function getMetaTasksOfUser(string $userId): array {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
Expand All @@ -162,18 +162,18 @@ public function getTasksOfUser(string $userId): array {
* @param int $status
* @param int $category
* @param string $identifier
* @return Task
* @return MetaTask
* @throws Exception
*/
public function createTask(
public function createMetaTask(
string $userId, array $inputs, ?string $output, ?int $timestamp = null, ?int $ocpTaskId = null,
?string $taskType = null, ?string $appId = null, int $status = 0, int $category = 0, string $identifier = ''
): Task {
): MetaTask {
if ($timestamp === null) {
$timestamp = (new DateTime())->getTimestamp();
}

$task = new Task();
$task = new MetaTask();
$task->setUserId($userId);
$task->setInputs(json_encode($inputs));
$task->setTimestamp($timestamp);
Expand All @@ -183,7 +183,7 @@ public function createTask(
$task->setAppId($appId);
$task->setStatus($status);
$task->setCategory($category);
$task->setIndentifer($identifier);
$task->setIdentifier($identifier);
return $this->insert($task);
}

Expand All @@ -192,7 +192,7 @@ public function createTask(
* @return void
* @throws Exception
*/
public function deleteUserTasks(string $userId): void {
public function deleteUserMetaTasks(string $userId): void {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->where(
Expand All @@ -210,7 +210,7 @@ public function deleteUserTasks(string $userId): void {
* @throws Exception
* @throws \RuntimeException
*/
public function cleanupOldTasks(int $olderThanSeconds = Application::DEFAULT_ASSISTANT_TASK_IDLE_TIME): int {
public function cleanupOldMetaTasks(int $olderThanSeconds = Application::DEFAULT_ASSISTANT_TASK_IDLE_TIME): int {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->where(
Expand Down
Loading

0 comments on commit 11a9537

Please sign in to comment.