Skip to content

Commit 2d3a2f9

Browse files
feat(app): add update_opened_at method for workflows
This method simply sets the `opened_at` attribute to the current time. Previously `opened_at` was set when calling `get`, but that is not correct. We `get` workflows often, even when not opening them. So this needs to be a separate thing
1 parent 0088376 commit 2d3a2f9

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

invokeai/app/api/routers/workflows.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,14 @@ async def get_counts(
229229
"""Gets a the count of workflows that include the specified tags and categories"""
230230

231231
return ApiDependencies.invoker.services.workflow_records.get_counts(tags=tags, categories=categories)
232+
233+
234+
@workflows_router.put(
235+
"/i/{workflow_id}/opened_at",
236+
operation_id="update_opened_at",
237+
)
238+
async def update_opened_at(
239+
workflow_id: str = Path(description="The workflow to update"),
240+
) -> None:
241+
"""Updates the opened_at field of a workflow"""
242+
ApiDependencies.invoker.services.workflow_records.update_opened_at(workflow_id)

invokeai/app/services/workflow_records/workflow_records_base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,8 @@ def get_counts(
5858
) -> int:
5959
"""Gets the count of workflows for the given tags and categories."""
6060
pass
61+
62+
@abstractmethod
63+
def update_opened_at(self, workflow_id: str) -> None:
64+
"""Open a workflow."""
65+
pass

invokeai/app/services/workflow_records/workflow_records_sqlite.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
)
2020
from invokeai.app.util.misc import uuid_string
2121

22+
SQL_TIME_FORMAT = "%Y-%m-%d %H:%M:%f"
23+
2224

2325
class SqliteWorkflowRecordsStorage(WorkflowRecordsStorageBase):
2426
def __init__(self, db: SqliteDatabase) -> None:
@@ -32,15 +34,6 @@ def start(self, invoker: Invoker) -> None:
3234
def get(self, workflow_id: str) -> WorkflowRecordDTO:
3335
"""Gets a workflow by ID. Updates the opened_at column."""
3436
cursor = self._conn.cursor()
35-
cursor.execute(
36-
"""--sql
37-
UPDATE workflow_library
38-
SET opened_at = STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')
39-
WHERE workflow_id = ?;
40-
""",
41-
(workflow_id,),
42-
)
43-
self._conn.commit()
4437
cursor.execute(
4538
"""--sql
4639
SELECT workflow_id, workflow, name, created_at, updated_at, opened_at
@@ -286,6 +279,22 @@ def get_counts(
286279
cursor.execute(stmt, tuple(params))
287280
return cursor.fetchone()[0]
288281

282+
def update_opened_at(self, workflow_id: str) -> None:
283+
try:
284+
cursor = self._conn.cursor()
285+
cursor.execute(
286+
f"""--sql
287+
UPDATE workflow_library
288+
SET opened_at = STRFTIME('{SQL_TIME_FORMAT}', 'NOW')
289+
WHERE workflow_id = ?;
290+
""",
291+
(workflow_id,),
292+
)
293+
self._conn.commit()
294+
except Exception:
295+
self._conn.rollback()
296+
raise
297+
289298
def _sync_default_workflows(self) -> None:
290299
"""Syncs default workflows to the database. Internal use only."""
291300

0 commit comments

Comments
 (0)