-
Notifications
You must be signed in to change notification settings - Fork 2
/
new_cdr.sql
346 lines (275 loc) · 10.5 KB
/
new_cdr.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
-- Table: domain.cdr
DROP FUNCTION domain.delete_me_cron_new_cdr_table_delete_me();
DROP TABLE domain.cdr;
CREATE TABLE domain.cdr
(
id bigserial NOT NULL,
sqltime timestamp NOT NULL DEFAULT now(),
yatetime numeric(14,3) NOT NULL,
billid text COLLATE pg_catalog."default" NOT NULL,
chan text COLLATE pg_catalog."default",
address inet,
port integer,
caller text COLLATE pg_catalog."default",
callername text COLLATE pg_catalog."default",
called text COLLATE pg_catalog."default",
status text COLLATE pg_catalog."default",
reason text COLLATE pg_catalog."default",
ended boolean NOT NULL DEFAULT false,
gateway_account_id integer,
gateway_ip_id integer,
customer_ip_id integer,
gateway_price_per_min numeric,
gateway_price_total numeric,
gateway_currency character(3) COLLATE pg_catalog."default",
gateway_price_id bigint,
customer_price_per_min numeric,
customer_price_total numeric,
customer_currency character(3) COLLATE pg_catalog."default",
customer_price_id bigint,
node_id integer NOT NULL,
billed_on timestamp,
gateway_id integer,
customer_id integer,
format text COLLATE pg_catalog."default",
formats text COLLATE pg_catalog."default",
sqltime_end timestamp,
tech_called text COLLATE pg_catalog."default",
rtp_port integer,
trackingid text COLLATE pg_catalog."default",
billtime bigint,
ringtime bigint,
duration bigint,
direction domain.direction NOT NULL,
cause_q931 text COLLATE pg_catalog."default",
preroute_duration bigint,
route_duration bigint,
error text COLLATE pg_catalog."default",
cause_sip text COLLATE pg_catalog."default",
sip_user_agent text COLLATE pg_catalog."default",
sip_x_asterisk_hangupcause text COLLATE pg_catalog."default",
sip_x_asterisk_hangupcausecode text COLLATE pg_catalog."default",
responsetime integer,
routing_processing_time integer,
routing_waiting_time integer,
rtp_forward boolean,
rtp_packets_sent bigint,
rtp_octets_sent bigint,
rtp_packets_received bigint,
rtp_octets_received bigint,
rtp_packet_loss bigint,
rtp_address inet,
dialcode_master_id integer,
routing_tree jsonb,
CONSTRAINT fk_c_dm FOREIGN KEY (dialcode_master_id)
REFERENCES domain.dialcode_master (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT fk_cdr_customer FOREIGN KEY (customer_id)
REFERENCES domain.customer (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT fk_cdr_customer_ip FOREIGN KEY (customer_ip_id)
REFERENCES domain.customer_ip (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT fk_cdr_customer_price FOREIGN KEY (customer_price_id)
REFERENCES domain.customer_price (id) MATCH SIMPLE
ON UPDATE RESTRICT
ON DELETE RESTRICT,
CONSTRAINT fk_cdr_gateway FOREIGN KEY (gateway_id)
REFERENCES domain.gateway (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT fk_cdr_gateway_account FOREIGN KEY (gateway_account_id)
REFERENCES domain.gateway_account (id) MATCH SIMPLE
ON UPDATE RESTRICT
ON DELETE RESTRICT,
CONSTRAINT fk_cdr_gateway_ip FOREIGN KEY (gateway_ip_id)
REFERENCES domain.gateway_ip (id) MATCH SIMPLE
ON UPDATE RESTRICT
ON DELETE RESTRICT,
CONSTRAINT fk_cdr_gateway_price FOREIGN KEY (gateway_price_id)
REFERENCES domain.gateway_price (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT fk_cdr_node FOREIGN KEY (node_id)
REFERENCES domain.node (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
PARTITION BY LIST (DATE_PART('year', sqltime))
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE domain.cdr
OWNER to postgres;
GRANT ALL ON TABLE domain.cdr TO postgres;
CREATE INDEX ix_pk_cdr
ON domain.cdr USING btree
(id);
-- Index: ix_cdr_cp
-- DROP INDEX domain.ix_cdr_cp;
CREATE INDEX ix_cdr_cp
ON domain.cdr USING btree
(customer_price_id)
TABLESPACE pg_default WHERE customer_price_id IS NOT NULL
;
-- Index: ix_cdr_customer
-- DROP INDEX domain.ix_cdr_customer;
CREATE INDEX ix_cdr_customer
ON domain.cdr USING btree
(customer_id)
TABLESPACE pg_default;
-- Index: ix_cdr_dm
-- DROP INDEX domain.ix_cdr_dm;
CREATE INDEX ix_cdr_dm
ON domain.cdr USING btree
(dialcode_master_id)
TABLESPACE pg_default;
-- Index: ix_cdr_ended_sqltime
-- DROP INDEX domain.ix_cdr_ended_sqltime;
CREATE INDEX ix_cdr_ended_sqltime
ON domain.cdr USING btree
(ended, sqltime DESC)
TABLESPACE pg_default;
-- Index: ix_cdr_gateway
-- DROP INDEX domain.ix_cdr_gateway;
CREATE INDEX ix_cdr_gateway
ON domain.cdr USING btree
(gateway_id)
TABLESPACE pg_default;
-- Index: ix_cdr_gp
-- DROP INDEX domain.ix_cdr_gp;
CREATE INDEX ix_cdr_gp
ON domain.cdr USING btree
(gateway_price_id)
TABLESPACE pg_default WHERE gateway_price_id IS NOT NULL
;
-- Index: ix_cdr_node_ended_for_initialize
-- DROP INDEX domain.ix_cdr_node_ended_for_initialize;
CREATE INDEX ix_cdr_node_ended_for_initialize
ON domain.cdr USING btree
(node_id, ended)
TABLESPACE pg_default WHERE ended = false
;
-- Index: ix_cdr_sqltime_customer
-- DROP INDEX domain.ix_cdr_sqltime_customer;
CREATE INDEX ix_cdr_sqltime_customer
ON domain.cdr USING btree
(sqltime, customer_id)
TABLESPACE pg_default;
-- Index: ix_cdr_sqltime_gateway
-- DROP INDEX domain.ix_cdr_sqltime_gateway;
CREATE INDEX ix_cdr_sqltime_gateway
ON domain.cdr USING btree
(sqltime, gateway_id)
TABLESPACE pg_default;
-- Index: ix_cdr_sqltime_gatewayidnotnull
-- DROP INDEX domain.ix_cdr_sqltime_gatewayidnotnull;
CREATE INDEX ix_cdr_sqltime_gatewayidnotnull
ON domain.cdr USING btree
(sqltime)
TABLESPACE pg_default WHERE gateway_id IS NOT NULL
;
-- Index: ix_cdr_sqltime_sqltime_end
-- DROP INDEX domain.ix_cdr_sqltime_sqltime_end;
CREATE INDEX ix_cdr_sqltime_sqltime_end
ON domain.cdr USING btree
(sqltime, sqltime_end)
TABLESPACE pg_default;
-- Index: uq_cdr_billid_chan_node
-- DROP INDEX domain.uq_cdr_billid_chan_node;
CREATE INDEX ix_cdr_billid_chan_node
ON domain.cdr USING btree
(billid COLLATE pg_catalog."default", chan COLLATE pg_catalog."default", node_id)
TABLESPACE pg_default;
-- Trigger: t_statistic_update
-- DROP TRIGGER t_statistic_update ON domain.cdr;
CREATE TRIGGER t_statistic_update
AFTER INSERT OR UPDATE
ON domain.cdr
FOR EACH ROW
EXECUTE PROCEDURE domain.tr_statistic_update();
ALTER TABLE domain.cdr
DISABLE TRIGGER t_statistic_update;
-- DEFAULT CDR TABLE
CREATE TABLE cdr_default PARTITION OF cdr DEFAULT;
-- FUNCTION: domain.cron_new_cdr_table()
-- DROP FUNCTION domain.cron_new_cdr_table();
DROP FUNCTION domain.cron_new_cdr_table();
CREATE OR REPLACE FUNCTION domain.cron_new_cdr_table(
)
RETURNS void
LANGUAGE 'plpgsql'
COST 100
VOLATILE STRICT
AS $BODY$
DECLARE i_week INT=1;
i_new_table_name TEXT;
i_index_def TEXT;
i_constraint_def TEXT;
i_trigger_def TEXT;
i_date_start DATE;
i_date_end DATE;
i_count INT=1;
i_start_date DATE = CURRENT_DATE - INTERVAL '1 week';
i_record RECORD;
BEGIN
CREATE TEMP TABLE cdr_tables (name text, year int, week int, sweek text, is_default boolean ) ON COMMIT DROP;
set search_path = domain;
WHILE EXTRACT(dow FROM i_start_date) <> 1 LOOP
i_start_date := i_start_date + INTERVAL '1 day';
END LOOP;
--ADD CDR TABLE FOR PLUS 7 WEEKS
WHILE i_count <= 7 LOOP
i_date_start := (i_start_date + i_count * INTERVAL '1 week')::DATE;
i_date_end := (i_date_start + INTERVAL '1 week')::DATE;
--raise notice 'start: %', i_date_start;
--raise notice 'end: %', i_date_end;
i_week := EXTRACT(week FROM i_date_start);
-- ALWAYS CHECK FOR DEFAULT TABLES - FOR SIMPLICITY
i_new_table_name := CONCAT('cdr_', EXTRACT(year from i_date_end));
INSERT INTO cdr_tables (name, year, week, sweek, is_default)
VALUES (i_new_table_name, EXTRACT(year from i_date_end), 0, 0::text, true);
IF i_week < 10 THEN
IF i_week = 1 AND EXTRACT(year FROM i_date_start) <> EXTRACT(year FROM i_date_end) THEN
i_new_table_name := CONCAT('cdr_', EXTRACT(year from i_date_end), '_0', i_week::text);
INSERT INTO cdr_tables (name, year, week, sweek, is_default)
VALUES (i_new_table_name, EXTRACT(year from i_date_end), i_week, i_week::text, false);
ELSE
i_new_table_name := CONCAT('cdr_', EXTRACT(year from i_date_start), '_0', i_week::text);
INSERT INTO cdr_tables (name, year, week, sweek, is_default)
VALUES (i_new_table_name, EXTRACT(year from i_date_start), i_week, i_week::text, false);
END IF;
ELSE
i_new_table_name := CONCAT('cdr_', EXTRACT(year from i_date_start), '_', i_week::text);
INSERT INTO cdr_tables (name, year, week, sweek, is_default)
VALUES (i_new_table_name, EXTRACT(year from i_date_start), i_week, i_week::text, false);
END IF;
i_count := i_count + 1;
END LOOP;
--raise notice 'week: %', i_week;
--raise notice 'tbn: %', i_new_table_name;
FOR i_record IN (SELECT * FROM cdr_tables ORDER BY cdr_tables.name ASC) LOOP
-- CHECK IF EXISTS
PERFORM * FROM pg_catalog.pg_class WHERE relname::TEXT = i_record.name;
IF FOUND THEN
CONTINUE;
END IF;
-- CREATE TABLE
IF i_record.is_default THEN
EXECUTE CONCAT('CREATE TABLE ', i_record.name, ' PARTITION OF cdr FOR VALUES IN (', i_record.year, ') PARTITION BY LIST (DATE_PART(''week'', sqltime)) WITH (OIDS=FALSE);');
ELSE
EXECUTE CONCAT('CREATE TABLE ', i_record.name, ' PARTITION OF cdr_', i_record.year, ' FOR VALUES IN (', i_record.week, ') WITH (OIDS=FALSE);');
END IF;
-- ADD PERMISSIONS
EXECUTE CONCAT('GRANT SELECT ON TABLE ', i_record.name, ' TO webuser_group;');
EXECUTE CONCAT('GRANT SELECT, UPDATE, INSERT, TRIGGER ON TABLE ', i_record.name, ' TO cl_controller_group;');
END LOOP;
END;
$BODY$;
ALTER FUNCTION domain.cron_new_cdr_table()
OWNER TO postgres;
select * from cron_new_cdr_table();