Skip to content

Commit 5988b4c

Browse files
Adds various hotfixes (#319)
* debounce postprocess * remove print * add info * id preprocess * seconds + model
1 parent 31853a2 commit 5988b4c

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

api/transfer.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,12 @@ def __recalculate_missing_attributes_and_embeddings(
182182

183183
for integration_item in integration_items:
184184
integration_id = str(integration_item.id)
185-
url = f"{COGNITION_INTEGRATION_PROVIDER}/integrations/postprocess/{integration_id}"
186-
service_requests.post_call_or_raise(url, data=None)
185+
post_process_integration(integration_id)
186+
187+
188+
def post_process_integration(integration_id: str) -> None:
189+
url = f"{COGNITION_INTEGRATION_PROVIDER}/integrations/postprocess/{integration_id}"
190+
service_requests.post_call_or_raise(url, data=None)
187191

188192

189193
def __calculate_missing_attributes(project_id: str, user_id: str) -> None:

controller/organization/manager.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
from controller.auth import kratos
88
from submodules.model.util import sql_alchemy_to_dict
99
from submodules.s3 import controller as s3
10+
from submodules.model.cognition_objects import integration
11+
from api import transfer as transfer_api
12+
from util.decorator import param_debounce
13+
1014

1115
USER_INFO_WHITELIST = {"id", "role"}
1216
ORGANIZATION_WHITELIST = {"id", "name", "max_rows", "max_cols", "max_char_count"}
@@ -93,3 +97,17 @@ def get_overview_stats(org_id: str) -> List[Dict[str, Union[str, int]]]:
9397
if org_id is None:
9498
return []
9599
return organization.get_organization_overview_stats(org_id)
100+
101+
102+
# INFO: Not fully debounced if server runs multiple instances
103+
# TODO: Change to 60 to 300 for prod
104+
@param_debounce(seconds=60)
105+
def sync_organization_sharepoint_integrations(org_id: str) -> None:
106+
all_integrations = integration.get_all_in_org(
107+
org_id, enums.CognitionIntegrationType.SHAREPOINT.value
108+
)
109+
all_integration_ids = [
110+
str(integration_entity.id) for integration_entity in all_integrations
111+
]
112+
for integration_id in all_integration_ids:
113+
transfer_api.post_process_integration(integration_id)

controller/user/manager.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ def update_organization_of_user(organization_name: str, user_mail: str) -> None:
5454
raise Exception(
5555
f"User {user_mail} is already part of organization {user_item.organization.name}"
5656
)
57+
5758
user.update_organization(user_item.id, organization.id, with_commit=True)
59+
organization_manager.sync_organization_sharepoint_integrations(organization.id)
5860

5961

6062
def update_user_role(user_id: str, role: str) -> User:

util/decorator.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from threading import Timer
22
from inspect import signature
3-
43
from datetime import datetime, timedelta
54
from functools import wraps
5+
import threading
66

77

88
def debounce(wait):
@@ -99,3 +99,36 @@ def wrapper(*args, **kwargs):
9999
return fn(*args, **kwargs)
100100

101101
return wrapper
102+
103+
104+
class param_debounce:
105+
106+
def __init__(self, seconds=0, minutes=0, hours=0):
107+
# total debounce delay in seconds
108+
self.delay = timedelta(
109+
seconds=seconds, minutes=minutes, hours=hours
110+
).total_seconds()
111+
# map: key -> (Timer, last_args, last_kwargs)
112+
self._timers = {}
113+
114+
def __call__(self, fn):
115+
@wraps(fn)
116+
def wrapper(*args, **kwargs):
117+
key = args[0] if args else None
118+
119+
existing = self._timers.get(key)
120+
if existing:
121+
timer, _, _ = existing
122+
timer.cancel()
123+
124+
def call_it():
125+
_, last_args, last_kwargs = self._timers.pop(key, (None, None, None))
126+
fn(*last_args, **last_kwargs)
127+
128+
timer = threading.Timer(self.delay, call_it)
129+
130+
self._timers[key] = (timer, args, kwargs)
131+
timer.daemon = True
132+
timer.start()
133+
134+
return wrapper

0 commit comments

Comments
 (0)