Skip to content

Commit

Permalink
Merge pull request #1475 from betagouv/fix/policy
Browse files Browse the repository at this point in the history
Update policy engine
  • Loading branch information
nmrgt authored Nov 3, 2021
2 parents 9f6f696 + d164b18 commit 4a29c7c
Show file tree
Hide file tree
Showing 112 changed files with 1,741 additions and 1,026 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/analysis-test-integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: 'Analysis - Integration test'

on:
pull_request:
branches: [dev]
workflow_dispatch:

defaults:
run:
shell: bash

jobs:
e2e:
name: Test integration
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Start
run: sh integration.sh

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ bash e2e.sh

```shell
# local e2e
vi .env (set your database to 'test')
docker-compose up api (worker is started too)
docker-compose up dashboard
vi .env (set your database to 'test')
docker-compose run api yarn workspace @pdc/proxy ilos seed
cd tests
yarn
Expand Down
4 changes: 2 additions & 2 deletions api/db/migrations/20200325000000-update-trip-views.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/
var { createMigration } = require('../helpers/createMigration');
var { setup, up, down } = createMigration([
'trip/202003250000000_update_trip_view',
'trip/202003250000000_update_export_view',
'trip/20200325000000_update_trip_view',
'trip/20200325000000_update_export_view',
], __dirname);

exports.setup = setup;
Expand Down
4 changes: 2 additions & 2 deletions api/db/migrations/20200325000000-update_trip_views.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/
var { createMigration } = require('../helpers/createMigration');
var { setup, up, down } = createMigration([
'trip/202003250000000_update_trip_view',
'trip/202003250000000_update_export_view',
'trip/20200325000000_update_trip_view',
'trip/20200325000000_update_export_view',
], __dirname);

exports.setup = setup;
Expand Down
17 changes: 17 additions & 0 deletions api/db/migrations/20210901000000-update_policy_tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
/**
* Cast all foreign keys *_id as integer to match PostgreSQL types
* Current type is 'varchar' as fkeys were migrated from MongoDB
* as a toString() of ObjectID objects.
*/
var { createMigration } = require('../helpers/createMigration');

var { setup, up, down } = createMigration([
'policy/20210901000000_update_policy_meta',
'policy/20210901000000_update_policy_trip_view',
'trip/20210901000000_add_territory_index'
], __dirname);

exports.setup = setup;
exports.up = up;
exports.down = down;
8 changes: 8 additions & 0 deletions api/db/migrations/20210930135857-create_policy_trips_view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

var { createMigration } = require('../helpers/createMigration');
var { setup, up, down } = createMigration(['policy/20210930135857_create_policy_trips_view'], __dirname);

exports.setup = setup;
exports.up = up;
exports.down = down;
8 changes: 8 additions & 0 deletions api/db/migrations/20211005110112-fix_hydrate_from_policy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

var { createMigration } = require('../helpers/createMigration');
var { setup, up, down } = createMigration(['trip/20211005110112-fix_hydrate_from_policy'], __dirname);

exports.setup = setup;
exports.up = up;
exports.down = down;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE policy.policy_metas
DROP COLUMN datetime,
ALTER COLUMN value TYPE JSON
USING value::text::json;
DROP INDEX IF EXISTS policy.policy_meta_id_key;
CREATE UNIQUE INDEX policy_meta_unique_key ON policy.policy_metas (policy_id, key);

10 changes: 10 additions & 0 deletions api/db/migrations/policy/20210901000000_update_policy_meta.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ALTER TABLE policy.policy_metas
ADD COLUMN datetime timestamp,
ALTER COLUMN value TYPE INT
USING CASE
WHEN value::text = '{}' THEN 0
ELSE value::text::int
END;
DROP INDEX IF EXISTS policy.policy_meta_unique_key;
CREATE INDEX policy_meta_id_key ON policy.policy_metas (policy_id, key);
CREATE INDEX policy_meta_incentive ON policy.policy_metas (datetime);
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
CREATE EXTENSION IF NOT EXISTS intarray;
DROP MATERIALIZED VIEW IF EXISTS policy.trips;

CREATE MATERIALIZED VIEW policy.trips AS (
SELECT
cp._id as carpool_id,
cp.status as carpool_status,
cp.trip_id as trip_id,
tsc.value[1] as start_insee,
tec.value[1] as end_insee,
cp.operator_id::int as operator_id,
cp.operator_class as operator_class,
cp.datetime as datetime,
cp.seats as seats,
cp.cost as cost,
cp.is_driver as is_driver,
(CASE WHEN cp.distance IS NOT NULL THEN cp.distance ELSE (cp.meta::json->>'calc_distance')::int END) as distance,
(CASE WHEN cp.duration IS NOT NULL THEN cp.duration ELSE (cp.meta::json->>'calc_duration')::int END) as duration,
id.identity_uuid as identity_uuid,
id.has_travel_pass as has_travel_pass,
id.is_over_18 as is_over_18,
ats || cp.start_territory_id as start_territory_id,
ate || cp.end_territory_id as end_territory_id,
ap.applicable_policies as applicable_policies,
pp.processed_policies as processed_policies,
(ap.applicable_policies - pp.processed_policies) as processable_policies
FROM carpool.carpools as cp
LEFT JOIN territory.get_ancestors(ARRAY[cp.start_territory_id]) as ats ON TRUE
LEFT JOIN territory.get_ancestors(ARRAY[cp.end_territory_id]) as ate ON TRUE,
LATERAL (
SELECT
array_agg(value) as value
FROM territory.territory_codes
WHERE territory_id = cp.start_territory_id
AND type = 'insee'
) as tsc,
LATERAL (
SELECT
array_agg(value) as value
FROM territory.territory_codes
WHERE territory_id = cp.end_territory_id
AND type = 'insee'
) as tec,
-- Find all policies that appliable to carpool
LATERAL (
SELECT
COALESCE(array_agg(pp._id), ARRAY[]::int[]) as applicable_policies
FROM policy.policies as pp
WHERE
pp.territory_id = any(cp.start_territory_id || ats || ate || cp.end_territory_id)
AND pp.start_date <= cp.datetime
AND pp.end_date >= cp.datetime
AND pp.status = 'active'
) as ap,
-- Find all already processed policies
LATERAL (
SELECT
COALESCE(array_agg(pi.policy_id), ARRAY[]::int[]) as processed_policies
FROM policy.incentives as pi
WHERE
pi.carpool_id::int = cp._id
) as pp,
-- Find identity relative data
LATERAL (
SELECT
(CASE WHEN ci.travel_pass_user_id IS NOT NULL THEN true ELSE false END) as has_travel_pass,
(CASE WHEN ci.over_18 IS NOT NULL THEN ci.over_18 ELSE null END) as is_over_18,
ci.uuid as identity_uuid
FROM carpool.identities as ci
WHERE
cp.identity_id = ci._id
) as id
WHERE cp.datetime >= (NOW() - interval '140 days') AND cp.datetime < (NOW() - interval '5 days')
);

CREATE UNIQUE INDEX IF NOT EXISTS trips_carpool_id_idx ON policy.trips (carpool_id);
CREATE INDEX IF NOT EXISTS trips_datetime_idx ON policy.trips (datetime);
CREATE INDEX IF NOT EXISTS trips_trip_id_idx ON policy.trips (trip_id);
CREATE INDEX IF NOT EXISTS trips_applicable_policies_idx ON policy.trips (applicable_policies);
CREATE INDEX IF NOT EXISTS trips_processable_policies_idx ON policy.trips (processable_policies);
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
CREATE EXTENSION IF NOT EXISTS intarray;
DROP MATERIALIZED VIEW IF EXISTS policy.trips;

CREATE VIEW policy.trips AS (
SELECT
cp._id as carpool_id,
cp.status as carpool_status,
cp.trip_id as trip_id,
cp.acquisition_id as acquisition_id,
cp.operator_id::int as operator_id,
cp.operator_class as operator_class,
cp.datetime as datetime,
cp.seats as seats,
cp.cost as cost,
cp.is_driver as is_driver,
(CASE WHEN cp.distance IS NOT NULL THEN cp.distance ELSE (cp.meta::json->>'calc_distance')::int END) as distance,
(CASE WHEN cp.duration IS NOT NULL THEN cp.duration ELSE (cp.meta::json->>'calc_duration')::int END) as duration,
(CASE WHEN ci.travel_pass_user_id IS NOT NULL THEN true ELSE false END) as has_travel_pass,
(CASE WHEN ci.over_18 IS NOT NULL THEN ci.over_18 ELSE null END) as is_over_18,
ci.uuid as identity_uuid,
ats || cp.start_territory_id as start_territory_id,
ate || cp.end_territory_id as end_territory_id
FROM carpool.carpools as cp
LEFT JOIN territory.get_ancestors(ARRAY[cp.start_territory_id]) as ats ON TRUE
LEFT JOIN territory.get_ancestors(ARRAY[cp.end_territory_id]) as ate ON TRUE
LEFT JOIN carpool.identities as ci
ON cp.identity_id = ci._id
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
CREATE EXTENSION IF NOT EXISTS intarray;
DROP VIEW IF EXISTS policy.trips;

CREATE VIEW policy.trips AS (
SELECT
cp._id as carpool_id,
cp.status as carpool_status,
cp.trip_id as trip_id,
cp.acquisition_id as acquisition_id,
cp.operator_id::int as operator_id,
cp.operator_class as operator_class,
cp.datetime as datetime,
cp.seats as seats,
cp.cost as cost,
cp.is_driver as is_driver,
(CASE WHEN cp.distance IS NOT NULL THEN cp.distance ELSE (cp.meta::json->>'calc_distance')::int END) as distance,
(CASE WHEN cp.duration IS NOT NULL THEN cp.duration ELSE (cp.meta::json->>'calc_duration')::int END) as duration,
(CASE WHEN ci.travel_pass_user_id IS NOT NULL THEN true ELSE false END) as has_travel_pass,
(CASE WHEN ci.over_18 IS NOT NULL THEN ci.over_18 ELSE null END) as is_over_18,
ci.uuid as identity_uuid,
ats || cp.start_territory_id as start_territory_id,
ate || cp.end_territory_id as end_territory_id
FROM carpool.carpools as cp
LEFT JOIN territory.get_ancestors(ARRAY[cp.start_territory_id]) as ats ON TRUE
LEFT JOIN territory.get_ancestors(ARRAY[cp.end_territory_id]) as ate ON TRUE
LEFT JOIN carpool.identities as ci
ON cp.identity_id = ci._id
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CREATE EXTENSION IF NOT EXISTS intarray;
DROP VIEW IF EXISTS policy.trips;

CREATE VIEW policy.trips AS (
SELECT
cp._id as carpool_id,
cp.status as carpool_status,
cp.trip_id as trip_id,
cp.acquisition_id as acquisition_id,
cp.operator_id::int as operator_id,
cp.operator_class as operator_class,
cp.datetime as datetime,
cp.start_territory_id as start_territory_id,
cp.end_territory_id as end_territory_id,
cp.seats as seats,
cp.cost as cost,
cp.is_driver as is_driver,
(CASE WHEN cp.distance IS NOT NULL THEN cp.distance ELSE (cp.meta::json->>'calc_distance')::int END) as distance,
(CASE WHEN cp.duration IS NOT NULL THEN cp.duration ELSE (cp.meta::json->>'calc_duration')::int END) as duration,
(CASE WHEN ci.travel_pass_user_id IS NOT NULL THEN true ELSE false END) as has_travel_pass,
(CASE WHEN ci.over_18 IS NOT NULL THEN ci.over_18 ELSE null END) as is_over_18,
ci.uuid as identity_uuid,
tcs.value AS start_insee,
tce.value AS end_insee
FROM carpool.carpools as cp
LEFT JOIN carpool.identities as ci ON cp.identity_id = ci._id
LEFT JOIN territory.territory_codes tcs ON cp.start_territory_id = tcs.territory_id AND tcs.type::text = 'insee'::text
LEFT JOIN territory.territory_codes tce ON cp.end_territory_id = tce.territory_id AND tce.type::text = 'insee'::text
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP INDEX IF EXISTS trip.start_territory_id_idx;
DROP INDEX IF EXISTS trip.end_territory_id_idx;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE INDEX start_territory_id_idx ON carpool.carpools (start_territory_id);
CREATE INDEX end_territory_id_idx ON carpool.carpools (end_territory_id);
Loading

0 comments on commit 4a29c7c

Please sign in to comment.