Coverage for src / local_deep_research / web / services / resource_service.py: 100%

52 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2026-01-11 00:51 +0000

1from datetime import datetime, UTC 

2 

3from loguru import logger 

4 

5from ...database.models import ResearchResource 

6from ...database.session_context import get_user_db_session 

7 

8 

9def get_resources_for_research(research_id): 

10 """ 

11 Retrieve resources associated with a specific research project 

12 

13 Args: 

14 research_id (str): The UUID of the research 

15 

16 Returns: 

17 list: List of resource objects for the research 

18 """ 

19 try: 

20 with get_user_db_session() as db_session: 

21 # Query to get resources for the research 

22 resources_list = ( 

23 db_session.query(ResearchResource) 

24 .filter_by(research_id=research_id) 

25 .order_by(ResearchResource.id.asc()) 

26 .all() 

27 ) 

28 

29 resources = [] 

30 for resource in resources_list: 

31 resources.append( 

32 { 

33 "id": resource.id, 

34 "research_id": resource.research_id, 

35 "title": resource.title, 

36 "url": resource.url, 

37 "content_preview": resource.content_preview, 

38 "source_type": resource.source_type, 

39 "metadata": resource.resource_metadata or {}, 

40 } 

41 ) 

42 

43 return resources 

44 

45 except Exception: 

46 logger.exception("Error retrieving resources for research") 

47 raise 

48 

49 

50def add_resource( 

51 research_id, 

52 title, 

53 url, 

54 content_preview=None, 

55 source_type="web", 

56 metadata=None, 

57): 

58 """ 

59 Add a new resource to the research_resources table 

60 

61 Args: 

62 research_id (str): The UUID of the research 

63 title (str): The title of the resource 

64 url (str): The URL of the resource 

65 content_preview (str, optional): A preview of the content 

66 source_type (str, optional): The type of source 

67 metadata (dict, optional): Additional metadata 

68 

69 Returns: 

70 ResearchResource: The created resource object 

71 """ 

72 try: 

73 with get_user_db_session() as db_session: 

74 resource = ResearchResource( 

75 research_id=research_id, 

76 title=title, 

77 url=url, 

78 content_preview=content_preview, 

79 source_type=source_type, 

80 resource_metadata=metadata, 

81 accessed_at=datetime.now(UTC), 

82 ) 

83 

84 db_session.add(resource) 

85 db_session.commit() 

86 

87 return resource 

88 

89 except Exception: 

90 logger.exception("Error adding resource") 

91 raise 

92 

93 

94def delete_resource(resource_id): 

95 """ 

96 Delete a resource from the database 

97 

98 Args: 

99 resource_id (int): The ID of the resource to delete 

100 

101 Returns: 

102 bool: True if deletion was successful 

103 

104 Raises: 

105 ValueError: If resource not found 

106 """ 

107 try: 

108 with get_user_db_session() as db_session: 

109 # Find the resource 

110 resource = ( 

111 db_session.query(ResearchResource) 

112 .filter_by(id=resource_id) 

113 .first() 

114 ) 

115 

116 if not resource: 

117 raise ValueError(f"Resource with ID {resource_id} not found") 

118 

119 db_session.delete(resource) 

120 db_session.commit() 

121 

122 logger.info(f"Deleted resource {resource_id}") 

123 return True 

124 

125 except ValueError: 

126 raise 

127 except Exception: 

128 logger.exception("Error deleting resource") 

129 raise 

130 

131 

132def update_resource_content(resource_id, content): 

133 """Update resource content if needed""" 

134 try: 

135 with get_user_db_session() as db_session: 

136 resource = ( 

137 db_session.query(ResearchResource) 

138 .filter_by(id=resource_id) 

139 .first() 

140 ) 

141 if resource: 

142 resource.content = content 

143 resource.last_fetched = datetime.now(UTC) 

144 db_session.commit() 

145 return resource 

146 except Exception: 

147 logger.exception("Error updating resource content") 

148 return None