reporter cleanup -- ready for mini-release (I think)
[Evergreen.git] / Open-ILS / src / reporter / templates / stage3.ttk
1 [%
2
3 PROCESS inputs;
4 PROCESS class_manip;
5 PROCESS widget_manip;
6 PROCESS logic_header.ttk;
7 INCLUDE logout.ttk;
8
9 templates = DBI.tie('reporter.stage2', 'id')
10 reports = DBI.tie('reporter.stage3', 'id')
11 output = DBI.tie('reporter.output', 'id')
12 rpt = {};
13 tmpl = {};
14
15
16 IF CGI.param('output');
17         oid = CGI.param('output');
18         out = output.$oid;
19
20         rid = out.stage3;
21         rpt = reports.$rid;
22
23         tid = rpt.stage2;
24         tmpl = templates.$tid;
25 END;
26
27 IF CGI.param('id');
28         rid = CGI.param('id');
29         rpt = reports.$rid;
30         tid = rpt.stage2;
31         tmpl = templates.$tid;
32 END;
33
34
35 WRAPPER html/html;
36    INCLUDE header.ttk + navbar.ttk title="Reports";
37    WRAPPER html/body;
38       IF CGI.param('action') == 'Schedule';
39          PROCESS schedule_stage3;
40       ELSIF CGI.param('action') == 'Save';
41          PROCESS save_stage3;
42       ELSIF CGI.param('action') == 'delete_stage3';
43          PROCESS delete_stage3;
44       ELSIF CGI.param('action') == 'delete_output';
45          PROCESS delete_output;
46       ELSIF CGI.param('action') == 'rerun';
47          PROCESS rerun;
48       ELSIF CGI.param('action') == 'Run Now';
49          PROCESS save_stage3;
50                 ELSIF CGI.param('id');
51                         PROCESS view_stage3;
52                 ELSE;
53                         PROCESS list_stage3s;
54       END;
55    END;
56    INCLUDE footer.ttk;
57 END;
58
59
60 BLOCK schedule_stage3;
61         WRAPPER form method='POST';
62                 'Run Date:';
63                 INCLUDE text name='runtime' value='yyyy-mm-dd';
64                 '<br>';
65
66                 'Optional Recurrence:';
67                 INCLUDE text name='recurrence-count' value='0';
68                 WRAPPER select name='recurrence-interval';
69                         INCLUDE option value='minutes' content='Minutes(s)'; # for testing
70                         INCLUDE option value='hours' content='Hours(s)'; # for testing
71                         INCLUDE option value='days' content='Day(s)';
72                         INCLUDE option value='weeks' content='Week(s)';
73                         INCLUDE option value='months' content='Month(s)';
74                 END;
75
76                 FOR p = CGI.param();
77                         NEXT IF p == 'action';
78                         NEXT IF p == 'runtime';
79                         FOR pval = CGI.param(p);
80                                 INCLUDE hidden name=p value=pval;
81                         END;
82                 END;
83                 '<br>';
84                 INCLUDE submit name='action' value='Save';
85         END;
86 END;
87
88 BLOCK list_stage3s;
89 END;
90
91
92 BLOCK save_stage3;
93         stage2 = CGI.param('stage2');
94         runtime = CGI.param('runtime') OR 'now';
95         pub = CGI.param('publicreport') OR 'f';
96         owner = user.id();
97
98         p_obj = {};
99         FOR p = CGI.param();
100
101                 logme([p,CGI.param(p)]);
102
103                 parts = p.split(':');
104                 p1 = parts.0;
105                 p2 = parts.1;
106                 p3 = parts.2;
107                 p4 = parts.3;
108                 p5 = parts.4;
109                 p6 = parts.5;
110                 
111                 IF p6;
112
113                         IF p_obj.$p1.$p2.$p3.$p4.$p5.$p6.defined;
114                                 IF p_obj.$p1.$p2.$p3.$p4.$p5.$p6.size > 1;
115                                         p_obj.$p1.$p2.$p3.$p4.$p5.$p6.push(CGI.param(p));
116                                 ELSE;
117                                         tmp = [ p_obj.$p1.$p2.$p3.$p4.$p5.$p6, CGI.param(p) ];
118                                         p_obj.$p1.$p2.$p3.$p4.$p5.$p6 = tmp;
119                                 END;
120                         ELSE;
121                                 p_obj.$p1.$p2.$p3.$p4.$p5.$p6 = CGI.param(p);
122                         END;
123
124                 ELSIF p5;
125
126                         IF p_obj.$p1.$p2.$p3.$p4.$p5.defined;
127                                 IF p_obj.$p1.$p2.$p3.$p4.$p5.size > 1;
128                                         p_obj.$p1.$p2.$p3.$p4.$p5.push(CGI.param(p));
129                                 ELSE;
130                                         tmp = [ p_obj.$p1.$p2.$p3.$p4.$p5, CGI.param(p) ];
131                                         p_obj.$p1.$p2.$p3.$p4.$p5 = tmp;
132                                 END;
133                         ELSE;
134                                 p_obj.$p1.$p2.$p3.$p4.$p5 = CGI.param(p);
135                         END;
136
137                 ELSIF p4;
138
139                         IF p_obj.$p1.$p2.$p3.$p4.defined;
140                                 IF p_obj.$p1.$p2.$p3.$p4.size > 1;
141                                         p_obj.$p1.$p2.$p3.$p4.push(CGI.param(p));
142                                 ELSE;
143                                         tmp = [ p_obj.$p1.$p2.$p3.$p4, CGI.param(p) ];
144                                         p_obj.$p1.$p2.$p3.$p4 = tmp;
145                                 END;
146                         ELSE;
147                                 p_obj.$p1.$p2.$p3.$p4 = CGI.param(p);
148                         END;
149
150                 ELSIF p3;
151
152                         IF p_obj.$p1.$p2.$p3.defined;
153                                 IF p_obj.$p1.$p2.$p3.size > 1;
154                                         p_obj.$p1.$p2.$p3.push(CGI.param(p));
155                                 ELSE;
156                                         tmp = [ p_obj.$p1.$p2.$p3, CGI.param(p) ];
157                                         p_obj.$p1.$p2.$p3 = tmp;
158                                 END;
159                         ELSE;
160                                 p_obj.$p1.$p2.$p3 = CGI.param(p);
161                         END;
162
163                 ELSIF p2;
164
165                         IF p_obj.$p1.$p2.defined;
166                                 IF p_obj.$p1.$p2.size > 1;
167                                         p_obj.$p1.$p2.push(CGI.param(p));
168                                 ELSE;
169                                         tmp = [ p_obj.$p1.$p2, CGI.param(p) ];
170                                         p_obj.$p1.$p2 = tmp;
171                                 END;
172                         ELSE;
173                                 p_obj.$p1.$p2 = CGI.param(p);
174                         END;
175
176                 ELSE;
177
178                         p_obj.$p = CGI.param(p);
179
180                 END;
181         END;
182
183         params = utils.perl2JSON( p_obj );
184
185         recurrence = '0 sec';
186         IF CGI.param('recurrence-count') > 0;
187                 recurrence = CGI.param('recurrence-count') _ ' ' _ CGI.param('recurrence-interval');
188         END;
189
190         logme([stage2, params, owner, pub]);
191
192         q = DBI.prepare( stage3_insert );
193         CALL q.execute(stage2, params, owner, pub, runtime, recurrence);
194
195         FOR new_s3 = DBI.query("SELECT * FROM reporter.stage3_id_seq;");
196                 rid = new_s3.last_value;
197                 rpt = reports.$rid;
198                 tid = rpt.stage2;
199                 tmpl = templates.$tid;
200                 INCLUDE view_stage3;
201         END;
202 END;
203
204 BLOCK delete_stage3;
205         q = DBI.prepare( 'DELETE FROM reporter.stage3 WHERE id = ?' );
206         CALL q.execute(rpt.id);
207 END;
208
209 BLOCK delete_output;
210         q = DBI.prepare( 'DELETE FROM reporter.output WHERE id = ?' );
211         CALL q.execute(out.id);
212         INCLUDE view_stage3;
213 END;
214
215 BLOCK rerun;
216         q = DBI.prepare( 'UPDATE reporter.stage3 SET rerun = TRUE WHERE id = ?' );
217         CALL q.execute(rpt.id);
218         INCLUDE view_stage3;
219 END;
220
221 BLOCK view_stage3;
222         logme(rpt.params);
223         params = utils.JSON2perl( rpt.params );
224         s2_params = utils.JSON2perl( tmpl.params );
225
226
227         WRAPPER html/center;
228                 WRAPPER html/table style="border-collapse:collapse";
229                         WRAPPER html/row;
230                                 INCLUDE html/cell content="<b>Report Name</b>" style="border:solid gray 1px;";
231                                 INCLUDE html/cell content=params.reportname style="border:solid gray 1px;";
232                         END;
233                         WRAPPER html/row;
234                                 INCLUDE html/cell content="<b>Based on Template</b>" style="border:solid gray 1px;";
235                                 INCLUDE html/cell content=s2_params.templatename style="border:solid gray 1px;";
236                         END;
237                         WRAPPER html/row;
238                                 INCLUDE html/cell content="<b>Creation Time</b>" style="border:solid gray 1px;";
239                                 INCLUDE html/cell content=rpt.create_date style="border:solid gray 1px;";
240                         END;
241                         WRAPPER html/row;
242                                 INCLUDE html/cell content="<b>Scheduled Run Time</b>" style="border:solid gray 1px;";
243                                 INCLUDE html/cell content=rpt.runtime style="border:solid gray 1px;";
244                         END;
245                         WRAPPER html/row;
246                                 INCLUDE html/cell content="<b>Recurrence Interval</b>" style="border:solid gray 1px;";
247                                 INCLUDE html/cell content=rpt.recurrence style="border:solid gray 1px;";
248                         END;
249                         WRAPPER html/row;
250                                 INCLUDE html/cell content="<b>Actions</b>" style="border:solid gray 1px;";
251                                 WRAPPER html/cell style="border:solid gray 1px;";
252                                         INCLUDE anchor content='Refresh' href=CGI.url('-path', 1) _ '?id=' _ rpt.id;
253                                         '&nbsp;&nbsp;&nbsp;';
254                                         IF user.id == rpt.owner;
255                                                 INCLUDE anchor content='Run again' href=CGI.url('-path', 1) _ '?action=rerun&id=' _ rpt.id;
256                                                 '&nbsp;&nbsp;&nbsp;';
257                                                 INCLUDE anchor content='Delete' href=CGI.url('-path', 1) _ '?action=delete_stage3&id=' _ rpt.id;
258                                                 '&nbsp;&nbsp;&nbsp;';
259                                         END;
260                                 END;
261                         END;
262                 END;
263         END;
264
265         '<br><br>';
266
267         type_map = { 'csv' => 'csv', 'excel' => 'xls', 'html' => 'html' };
268
269         WRAPPER html/center;
270                 '<b>Report Runs:</b>';
271                 run_q = DBI.prepare("SELECT * FROM reporter.output WHERE stage3 = ? ORDER BY queue_time DESC");
272                 FOR run = run_q.execute(rpt.id);
273                         WRAPPER html/table style="border-collapse:collapse";
274                                 WRAPPER html/row;
275                                         INCLUDE html/cell content="<b>Current State</b>" style="border:solid gray 1px;" width="20%";
276                                         WRAPPER html/cell style="border:solid gray 1px;";
277                                                 run.state;
278                                                 IF run.state == 'complete';
279                                                         '<br>';
280                                                         FOR f = params.output_format.list;
281                                                                 h = tmpl.stage1 _ '/' _ tmpl.id _
282                                                                         '/' _ rpt.id _ '/' _ run.id _
283                                                                         '/report-data.' _ type_map.$f;
284                                                                 link(content=f, href=h);
285                                                                 IF !loop.last;
286                                                                         '  |  ';
287                                                                 END;
288                                                         END;
289                                                 END;
290                                         END;
291                                 END;
292                                 WRAPPER html/row;
293                                         INCLUDE html/cell content="<b>Queue Time</b>" style="border:solid gray 1px;";
294                                         INCLUDE html/cell content=run.queue_time style="border:solid gray 1px;";
295                                 END;
296                                 IF run.run_time;
297                                         WRAPPER html/row;
298                                                 INCLUDE html/cell content="<b>Run Time</b>" style="border:solid gray 1px;";
299                                                 INCLUDE html/cell content=run.run_time style="border:solid gray 1px;";
300                                         END;
301                                 END;
302                                 IF run.complete_time;
303                                         WRAPPER html/row;
304                                                 INCLUDE html/cell content="<b>Complete Time</b>" style="border:solid gray 1px;";
305                                                 INCLUDE html/cell content=run.complete_time style="border:solid gray 1px;";
306                                         END;
307                                 END;
308                                 IF run.query;
309                                         WRAPPER html/row;
310                                                 INCLUDE html/cell content="<b>Query</b>" style="border:solid gray 1px;";
311                                                 INCLUDE html/cell content=run.query style="border:solid gray 1px;";
312                                         END;
313                                 END;
314                                 IF run.error;
315                                         WRAPPER html/row;
316                                                 INCLUDE html/cell content="<b>Error</b>" style="border:solid gray 1px;";
317                                                 INCLUDE html/cell content=run.error style="border:solid gray 1px;";
318                                         END;
319                                 END;
320                                 IF run.error_time;
321                                         WRAPPER html/row;
322                                                 INCLUDE html/cell content="<b>Error Time</b>" style="border:solid gray 1px;";
323                                                 INCLUDE html/cell content=run.error_time style="border:solid gray 1px;";
324                                         END;
325                                 END;
326                                 IF run.complete_time || run.error_time;
327                                         WRAPPER html/row;
328                                                 INCLUDE html/cell content="<b>Actions</b>" style="border:solid gray 1px;";
329                                                 WRAPPER html/cell style="border:solid gray 1px;";
330                                                         IF user.id == rpt.owner;
331                                                                 INCLUDE anchor content='Delete' href=CGI.url('-path', 1) _ '?action=delete_output&output=' _ run.id;
332                                                                 '&nbsp;&nbsp;&nbsp;';
333                                                         END;
334                                                 END;
335                                         END;
336                                 END;
337                         END;
338                         '<br><br>';
339                 END;
340         END;
341 END;
342
343 %]