From ab986f5a494bcb9f6ced6082988028c233b66e81 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 18 Jan 2019 22:45:11 +0100 Subject: [PATCH 4/5] add measure on max delay before status (#14297) --- tests/olap.model | 51 ++++++++++++++++++++++++++++++++++++++++++++++ tests/test_wcs.py | 15 ++++++++++++++ wcs_olap/feeder.py | 21 +++++++++++++++++++ 3 files changed, 87 insertions(+) diff --git a/tests/olap.model b/tests/olap.model index 1b9ba93..25f267c 100644 --- a/tests/olap.model +++ b/tests/olap.model @@ -329,6 +329,12 @@ "master" : "field_field_item", "name" : "field_item", "table" : "formdata_demande_field_field_item" + }, + { + "facts" : "formdata_id", + "master" : "id", + "name" : "evolution", + "table" : "evolution_demande" } ], "key" : "id", @@ -369,6 +375,51 @@ "label" : "localisation géographique", "name" : "geolocation", "type" : "point" + }, + { + "expression" : "MAX(COALESCE(evolution.delay, NOW() - {fact_table}.receipt_time2)) FILTER (WHERE evolution.status_id = 0)", + "join" : [ + "evolution" + ], + "label" : "délai maximum avant le statut Just Submitted", + "name" : "max_delay_until_0_Just_Submitted", + "type" : "duration" + }, + { + "expression" : "MAX(COALESCE(evolution.delay, NOW() - {fact_table}.receipt_time2)) FILTER (WHERE evolution.status_id = 1)", + "join" : [ + "evolution" + ], + "label" : "délai maximum avant le statut New", + "name" : "max_delay_until_1_New", + "type" : "duration" + }, + { + "expression" : "MAX(COALESCE(evolution.delay, NOW() - {fact_table}.receipt_time2)) FILTER (WHERE evolution.status_id = 2)", + "join" : [ + "evolution" + ], + "label" : "délai maximum avant le statut Rejected", + "name" : "max_delay_until_2_Rejected", + "type" : "duration" + }, + { + "expression" : "MAX(COALESCE(evolution.delay, NOW() - {fact_table}.receipt_time2)) FILTER (WHERE evolution.status_id = 3)", + "join" : [ + "evolution" + ], + "label" : "délai maximum avant le statut Accepted", + "name" : "max_delay_until_3_Accepted", + "type" : "duration" + }, + { + "expression" : "MAX(COALESCE(evolution.delay, NOW() - {fact_table}.receipt_time2)) FILTER (WHERE evolution.status_id = 4)", + "join" : [ + "evolution" + ], + "label" : "délai maximum avant le statut Finished", + "name" : "max_delay_until_4_Finished", + "type" : "duration" } ], "name" : "formdata_demande" diff --git a/tests/test_wcs.py b/tests/test_wcs.py index 49a11f7..8277b4c 100644 --- a/tests/test_wcs.py +++ b/tests/test_wcs.py @@ -1,3 +1,5 @@ +import isodate +import pprint import json import pathlib2 @@ -104,3 +106,16 @@ schema = olap expected_json_schema = json.load(fd2) expected_json_schema['pg_dsn'] = postgres_db.dsn assert json_schema == expected_json_schema + + with postgres_db.conn() as conn: + with conn.cursor() as c: + c.execute('SET search_path = olap') + c.execute('''SELECT + item.label, + COUNT(formdata.id) AS demande_count, + MAX(COALESCE(evolution.delay, NOW() - formdata.receipt_time2)) FILTER (WHERE evolution.status_id = 1) AS demande_delai + FROM formdata_demande AS formdata + LEFT OUTER JOIN formdata_demande_field_field_item AS item + ON item.id = formdata.field_field_item + LEFT OUTER JOIN evolution_demande AS evolution + ON evolution.formdata_id = formdata.id GROUP BY item.label''') diff --git a/wcs_olap/feeder.py b/wcs_olap/feeder.py index b6224b0..c45a45e 100644 --- a/wcs_olap/feeder.py +++ b/wcs_olap/feeder.py @@ -916,6 +916,27 @@ class WcsFormdefFeeder(object): cube['joins'].append(join) cube['dimensions'].append(dimension) + # add join for evolutions + cube['joins'].append({ + 'name': 'evolution', + 'table': self.evolution_table_name, + 'master': 'id', + 'facts': 'formdata_id', + }) + + # add measure of delay for each status since receipt_time + for status_id, status in enumerate(self.formdef.schema.workflow.statuses): + cube['measures'].append( + { + 'name': 'max_delay_until_%s_%s' % (status_id, slugify(status.name)), + 'label': u'délai maximum avant le statut %s' % status.name, + 'type': 'duration', + 'expression': 'MAX(COALESCE(evolution.delay, NOW() - {fact_table}.receipt_time2)) FILTER (WHERE evolution.status_id = %s)' % status_id, + 'join': ['evolution'], + } + ) + + self.model['cubes'].append(cube) if self.do_feed: try: -- 2.20.1