root/sql/sprocs/stratcon.rollup_metric_numeric.sql

Revision f681bab3178f70248141454992ab4ba6479ffc8f, 6.9 kB (checked in by Robert Treat <robert@omniti.com>, 4 years ago)

one more dt fix

  • Property mode set to 100644
Line 
1 CREATE OR REPLACE FUNCTION stratcon.rollup_metric_numeric(in_roll text) RETURNS int AS $$
2 DECLARE
3     v_rec           stratcon.metric_numeric_rollup_segment%rowtype;
4     v_segment       stratcon.metric_numeric_rollup%rowtype;
5     v_conf          RECORD;
6     v_sql           TEXT;
7     v_min_whence    TIMESTAMPTZ;
8     v_max_rollup    TIMESTAMPTZ;
9     v_whence        TIMESTAMPTZ;
10     v_taskid        INTEGER;
11     v_locked        BOOLEAN;
12     v_this_roll     TEXT;
13     v_stored_rollup INTEGER;
14     v_stored_rollup_tm TIMESTAMPTZ;
15     v_offset        INTEGER;
16     v_init          BOOLEAN := FALSE;
17     v_i             SMALLINT;
18     v_temprec       RECORD;
19     v_count         INTEGER;
20 BEGIN
21     -- Get rollup config based on given name, and fail if its wrong name.
22     SELECT * FROM metric_numeric_rollup_config WHERE rollup = in_roll INTO v_conf;
23     IF NOT FOUND THEN
24         raise exception 'Given rollup name is invalid! [%]', in_roll;
25     END IF;
26
27     -- Get task id - used for locking - based on given roll name
28     v_this_roll := 'rollup_metric_numeric_'||in_roll;
29     SELECT id FROM tasklock WHERE "name" = v_this_roll INTO v_taskid;
30     IF v_taskid IS NULL THEN
31         INSERT INTO tasklock (id, "name") VALUES (nextval('tasklock_id_seq'), v_this_roll) RETURNING id into v_taskid;
32     END IF;
33
34     -- Try to lock task_id - to make sure only one stratcon.rollup_metric_numeric_generic() runs at a time for this particular in_roll.
35     SELECT pg_try_advisory_lock(43191, v_taskid) INTO v_locked;
36     IF v_locked = false THEN
37         RAISE NOTICE 'rollup for metric numeric (%) already running', in_roll;
38         RETURN 0;
39     END IF;
40
41     LOOP
42         IF v_i > 10 THEN
43             RETURN 1;
44         END IF;
45
46         SELECT MIN(whence) FROM metric_numeric_rollup_queue WHERE "interval" = in_roll INTO v_min_whence;
47         EXIT WHEN NOT FOUND OR v_min_whence IS NULL;
48
49         v_sql := 'SELECT MAX(rollup_time) FROM metric_numeric_rollup_' || in_roll;
50         EXECUTE v_sql INTO v_max_rollup;
51
52 RAISE NOTICE 'v_min_whence was (%), v_max_rollup was (%)',v_min_whence, v_max_rollup; 
53         IF v_min_whence <= v_max_rollup THEN
54             v_sql := 'DELETE FROM metric_numeric_rollup_'||in_roll||' WHERE rollup_time = '||quote_literal(v_min_whence);
55             EXECUTE v_sql;
56         END IF;
57
58         -- now() in following query is just a placeholder to get named field (use_whence) in temprec.
59         FOR v_temprec IN SELECT *, now() as use_whence FROM noit.metric_numeric_rollup_config WHERE dependent_on = in_roll LOOP
60             -- Following formula gives equivalent of date_trunc(..) but working on basically any unit - like "10 minutes"
61             -- The unit has to be given in seconds, AND provided as v_temprec.seconds
62             v_temprec.use_whence := 'epoch'::timestamptz + '1 second'::INTERVAL * v_temprec.seconds * floor(extract( epoch FROM now() ) / v_temprec.seconds);
63
64 RAISE NOTICE '(%,%)',v_temprec.rollup, v_temprec.use_whence;
65             -- Poor mans UPSERT :)
66             INSERT INTO metric_numeric_rollup_queue ("interval", whence)
67                 SELECT v_temprec.rollup, v_temprec.use_whence
68                 WHERE NOT EXISTS (
69                     SELECT * FROM metric_numeric_rollup_queue WHERE ( "interval", whence ) = ( v_temprec.rollup, v_temprec.use_whence )
70                 );
71         END LOOP;
72
73         v_sql := 'SELECT sid, name, $1 as rollup_time, SUM(1) as count_rows, (SUM(avg_value*1)/SUM(1)) as avg_value, (SUM(counter_dev*1)/SUM(1)) as counter_dev
74                   FROM  stratcon.unroll_metric_numeric( $1, $2, $3)
75                   GROUP BY sid, name';
76 RAISE NOTICE 'v_sql was (%), %, %, %',v_sql,v_min_whence, v_min_whence + (v_conf.seconds - 1) * '1 second'::interval, v_conf.dependent_on;
77         FOR v_rec IN EXECUTE v_sql USING v_min_whence,
78                                          v_min_whence + (v_conf.seconds - 1) * '1 second'::interval,
79                                          v_conf.dependent_on LOOP
80             v_stored_rollup := floor( extract('epoch' from v_rec.rollup_time) / v_conf.span ) * v_conf.span;
81             v_stored_rollup_tm := 'epoch'::timestamptz + v_stored_rollup * '1 second'::interval;
82             v_offset        := floor( ( extract('epoch' from v_rec.rollup_time) - v_stored_rollup) / v_conf.seconds );
83
84             --v_offset := ( 12*(extract('hour' from v_info.rollup_time))+floor(extract('minute' from v_info.rollup_time)/5) );
85             --v_stored_rollup := v_info.rollup_time::date;
86             -- RAISE NOTICE 'sid %, name %, rollup_time %, offset %', v_rec.sid, v_rec.name, v_stored_rollup, v_offset;
87
88             v_sql := 'SELECT * FROM metric_numeric_rollup_'||in_roll||' WHERE rollup_time = '||quote_literal(v_stored_rollup_tm);
89             v_sql := v_sql ||' and sid='||v_rec.sid||' and name = '|| quote_literal(v_rec.name);
90
91             EXECUTE v_sql INTO v_segment;
92             GET DIAGNOSTICS v_count = ROW_COUNT;
93             IF v_count = 0 THEN
94                 v_segment := stratcon.init_metric_numeric_rollup( in_roll );
95                 v_init := true;
96                 RAISE NOTICE 'didnt find sid %, name %, rollup_time %, offset %', v_rec.sid, v_rec.name, v_stored_rollup_tm, v_offset;
97             END IF;
98
99             v_segment.sid                   := v_rec.sid;
100             v_segment.name                  := v_rec.name;
101             v_segment.count_rows[v_offset]  := v_rec.count_rows;
102             v_segment.avg_value[v_offset]   := v_rec.avg_value;
103             v_segment.counter_dev[v_offset] := v_rec.counter_dev;
104
105             IF v_init THEN
106                 v_sql := 'INSERT INTO metric_numeric_rollup_'||in_roll||' (sid,name,rollup_time,count_rows,avg_value,counter_dev)
107                     VALUES ('|| v_segment.sid||','||quote_literal(v_segment.name)||','||quote_literal(v_stored_rollup_tm)||','||v_segment.count_rows
108                     ||','||v_segment.avg_value||','||v_segment.counter_dev||')';
109                 EXECUTE v_sql;
110                 v_init := false;
111             ELSE
112                 v_sql := 'UPDATE metric_numeric_rollup_'||in_roll;
113                 v_sql := v_sql || 'SET (count_rows,avg_value,counter_dev) = ('||v_rec.count_rows||','||v_rec.avg_value||','||v_rec.counter_dev||')';
114                 v_sql := v_sql || 'WHERE rollup_time = '||v_stored_rollup_tm||'  AND sid = '||v_info.sid||'  AND name = '||quote_literal(v_info.name);
115                 EXECUTE v_sql;
116             END IF;
117
118         v_i := v_i + 1;
119         END LOOP;
120
121         -- Delete from whence log table
122         DELETE FROM metric_numeric_rollup_queue WHERE whence=v_min_whence AND "interval"=in_roll;
123
124         v_min_whence := NULL;
125         v_max_rollup := NULL;
126
127     END LOOP;
128
129     perform pg_advisory_unlock(43191, v_taskid);
130
131     RETURN 0;
132
133 EXCEPTION
134     WHEN RAISE_EXCEPTION THEN
135        perform pg_advisory_unlock(43191, v_taskid);
136        RAISE EXCEPTION '%', SQLERRM;
137     WHEN OTHERS THEN
138        perform pg_advisory_unlock(43191, v_taskid);
139        RAISE NOTICE '%', SQLERRM;
140 END
141 $$ LANGUAGE plpgsql
142 SECURITY DEFINER
143 ;
Note: See TracBrowser for help on using the browser.