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

digiteinfotech / kairon / 15019638846

14 May 2025 11:35AM UTC coverage: 90.49% (+0.009%) from 90.481%
15019638846

Pull #1958

github

web-flow
Merge e3abfe100 into cf147d569
Pull Request #1958: fix for datetime issue in get_data

16 of 16 new or added lines in 1 file covered. (100.0%)

2 existing lines in 1 file now uncovered.

26214 of 28969 relevant lines covered (90.49%)

0.9 hits per line

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

97.7
/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
cognition_processor = CognitionDataProcessor()
1✔
13

14
class PyscriptSharedUtility:
1✔
15

16
    @staticmethod
1✔
17
    def fetch_collection_data(query: dict):
1✔
18

19
        collection_data = CollectionData.objects(__raw__= query)
1✔
20

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

29
            final_data["_id"] = str(item["_id"])
1✔
30
            final_data['collection_name'] = collection_name
1✔
31
            final_data['is_secure'] = is_secure
1✔
32
            final_data['timestamp'] = item.get("timestamp")
1✔
33
            final_data['data'] = data
1✔
34

35
            yield final_data
1✔
36

37
    @staticmethod
1✔
38
    def ensure_datetime(dt):
1✔
39
        if isinstance(dt, str):
1✔
40
            dt = isoparse(dt)
1✔
41
        elif isinstance(dt, date) and not isinstance(dt, datetime):
1✔
42
            # Convert date to datetime at midnight UTC
43
            dt = datetime.combine(dt, datetime.min.time()).replace(tzinfo=pytz.UTC)
1✔
44
        elif isinstance(dt, datetime):
1✔
45
            if dt.tzinfo is None:
1✔
46
                dt = dt.replace(tzinfo=pytz.UTC)
1✔
47
        return dt
1✔
48
    @staticmethod
1✔
49
    def get_data(collection_name: str, user: str, data_filter: dict, kwargs=None, bot: Text = None):
1✔
50
        if not bot:
1✔
51
            raise Exception("Missing bot id")
1✔
52

53
        collection_name = collection_name.lower()
1✔
54
        query = {"bot": bot, "collection_name": collection_name}
1✔
55
        start_time = kwargs.pop("start_time", None) if kwargs else None
1✔
56
        end_time = kwargs.pop("end_time", None) if kwargs else None
1✔
57

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

71

72
    @staticmethod
1✔
73
    def add_data(user: str, payload: dict, bot: str = None):
1✔
74
        if not bot:
1✔
75
            raise Exception("Missing bot id")
1✔
76

77
        collection_id = cognition_processor.save_collection_data(payload, user, bot)
1✔
78
        return {
1✔
79
            "message": "Record saved!",
80
            "data": {"_id": collection_id}
81
        }
82

83

84
    @staticmethod
1✔
85
    def update_data(collection_id: str, user: str, payload: dict, bot: str = None):
1✔
86
        if not bot:
1✔
87
            raise Exception("Missing bot id")
1✔
88

89
        collection_id = cognition_processor.update_collection_data(collection_id, payload, user, bot)
1✔
90
        return {
1✔
91
            "message": "Record updated!",
92
            "data": {"_id": collection_id}
93
        }
94

95

96
    @staticmethod
1✔
97
    def delete_data(collection_id: str, user: Text, bot: Text = None):
1✔
98
        if not bot:
1✔
99
            raise Exception("Missing bot id")
1✔
100

101
        cognition_processor.delete_collection_data(collection_id, bot, user)
1✔
102

103
        return {
1✔
104
            "message": f"Collection with ID {collection_id} has been successfully deleted.",
105
            "data": {"_id": collection_id}
106
        }
107

108
    @staticmethod
1✔
109
    def delete_schedule_job(event_id: Text, bot: Text):
1✔
110
        if not bot:
1✔
111
            raise AppException("Missing bot id")
1✔
112

113
        if not event_id:
1✔
114
            raise AppException("Missing event id")
1✔
115

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

118
        event_server = Utility.environment['events']['server_url']
1✔
119

120
        http_response = ActionUtility.execute_http_request(
1✔
121
            f"{event_server}/api/events/{event_id}",
122
            "DELETE")
123

124
        if not http_response.get("success"):
1✔
125
            raise AppException(http_response)
1✔
126
        else:
127
            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