Skip to content

Commit

Permalink
Modified is_event_in_progress of every event.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahesh committed Nov 10, 2023
1 parent 521dd7b commit fc51661
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 54 deletions.
2 changes: 1 addition & 1 deletion kairon/api/app/routers/bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ async def abort_event(
"""
Aborts the event
"""
ModelProcessor.abort_current_event(current_user.get_bot(), event_type)
mongo_processor.abort_current_event(current_user.get_bot(), event_type)

return {"message": f"{event_type} aborted."}

Expand Down
3 changes: 2 additions & 1 deletion kairon/shared/data/history_log_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def is_event_in_progress(bot: str, raise_exception=True):
try:
ConversationsHistoryDeleteLogs.objects(bot=bot).filter(
Q(status__ne=EVENT_STATUS.COMPLETED.value) &
Q(status__ne=EVENT_STATUS.FAIL.value)).get()
Q(status__ne=EVENT_STATUS.FAIL.value) &
Q(status__ne=EVENT_STATUS.ABORTED.value)).get()

if raise_exception:
raise AppException("Event already in progress! Check logs.")
Expand Down
42 changes: 3 additions & 39 deletions kairon/shared/data/model_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
from mongoengine.errors import DoesNotExist
from kairon.exceptions import AppException
from .constant import EVENT_STATUS
from .data_objects import ModelTraining, BotSettings, ConversationsHistoryDeleteLogs, TrainingDataGenerator
from ..chat.broadcast.data_objects import MessageBroadcastLogs
from ..constants import EventClass
from ..importer.data_objects import ValidationLogs
from ..multilingual.data_objects import BotReplicationLogs
from ..test.data_objects import ModelTestingLogs
from .data_objects import ModelTraining, BotSettings


class ModelProcessor:
Expand Down Expand Up @@ -58,38 +53,6 @@ def set_training_status(
doc.exception = exception
doc.save()

@staticmethod
def abort_current_event(bot: Text, event_type: EventClass):
"""
sets event status to aborted if there is any event in progress or enqueued
:param bot: bot id
:param event_type: type of the event
:return: None
:raises: AppException
"""
events_dict = {
EventClass.model_training: ModelTraining,
EventClass.model_testing: ModelTestingLogs,
EventClass.delete_history: ConversationsHistoryDeleteLogs,
EventClass.data_importer: ValidationLogs,
EventClass.multilingual: BotReplicationLogs,
EventClass.data_generator: TrainingDataGenerator,
EventClass.faq_importer: ValidationLogs,
EventClass.message_broadcast: MessageBroadcastLogs
}
event_data_object = events_dict.get(event_type)
if event_data_object:
try:
event_object = event_data_object.objects.get(
bot=bot,
status__in=[EVENT_STATUS.INPROGRESS.value, EVENT_STATUS.ENQUEUED.value]
)
event_object.status = EVENT_STATUS.ABORTED.value
event_object.save()
except DoesNotExist:
raise AppException(f"No Enqueued {event_type} present for this bot.")

@staticmethod
def is_training_inprogress(bot: Text, raise_exception=True):
"""
Expand All @@ -102,7 +65,8 @@ def is_training_inprogress(bot: Text, raise_exception=True):
"""
if ModelTraining.objects(bot=bot).filter(
Q(status__ne=EVENT_STATUS.DONE.value) &
Q(status__ne=EVENT_STATUS.FAIL.value)).count():
Q(status__ne=EVENT_STATUS.FAIL.value) &
Q(status__ne=EVENT_STATUS.ABORTED.value)).count():
if raise_exception:
raise AppException("Previous model training in progress.")
else:
Expand Down
44 changes: 42 additions & 2 deletions kairon/shared/data/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,15 @@
Rules,
Utterances, BotSettings, ChatClientConfig, SlotMapping, KeyVault, EventConfig, TrainingDataGenerator,
MultiflowStories, MultiflowStoryEvents, MultiFlowStoryMetadata,
Synonyms, Lookup, Analytics
Synonyms, Lookup, Analytics, ModelTraining, ConversationsHistoryDeleteLogs
)
from .utils import DataUtility
from ..constants import KaironSystemSlots, PluginTypes
from ..chat.broadcast.data_objects import MessageBroadcastLogs
from ..constants import KaironSystemSlots, PluginTypes, EventClass
from ..custom_widgets.data_objects import CustomWidgets
from ..importer.data_objects import ValidationLogs
from ..multilingual.data_objects import BotReplicationLogs
from ..test.data_objects import ModelTestingLogs


class MongoProcessor:
Expand Down Expand Up @@ -3537,6 +3541,42 @@ def delete_slot(
"Slot does not exist."
)

@staticmethod
def abort_current_event(bot: Text, event_type: EventClass):
"""
sets event status to aborted if there is any event in progress or enqueued
:param bot: bot id
:param event_type: type of the event
:return: None
:raises: AppException
"""
events_dict = {
EventClass.model_training: ModelTraining,
EventClass.model_testing: ModelTestingLogs,
EventClass.delete_history: ConversationsHistoryDeleteLogs,
EventClass.data_importer: ValidationLogs,
EventClass.multilingual: BotReplicationLogs,
EventClass.data_generator: TrainingDataGenerator,
EventClass.faq_importer: ValidationLogs,
EventClass.message_broadcast: MessageBroadcastLogs
}
status_field = "status" if event_type in {EventClass.model_training,
EventClass.delete_history,
EventClass.data_generator} else "event_status"
event_data_object = events_dict.get(event_type)
if event_data_object:
try:
filter_params = {'bot': bot,
f'{status_field}__in': [EVENT_STATUS.INPROGRESS.value, EVENT_STATUS.ENQUEUED.value]}

event_object = event_data_object.objects.get(**filter_params)
update_params = {f'set__{status_field}': EVENT_STATUS.ABORTED.value}
event_object.update(**update_params)
event_object.save()
except DoesNotExist:
raise AppException(f"No Enqueued {event_type} present for this bot.")

@staticmethod
def get_row_count(document: Document, bot: str, **kwargs):
"""
Expand Down
2 changes: 1 addition & 1 deletion kairon/shared/data/training_data_generation_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def is_in_progress(bot: Text, raise_exception=True):
"""
if TrainingDataGenerator.objects(__raw__={
"bot": bot,
"status": {"$nin": [EVENT_STATUS.FAIL.value, EVENT_STATUS.COMPLETED.value]}
"status": {"$nin": [EVENT_STATUS.FAIL.value, EVENT_STATUS.COMPLETED.value, EVENT_STATUS.ABORTED.value]}
}).count():
if raise_exception:
raise AppException("Event already in progress! Check logs.")
Expand Down
3 changes: 2 additions & 1 deletion kairon/shared/importer/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def is_event_in_progress(bot: str, raise_exception=True):
try:
ValidationLogs.objects(bot=bot).filter(
Q(event_status__ne=EVENT_STATUS.COMPLETED.value) &
Q(event_status__ne=EVENT_STATUS.FAIL.value)).get()
Q(event_status__ne=EVENT_STATUS.FAIL.value) &
Q(event_status__ne=EVENT_STATUS.ABORTED.value)).get()

if raise_exception:
raise AppException("Event already in progress! Check logs.")
Expand Down
3 changes: 2 additions & 1 deletion kairon/shared/multilingual/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ def is_event_in_progress(source_bot: str, raise_exception=True):
try:
BotReplicationLogs.objects(bot=source_bot).filter(
Q(event_status__ne=EVENT_STATUS.COMPLETED.value) &
Q(event_status__ne=EVENT_STATUS.FAIL.value)).get()
Q(event_status__ne=EVENT_STATUS.FAIL.value) &
Q(event_status__ne=EVENT_STATUS.ABORTED.value)).get()

if raise_exception:
raise AppException("Event already in progress! Check logs.")
Expand Down
3 changes: 2 additions & 1 deletion kairon/shared/test/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def is_event_in_progress(bot: str, raise_exception=True):
try:
ModelTestingLogs.objects(bot=bot, type='common').filter(
Q(event_status__ne=EVENT_STATUS.COMPLETED.value) &
Q(event_status__ne=EVENT_STATUS.FAIL.value)).get()
Q(event_status__ne=EVENT_STATUS.FAIL.value) &
Q(event_status__ne=EVENT_STATUS.ABORTED.value)).get()

if raise_exception:
raise AppException("Event already in progress! Check logs.")
Expand Down
35 changes: 28 additions & 7 deletions tests/unit_test/data_processor/data_processor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
from kairon.shared.chat.data_objects import Channels
from kairon.shared.cognition.data_objects import CognitionData, CognitionSchema
from kairon.shared.cognition.processor import CognitionDataProcessor
from kairon.shared.constants import SLOT_SET_TYPE
from kairon.shared.constants import SLOT_SET_TYPE, EventClass
from kairon.shared.data.audit.data_objects import AuditLogData
from kairon.shared.data.constant import ENDPOINT_TYPE
from kairon.shared.data.constant import UTTERANCE_TYPE, EVENT_STATUS, STORY_EVENT, ALLOWED_DOMAIN_FORMATS, \
Expand Down Expand Up @@ -963,38 +963,53 @@ def test_edit_bot_settings(self):
assert updated_settings.llm_settings.to_mongo().to_dict() == {'enable_faq': False, 'provider': 'openai'}

def test_abort_current_event_with_no_model_training_event(self):
mongo_processor = MongoProcessor()
bot = "test_bot"
with pytest.raises(AppException, match="No Enqueued model_training present for this bot."):
ModelProcessor.abort_current_event(bot=bot, event_type="model_training")
mongo_processor.abort_current_event(bot=bot, event_type=EventClass.model_training)

def test_abort_current_event_with_no_model_testing_event(self):
mongo_processor = MongoProcessor()
bot = "test_bot"
with pytest.raises(AppException, match="No Enqueued model_testing present for this bot."):
ModelProcessor.abort_current_event(bot=bot, event_type="model_testing")
mongo_processor.abort_current_event(bot=bot, event_type=EventClass.model_testing)

def test_abort_current_event_with_no_delete_history_event(self):
mongo_processor = MongoProcessor()
bot = "test_bot"
with pytest.raises(AppException, match="No Enqueued delete_history present for this bot."):
ModelProcessor.abort_current_event(bot=bot, event_type="delete_history")
mongo_processor.abort_current_event(bot=bot, event_type=EventClass.delete_history)

def test_abort_current_event_with_no_data_importer_event(self):
mongo_processor = MongoProcessor()
bot = "test_bot"
with pytest.raises(AppException, match="No Enqueued data_importer present for this bot."):
ModelProcessor.abort_current_event(bot=bot, event_type="data_importer")
mongo_processor.abort_current_event(bot=bot, event_type=EventClass.data_importer)

def test_abort_current_event_with_model_training(self):
mongo_processor = MongoProcessor()
bot = "test_bot"
user = "test_user"
ModelProcessor.set_training_status(bot=bot, user=user, status=EVENT_STATUS.ENQUEUED.value)
ModelProcessor.abort_current_event(bot=bot, event_type="model_training")
mongo_processor.abort_current_event(bot=bot, event_type=EventClass.model_training)
model_training_object = ModelTraining.objects(bot=bot).get()
assert model_training_object.status == EVENT_STATUS.ABORTED.value

def test_abort_current_event_with_model_testing(self):
mongo_processor = MongoProcessor()
bot = "test_bot"
user = "test_user"
ModelTestingLogProcessor.log_test_result(bot=bot, user=user, event_status=EVENT_STATUS.ENQUEUED.value)
mongo_processor.abort_current_event(bot=bot, event_type=EventClass.model_testing)
model_testing_object = ModelTestingLogs.objects(bot=bot).get()
assert model_testing_object.event_status == EVENT_STATUS.ABORTED.value

def test_abort_current_event_with_data_generator(self):
mongo_processor = MongoProcessor()
bot = "test_bot"
user = "test_user"
TrainingDataGenerationProcessor.set_status(bot=bot, user=user, status=EVENT_STATUS.ENQUEUED.value)
ModelProcessor.abort_current_event(bot=bot, event_type="data_generator")
mongo_processor.abort_current_event(bot=bot, event_type=EventClass.data_generator)
data_generator_object = TrainingDataGenerator.objects(bot=bot).get()
assert data_generator_object.status == EVENT_STATUS.ABORTED.value

Expand Down Expand Up @@ -15205,6 +15220,12 @@ def test_is_training_inprogress_False(self):
actual_response = ModelProcessor.is_training_inprogress("tests")
assert actual_response is False

def test_is_training_inprogress_with_aborted(self):
ModelProcessor.set_training_status("testbot", "testuser", "Aborted")
model_training = ModelTraining.objects(bot="testbot", status="Aborted")
actual_response = ModelProcessor.is_training_inprogress("tests", False)
assert actual_response is False

def test_is_training_inprogress_True(self, test_set_training_status_inprogress):
assert test_set_training_status_inprogress.__len__() == 1
assert test_set_training_status_inprogress.first().bot == "tests"
Expand Down
6 changes: 6 additions & 0 deletions tests/unit_test/data_processor/history_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ def test_delete_bot_history(self, mock_client):
def test_is_event_in_progress(self, get_connection_delete_history):
assert not HistoryDeletionLogProcessor.is_event_in_progress('5f1928bda7c0280ca4869da3')

def test_is_event_in_progress_with_aborted(self, get_connection_delete_history):
till_date = datetime.utcnow().date()
HistoryDeletionLogProcessor.add_log('5f1928bda7c0280ca4869da3', 'test_user',
till_date, status='Aborted')
assert not HistoryDeletionLogProcessor.is_event_in_progress('5f1928bda7c0280ca4869da3', False)

def test_is_event_in_progress_failure(self, get_connection_delete_history):
till_date = datetime.utcnow().date()
HistoryDeletionLogProcessor.add_log('5f1928bda7c0280ca4869da3', 'test_user',
Expand Down

0 comments on commit fc51661

Please sign in to comment.