use entity.mtime for updated at in api

Signed-off-by: phernandez <paul@basicmachines.co>
This commit is contained in:
phernandez
2025-11-29 13:21:58 -06:00
parent b5d4fb559c
commit 7d763a66ff
3 changed files with 42 additions and 7 deletions
@@ -25,6 +25,17 @@ from datetime import datetime
router = APIRouter(prefix="/resource", tags=["resources"])
def _mtime_to_datetime(entity: EntityModel) -> datetime:
"""Convert entity mtime (file modification time) to datetime.
Returns the file's actual modification time, falling back to updated_at
if mtime is not available.
"""
if entity.mtime:
return datetime.fromtimestamp(entity.mtime).astimezone()
return entity.updated_at
def get_entity_ids(item: SearchIndexRow) -> set[int]:
match item.type:
case SearchItemType.ENTITY:
@@ -97,7 +108,7 @@ async def get_resource_content(
# Read content for each entity
content = await file_service.read_entity_content(result)
memory_url = normalize_memory_url(result.permalink)
modified_date = result.updated_at.isoformat()
modified_date = _mtime_to_datetime(result).isoformat()
checksum = result.checksum[:8] if result.checksum else ""
# Prepare the delimited content
+15 -2
View File
@@ -3,7 +3,9 @@
import fnmatch
import logging
import os
from datetime import datetime
from typing import Dict, List, Optional, Sequence
import logfire
from basic_memory.models import Entity
@@ -13,6 +15,17 @@ from basic_memory.schemas.directory import DirectoryNode
logger = logging.getLogger(__name__)
def _mtime_to_datetime(entity: Entity) -> datetime:
"""Convert entity mtime (file modification time) to datetime.
Returns the file's actual modification time, falling back to updated_at
if mtime is not available.
"""
if entity.mtime:
return datetime.fromtimestamp(entity.mtime).astimezone()
return entity.updated_at
class DirectoryService:
"""Service for working with directory trees."""
@@ -79,7 +92,7 @@ class DirectoryService:
entity_id=file.id,
entity_type=file.entity_type,
content_type=file.content_type,
updated_at=file.updated_at,
updated_at=_mtime_to_datetime(file),
)
# Add to parent directory's children
@@ -245,7 +258,7 @@ class DirectoryService:
entity_id=file.id,
entity_type=file.entity_type,
content_type=file.content_type,
updated_at=file.updated_at,
updated_at=_mtime_to_datetime(file),
)
# Add to parent directory's children
+15 -4
View File
@@ -17,6 +17,17 @@ from basic_memory.schemas.search import SearchQuery, SearchItemType
from basic_memory.services import FileService
def _mtime_to_datetime(entity: Entity) -> datetime:
"""Convert entity mtime (file modification time) to datetime.
Returns the file's actual modification time, falling back to updated_at
if mtime is not available.
"""
if entity.mtime:
return datetime.fromtimestamp(entity.mtime).astimezone()
return entity.updated_at
class SearchService:
"""Service for search operations.
@@ -200,7 +211,7 @@ class SearchService:
"entity_type": entity.entity_type,
},
created_at=entity.created_at,
updated_at=entity.updated_at,
updated_at=_mtime_to_datetime(entity),
project_id=entity.project_id,
)
)
@@ -279,7 +290,7 @@ class SearchService:
"entity_type": entity.entity_type,
},
created_at=entity.created_at,
updated_at=entity.updated_at,
updated_at=_mtime_to_datetime(entity),
project_id=entity.project_id,
)
)
@@ -313,7 +324,7 @@ class SearchService:
"tags": obs.tags,
},
created_at=entity.created_at,
updated_at=entity.updated_at,
updated_at=_mtime_to_datetime(entity),
project_id=entity.project_id,
)
)
@@ -343,7 +354,7 @@ class SearchService:
to_id=rel.to_id,
relation_type=rel.relation_type,
created_at=entity.created_at,
updated_at=entity.updated_at,
updated_at=_mtime_to_datetime(entity),
project_id=entity.project_id,
)
)