reporter (nearing) mini-release state
[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='days' content='Day(s)';
70                         INCLUDE option value='weeks' content='Week(s)';
71                         INCLUDE option value='months' content='Month(s)';
72                 END;
73
74                 FOR p = CGI.param();
75                         NEXT IF p == 'action';
76                         NEXT IF p == 'runtime';
77                         INCLUDE hidden name=p value=CGI.param(p);
78                 END;
79                 '<br>';
80                 INCLUDE submit name='action' value='Save';
81         END;
82 END;
83
84 BLOCK list_stage3s;
85 END;
86
87
88 BLOCK save_stage3;
89         stage2 = CGI.param('stage2');
90         runtime = CGI.param('runtime') OR 'now';
91         pub = CGI.param('publicreport') OR 'f';
92         owner = user.id();
93
94         p_obj = {};
95         FOR p = CGI.param();
96
97                 logme([p,CGI.param(p)]);
98
99                 parts = p.split(':');
100                 p1 = parts.0;
101                 p2 = parts.1;
102                 p3 = parts.2;
103                 p4 = parts.3;
104                 p5 = parts.4;
105                 p6 = parts.5;
106                 
107                 IF p6;
108
109                         IF p_obj.$p1.$p2.$p3.$p4.$p5.$p6.defined;
110                                 IF p_obj.$p1.$p2.$p3.$p4.$p5.$p6.size > 1;
111                                         p_obj.$p1.$p2.$p3.$p4.$p5.$p6.push(CGI.param(p));
112                                 ELSE;
113                                         tmp = [ p_obj.$p1.$p2.$p3.$p4.$p5.$p6, CGI.param(p) ];
114                                         p_obj.$p1.$p2.$p3.$p4.$p5.$p6 = tmp;
115                                 END;
116                         ELSE;
117                                 p_obj.$p1.$p2.$p3.$p4.$p5.$p6 = CGI.param(p);
118                         END;
119
120                 ELSIF p5;
121
122                         IF p_obj.$p1.$p2.$p3.$p4.$p5.defined;
123                                 IF p_obj.$p1.$p2.$p3.$p4.$p5.size > 1;
124                                         p_obj.$p1.$p2.$p3.$p4.$p5.push(CGI.param(p));
125                                 ELSE;
126                                         tmp = [ p_obj.$p1.$p2.$p3.$p4.$p5, CGI.param(p) ];
127                                         p_obj.$p1.$p2.$p3.$p4.$p5 = tmp;
128                                 END;
129                         ELSE;
130                                 p_obj.$p1.$p2.$p3.$p4.$p5 = CGI.param(p);
131                         END;
132
133                 ELSIF p4;
134
135                         IF p_obj.$p1.$p2.$p3.$p4.defined;
136                                 IF p_obj.$p1.$p2.$p3.$p4.size > 1;
137                                         p_obj.$p1.$p2.$p3.$p4.push(CGI.param(p));
138                                 ELSE;
139                                         tmp = [ p_obj.$p1.$p2.$p3.$p4, CGI.param(p) ];
140                                         p_obj.$p1.$p2.$p3.$p4 = tmp;
141                                 END;
142                         ELSE;
143                                 p_obj.$p1.$p2.$p3.$p4 = CGI.param(p);
144                         END;
145
146                 ELSIF p3;
147
148                         IF p_obj.$p1.$p2.$p3.defined;
149                                 IF p_obj.$p1.$p2.$p3.size > 1;
150                                         p_obj.$p1.$p2.$p3.push(CGI.param(p));
151                                 ELSE;
152                                         tmp = [ p_obj.$p1.$p2.$p3, CGI.param(p) ];
153                                         p_obj.$p1.$p2.$p3 = tmp;
154                                 END;
155                         ELSE;
156                                 p_obj.$p1.$p2.$p3 = CGI.param(p);
157                         END;
158
159                 ELSIF p2;
160
161                         IF p_obj.$p1.$p2.defined;
162                                 IF p_obj.$p1.$p2.size > 1;
163                                         p_obj.$p1.$p2.push(CGI.param(p));
164                                 ELSE;
165                                         tmp = [ p_obj.$p1.$p2, CGI.param(p) ];
166                                         p_obj.$p1.$p2 = tmp;
167                                 END;
168                         ELSE;
169                                 p_obj.$p1.$p2 = CGI.param(p);
170                         END;
171
172                 ELSE;
173
174                         p_obj.$p = CGI.param(p);
175
176                 END;
177         END;
178
179         params = utils.perl2JSON( p_obj );
180
181         recurrence = '0 sec';
182         IF CGI.param('recurrence-count') > 0;
183                 recurrence = CGI.param('recurrence-count') _ ' ' _ CGI.param('recurrence-interval');
184         END;
185
186         logme([stage2, params, owner, pub]);
187
188         q = DBI.prepare( stage3_insert );
189         CALL q.execute(stage2, params, owner, pub, runtime, recurrence);
190
191         FOR new_s3 = DBI.query("SELECT * FROM reporter.stage3_id_seq;");
192                 rid = new_s3.last_value;
193                 rpt = reports.$rid;
194                 tid = rpt.stage2;
195                 tmpl = templates.$tid;
196                 INCLUDE view_stage3;
197         END;
198 END;
199
200 BLOCK delete_stage3;
201         q = DBI.prepare( 'DELETE FROM reporter.stage3 WHERE id = ?' );
202         CALL q.execute(rpt.id);
203 END;
204
205 BLOCK delete_output;
206         q = DBI.prepare( 'DELETE FROM reporter.output WHERE id = ?' );
207         CALL q.execute(out.id);
208         INCLUDE view_stage3;
209 END;
210
211 BLOCK rerun;
212         q = DBI.prepare( 'UPDATE reporter.stage3 SET rerun = TRUE WHERE id = ?' );
213         CALL q.execute(rpt.id);
214         INCLUDE view_stage3;
215 END;
216
217 BLOCK view_stage3;
218         logme(rpt.params);
219         params = utils.JSON2perl( rpt.params );
220         s2_params = utils.JSON2perl( tmpl.params );
221
222
223         WRAPPER html/center;
224                 WRAPPER html/table style="border-collapse:collapse";
225                         WRAPPER html/row;
226                                 INCLUDE html/cell content="<b>Report Name</b>" style="border:solid gray 1px;";
227                                 INCLUDE html/cell content=params.reportname style="border:solid gray 1px;";
228                         END;
229                         WRAPPER html/row;
230                                 INCLUDE html/cell content="<b>Based on Template</b>" style="border:solid gray 1px;";
231                                 INCLUDE html/cell content=s2_params.templatename style="border:solid gray 1px;";
232                         END;
233                         WRAPPER html/row;
234                                 INCLUDE html/cell content="<b>Creation Time</b>" style="border:solid gray 1px;";
235                                 INCLUDE html/cell content=rpt.create_date style="border:solid gray 1px;";
236                         END;
237                         WRAPPER html/row;
238                                 INCLUDE html/cell content="<b>Scheduled Run Time</b>" style="border:solid gray 1px;";
239                                 INCLUDE html/cell content=rpt.runtime style="border:solid gray 1px;";
240                         END;
241                         WRAPPER html/row;
242                                 INCLUDE html/cell content="<b>Recurrence Interval</b>" style="border:solid gray 1px;";
243                                 INCLUDE html/cell content=rpt.recurrence style="border:solid gray 1px;";
244                         END;
245                         WRAPPER html/row;
246                                 INCLUDE html/cell content="<b>Actions</b>" style="border:solid gray 1px;";
247                                 WRAPPER html/cell style="border:solid gray 1px;";
248                                         INCLUDE anchor content='Refresh' href=CGI.url('-path', 1) _ '?id=' _ rpt.id;
249                                         '&nbsp;&nbsp;&nbsp;';
250                                         INCLUDE anchor content='Run again' href=CGI.url('-path', 1) _ '?action=rerun&id=' _ rpt.id;
251                                         '&nbsp;&nbsp;&nbsp;';
252                                         INCLUDE anchor content='Delete' href=CGI.url('-path', 1) _ '?action=delete_stage3&id=' _ rpt.id;
253                                         '&nbsp;&nbsp;&nbsp;';
254                                 END;
255                         END;
256                 END;
257         END;
258
259         '<br><br>';
260
261         type_map = { 'csv' => 'csv', 'excel' => 'xls', 'html' => 'html' };
262
263         WRAPPER html/center;
264                 '<b>Report Runs:</b>';
265                 run_q = DBI.prepare("SELECT * FROM reporter.output WHERE stage3 = ? ORDER BY queue_time DESC");
266                 FOR run = run_q.execute(rpt.id);
267                         WRAPPER html/table style="border-collapse:collapse";
268                                 WRAPPER html/row;
269                                         INCLUDE html/cell content="<b>Current State</b>" style="border:solid gray 1px;" width="20%";
270                                         WRAPPER html/cell style="border:solid gray 1px;";
271                                                 run.state;
272                                                 IF run.state == 'complete';
273                                                         '<br>';
274                                                         FOR f = params.output_format.list;
275                                                                 h = tmpl.stage1 _ '/' _ tmpl.id _
276                                                                         '/' _ rpt.id _ '/' _ run.id _
277                                                                         '/report-data.' _ type_map.$f;
278                                                                 link(content=f, href=h);
279                                                                 IF !loop.last;
280                                                                         '  |  ';
281                                                                 END;
282                                                         END;
283                                                 END;
284                                         END;
285                                 END;
286                                 WRAPPER html/row;
287                                         INCLUDE html/cell content="<b>Queue Time</b>" style="border:solid gray 1px;";
288                                         INCLUDE html/cell content=run.queue_time style="border:solid gray 1px;";
289                                 END;
290                                 IF run.run_time;
291                                         WRAPPER html/row;
292                                                 INCLUDE html/cell content="<b>Run Time</b>" style="border:solid gray 1px;";
293                                                 INCLUDE html/cell content=run.run_time style="border:solid gray 1px;";
294                                         END;
295                                 END;
296                                 IF run.complete_time;
297                                         WRAPPER html/row;
298                                                 INCLUDE html/cell content="<b>Complete Time</b>" style="border:solid gray 1px;";
299                                                 INCLUDE html/cell content=run.complete_time style="border:solid gray 1px;";
300                                         END;
301                                 END;
302                                 IF run.query;
303                                         WRAPPER html/row;
304                                                 INCLUDE html/cell content="<b>Query</b>" style="border:solid gray 1px;";
305                                                 INCLUDE html/cell content=run.query style="border:solid gray 1px;";
306                                         END;
307                                 END;
308                                 IF run.error;
309                                         WRAPPER html/row;
310                                                 INCLUDE html/cell content="<b>Error</b>" style="border:solid gray 1px;";
311                                                 INCLUDE html/cell content=run.error style="border:solid gray 1px;";
312                                         END;
313                                 END;
314                                 IF run.error_time;
315                                         WRAPPER html/row;
316                                                 INCLUDE html/cell content="<b>Error Time</b>" style="border:solid gray 1px;";
317                                                 INCLUDE html/cell content=run.error_time style="border:solid gray 1px;";
318                                         END;
319                                 END;
320                                 IF run.complete_time || run.error_time;
321                                         WRAPPER html/row;
322                                                 INCLUDE html/cell content="<b>Actions</b>" style="border:solid gray 1px;";
323                                                 WRAPPER html/cell style="border:solid gray 1px;";
324                                                         INCLUDE anchor content='Delete' href=CGI.url('-path', 1) _ '?action=delete_output&output=' _ run.id;
325                                                         '&nbsp;&nbsp;&nbsp;';
326                                                 END;
327                                         END;
328                                 END;
329                         END;
330                         '<br><br>';
331                 END;
332         END;
333 END;
334
335 %]