From 9ae9b418ef65710124bdd8bdfab85cd9996889d2 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Mon, 24 Jul 2023 13:09:00 +0200 Subject: [PATCH 01/11] WIP --- coderd/database/queries/insights.sql | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/coderd/database/queries/insights.sql b/coderd/database/queries/insights.sql index e7e5f34b807c9..a87f45ca5d1d8 100644 --- a/coderd/database/queries/insights.sql +++ b/coderd/database/queries/insights.sql @@ -95,10 +95,19 @@ WITH d AS ( ), template_ids AS ( SELECT from_, - array_agg(DISTINCT template_id) AS ids - FROM usage_by_day, unnest(template_ids) template_id - WHERE template_id IS NOT NULL - GROUP BY from_, template_ids + array_agg(DISTINCT id) AS ids + FROM ( + SELECT from_, unnest(ids) AS id + FROM ( + SELECT DISTINCT + from_, + array_agg(DISTINCT template_id) AS ids + FROM usage_by_day, unnest(template_ids) template_id + WHERE template_id IS NOT NULL + GROUP BY from_, template_ids + ) AS template_usage_by_day_unnested + ) AS template_usage_by_day + GROUP BY from_ ) SELECT From 540efbde43255920f9da6ae8ff6bb84403460c73 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Mon, 24 Jul 2023 13:21:03 +0200 Subject: [PATCH 02/11] fix --- coderd/database/queries/insights.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/coderd/database/queries/insights.sql b/coderd/database/queries/insights.sql index a87f45ca5d1d8..1f95619e86b36 100644 --- a/coderd/database/queries/insights.sql +++ b/coderd/database/queries/insights.sql @@ -94,10 +94,10 @@ WITH d AS ( GROUP BY ts.from_, ts.to_, was.user_id ), template_ids AS ( SELECT - from_, + template_usage_by_day.from_, array_agg(DISTINCT id) AS ids FROM ( - SELECT from_, unnest(ids) AS id + SELECT template_usage_by_day_unnested.from_, unnest(ids) AS id FROM ( SELECT DISTINCT from_, @@ -107,7 +107,7 @@ WITH d AS ( GROUP BY from_, template_ids ) AS template_usage_by_day_unnested ) AS template_usage_by_day - GROUP BY from_ + GROUP BY template_usage_by_day.from_ ) SELECT From efac787563a806412a25234581f42ee2779836d8 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Mon, 24 Jul 2023 11:21:58 +0000 Subject: [PATCH 03/11] Fix --- coderd/database/queries.sql.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index 841b1ef1dc9d8..7f2fee4001ae7 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -1400,11 +1400,20 @@ WITH d AS ( GROUP BY ts.from_, ts.to_, was.user_id ), template_ids AS ( SELECT - from_, - array_agg(DISTINCT template_id) AS ids - FROM usage_by_day, unnest(template_ids) template_id - WHERE template_id IS NOT NULL - GROUP BY from_, template_ids + template_usage_by_day.from_, + array_agg(DISTINCT id) AS ids + FROM ( + SELECT template_usage_by_day_unnested.from_, unnest(ids) AS id + FROM ( + SELECT DISTINCT + from_, + array_agg(DISTINCT template_id) AS ids + FROM usage_by_day, unnest(template_ids) template_id + WHERE template_id IS NOT NULL + GROUP BY from_, template_ids + ) AS template_usage_by_day_unnested + ) AS template_usage_by_day + GROUP BY template_usage_by_day.from_ ) SELECT From a759b38ff313185d9d11f5501a6844c096fc3d52 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Mon, 24 Jul 2023 13:43:12 +0200 Subject: [PATCH 04/11] Perf improvement --- coderd/database/queries/insights.sql | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/coderd/database/queries/insights.sql b/coderd/database/queries/insights.sql index 1f95619e86b36..854ef58766b55 100644 --- a/coderd/database/queries/insights.sql +++ b/coderd/database/queries/insights.sql @@ -95,17 +95,13 @@ WITH d AS ( ), template_ids AS ( SELECT template_usage_by_day.from_, - array_agg(DISTINCT id) AS ids + array_agg(template_id) AS ids FROM ( - SELECT template_usage_by_day_unnested.from_, unnest(ids) AS id - FROM ( - SELECT DISTINCT - from_, - array_agg(DISTINCT template_id) AS ids - FROM usage_by_day, unnest(template_ids) template_id - WHERE template_id IS NOT NULL - GROUP BY from_, template_ids - ) AS template_usage_by_day_unnested + SELECT DISTINCT + from_, + template_id + FROM usage_by_day, unnest(template_ids) template_id + WHERE template_id IS NOT NULL ) AS template_usage_by_day GROUP BY template_usage_by_day.from_ ) From ee7cca55e900ed8b6dfdda0fc5a60a65e3c16f58 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Mon, 24 Jul 2023 13:54:45 +0200 Subject: [PATCH 05/11] WIP --- coderd/database/queries/insights.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coderd/database/queries/insights.sql b/coderd/database/queries/insights.sql index 854ef58766b55..da2084318953e 100644 --- a/coderd/database/queries/insights.sql +++ b/coderd/database/queries/insights.sql @@ -95,7 +95,7 @@ WITH d AS ( ), template_ids AS ( SELECT template_usage_by_day.from_, - array_agg(template_id) AS ids + array_agg(template_usage_by_day.template_id) AS ids FROM ( SELECT DISTINCT from_, From 0c066b8708e00d6a5c40356f8b530a817dde2778 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Mon, 24 Jul 2023 14:00:40 +0200 Subject: [PATCH 06/11] WIP --- coderd/database/queries/insights.sql | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/coderd/database/queries/insights.sql b/coderd/database/queries/insights.sql index da2084318953e..20d89e24c70c2 100644 --- a/coderd/database/queries/insights.sql +++ b/coderd/database/queries/insights.sql @@ -93,17 +93,17 @@ WITH d AS ( ) GROUP BY ts.from_, ts.to_, was.user_id ), template_ids AS ( - SELECT - template_usage_by_day.from_, - array_agg(template_usage_by_day.template_id) AS ids - FROM ( - SELECT DISTINCT - from_, - template_id - FROM usage_by_day, unnest(template_ids) template_id - WHERE template_id IS NOT NULL - ) AS template_usage_by_day - GROUP BY template_usage_by_day.from_ + SELECT + template_usage_by_day.from_, + array_agg(template_id) AS ids + FROM ( + SELECT DISTINCT + from_, + unnest(template_ids) AS template_id + FROM usage_by_day + WHERE usage_by_day.template_ids IS NOT NULL + ) AS template_usage_by_day + GROUP BY template_usage_by_day.from_ ) SELECT From b7ab38f40963840c6e6029875812ec2711d16864 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Mon, 24 Jul 2023 12:03:09 +0000 Subject: [PATCH 07/11] make gen --- coderd/database/queries.sql.go | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index 7f2fee4001ae7..473b537f271f6 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -1399,21 +1399,17 @@ WITH d AS ( ) GROUP BY ts.from_, ts.to_, was.user_id ), template_ids AS ( - SELECT - template_usage_by_day.from_, - array_agg(DISTINCT id) AS ids - FROM ( - SELECT template_usage_by_day_unnested.from_, unnest(ids) AS id - FROM ( - SELECT DISTINCT - from_, - array_agg(DISTINCT template_id) AS ids - FROM usage_by_day, unnest(template_ids) template_id - WHERE template_id IS NOT NULL - GROUP BY from_, template_ids - ) AS template_usage_by_day_unnested - ) AS template_usage_by_day - GROUP BY template_usage_by_day.from_ + SELECT + template_usage_by_day.from_, + array_agg(template_id) AS ids + FROM ( + SELECT DISTINCT + from_, + unnest(template_ids) AS template_id + FROM usage_by_day + WHERE usage_by_day.template_ids IS NOT NULL + ) AS template_usage_by_day + GROUP BY template_usage_by_day.from_ ) SELECT From ea365ac9adb4500db18dc32e5b95d1ee8b244544 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Mon, 24 Jul 2023 14:04:16 +0200 Subject: [PATCH 08/11] fmt --- coderd/database/queries/insights.sql | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/coderd/database/queries/insights.sql b/coderd/database/queries/insights.sql index 20d89e24c70c2..6d896772a23f6 100644 --- a/coderd/database/queries/insights.sql +++ b/coderd/database/queries/insights.sql @@ -93,17 +93,17 @@ WITH d AS ( ) GROUP BY ts.from_, ts.to_, was.user_id ), template_ids AS ( - SELECT - template_usage_by_day.from_, - array_agg(template_id) AS ids - FROM ( - SELECT DISTINCT - from_, - unnest(template_ids) AS template_id - FROM usage_by_day - WHERE usage_by_day.template_ids IS NOT NULL - ) AS template_usage_by_day - GROUP BY template_usage_by_day.from_ + SELECT + template_usage_by_day.from_, + array_agg(template_id) AS ids + FROM ( + SELECT DISTINCT + from_, + unnest(template_ids) AS template_id + FROM usage_by_day + WHERE usage_by_day.template_ids IS NOT NULL + ) AS template_usage_by_day + GROUP BY template_usage_by_day.from_ ) SELECT From fd8fc7e4da49356a1b394a3a55f4882abe6409a2 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Mon, 24 Jul 2023 12:05:40 +0000 Subject: [PATCH 09/11] make gen --- coderd/database/queries.sql.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index 473b537f271f6..5771e4752aa69 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -1399,17 +1399,17 @@ WITH d AS ( ) GROUP BY ts.from_, ts.to_, was.user_id ), template_ids AS ( - SELECT - template_usage_by_day.from_, - array_agg(template_id) AS ids - FROM ( - SELECT DISTINCT - from_, - unnest(template_ids) AS template_id - FROM usage_by_day - WHERE usage_by_day.template_ids IS NOT NULL - ) AS template_usage_by_day - GROUP BY template_usage_by_day.from_ + SELECT + template_usage_by_day.from_, + array_agg(template_id) AS ids + FROM ( + SELECT DISTINCT + from_, + unnest(template_ids) AS template_id + FROM usage_by_day + WHERE usage_by_day.template_ids IS NOT NULL + ) AS template_usage_by_day + GROUP BY template_usage_by_day.from_ ) SELECT From 6458e68160eb229888c3402e743ff7e8d0f862dd Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Mon, 24 Jul 2023 14:19:50 +0200 Subject: [PATCH 10/11] WIP --- coderd/database/queries/insights.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coderd/database/queries/insights.sql b/coderd/database/queries/insights.sql index 6d896772a23f6..e611ab209ff68 100644 --- a/coderd/database/queries/insights.sql +++ b/coderd/database/queries/insights.sql @@ -101,8 +101,8 @@ WITH d AS ( from_, unnest(template_ids) AS template_id FROM usage_by_day - WHERE usage_by_day.template_ids IS NOT NULL ) AS template_usage_by_day + WHERE template_id IS NOT NULL GROUP BY template_usage_by_day.from_ ) From 885ad987ff0a07d7bd48ca496290b7a84987a899 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Mon, 24 Jul 2023 12:21:08 +0000 Subject: [PATCH 11/11] make gen --- coderd/database/queries.sql.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index 5771e4752aa69..120edc4eb949c 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -1407,8 +1407,8 @@ WITH d AS ( from_, unnest(template_ids) AS template_id FROM usage_by_day - WHERE usage_by_day.template_ids IS NOT NULL ) AS template_usage_by_day + WHERE template_id IS NOT NULL GROUP BY template_usage_by_day.from_ )