From e8a48812f4b39502a3b135e4c61757a536cf3191 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 18 Jan 2019 22:45:11 +0100 Subject: [PATCH 5/7] add measure on max delay before status (#14297) --- tests/olap.model | 51 ++++++++++++++++++++++++++++++++++++++++++++++ tests/test_wcs.py | 17 ++++++++++++++++ wcs_olap/feeder.py | 21 +++++++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/tests/olap.model b/tests/olap.model index 82fcea9..6e727c3 100644 --- a/tests/olap.model +++ b/tests/olap.model @@ -353,6 +353,12 @@ "master" : "\"field_itemOpen\"", "name" : "itemOpen", "table" : "formdata_demande_field_itemOpen" + }, + { + "facts" : "formdata_id", + "master" : "id", + "name" : "evolution", + "table" : "evolution_demande" } ], "key" : "id", @@ -393,6 +399,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 7266871..b5819f0 100644 --- a/tests/test_wcs.py +++ b/tests/test_wcs.py @@ -1,3 +1,5 @@ +import isodate +import pprint import json import pytest @@ -98,6 +100,21 @@ def test_wcs_fixture(wcs, postgres_db, tmpdir, olap_cmd, caplog): 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_item AS item + ON item.id = formdata.field_item + LEFT OUTER JOIN evolution_demande AS evolution + ON evolution.formdata_id = formdata.id GROUP BY item.label''') + def test_requests_exception(wcs, postgres_db, tmpdir, olap_cmd, caplog): with mock.patch('requests.get', side_effect=requests.RequestException('wat!')): diff --git a/wcs_olap/feeder.py b/wcs_olap/feeder.py index 31add29..857a76a 100644 --- a/wcs_olap/feeder.py +++ b/wcs_olap/feeder.py @@ -997,6 +997,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.23.0