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

digiteinfotech / kairon / 16048298422

03 Jul 2025 10:42AM UTC coverage: 90.751% (-0.05%) from 90.804%
16048298422

Pull #2039

github

web-flow
Merge 663b0adc7 into 7d8acaf28
Pull Request #2039: CRUD metadata

6 of 21 new or added lines in 4 files covered. (28.57%)

3 existing lines in 1 file now uncovered.

27602 of 30415 relevant lines covered (90.75%)

0.91 hits per line

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

91.84
/kairon/shared/pyscript/shared_pyscript_utils.py
1
from datetime import date, datetime
1✔
2
from typing import Text
1✔
3

4
import pytz
1✔
5
from dateutil.parser import isoparse
1✔
6
from loguru import logger
1✔
7
from kairon import Utility
1✔
8
from kairon.exceptions import AppException
1✔
9
from kairon.shared.actions.utils import ActionUtility
1✔
10
from kairon.shared.cognition.data_objects import CollectionData
1✔
11
from kairon.api.app.routers.bot.data import CognitionDataProcessor
1✔
12
from kairon.shared.data.collection_processor import DataProcessor
1✔
13

14
cognition_processor = CognitionDataProcessor()
1✔
15

16
class PyscriptSharedUtility:
1✔
17

18
    @staticmethod
1✔
19
    def fetch_collection_data(query: dict):
1✔
20

21
        collection_data = CollectionData.objects(__raw__= query)
1✔
22

23
        for value in collection_data:
1✔
24
            final_data = {}
1✔
25
            item = value.to_mongo().to_dict()
1✔
26
            collection_name = item.pop('collection_name', None)
1✔
27
            is_secure = item.pop('is_secure')
1✔
28
            is_non_editable=item.pop('is_non_editable')
1✔
29
            data = item.pop('data')
1✔
30
            data = DataProcessor.prepare_decrypted_data(data, is_secure)
1✔
31

32
            final_data["_id"] = str(item["_id"])
1✔
33
            final_data['collection_name'] = collection_name
1✔
34
            final_data['is_secure'] = is_secure
1✔
35
            final_data['is_non_editable']=is_non_editable
1✔
36
            final_data['timestamp'] = item.get("timestamp")
1✔
37
            final_data['data'] = data
1✔
38

39
            yield final_data
1✔
40

41
    @staticmethod
1✔
42
    def ensure_datetime(dt):
1✔
43
        if isinstance(dt, str):
1✔
44
            dt = isoparse(dt)
1✔
45
        elif isinstance(dt, date) and not isinstance(dt, datetime):
1✔
46
            dt = datetime.combine(dt, datetime.min.time()).replace(tzinfo=pytz.UTC)
1✔
47
        elif isinstance(dt, datetime):
1✔
48
            if dt.tzinfo is None:
1✔
49
                dt = dt.replace(tzinfo=pytz.UTC)
1✔
50
        return dt
1✔
51

52
    @staticmethod
1✔
53
    def get_data(collection_name: str, user: str, data_filter: dict, kwargs=None, bot: Text = None):
1✔
54
        if not bot:
1✔
55
            raise Exception("Missing bot id")
1✔
56

57
        collection_name = collection_name.lower()
1✔
58
        query = {"bot": bot, "collection_name": collection_name}
1✔
59
        start_time = kwargs.pop("start_time", None) if kwargs else None
1✔
60
        end_time = kwargs.pop("end_time", None) if kwargs else None
1✔
61

62
        start_time = PyscriptSharedUtility.ensure_datetime(start_time) if start_time else None
1✔
63
        end_time = PyscriptSharedUtility.ensure_datetime(end_time) if end_time else None
1✔
64
        if start_time:
1✔
65
            query.setdefault("timestamp", {})["$gte"] = start_time
1✔
66
        if end_time:
1✔
67
            query.setdefault("timestamp", {})["$lte"] = end_time
×
68
        if data_filter.get("raw_query"):
1✔
69
            query.update(data_filter.get("raw_query"))
×
70
        else:
71
            query.update({f"data.{key}": value for key, value in data_filter.items()})
1✔
72
        data = list(PyscriptSharedUtility.fetch_collection_data(query))
1✔
73
        return {"data": data}
1✔
74

75
    @staticmethod
1✔
76
    def get_crud_metadata(collection_name: Text, user: Text,  bot: Text = None):
1✔
NEW
77
        if not bot:
×
NEW
78
            raise Exception("Missing bot id")
×
79

NEW
80
        if not collection_name:
×
NEW
81
            raise Exception("Missing collection name")
×
82

NEW
83
        metadata = DataProcessor.get_crud_metadata(user, bot, collection_name)
×
NEW
84
        return {
×
85
            'metadata': metadata
86
        }
87

88

89
    @staticmethod
1✔
90
    def add_data(user: str, payload: dict, bot: str = None):
1✔
91
        if not bot:
1✔
92
            raise Exception("Missing bot id")
1✔
93

94
        collection_id = DataProcessor.save_collection_data(payload, user, bot)
1✔
95
        return {
1✔
96
            "message": "Record saved!",
97
            "data": {"_id": collection_id}
98
        }
99

100

101
    @staticmethod
1✔
102
    def update_data(collection_id: str, user: str, payload: dict, bot: str = None):
1✔
103
        if not bot:
1✔
104
            raise Exception("Missing bot id")
1✔
105

106
        collection_id = DataProcessor.update_collection_data(collection_id, payload, user, bot)
1✔
107
        return {
1✔
108
            "message": "Record updated!",
109
            "data": {"_id": collection_id}
110
        }
111

112

113
    @staticmethod
1✔
114
    def delete_data(collection_id: str, user: Text, bot: Text = None):
1✔
115
        if not bot:
1✔
116
            raise Exception("Missing bot id")
1✔
117

118
        DataProcessor.delete_collection_data(collection_id, bot, user)
1✔
119

120
        return {
1✔
121
            "message": f"Collection with ID {collection_id} has been successfully deleted.",
122
            "data": {"_id": collection_id}
123
        }
124

125
    @staticmethod
1✔
126
    def delete_schedule_job(event_id: Text, bot: Text):
1✔
127
        if not bot:
1✔
128
            raise AppException("Missing bot id")
1✔
129

130
        if not event_id:
1✔
131
            raise AppException("Missing event id")
1✔
132

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

135
        event_server = Utility.environment['events']['server_url']
1✔
136

137
        http_response = ActionUtility.execute_http_request(
1✔
138
            f"{event_server}/api/events/{event_id}",
139
            "DELETE")
140

141
        if not http_response.get("success"):
1✔
142
            raise AppException(http_response)
1✔
143
        else:
144
            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