• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

digiteinfotech / kairon / 14712262789

28 Apr 2025 03:59PM UTC coverage: 90.357% (-0.05%) from 90.403%
14712262789

Pull #1934

github

web-flow
Merge e82c19f9d into 18c5b74ac
Pull Request #1934: moved main pyscript to callback server

251 of 318 new or added lines in 9 files covered. (78.93%)

26040 of 28819 relevant lines covered (90.36%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.89
/kairon/shared/pyscript/shared_pyscript_utils.py
1
from typing import Text, Dict, Callable, List
1✔
2
from typing import Text
1✔
3
from loguru import logger
1✔
4
from kairon import Utility
1✔
5
from kairon.exceptions import AppException
1✔
6
from kairon.shared.actions.utils import ActionUtility
1✔
7
from kairon.shared.cognition.data_objects import CollectionData
1✔
8
from kairon.api.app.routers.bot.data import CognitionDataProcessor
1✔
9
cognition_processor = CognitionDataProcessor()
1✔
10

11
class PyscriptSharedUtility:
1✔
12

13
    @staticmethod
1✔
14
    def fetch_collection_data(query: dict):
1✔
15

16
        collection_data = CollectionData.objects(__raw__= query)
1✔
17

18
        for value in collection_data:
1✔
19
            final_data = {}
1✔
20
            item = value.to_mongo().to_dict()
1✔
21
            collection_name = item.pop('collection_name', None)
1✔
22
            is_secure = item.pop('is_secure')
1✔
23
            data = item.pop('data')
1✔
24
            data = cognition_processor.prepare_decrypted_data(data, is_secure)
1✔
25

26
            final_data["_id"] = str(item["_id"])
1✔
27
            final_data['collection_name'] = collection_name
1✔
28
            final_data['is_secure'] = is_secure
1✔
29
            final_data['timestamp'] = item.get("timestamp")
1✔
30
            final_data['data'] = data
1✔
31

32
            yield final_data
1✔
33

34

35
    @staticmethod
1✔
36
    def get_data(collection_name: str, user: str, data_filter: dict, bot: Text = None,kwargs=None):
1✔
37
        if not bot:
1✔
38
            raise Exception("Missing bot id")
1✔
39

40
        collection_name = collection_name.lower()
1✔
41
        query = {"bot": bot, "collection_name": collection_name}
1✔
42
        start_time = kwargs.pop("start_time", None) if kwargs else None
1✔
43
        end_time = kwargs.pop("end_time", None) if kwargs else None
1✔
44
        if start_time:
1✔
NEW
45
            query["timestamp__gte"] = start_time
×
46
        if end_time:
1✔
NEW
47
            query["timestamp__lte"] = end_time
×
48
        if data_filter.get("raw_query"):
1✔
NEW
49
            query.update(data_filter.get("raw_query"))
×
50
        else:
51
            query.update({f"data.{key}": value for key, value in data_filter.items()})
1✔
52
        data = list(PyscriptSharedUtility.fetch_collection_data(query))
1✔
53
        return {"data": data}
1✔
54

55

56
    @staticmethod
1✔
57
    def add_data(user: str, payload: dict, bot: str = None):
1✔
58
        if not bot:
1✔
59
            raise Exception("Missing bot id")
1✔
60

61
        collection_id = cognition_processor.save_collection_data(payload, user, bot)
1✔
62
        return {
1✔
63
            "message": "Record saved!",
64
            "data": {"_id": collection_id}
65
        }
66

67

68
    @staticmethod
1✔
69
    def update_data(collection_id: str, user: str, payload: dict, bot: str = None):
1✔
70
        if not bot:
1✔
71
            raise Exception("Missing bot id")
1✔
72

73
        collection_id = cognition_processor.update_collection_data(collection_id, payload, user, bot)
1✔
74
        return {
1✔
75
            "message": "Record updated!",
76
            "data": {"_id": collection_id}
77
        }
78

79

80
    @staticmethod
1✔
81
    def delete_data(collection_id: str, user: Text, bot: Text = None):
1✔
82
        if not bot:
1✔
83
            raise Exception("Missing bot id")
1✔
84

85
        cognition_processor.delete_collection_data(collection_id, bot, user)
1✔
86

87
        return {
1✔
88
            "message": f"Collection with ID {collection_id} has been successfully deleted.",
89
            "data": {"_id": collection_id}
90
        }
91

92
    @staticmethod
1✔
93
    def delete_schedule_job(event_id: Text, bot: Text):
1✔
94
        if not bot:
1✔
95
            raise AppException("Missing bot id")
1✔
96

97
        if not event_id:
1✔
98
            raise AppException("Missing event id")
1✔
99

100
        logger.info(f"event: {event_id}, bot: {bot}")
1✔
101

102
        event_server = Utility.environment['events']['server_url']
1✔
103

104
        http_response = ActionUtility.execute_http_request(
1✔
105
            f"{event_server}/api/events/{event_id}",
106
            "DELETE")
107

108
        if not http_response.get("success"):
1✔
109
            raise AppException(http_response)
1✔
110
        else:
111
            logger.info(http_response)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc