\q
--
-- PostgreSQL database dump
--

-- Dumped from database version 15.13
-- Dumped by pg_dump version 15.13

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: dataimpulse_wrapper; Type: SCHEMA; Schema: -; Owner: postgres
--

CREATE SCHEMA dataimpulse_wrapper;


ALTER SCHEMA dataimpulse_wrapper OWNER TO postgres;

--
-- Name: sub-user.usage-stat.detail.all(integer, common.period, integer, integer, common.datetime_aggregate); Type: FUNCTION; Schema: dataimpulse_wrapper; Owner: postgres
--

CREATE FUNCTION dataimpulse_wrapper."sub-user.usage-stat.detail.all"(subuser_id integer, period common.period DEFAULT 'week'::common.period, "limit" integer DEFAULT 100, "offset" integer DEFAULT 0, datetime_aggregate common.datetime_aggregate DEFAULT 'minute'::common.datetime_aggregate) RETURNS jsonb
    LANGUAGE plpgsql
    AS $$
DECLARE
    total_count bigint := dataimpulse_wrapper."sub-user.usage-stat.detail.total-count"(
      subuser_id,
      period,
      datetime_aggregate
    );
    usage_chunk JSONB;
    entry RECORD;
    full_log JSONB[] = ARRAY[]::JSONB[];
    offset INTEGER := 0;
BEGIN
  RAISE NOTICE 'total_count = %', total_count;

  LOOP
    usage_chunk := dataimpulse."sub-user.usage-stat.detail"(
      "subuser_id" := subuser_id,
      "period" := period,
      "limit" := 10000,
      "offset" := "offset",
      "datetime_aggregate" := datetime_aggregate
    )->'usage';

    FOR entry IN SELECT value FROM jsonb_array_elements(usage_chunk) AS value LOOP
      full_log := array_append(full_log, entry.value);
    END LOOP;

    "offset" = "offset" + 10000;

    EXIT WHEN jsonb_array_length(usage_chunk) <> 10000 OR "offset" > 10000;
  END LOOP;

  RAISE NOTICE 'full_log = %', to_jsonb(full_log);
  RETURN jsonb_build_object(
    'usage', array_to_json(full_log),
    'total_count', COALESCE(array_length(full_log, 1), 0)
  );

END
$$;


ALTER FUNCTION dataimpulse_wrapper."sub-user.usage-stat.detail.all"(subuser_id integer, period common.period, "limit" integer, "offset" integer, datetime_aggregate common.datetime_aggregate) OWNER TO postgres;

--
-- Name: sub-user.usage-stat.detail.total-count(integer, common.period, common.datetime_aggregate); Type: FUNCTION; Schema: dataimpulse_wrapper; Owner: postgres
--

CREATE FUNCTION dataimpulse_wrapper."sub-user.usage-stat.detail.total-count"(subuser_id integer, period common.period DEFAULT 'week'::common.period, datetime_aggregate common.datetime_aggregate DEFAULT 'minute'::common.datetime_aggregate) RETURNS bigint
    LANGUAGE sql
    AS $$
  SELECT (dataimpulse."sub-user.usage-stat.detail"(subuser_id, period, 1, 0, datetime_aggregate)->'total_count')::bigint;
$$;


ALTER FUNCTION dataimpulse_wrapper."sub-user.usage-stat.detail.total-count"(subuser_id integer, period common.period, datetime_aggregate common.datetime_aggregate) OWNER TO postgres;

--
-- Name: SCHEMA dataimpulse_wrapper; Type: ACL; Schema: -; Owner: postgres
--

GRANT USAGE ON SCHEMA dataimpulse_wrapper TO anon;


--
-- PostgreSQL database dump complete
--

