From b8de85e311d5642133e6edccc01d0c4126a6489d Mon Sep 17 00:00:00 2001 From: freddyDOTCMS <147462678+freddyDOTCMS@users.noreply.github.com> Date: Thu, 12 Dec 2024 15:24:23 -0600 Subject: [PATCH] Filtering by customer_id any time that execute a query in CubeJs (#30931) ### Proposed Changes * Setting the Cube.js in the Docker Container https://github.com/dotCMS/core/pull/30931/files#diff-106fd8c9a322da82fa432a4a4e9447566d08b515d01153f78854b0ffe892d2faR134 * Setting the JWK URL right https://github.com/dotCMS/core/pull/30931/files#diff-106fd8c9a322da82fa432a4a4e9447566d08b515d01153f78854b0ffe892d2faR125 * Setting the CUBEJS_JWT_ISSUER right the value in the token for the 'iss' attribute is 'http://localhost:61111/realms/dotcms' https://github.com/dotCMS/core/pull/30931/files#diff-106fd8c9a322da82fa432a4a4e9447566d08b515d01153f78854b0ffe892d2faR127 * Include filter by cluster and customer but just if the Query is using the request scope it means if it not a Experiment Query https://github.com/dotCMS/core/pull/30931/files#diff-3b5963eeaa042b27689c692068c4ae553d2ccedabb046e78543c28784c18e777R24-R36 * Include cluster_id and customer_id in the CubeJS Schema https://github.com/dotCMS/core/pull/30931/files#diff-c291c8a51311832c67ceb2e3fb2a2d4fe1d9c4e02abda400c156e10bed18200eR182-R183 --- .../analytics/docker-compose.yml | 6 ++++-- .../analytics/setup/config/dev/cube/cube.js | 16 ++++++++++++++-- .../setup/config/dev/cube/schema/Events.js | 4 ++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/docker/docker-compose-examples/analytics/docker-compose.yml b/docker/docker-compose-examples/analytics/docker-compose.yml index 21449399e1ec..f99e2210a4ad 100644 --- a/docker/docker-compose-examples/analytics/docker-compose.yml +++ b/docker/docker-compose-examples/analytics/docker-compose.yml @@ -122,14 +122,16 @@ services: - CUBEJS_DB_NAME=${CH_DB:-clickhouse_test_db} - CUBEJS_DB_USER=${CH_USER:-clickhouse_test_user} - CUBEJS_DB_PASS=${CH_PWD:-clickhouse_password} - - CUBEJS_JWK_URL=${JWKS_URL:-http://keycloak:61111/realms/dotcms/protocol/openid-connect/certs} + - CUBEJS_JWK_URL=${JWKS_URL:-http://host.docker.internal:61111/realms/dotcms/protocol/openid-connect/certs} - CUBEJS_JWT_AUDIENCE=api-dotcms-analytics-audience - - CUBEJS_JWT_ISSUER=${AUTH_SERVER_URL:-http://keycloak:61111/realms/dotcms} + - CUBEJS_JWT_ISSUER=${AUTH_SERVER_URL:-http://localhost:61111/realms/dotcms} - CUBEJS_JWT_ALGS=RS256 - CUBEJS_JWT_CLAIMS_NAMESPACE=https://dotcms.com/analytics + - CUBEJS_LOG_LEVEL=debug volumes: - cube_metastore:/cube/conf/.cubestore - ./setup/config/dev/cube/schema:/cube/conf/schema + - ./setup/config/dev/cube/cube.js:/cube/conf/cube.js ch_server: container_name: ch_server diff --git a/docker/docker-compose-examples/analytics/setup/config/dev/cube/cube.js b/docker/docker-compose-examples/analytics/setup/config/dev/cube/cube.js index 3bd1b3ca920c..ab2d68f32518 100644 --- a/docker/docker-compose-examples/analytics/setup/config/dev/cube/cube.js +++ b/docker/docker-compose-examples/analytics/setup/config/dev/cube/cube.js @@ -1,3 +1,5 @@ + + // cube.js configuration file module.exports = { /* @@ -7,8 +9,8 @@ module.exports = { `pre_aggregations_${securityContext.customerId}`, */ - queryRewrite: (query, { securityContext }) => { + queryRewrite: (query, { securityContext }) => { if (!securityContext) { throw new Error('No valid token'); @@ -16,12 +18,22 @@ module.exports = { const tokenData = securityContext["https://dotcms.com/analytics"]; + const isRequestQuery = (query.measures + query.dimensions).includes("request."); + + if (isRequestQuery) { query.filters.push({ - member: 'Events.clusterId', + member: 'request.clusterId', operator: 'equals', values: [tokenData.clusterId], }); + query.filters.push({ + member: 'request.customerId', + operator: 'equals', + values: [tokenData.customerId], + }); + } + return query; }, diff --git a/docker/docker-compose-examples/analytics/setup/config/dev/cube/schema/Events.js b/docker/docker-compose-examples/analytics/setup/config/dev/cube/schema/Events.js index 280d7bc22e59..5950342bc17d 100644 --- a/docker/docker-compose-examples/analytics/setup/config/dev/cube/schema/Events.js +++ b/docker/docker-compose-examples/analytics/setup/config/dev/cube/schema/Events.js @@ -179,6 +179,8 @@ cube('request', { MAX(CASE WHEN event_type = 'URL_MAP' THEN object_content_type_var_name ELSE NULL END) as url_map_content_type_var_name, MAX(object_detail_page_url) as url_map_detail_page_url, MAX(url) AS url, + MAX(cluster_id) AS cluster_id, + MAX(customer_id) AS customer_id, CASE WHEN MAX(CASE WHEN event_type = 'FILE_REQUEST' THEN 1 ELSE 0 END) = 1 THEN 'FILE' WHEN MAX(CASE WHEN event_type = 'PAGE_REQUEST' THEN 1 ELSE 0 END) = 1 THEN 'PAGE' @@ -189,6 +191,8 @@ cube('request', { GROUP BY request_id`, dimensions: { requestId: { sql: 'request_id', type: `string` }, + clusterId: { sql: 'cluster_id', type: `string` }, + customerId: { sql: 'customer_id', type: `string` }, sessionId: { sql: 'sessionid', type: `string` }, isSessionNew: { sql: 'isSessionNew', type: `boolean` }, createdAt: { sql: 'createdAt', type: `time`, },