
-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- CREATE OR REPLACE FUNCTION public.cloud_resource_query_perf_groupings(group_by text[] DEFAULT '{}'::text[], "where" json DEFAULT NULL::json, hasura_session json DEFAULT '{}'::json)
--  RETURNS SETOF cloud_resource_query_perf_groupings_type
--  LANGUAGE sql
--  STABLE
-- AS $function$
-- SELECT
--     (CASE WHEN 'tenant_id' = ANY(group_by) THEN tenant_id ELSE NULL END) AS tenant_id
--     , (CASE WHEN 'account_id' = ANY(group_by) THEN account_id ELSE NULL END) AS account_id
--     , (CASE WHEN 'resource_id' = ANY(group_by) THEN resource_id ELSE NULL END) AS resource_id
--     , (CASE WHEN 'database_name' = ANY(group_by) THEN database_name ELSE NULL END) AS database_name
--     , (CASE WHEN 'db_username' = ANY(group_by) THEN db_username ELSE NULL END) AS db_username
--     , (CASE WHEN 'query_type' = ANY(group_by) THEN query_type ELSE NULL END) AS query_type
-- 	, avg(query_exec_duration_micro) as avg_query_exec_duration_micro
-- 	, max(query_exec_duration_micro) as max_query_exec_duration_micro
-- 	, sum(query_exec_duration_micro) as sum_query_exec_duration_micro
-- 	, avg(bill) as avg_bill
-- 	, max(bill) as max_bill
-- 	, sum(bill) as sum_bill	
--     from cloud_resource_query_perf
--     where
--       -- ("hasura_session" ->> 'x-hasura-user-tenant-id' IS NULL OR ("tenant_id" = ("hasura_session" ->> 'x-hasura-user-tenant-id')::uuid))
--       -- AND
--       ("where" #>> '{account_id,_eq}' IS NULL OR ("account_id" = ("where" #>> '{account_id,_eq}')::uuid))
--       AND
--       ("where" #>> '{resource_id,_eq}' IS NULL OR ("resource_id" = ("where" #>> '{resource_id,_eq}')::uuid))
--       AND
--       ("where" #>> '{database_name,_eq}' IS NULL OR ("database_name" = ("where" #>> '{database_name,_eq}')))
--       AND
--       ("where" #>> '{db_username,_eq}' IS NULL OR ("db_username" = ("where" #>> '{db_username,_eq}')))
--       AND
--       ("where" #>> '{query_type,_eq}' IS NULL OR ("query_type" = ("where" #>> '{query_type,_eq}')))
--       AND
--       ("where" #>> '{query_started_at,_gt}' IS NULL OR ("query_started_at" > ("where" #>> '{query_started_at,_gt}')::timestamp))
--       AND
--       ("where" #>> '{query_started_at,_lt}' IS NULL OR ("query_started_at" > ("where" #>> '{query_started_at,_lt}')::timestamp))
--       AND
--       ("where" #>> '{query_started_at,_le}' IS NULL OR ("query_started_at" <= ("where" #>> '{query_started_at,_le}')::timestamp))
--       AND
--       ("where" #>> '{query_started_at,_ge}' IS NULL OR ("query_started_at" >= ("where" #>> '{query_started_at,_ge}')::timestamp))
--     group by
--     (CASE WHEN 'tenant_id' = ANY(group_by) THEN tenant_id END),
--     (CASE WHEN 'account_id' = ANY(group_by) THEN account_id END),
--     (CASE WHEN 'resource_id' = ANY(group_by) THEN resource_id END),
--     (CASE WHEN 'database_name' = ANY(group_by) THEN database_name END),
--     (CASE WHEN 'db_username' = ANY(group_by) THEN db_username END),
--     (CASE WHEN 'query_type' = ANY(group_by) THEN query_type END)
-- $function$;

-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- CREATE OR REPLACE FUNCTION public.cloud_resource_query_perf_groupings(group_by text[] DEFAULT '{}'::text[], "where" json DEFAULT NULL::json, hasura_session json DEFAULT '{}'::json)
--  RETURNS SETOF cloud_resource_query_perf_groupings_type
--  LANGUAGE sql
--  STABLE
-- AS $function$
-- SELECT
--     (CASE WHEN 'tenant_id' = ANY(group_by) THEN tenant_id ELSE NULL END) AS tenant_id
--     , (CASE WHEN 'account_id' = ANY(group_by) THEN account_id ELSE NULL END) AS account_id
--     , (CASE WHEN 'resource_id' = ANY(group_by) THEN resource_id ELSE NULL END) AS resource_id
--     , (CASE WHEN 'database_name' = ANY(group_by) THEN database_name ELSE NULL END) AS database_name
--     , (CASE WHEN 'db_username' = ANY(group_by) THEN db_username ELSE NULL END) AS db_username
--     , (CASE WHEN 'query_type' = ANY(group_by) THEN query_type ELSE NULL END) AS query_type
-- 	, avg(query_exec_duration_micro) as avg_query_exec_duration_micro
-- 	, max(query_exec_duration_micro) as max_query_exec_duration_micro
-- 	, sum(query_exec_duration_micro) as sum_query_exec_duration_micro
-- 	, avg(bill) as avg_bill
-- 	, max(bill) as max_bill
-- 	, sum(bill) as sum_bill	
--     from cloud_resource_query_perf
--     where
--       ("hasura_session" ->> 'x-hasura-user-tenant-id' IS NULL OR ("tenant_id" = ("hasura_session" ->> 'x-hasura-user-tenant-id')::uuid))
--       AND
--       ("where" #>> '{account_id,_eq}' IS NULL OR ("account_id" = ("where" #>> '{account_id,_eq}')::uuid))
--       AND
--       ("where" #>> '{resource_id,_eq}' IS NULL OR ("resource_id" = ("where" #>> '{resource_id,_eq}')::uuid))
--       AND
--       ("where" #>> '{database_name,_eq}' IS NULL OR ("database_name" = ("where" #>> '{database_name,_eq}')))
--       AND
--       ("where" #>> '{db_username,_eq}' IS NULL OR ("db_username" = ("where" #>> '{db_username,_eq}')))
--       AND
--       ("where" #>> '{query_type,_eq}' IS NULL OR ("query_type" = ("where" #>> '{query_type,_eq}')))
--       AND
--       ("where" #>> '{query_started_at,_gt}' IS NULL OR ("query_started_at" > ("where" #>> '{query_started_at,_gt}')::timestamp))
--       AND
--       ("where" #>> '{query_started_at,_lt}' IS NULL OR ("query_started_at" > ("where" #>> '{query_started_at,_lt}')::timestamp))
--       AND
--       ("where" #>> '{query_started_at,_le}' IS NULL OR ("query_started_at" <= ("where" #>> '{query_started_at,_le}')::timestamp))
--       AND
--       ("where" #>> '{query_started_at,_ge}' IS NULL OR ("query_started_at" >= ("where" #>> '{query_started_at,_ge}')::timestamp))
--     group by
--     (CASE WHEN 'tenant_id' = ANY(group_by) THEN tenant_id END),
--     (CASE WHEN 'account_id' = ANY(group_by) THEN account_id END),
--     (CASE WHEN 'resource_id' = ANY(group_by) THEN resource_id END),
--     (CASE WHEN 'database_name' = ANY(group_by) THEN database_name END),
--     (CASE WHEN 'db_username' = ANY(group_by) THEN db_username END),
--     (CASE WHEN 'query_type' = ANY(group_by) THEN query_type END)
-- $function$;

-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- CREATE OR REPLACE FUNCTION public.cloud_resource_query_perf_groupings(group_by text[] DEFAULT '{}'::text[], "where" json DEFAULT NULL::json, hasura_session json DEFAULT '{}'::json)
--  RETURNS SETOF cloud_resource_query_perf_groupings_type
--  LANGUAGE sql
--  STABLE
-- AS $function$
-- SELECT
--     (CASE WHEN 'tenant_id' = ANY(group_by) THEN tenant_id ELSE NULL END) AS tenant_id
--     , (CASE WHEN 'account_id' = ANY(group_by) THEN account_id ELSE NULL END) AS account_id
--     , (CASE WHEN 'resource_id' = ANY(group_by) THEN resource_id ELSE NULL END) AS resource_id
--     , (CASE WHEN 'database_name' = ANY(group_by) THEN database_name ELSE NULL END) AS database_name
--     , (CASE WHEN 'db_username' = ANY(group_by) THEN db_username ELSE NULL END) AS db_username
--     , (CASE WHEN 'query_type' = ANY(group_by) THEN query_type ELSE NULL END) AS query_type
-- 	, avg(query_exec_duration_micro) as avg_query_exec_duration_micro
-- 	, max(query_exec_duration_micro) as max_query_exec_duration_micro
-- 	, sum(query_exec_duration_micro) as sum_query_exec_duration_micro
-- 	, avg(bill) as avg_bill
-- 	, max(bill) as max_bill
-- 	, sum(bill) as sum_bill	
--     from cloud_resource_query_perf
--     where
--       ("hasura_session" #>> '{x-hasura-user-tenant-id,_eq}' IS NULL OR ("tenant_id" = ("hasura_session" #>> '{x-hasura-user-tenant-id,_eq}')::uuid))
--       AND
--       ("where" #>> '{account_id,_eq}' IS NULL OR ("account_id" = ("where" #>> '{account_id,_eq}')::uuid))
--       AND
--       ("where" #>> '{resource_id,_eq}' IS NULL OR ("resource_id" = ("where" #>> '{resource_id,_eq}')::uuid))
--       AND
--       ("where" #>> '{database_name,_eq}' IS NULL OR ("database_name" = ("where" #>> '{database_name,_eq}')))
--       AND
--       ("where" #>> '{db_username,_eq}' IS NULL OR ("db_username" = ("where" #>> '{db_username,_eq}')))
--       AND
--       ("where" #>> '{query_type,_eq}' IS NULL OR ("query_type" = ("where" #>> '{query_type,_eq}')))
--       AND
--       ("where" #>> '{query_started_at,_gt}' IS NULL OR ("query_started_at" > ("where" #>> '{query_started_at,_gt}')::timestamp))
--       AND
--       ("where" #>> '{query_started_at,_lt}' IS NULL OR ("query_started_at" > ("where" #>> '{query_started_at,_lt}')::timestamp))
--       AND
--       ("where" #>> '{query_started_at,_le}' IS NULL OR ("query_started_at" <= ("where" #>> '{query_started_at,_le}')::timestamp))
--       AND
--       ("where" #>> '{query_started_at,_ge}' IS NULL OR ("query_started_at" >= ("where" #>> '{query_started_at,_ge}')::timestamp))
--     group by
--     (CASE WHEN 'tenant_id' = ANY(group_by) THEN tenant_id END),
--     (CASE WHEN 'account_id' = ANY(group_by) THEN account_id END),
--     (CASE WHEN 'resource_id' = ANY(group_by) THEN resource_id END),
--     (CASE WHEN 'database_name' = ANY(group_by) THEN database_name END),
--     (CASE WHEN 'db_username' = ANY(group_by) THEN db_username END),
--     (CASE WHEN 'query_type' = ANY(group_by) THEN query_type END)
-- $function$;

-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- CREATE OR REPLACE FUNCTION public.cloud_resource_query_perf_groupings(group_by text[] DEFAULT '{}'::text[], "where" json DEFAULT NULL::json, hasura_session json DEFAULT '{}'::json)
--  RETURNS SETOF cloud_resource_query_perf_groupings_type
--  LANGUAGE sql
--  STABLE
-- AS $function$
-- SELECT
--     (CASE WHEN 'tenant_id' = ANY(group_by) THEN tenant_id ELSE NULL END) AS tenant_id
--     , (CASE WHEN 'account_id' = ANY(group_by) THEN account_id ELSE NULL END) AS account_id
--     , (CASE WHEN 'resource_id' = ANY(group_by) THEN resource_id ELSE NULL END) AS resource_id
--     , (CASE WHEN 'database_name' = ANY(group_by) THEN database_name ELSE NULL END) AS database_name
--     , (CASE WHEN 'db_username' = ANY(group_by) THEN db_username ELSE NULL END) AS db_username
--     , (CASE WHEN 'query_type' = ANY(group_by) THEN query_type ELSE NULL END) AS query_type
-- 	, avg(query_exec_duration_micro) as avg_query_exec_duration_micro
-- 	, max(query_exec_duration_micro) as max_query_exec_duration_micro
-- 	, sum(query_exec_duration_micro) as sum_query_exec_duration_micro
-- 	, avg(bill) as avg_bill
-- 	, max(bill) as max_bill
-- 	, sum(bill) as sum_bill	
--     from cloud_resource_query_perf
--     where
--       ("hasura_session" #>> '{x-hasura-tenant-id,_eq}' IS NULL OR ("tenant_id" = ("hasura_session" #>> '{x-hasura-tenant-id,_eq}')::uuid))
--       AND
--       ("where" #>> '{account_id,_eq}' IS NULL OR ("account_id" = ("where" #>> '{account_id,_eq}')::uuid))
--       AND
--       ("where" #>> '{resource_id,_eq}' IS NULL OR ("resource_id" = ("where" #>> '{resource_id,_eq}')::uuid))
--       AND
--       ("where" #>> '{database_name,_eq}' IS NULL OR ("database_name" = ("where" #>> '{database_name,_eq}')))
--       AND
--       ("where" #>> '{db_username,_eq}' IS NULL OR ("db_username" = ("where" #>> '{db_username,_eq}')))
--       AND
--       ("where" #>> '{query_type,_eq}' IS NULL OR ("query_type" = ("where" #>> '{query_type,_eq}')))
--       AND
--       ("where" #>> '{query_started_at,_gt}' IS NULL OR ("query_started_at" > ("where" #>> '{query_started_at,_gt}')::timestamp))
--       AND
--       ("where" #>> '{query_started_at,_lt}' IS NULL OR ("query_started_at" > ("where" #>> '{query_started_at,_lt}')::timestamp))
--       AND
--       ("where" #>> '{query_started_at,_le}' IS NULL OR ("query_started_at" <= ("where" #>> '{query_started_at,_le}')::timestamp))
--       AND
--       ("where" #>> '{query_started_at,_ge}' IS NULL OR ("query_started_at" >= ("where" #>> '{query_started_at,_ge}')::timestamp))
--     group by
--     (CASE WHEN 'tenant_id' = ANY(group_by) THEN tenant_id END),
--     (CASE WHEN 'account_id' = ANY(group_by) THEN account_id END),
--     (CASE WHEN 'resource_id' = ANY(group_by) THEN resource_id END),
--     (CASE WHEN 'database_name' = ANY(group_by) THEN database_name END),
--     (CASE WHEN 'db_username' = ANY(group_by) THEN db_username END),
--     (CASE WHEN 'query_type' = ANY(group_by) THEN query_type END)
-- $function$;

-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- drop function cloud_resource_query_perf_groupings;

-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- CREATE OR REPLACE FUNCTION public.cloud_resource_query_perf_groupings(group_bys text[] DEFAULT '{}'::text[], "customWhere" json DEFAULT NULL::json, hasura_session json DEFAULT '{}'::json)
--  RETURNS SETOF cloud_resource_query_perf_groupings_type
--  LANGUAGE sql
--  STABLE
-- AS $function$
-- SELECT
--     (CASE WHEN 'tenant_id' = ANY(group_bys) THEN tenant_id ELSE NULL END) AS tenant_id
--     , (CASE WHEN 'account_id' = ANY(group_bys) THEN account_id ELSE NULL END) AS account_id
--     , (CASE WHEN 'resource_id' = ANY(group_bys) THEN resource_id ELSE NULL END) AS resource_id
--     , (CASE WHEN 'database_name' = ANY(group_bys) THEN database_name ELSE NULL END) AS database_name
--     , (CASE WHEN 'db_username' = ANY(group_bys) THEN db_username ELSE NULL END) AS db_username
--     , (CASE WHEN 'query_type' = ANY(group_bys) THEN query_type ELSE NULL END) AS query_type
-- 	, avg(query_exec_duration_micro) as avg_query_exec_duration_micro
-- 	, max(query_exec_duration_micro) as max_query_exec_duration_micro
-- 	, sum(query_exec_duration_micro) as sum_query_exec_duration_micro
-- 	, avg(bill) as avg_bill
-- 	, max(bill) as max_bill
-- 	, sum(bill) as sum_bill	
--     from cloud_resource_query_perf
--     where
--       ("hasura_session" #>> '{x-hasura-tenant-id,_eq}' IS NULL OR ("tenant_id" = ("hasura_session" #>> '{x-hasura-tenant-id,_eq}')::uuid))
--       AND
--       ("customWhere" #>> '{account_id,_eq}' IS NULL OR ("account_id" = ("customWhere" #>> '{account_id,_eq}')::uuid))
--       AND
--       ("customWhere" #>> '{resource_id,_eq}' IS NULL OR ("resource_id" = ("customWhere" #>> '{resource_id,_eq}')::uuid))
--       AND
--       ("customWhere" #>> '{database_name,_eq}' IS NULL OR ("database_name" = ("customWhere" #>> '{database_name,_eq}')))
--       AND
--       ("customWhere" #>> '{db_username,_eq}' IS NULL OR ("db_username" = ("customWhere" #>> '{db_username,_eq}')))
--       AND
--       ("customWhere" #>> '{query_type,_eq}' IS NULL OR ("query_type" = ("customWhere" #>> '{query_type,_eq}')))
--       AND
--       ("customWhere" #>> '{query_started_at,_gt}' IS NULL OR ("query_started_at" > ("customWhere" #>> '{query_started_at,_gt}')::timestamp))
--       AND
--       ("customWhere" #>> '{query_started_at,_lt}' IS NULL OR ("query_started_at" > ("customWhere" #>> '{query_started_at,_lt}')::timestamp))
--       AND
--       ("customWhere" #>> '{query_started_at,_le}' IS NULL OR ("query_started_at" <= ("customWhere" #>> '{query_started_at,_le}')::timestamp))
--       AND
--       ("customWhere" #>> '{query_started_at,_ge}' IS NULL OR ("query_started_at" >= ("customWhere" #>> '{query_started_at,_ge}')::timestamp))
--     group by
--     (CASE WHEN 'tenant_id' = ANY(group_bys) THEN tenant_id END),
--     (CASE WHEN 'account_id' = ANY(group_bys) THEN account_id END),
--     (CASE WHEN 'resource_id' = ANY(group_bys) THEN resource_id END),
--     (CASE WHEN 'database_name' = ANY(group_bys) THEN database_name END),
--     (CASE WHEN 'db_username' = ANY(group_bys) THEN db_username END),
--     (CASE WHEN 'query_type' = ANY(group_bys) THEN query_type END)
-- $function$;

-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- CREATE OR REPLACE FUNCTION public.cloud_resource_query_perf_groupings(
--   group_bys                TEXT[] DEFAULT '{}',
--   "customWhere"           json default null,
--   hasura_session 		  json default '{}'
-- )
-- RETURNS SETOF cloud_resource_query_perf_groupings_type
-- LANGUAGE sql STABLE AS $$
-- SELECT
--     (CASE WHEN 'tenant_id' = ANY(group_bys) THEN tenant_id ELSE NULL END) AS tenant_id
--     , (CASE WHEN 'account_id' = ANY(group_bys) THEN account_id ELSE NULL END) AS account_id
--     , (CASE WHEN 'resource_id' = ANY(group_bys) THEN resource_id ELSE NULL END) AS resource_id
--     , (CASE WHEN 'database_name' = ANY(group_bys) THEN database_name ELSE NULL END) AS database_name
--     , (CASE WHEN 'db_username' = ANY(group_bys) THEN db_username ELSE NULL END) AS db_username
--     , (CASE WHEN 'query_type' = ANY(group_bys) THEN query_type ELSE NULL END) AS query_type
-- 	, avg(query_exec_duration_micro) as avg_query_exec_duration_micro
-- 	, max(query_exec_duration_micro) as max_query_exec_duration_micro
-- 	, sum(query_exec_duration_micro) as sum_query_exec_duration_micro
-- 	, avg(bill) as avg_bill
-- 	, max(bill) as max_bill
-- 	, sum(bill) as sum_bill	
--     from cloud_resource_query_perf
--     where
--       tenant_id = (hasura_session ->> 'x-hasura-tenant-id')::uuid
--       AND
--       ("customWhere" #>> '{account_id,_eq}' IS NULL OR ("account_id" = ("customWhere" #>> '{account_id,_eq}')::uuid))
--       AND
--       ("customWhere" #>> '{resource_id,_eq}' IS NULL OR ("resource_id" = ("customWhere" #>> '{resource_id,_eq}')::uuid))
--       AND
--       ("customWhere" #>> '{database_name,_eq}' IS NULL OR ("database_name" = ("customWhere" #>> '{database_name,_eq}')))
--       AND
--       ("customWhere" #>> '{db_username,_eq}' IS NULL OR ("db_username" = ("customWhere" #>> '{db_username,_eq}')))
--       AND
--       ("customWhere" #>> '{query_type,_eq}' IS NULL OR ("query_type" = ("customWhere" #>> '{query_type,_eq}')))
--       AND
--       ("customWhere" #>> '{query_started_at,_gt}' IS NULL OR ("query_started_at" > ("customWhere" #>> '{query_started_at,_gt}')::timestamp))
--       AND
--       ("customWhere" #>> '{query_started_at,_lt}' IS NULL OR ("query_started_at" > ("customWhere" #>> '{query_started_at,_lt}')::timestamp))
--       AND
--       ("customWhere" #>> '{query_started_at,_le}' IS NULL OR ("query_started_at" <= ("customWhere" #>> '{query_started_at,_le}')::timestamp))
--       AND
--       ("customWhere" #>> '{query_started_at,_ge}' IS NULL OR ("query_started_at" >= ("customWhere" #>> '{query_started_at,_ge}')::timestamp))
--     group by
--     (CASE WHEN 'tenant_id' = ANY(group_bys) THEN tenant_id END),
--     (CASE WHEN 'account_id' = ANY(group_bys) THEN account_id END),
--     (CASE WHEN 'resource_id' = ANY(group_bys) THEN resource_id END),
--     (CASE WHEN 'database_name' = ANY(group_bys) THEN database_name END),
--     (CASE WHEN 'db_username' = ANY(group_bys) THEN db_username END),
--     (CASE WHEN 'query_type' = ANY(group_bys) THEN query_type END)
-- $$;

-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- CREATE or REPLACE view cloud_resource_query_perf_groupings_type as
-- select tenant_id
-- 	, account_id
-- 	, resource_id
-- 	, database_name
-- 	, db_username
-- 	, query_type
-- 	, avg(query_exec_duration_micro) as avg_query_exec_duration_micro
-- 	, max(query_exec_duration_micro) as max_query_exec_duration_micro
-- 	, sum(query_exec_duration_micro) as sum_query_exec_duration_micro
-- 	, avg(bill) as avg_bill
-- 	, max(bill) as max_bill
-- 	, sum(bill) as sum_bill	
-- from cloud_resource_query_perf
-- where false
-- group by tenant_id, account_id, resource_id, database_name, db_username, query_type;
