Coverage for functions \ flipdare \ search \ factory \ group_search_factory.py: 100%

40 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-05-08 12:22 +1000

1#!/usr/bin/env python 

2# Copyright (c) 2026 Flipdare Pty Ltd. All rights reserved. 

3# 

4# This file is part of Flipdare's proprietary software and contains 

5# confidential and copyrighted material. Unauthorised copying, 

6# modification, distribution, or use of this file is strictly 

7# prohibited without prior written permission from Flipdare Pty Ltd. 

8# 

9# This software includes third-party components licensed under MIT, 

10# BSD, and Apache 2.0 licences. See THIRD_PARTY_NOTICES for details. 

11# 

12 

13 

14from typing import Any, override 

15from flipdare.app_log import LOG 

16from flipdare.constants import IS_DEBUG 

17from flipdare.core.tokenizer import Tokenizer 

18from flipdare.firestore.context.group_context import GroupContext 

19from flipdare.generated.shared.search.search_obj_type import SearchObjType 

20from flipdare.search.core.search_score import SearchScore 

21from flipdare.search.doc._search_document import SearchDocument 

22from flipdare.search.doc.general_document import GeneralDocument 

23from flipdare.search.factory._search_document_factory import SearchDocumentFactory 

24from flipdare.util.time_util import TypesenseTime 

25 

26__all__ = ["GroupSearchFactory"] 

27 

28 

29class GroupSearchFactory(SearchDocumentFactory): 

30 

31 def __init__(self, group_context: GroupContext, tokenizer: Tokenizer | None = None) -> None: 

32 self.group_context = group_context 

33 super().__init__(tokenizer=tokenizer) 

34 

35 @property 

36 @override 

37 def obj_type(self) -> SearchObjType: 

38 return SearchObjType.GROUP 

39 

40 @override 

41 def get_documents(self) -> list[SearchDocument[Any]] | None: 

42 group_context = self.group_context 

43 group = group_context.group 

44 owner = group_context.owner 

45 group_id = group.doc_id 

46 

47 score = SearchScore.score_group(group) 

48 values = group.model.searchable_values 

49 values.extend([owner.model.safe_name]) 

50 

51 token_result = self.tokenizer.create_tokens(values) 

52 

53 obj_id = group_id 

54 created_at = TypesenseTime.from_firestore(group.created_at_db) 

55 updated_at = TypesenseTime.from_firestore(group.updated_at_db) 

56 

57 creator_id = group.uid 

58 views = group.model.view_stats.views 

59 token_score = token_result.token_score.score 

60 tag_score = token_score * score 

61 

62 # Debug logging 

63 if IS_DEBUG: 

64 LOG().debug( 

65 f"Creating GroupSearchDocument with obj_id={obj_id}, creator_id={creator_id}, " 

66 f"obj_type={self.obj_type}, values={values}, " 

67 f"score={score}, token_score={token_score}, tag_score={tag_score}, views={views}, " 

68 f"created_at={created_at}, updated_at={updated_at}, tokens={token_result.tokens}", 

69 ) 

70 

71 return [ 

72 GeneralDocument.create( 

73 obj_id=obj_id, 

74 uid=creator_id, 

75 obj_type=self.obj_type, 

76 keywords=values, 

77 score=score, 

78 views=views, 

79 created_at=created_at, 

80 updated_at=updated_at, 

81 tags=token_result.tokens, 

82 tag_score=tag_score, 

83 ), 

84 ]