WIP: create a VisitedObjects class and table to reduce Sessions table usage (#81183) #624
|
@ -172,6 +172,7 @@ def create_temporary_pub(pickle_mode=False, lazy_mode=False):
|
|||
sql.Application.do_table()
|
||||
sql.ApplicationElement.do_table()
|
||||
sql.init_global_table()
|
||||
sql.VisitedObject.do_table()
|
||||
|
||||
conn.close()
|
||||
|
||||
|
|
|
@ -628,7 +628,7 @@ class ManagementDirectory(Directory):
|
|||
r += htmltext('<tbody>')
|
||||
workflows = {}
|
||||
session = get_session()
|
||||
visited_objects = session.get_visited_objects(exclude_user=session.user)
|
||||
visited_objects = sql.VisitedObject.get_visited_objects(exclude_user=session.user)
|
||||
|
||||
for formdata in formdatas:
|
||||
if formdata.formdef.workflow_id not in workflows:
|
||||
workflows[formdata.formdef.workflow_id] = formdata.formdef.workflow
|
||||
|
@ -3227,12 +3227,14 @@ class FormBackOfficeStatusPage(FormStatusPage):
|
|||
return response
|
||||
|
||||
def user_pending_forms(self):
|
||||
from wcs import sql
|
||||
|
||||
self.check_receiver()
|
||||
get_response().filter = {'raw': True}
|
||||
response = self.get_user_pending_forms()
|
||||
|
||||
# preemptive locking of forms
|
||||
all_visitors = get_session().get_object_visitors(self.filled)
|
||||
all_visitors = sql.VisitedObject.get_object_visitors(self.filled)
|
||||
visitors = [x for x in all_visitors if x[0] != get_session().user]
|
||||
me_in_visitors = bool(get_session().user in [x[0] for x in all_visitors])
|
||||
|
||||
|
@ -3245,7 +3247,7 @@ class FormBackOfficeStatusPage(FormStatusPage):
|
|||
if user_roles.intersection(
|
||||
user_formdata.get_actions_roles(condition_kwargs={'record_errors': False})
|
||||
):
|
||||
session.mark_visited_object(user_formdata)
|
||||
sql.VisitedObject.mark_visited_object(user_formdata, session.user)
|
||||
|
||||
return response
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
import urllib.parse
|
||||
|
||||
from quixote import get_publisher, get_request, get_session, redirect
|
||||
from quixote import get_publisher, get_request, redirect
|
||||
from quixote.html import TemplateIO, htmltext
|
||||
|
||||
from wcs.backoffice.pagination import pagination_links
|
||||
|
@ -254,6 +254,8 @@ class FormDefUI:
|
|||
return (items, total_count)
|
||||
|
||||
def tbody(self, fields=None, items=None, url_action=None, include_checkboxes=False):
|
||||
from wcs.sql import VisitedObject
|
||||
|
||||
r = TemplateIO(html=True)
|
||||
if url_action:
|
||||
pass
|
||||
|
@ -262,7 +264,7 @@ class FormDefUI:
|
|||
url_action = ''
|
||||
user = get_request().user
|
||||
user_roles = set(user.get_roles())
|
||||
visited_objects = get_session().get_visited_objects(exclude_user=user.id)
|
||||
visited_objects = VisitedObject.get_visited_objects(exclude_user=user.id)
|
||||
include_criticality_level = bool(self.formdef.workflow.criticality_levels)
|
||||
for i, filled in enumerate(items):
|
||||
classes = ['status-%s-%s' % (filled.formdef.workflow.id, filled.status)]
|
||||
|
|
|
@ -626,10 +626,13 @@ class FormStatusPage(Directory, FormTemplateMixin):
|
|||
return r.getvalue()
|
||||
|
||||
def status(self):
|
||||
from wcs.sql import VisitedObject
|
||||
|
||||
session = get_session()
|
||||
if get_request().get_query() == 'unlock':
|
||||
# mark user as active visitor of the object, then redirect to self,
|
||||
# the unlocked form will appear.
|
||||
get_session().mark_visited_object(self.filled)
|
||||
VisitedObject.mark_visited_object(self.filled, session.user)
|
||||
return redirect('./#lock-notice')
|
||||
|
||||
user = self.check_receiver()
|
||||
|
@ -641,7 +644,7 @@ class FormStatusPage(Directory, FormTemplateMixin):
|
|||
form.clear_errors()
|
||||
else:
|
||||
if response:
|
||||
get_session().unmark_visited_object(self.filled)
|
||||
VisitedObject.unmark_visited_object(self.filled)
|
||||
return response
|
||||
|
||||
get_response().add_javascript(['jquery.js', 'qommon.forms.js'])
|
||||
|
@ -663,8 +666,8 @@ class FormStatusPage(Directory, FormTemplateMixin):
|
|||
|
||||
locked = False
|
||||
if form:
|
||||
all_visitors = get_session().get_object_visitors(self.filled)
|
||||
visitors = [x for x in all_visitors if x[0] != get_session().user]
|
||||
all_visitors = VisitedObject.get_object_visitors(self.filled)
|
||||
visitors = [x for x in all_visitors if x[0] != session.user]
|
||||
if visitors:
|
||||
current_timestamp = time.time()
|
||||
visitor_users = []
|
||||
|
@ -701,7 +704,7 @@ class FormStatusPage(Directory, FormTemplateMixin):
|
|||
r += form.render()
|
||||
if form.widgets:
|
||||
r += htmltext('</div></div>')
|
||||
get_session().mark_visited_object(self.filled)
|
||||
VisitedObject.mark_visited_object(self.filled, session.user)
|
||||
|
||||
if not locked:
|
||||
if (self.filled.get_status() and self.filled.get_status().backoffice_info_text) or (
|
||||
|
@ -992,6 +995,8 @@ class FormStatusPage(Directory, FormTemplateMixin):
|
|||
return super()._q_traverse(path)
|
||||
|
||||
def wfedit(self, action_id):
|
||||
from wcs.sql import VisitedObject
|
||||
|
||||
wf_status = self.filled.get_status()
|
||||
for item in wf_status.items:
|
||||
if item.id != action_id:
|
||||
|
@ -1005,8 +1010,9 @@ class FormStatusPage(Directory, FormTemplateMixin):
|
|||
f.edited_data = self.filled
|
||||
f.edit_action = item
|
||||
f.action_url = 'wfedit-%s' % item.id
|
||||
session = get_session()
|
||||
if get_request().is_in_backoffice():
|
||||
get_session().mark_visited_object(self.filled)
|
||||
VisitedObject.mark_visited_object(self.filled, session.user)
|
||||
get_response().breadcrumb = get_response().breadcrumb[:-1]
|
||||
get_response().breadcrumb.append((f.action_url, _('Edit')))
|
||||
return f._q_index()
|
||||
|
|
|
@ -14,8 +14,6 @@
|
|||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import time
|
||||
|
||||
from .qommon import sessions
|
||||
from .qommon.sessions import Session
|
||||
|
||||
|
@ -60,61 +58,6 @@ class BasicSession(Session):
|
|||
return False
|
||||
return formdata.get_object_key() in self.anonymous_formdata_keys
|
||||
|
||||
def mark_visited_object(self, formdata):
|
||||
key = formdata.get_object_key()
|
||||
if not self.visiting_objects:
|
||||
self.visiting_objects = {}
|
||||
# first clean older objects
|
||||
current_timestamp = time.time()
|
||||
for object_key, object_timestamp in list(self.visiting_objects.items()):
|
||||
if object_timestamp < (current_timestamp - 30 * 60):
|
||||
del self.visiting_objects[object_key]
|
||||
self.visiting_objects[key] = current_timestamp
|
||||
|
||||
@classmethod
|
||||
def get_visited_objects(cls, exclude_user=None):
|
||||
# return the list of visited objects
|
||||
current_timestamp = time.time()
|
||||
visited_objects = {}
|
||||
for session in cls.select_recent(ignore_errors=True):
|
||||
if session.user and session.user == exclude_user:
|
||||
continue
|
||||
visiting_objects = getattr(session, 'visiting_objects', None)
|
||||
if not visiting_objects:
|
||||
continue
|
||||
for object_key, object_timestamp in visiting_objects.items():
|
||||
if object_timestamp > (current_timestamp - 30 * 60):
|
||||
visited_objects[object_key] = True
|
||||
return visited_objects.keys()
|
||||
|
||||
@classmethod
|
||||
def get_object_visitors(cls, formdata):
|
||||
'''return tuples of (user_id, last_visit_timestamp)'''
|
||||
object_key = formdata.get_object_key()
|
||||
current_timestamp = time.time()
|
||||
visitors = {}
|
||||
for session in cls.get_sessions_with_visited_object(object_key):
|
||||
object_timestamp = session.visiting_objects.get(object_key)
|
||||
if object_timestamp > (current_timestamp - 30 * 60):
|
||||
visitors[session.user] = max(object_timestamp, visitors.get(session.user, 0))
|
||||
return visitors.items()
|
||||
|
||||
def unmark_visited_object(self, formdata):
|
||||
object_key = formdata.get_object_key()
|
||||
# remove from current session
|
||||
if object_key in (getattr(self, 'visiting_objects', None) or {}):
|
||||
del self.visiting_objects[object_key]
|
||||
# and from others
|
||||
for session in self.__class__.select_recent(ignore_errors=True):
|
||||
if session.id == self.id:
|
||||
continue
|
||||
visiting_objects = getattr(session, 'visiting_objects', None)
|
||||
if not visiting_objects:
|
||||
continue
|
||||
if object_key in visiting_objects:
|
||||
del session.visiting_objects[object_key]
|
||||
session.store()
|
||||
|
||||
|
||||
sessions.BasicSession = BasicSession
|
||||
StorageSessionManager = sessions.StorageSessionManager
|
||||
|
|
148
wcs/sql.py
148
wcs/sql.py
|
@ -3218,6 +3218,120 @@ class TransientData(SqlMixin):
|
|||
return []
|
||||
|
||||
|
||||
class VisitedObject(SqlMixin):
|
||||
_table_name = 'visited_object'
|
||||
_table_static_fields = [
|
||||
('id', 'varchar'),
|
||||
('visitors', 'jsonb'), # user -> timestamp
|
||||
]
|
||||
_numerical_id = False
|
||||
|
||||
@classmethod
|
||||
@guard_postgres
|
||||
def do_table(cls):
|
||||
conn, cur = get_connection_and_cursor()
|
||||
sql_statement = '''CREATE TABLE IF NOT EXISTS %s (
|
||||
id varchar,
|
||||
visitors jsonb,
|
||||
primary key(id)
|
||||
);''' % (cls._table_name, )
|
||||
cur.execute(sql_statement)
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
@classmethod
|
||||
def clean(cls):
|
||||
conn, cur = get_connection_and_cursor()
|
||||
sql_statement = '''DELETE FROM %s WHERE EXISTS(
|
||||
SELECT 1 FROM jsonb_object_keys(visitors) keys WHERE (visitors->>keys)::timestamp without time zone > %%(date)s
|
||||
);''' % (cls._table_name, )
|
||||
cur.execute(sql_statement, datetime.datetime.now() - datetime.timedelta(seconds=30))
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
@guard_postgres
|
||||
def store(self):
|
||||
sql_dict = {
|
||||
'id': self.id,
|
||||
'visitors': {str(k):str(v) for (k,v) in self.visitors.items()},
|
||||
}
|
||||
|
||||
conn, cur = get_connection_and_cursor()
|
||||
column_names = sql_dict.keys()
|
||||
sql_statement = '''INSERT INTO %s (%s) VALUES (%s)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
visitors = EXCLUDED.visitors || %%(visitors)s;''' % (
|
||||
self._table_name,
|
||||
', '.join(column_names),
|
||||
', '.join(['%%(%s)s' % x for x in column_names])
|
||||
)
|
||||
cur.execute(sql_statement, sql_dict)
|
||||
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
@classmethod
|
||||
def mark_visited_object(cls, visited, visitor = None):
|
||||
new_object = VisitedObject()
|
||||
new_object.id = visited.get_object_key()
|
||||
if visitor:
|
||||
visitor_key = visitor
|
||||
else:
|
||||
visitor_key = ""
|
||||
fpeters
commented
Curieux la configuration pre-commit devrait éviter ça, utiliser des single quotes. Curieux la configuration pre-commit devrait éviter ça, utiliser des single quotes.
|
||||
new_object.visitors = {visitor_key: now()}
|
||||
new_object.store()
|
||||
|
||||
@classmethod
|
||||
@guard_postgres
|
||||
def get_visited_objects(cls, exclude_user=None):
|
||||
sql_dict = {
|
||||
"date": datetime.datetime.now() - datetime.timedelta(seconds=30 * 60),
|
||||
"exclude_user": str(exclude_user)
|
||||
}
|
||||
conn, cur = get_connection_and_cursor()
|
||||
if exclude_user:
|
||||
sql_statement = '''SELECT id FROM %s WHERE NOT EXISTS(
|
||||
SELECT 1 FROM jsonb_object_keys(visitors) keys WHERE (visitors->>keys)::timestamp without time zone > %%(date)s AND keys != %%(exclude_user)s
|
||||
);''' % (cls._table_name,)
|
||||
else:
|
||||
sql_statement = '''SELECT id FROM %s WHERE NOT EXISTS(
|
||||
SELECT 1 FROM jsonb_object_keys(visitors) keys WHERE (visitors->>keys)::timestamp without time zone > %%(date)s
|
||||
);''' % (cls._table_name,)
|
||||
cur.execute(sql_statement, sql_dict)
|
||||
|
||||
visited_objects = []
|
||||
for row in cur.fetchall():
|
||||
visited_objects.append(row[0])
|
||||
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
return visited_objects
|
||||
|
||||
@classmethod
|
||||
def get_object_visitors(cls, formdata):
|
||||
sql_dict = {
|
||||
"id": formdata.get_object_key(),
|
||||
"date": datetime.datetime.now() - datetime.timedelta(seconds=30 * 60),
|
||||
}
|
||||
conn, cur = get_connection_and_cursor()
|
||||
sql_statement = '''SELECT key, value FROM %s, LATERAL jsonb_each_text(visitors) WHERE value::timestamp without time zone > %%(date)s AND id=%%(id)s;''' % (cls._table_name, )
|
||||
cur.execute(sql_statement, sql_dict)
|
||||
|
||||
visited_objects = []
|
||||
for row in cur.fetchall():
|
||||
visited_objects.append(row[0])
|
||||
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
return visited_objects
|
||||
|
||||
@classmethod
|
||||
def unmark_visited_object(cls, formdata):
|
||||
cls.wipe(clause=[Equal("id", formdata.get_object_key())])
|
||||
|
||||
|
||||
class Session(SqlMixin, wcs.sessions.BasicSession):
|
||||
_table_name = 'sessions'
|
||||
_table_static_fields = [
|
||||
|
@ -3226,14 +3340,6 @@ class Session(SqlMixin, wcs.sessions.BasicSession):
|
|||
]
|
||||
_numerical_id = False
|
||||
|
||||
@classmethod
|
||||
@guard_postgres
|
||||
def select_recent(cls, seconds=30 * 60, **kwargs):
|
||||
clause = [
|
||||
GreaterOrEqual('last_update_time', datetime.datetime.now() - datetime.timedelta(seconds=seconds))
|
||||
]
|
||||
return cls.select(clause=clause, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def clean(cls):
|
||||
now = time.time()
|
||||
|
@ -3324,25 +3430,6 @@ class Session(SqlMixin, wcs.sessions.BasicSession):
|
|||
|
||||
return objects
|
||||
|
||||
@classmethod
|
||||
def get_sessions_with_visited_object(cls, object_key):
|
||||
conn, cur = get_connection_and_cursor()
|
||||
|
||||
sql_statement = '''SELECT %s
|
||||
FROM %s
|
||||
WHERE %%(value)s = ANY(visiting_objects_keys)
|
||||
AND last_update_time > (now() - interval '30 minutes')
|
||||
''' % (
|
||||
', '.join([x[0] for x in cls._table_static_fields] + cls.get_data_fields()),
|
||||
cls._table_name,
|
||||
)
|
||||
cur.execute(sql_statement, {'value': object_key})
|
||||
objects = cls.get_objects(cur)
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
return objects
|
||||
|
||||
@classmethod
|
||||
def get_data_fields(cls):
|
||||
return []
|
||||
|
@ -5030,7 +5117,7 @@ def get_period_total(
|
|||
# latest migration, number + description (description is not used
|
||||
# programmaticaly but will make sure git conflicts if two migrations are
|
||||
# separately added with the same number)
|
||||
SQL_LEVEL = (93, 'add application columns in snapshot table')
|
||||
SQL_LEVEL = (94, 'add VisitedObject table')
|
||||
|
||||
|
||||
def migrate_global_views(conn, cur):
|
||||
|
@ -5351,6 +5438,11 @@ def migrate():
|
|||
for formdef in CardDef.select():
|
||||
do_formdef_tables(formdef, rebuild_views=False, rebuild_global_views=False)
|
||||
|
||||
if sql_level < 94:
|
||||
# 94: add VisitedObject table
|
||||
VisitedObject.do_table()
|
||||
## TODO: migrate?
|
||||
|
||||
if sql_level != SQL_LEVEL[0]:
|
||||
cur.execute(
|
||||
'''UPDATE wcs_meta SET value = %s, updated_at=NOW() WHERE key = %s''',
|
||||
|
|
Loading…
Reference in New Issue
Plutôt garder les méthodes sur l'objet session, ce qui permettra de garder les modifications localisées. (ça résoudra l'échec sur test_sessions.test_sessions_visiting_objects par la même occasion).