]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/reports/oils_rpt_tforms.js
3a901ea460a0641cf82960e78459f1fef994ddc7
[Evergreen.git] / Open-ILS / web / reports / oils_rpt_tforms.js
1
2 var OILS_RPT_TRANSFORMS = {
3         Bare : {
4                 label : 'Raw Data'
5         },
6
7         first : {
8                 label : 'First Value'
9         },
10
11         last : {
12                 label : 'Last Value'
13         },
14
15         count : {
16                 aggregate : true,
17                 label :  'Count'
18         },
19
20         count_distinct : {
21                 aggregate : true,
22                 label : 'Count Distinct'
23         },
24
25         min : {
26                 aggregate : true,
27                 label : 'Min'
28         },
29
30         max : {
31                 aggregate : true,
32                 label : 'Max'
33         },
34
35         /* string transforms ------------------------- */
36
37    /* XXX not supported yet
38         substring : {
39                 datatype : OILS_RPT_DTYPE_STRING,
40                 label : 'Substring'
41         },
42    */
43
44         lower : {
45                 datatype : [OILS_RPT_DTYPE_STRING, 'text'],
46                 label : 'Lower case'
47         },
48
49         upper : {
50                 datatype : [OILS_RPT_DTYPE_STRING, 'text'],
51                 label : 'Upper case'
52         },
53
54         first5 : {
55                 datatype : [OILS_RPT_DTYPE_STRING, 'text'],
56                 label : 'First 5 characters (for US ZIP code)'
57         },
58
59         /* timestamp transforms ----------------------- */
60         dow : {
61                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
62                 label : 'Day of Week',
63                 regex : /^[0-6]$/
64         },
65         dom : {
66                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
67                 label : 'Day of Month',
68                 regex : /^[0-9]{1,2}$/
69         },
70
71         doy : {
72                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
73                 label : 'Day of Year',
74                 regex : /^[0-9]{1,3}$/
75         },
76
77         woy : {
78                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
79                 label : 'Week of Year',
80                 regex : /^[0-9]{1,2}$/
81         },
82
83         moy : {
84                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
85                 label : 'Month of Year',
86                 regex : /^\d{1,2}$/
87         },
88
89         qoy : {
90                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
91                 label : 'Quarter of Year',
92                 regex : /^[1234]$/
93         }, 
94
95         hod : {
96                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
97                 label : 'Hour of day',
98                 regex : /^\d{1,2}$/
99         }, 
100
101         date : {
102                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
103                 label : 'Date',
104                 regex : /^\d{4}-\d{2}-\d{2}$/,
105                 hint  : 'YYYY-MM-DD',
106                 cal_format : '%Y-%m-%d',
107                 input_size : 10
108         },
109
110         month_trunc : {
111                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
112                 label : 'Year + Month',
113                 regex : /^\d{4}-\d{2}$/,
114                 hint  : 'YYYY-MM',
115                 cal_format : '%Y-%m',
116                 input_size : 7
117         },
118
119         year_trunc : {
120                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
121                 label : 'Year',
122                 regex : /^\d{4}$/,
123                 hint  : 'YYYY',
124                 cal_format : '%Y',
125                 input_size : 4
126         },
127
128         hour_trunc : {
129                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
130                 label : 'Hour',
131                 regex : /^\d{2}$/,
132                 hint  : 'HH',
133                 cal_format : '%H',
134                 input_size : 2
135         },
136
137         day_name : {
138                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
139                 label : 'Day Name'
140         }, 
141
142         month_name : {
143                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
144                 label : 'Month Name'
145         },
146         age : {
147                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
148                 label : 'Age'
149         },
150
151         months_ago : {
152                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
153                 label : 'Months ago'
154         },
155
156         quarters_ago : {
157                 datatype : OILS_RPT_DTYPE_TIMESTAMP,
158                 label : 'Quarters ago'
159         },
160
161         /* int  / float transforms ----------------------------------- */
162         sum : {
163                 datatype : [ OILS_RPT_DTYPE_INT, OILS_RPT_DTYPE_FLOAT ],
164                 label : 'Sum',
165                 aggregate : true
166         }, 
167
168         average : {
169                 datatype : [ OILS_RPT_DTYPE_INT, OILS_RPT_DTYPE_FLOAT ],
170                 label : 'Average',
171                 aggregate : true
172         },
173
174         round : {
175                 datatype : [ OILS_RPT_DTYPE_INT, OILS_RPT_DTYPE_FLOAT ],
176                 label : 'Round',
177         },
178
179         'int' : {
180                 datatype : OILS_RPT_DTYPE_FLOAT,
181                 label : 'Drop trailing decimals'
182         }
183 }
184
185
186 function oilsRptGetTforms(args) {
187         var dtype = args.datatype;
188         var agg = args.aggregate;
189         var tforms = OILS_RPT_TRANSFORMS;
190         var nonagg = args.non_aggregate;
191
192         var keys = oilsRptObjectKeys(OILS_RPT_TRANSFORMS);
193         var tforms = [];
194
195         _debug('getting tform '+dtype+' : ' + agg + ' : ' + nonagg);
196
197         for( var i = 0; i < keys.length; i++ ) {
198                 var key = keys[i];
199                 var obj = OILS_RPT_TRANSFORMS[key];
200                 if( dtype && !oilsRptTformIsDtype(key,dtype) ) continue;
201                 if( agg && !nonagg && !obj.aggregate ) continue;
202                 if( !agg && nonagg && obj.aggregate ) continue;
203                 tforms.push(key);
204         }
205
206         return tforms;
207 }
208
209
210 function oilsRptTformIsDtype(tform, dtype) {
211         var obj = OILS_RPT_TRANSFORMS[tform];
212         if( typeof obj.datatype == 'string' )
213                 return (obj.datatype == dtype);
214         return !obj.datatype || grep(obj.datatype, function(d) { return (d == dtype) });
215 }
216
217
218
219
220 /* builds a new transform picker */
221 function oilsRptTformPicker(args) {
222         this.node = args.node;
223         this.selector = elem('select');
224         this.tforms = oilsRptGetTforms(args);
225         for( var i = 0; i < this.tforms.length; i++ ) 
226                 this.addOpt(this.tforms[i], this.tforms[i] == args.select );
227         appendClear(this.node, this.selector);
228 }
229
230 oilsRptTformPicker.prototype.addOpt = function(key, select) {
231         var tform = OILS_RPT_TRANSFORMS[key];           
232         var obj = this;
233         var opt = insertSelectorVal(this.selector, -1, tform.label, key);
234         if( select ) opt.selected = true;
235 }
236
237 oilsRptTformPicker.prototype.getSelected = function(key) {
238         return getSelectorVal(this.selector);
239 }
240
241
242