1- DELIMITER $$
2-
31/*
4- Delete a range of daily records from the billing database.
5- This procedure is only needed when troubleshooting a range of records.
6-
7- call clear_range('2013-05-22', '2013-05-23');
8- */
9- DROP PROCEDURE IF EXISTS clear_range$$
10- CREATE PROCEDURE clear_range(dstart date , dend date )
11- BEGIN
12- delete from
13- daily_mime_use_details
14- where
15- date_added >= dstart and date_added < dend
16- ;
17-
18- delete from
19- daily_billing
20- where
21- billing_totals_date >= dstart and billing_totals_date < dend
22- ;
23- END$$
24-
25- DELIMITER $$
26-
27-
28- /*
29- Pull a range of records into the daily_mime_use_details table.
30-
31- If a record already exists for a date/mime/owner/collection/source, a new record will not be inserted.
32- This will allow missing records to be inserted over a range of dates.
33-
34- This should not be called directly. This procedure is called by iterate_range.
35- */
36- DROP PROCEDURE IF EXISTS pull_range$$
37- CREATE PROCEDURE pull_range(dstart date , dend date )
38- BEGIN
39- insert into daily_mime_use_details(
40- date_added,
41- mime_type,
42- inv_owner_id,
43- inv_collection_id,
44- source,
45- count_files,
46- full_size,
47- billable_size
48- )
49- select
50- date (f .created ) as date_added,
51- f .mime_type ,
52- o .inv_owner_id ,
53- icio .inv_collection_id ,
54- f .source ,
55- count (f .id ),
56- sum (f .full_size ),
57- sum (f .billable_size )
58- from
59- inv .inv_files f
60- inner join inv .inv_collections_inv_objects icio
61- on icio .inv_object_id = f .inv_object_id
62- inner join inv .inv_objects o
63- on o .id = f .inv_object_id
64- where
65- f .created >= dstart and f .created < dend
66- and not exists (
67- select 1
68- from
69- daily_mime_use_details dmud
70- where
71- dmud .date_added = date (f .created )
72- and
73- dmud .mime_type = f .mime_type
74- and
75- dmud .inv_owner_id = o .inv_owner_id
76- and
77- dmud .inv_collection_id = icio .inv_collection_id
78- and
79- dmud .source = f .source
80- )
81- group by
82- date_added,
83- icio .inv_collection_id ,
84- o .inv_owner_id ,
85- f .mime_type ,
86- f .source
87- ;
88- END$$
89-
90- DELIMITER $$
91-
92- /*
93- Pull a range of records into the daily_billing table.
94-
95- If a record already exists for a date/owner/collection, a new record will not be inserted.
96- This will allow missing records to be inserted over a range of dates.
97-
98- This should not be called directly. This procedure is called by iterate_range.
99- */
100- DROP PROCEDURE IF EXISTS billing_day$$
101- CREATE PROCEDURE billing_day(dstart date )
102- BEGIN
103- insert into daily_billing(
104- billing_totals_date,
105- inv_owner_id,
106- inv_collection_id,
107- billable_size
108- )
109- select
110- dstart,
111- inv_owner_id,
112- inv_collection_id,
113- sum (billable_size)
114- from
115- daily_mime_use_details dmud
116- where
117- date_added <= dstart
118- and not exists (
119- select 1
120- from
121- daily_billing db
122- where
123- db .billing_totals_date = dstart
124- and
125- db .inv_owner_id = dmud .inv_owner_id
126- and
127- db .inv_collection_id = dmud .inv_collection_id
128- )
129- group by
130- dstart,
131- inv_owner_id,
132- inv_collection_id
133- ;
134- END$$
135-
136- DELIMITER $$
137-
138- /*
139- Pull a range of records into the daily_billing and daily_mime_use_details tables.
140-
141- Records will be pulled day by day to keep the transactions efficient.
142-
143- call iterate_range('2013-05-22', '2013-05-23');
144- */
145- DROP PROCEDURE IF EXISTS iterate_range$$
146- CREATE PROCEDURE iterate_range(dstart date , dend date )
147- BEGIN
148- if dend > dstart then
149- set @dcur = dstart;
150- loop_label: LOOP
151- set @dnext = adddate(@dcur, interval 1 day);
152-
153- call pull_range(@dcur, @dnext);
154- call billing_day(@dcur);
155-
156- set @dcur = @dnext;
157- if @dcur >= dend then
158- LEAVE loop_label;
159- end if;
160- END LOOP;
161- end if;
162- END$$
163-
164- DELIMITER $$
165-
166- DROP PROCEDURE IF EXISTS update_billing_range$$
167- CREATE PROCEDURE update_billing_range()
168- BEGIN
169- call iterate_range(
170- (
171- select
172- date_add(max (billing_totals_date), interval 1 day)
173- from
174- daily_billing
175- ),
176- date (now())
177- );
178- END$$
179-
180- DELIMITER ;
181-
182- DELIMITER $$
183-
184- DROP PROCEDURE IF EXISTS update_object_size$$
185- CREATE PROCEDURE update_object_size()
186- BEGIN
187-
188- select
189- ifnull(max (updated), ' 2013-01-01' )
190- into
191- @lastupdated
192- from
193- object_size
194- ;
195-
196- delete from
197- object_size
198- where exists (
199- select
200- 1
201- from
202- inv .inv_objects o
203- where
204- o .id = object_size .inv_object_id
205- and
206- o .modified > @lastupdated
207- );
208-
209- insert into
210- object_size(
211- inv_object_id,
212- file_count,
213- billable_size,
214- max_size,
215- average_size,
216- updated
217- )
218- select
219- inv_object_id,
220- count (* ) as file_count,
221- sum (billable_size) as billable_size,
222- max (billable_size) as max_size,
223- avg (billable_size) as average_size,
224- now()
225- from
226- inv .inv_files f
227- inner join inv .inv_objects o
228- on o .id = f .inv_object_id
229- where
230- f .billable_size = f .full_size
231- and
232- o .modified > @lastupdated
233- group by
234- inv_object_id
235- ;
236- END$$
237-
238- DELIMITER ;
239-
240- DELIMITER $$
241-
242- DROP PROCEDURE IF EXISTS update_node_counts$$
243- CREATE PROCEDURE update_node_counts()
244- BEGIN
245- delete from
246- daily_node_counts
247- where
248- as_of_date = date (now());
249- insert into daily_node_counts (
250- as_of_date,
251- inv_node_id,
252- number ,
253- object_count,
254- object_count_primary,
255- object_count_secondary,
256- file_count,
257- billable_size
258- )
259- select
260- date (now()),
261- n .id ,
262- n .number ,
263- count (inio .inv_object_id ),
264- sum (case when role = ' primary' then 1 else 0 end),
265- sum (case when role = ' secondary' then 1 else 0 end),
266- sum (os .file_count ),
267- sum (os .billable_size )
268- from
269- inv .inv_nodes n
270- inner join inv .inv_nodes_inv_objects inio
271- on n .id = inio .inv_node_id
272- inner join object_size os
273- on inio .inv_object_id = os .inv_object_id
274- group by
275- n .id ,
276- n .number
277- ;
278- END$$
279-
280- DELIMITER ;
2+ See https://github.com/CDLUC3/merritt-docker/blob/main/mrt-services/mysql/init.sql for stored procedure definitions
3+ */
0 commit comments