• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
You are now the owner of this repo.

digiteinfotech / kairon / 14725864374

29 Apr 2025 07:35AM UTC coverage: 90.456% (+0.1%) from 90.361%
14725864374

Pull #1937

github

web-flow
Merge 0c96b7471 into 9904c6212
Pull Request #1937: Move main pyscript into callback service new

291 of 317 new or added lines in 9 files covered. (91.8%)

26093 of 28846 relevant lines covered (90.46%)

0.9 hits per line

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

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

10
class PyscriptSharedUtility:
1✔
11

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

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

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

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

31
            yield final_data
1✔
32

33

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

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

54

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

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

66

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

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

78

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

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

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

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

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

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

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

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

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

© 2026 Coveralls, Inc