backoffice: use dedicated SQL query to get resolution times (#7107) #1059

Merged
fpeters merged 1 commits from wip/7107-form-resolution-times-local-stat into main 2024-01-29 12:11:37 +01:00
2 changed files with 17 additions and 11 deletions

View File

@ -3097,7 +3097,7 @@ class FormPage(Directory, TempfileDirectoryMixin):
r += stats_for_fields
r += htmltext('</div>')
stats_times = self.stats_resolution_time(values)
stats_times = self.stats_resolution_time(criterias)
if stats_times:
r += htmltext('<div class="bo-block">')
r += stats_times
@ -3159,25 +3159,23 @@ class FormPage(Directory, TempfileDirectoryMixin):
return r.getvalue()
def stats_resolution_time(self, values):
def stats_resolution_time(self, criterias):
possible_status = [('wf-%s' % x.id, x.id) for x in self.formdef.workflow.possible_status[1:]]
if len(possible_status) < 2:
return
start_status = 'wf-%s' % self.formdef.workflow.possible_status[0].id
r = TemplateIO(html=True)
r += htmltext('<h2>%s</h2>') % _('Resolution time')
for dummy, status_id in possible_status:
res_time_forms = []
for filled in values:
for evo in filled.evolution or []:
if evo.status == 'wf-%s' % status_id:
res_time_forms.append(time.mktime(evo.time) - time.mktime(filled.receipt_time))
break
for wf_status_id, status_id in possible_status:
res_time_forms = self.formdef.data_class().get_resolution_times(
start_status, [wf_status_id], extra_criterias=criterias
)
if not res_time_forms:
continue
res_time_forms.sort()
sum_times = sum(res_time_forms)
len_times = len(res_time_forms)
min_times = res_time_forms[0]

View File

@ -2312,12 +2312,20 @@ class SqlDataMixin(SqlMixin):
cur.close()
@classmethod
def get_resolution_times(cls, start_status, end_statuses, period_start=None, period_end=None):
def get_resolution_times(
cls, start_status, end_statuses, period_start=None, period_end=None, extra_criterias=None
):
criterias = [StrictNotEqual('f.status', 'draft')]
if period_start:
criterias.append(GreaterOrEqual('f.receipt_time', period_start))
if period_end:
criterias.append(Less('f.receipt_time', period_end))
if extra_criterias:
for criteria in extra_criterias:
# change attributes to point to the formdata table (f)
altered_criteria = copy.copy(criteria)
altered_criteria.attribute = f'f.{criteria.attribute}'
criterias.append(altered_criteria)
where_clauses, params, dummy = parse_clause(criterias)