forked from bimberlabinternal/LabDevKitModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLDKServiceImpl.java
More file actions
534 lines (451 loc) · 15.6 KB
/
LDKServiceImpl.java
File metadata and controls
534 lines (451 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package org.labkey.ldk;
import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.labkey.api.collections.CaseInsensitiveHashMap;
import org.labkey.api.data.AbstractTableInfo;
import org.labkey.api.data.ColumnInfo;
import org.labkey.api.data.Container;
import org.labkey.api.data.ContainerManager;
import org.labkey.api.data.DbSchema;
import org.labkey.api.data.SQLFragment;
import org.labkey.api.data.Selector;
import org.labkey.api.data.SqlExecutor;
import org.labkey.api.data.SqlSelector;
import org.labkey.api.data.Table;
import org.labkey.api.data.TableCustomizer;
import org.labkey.api.data.TableInfo;
import org.labkey.api.files.FileContentService;
import org.labkey.api.ldk.LDKService;
import org.labkey.api.ldk.notification.NotificationSection;
import org.labkey.api.ldk.table.ButtonConfigFactory;
import org.labkey.api.module.Module;
import org.labkey.api.module.ModuleLoader;
import org.labkey.api.module.ModuleProperty;
import org.labkey.api.security.User;
import org.labkey.api.security.UserManager;
import org.labkey.api.security.ValidEmail;
import org.labkey.ldk.query.BuiltInColumnsCustomizer;
import org.labkey.ldk.query.ColumnOrderCustomizer;
import org.labkey.ldk.query.DefaultTableCustomizer;
import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Created with IntelliJ IDEA.
* User: bimber
* Date: 11/4/12
* Time: 3:47 PM
*/
public class LDKServiceImpl extends LDKService
{
private static final Logger _log = LogManager.getLogger(LDKServiceImpl.class);
private final Set<NotificationSection> _summaryNotificationSections = new HashSet<>();
private final List<List<String>> _containerScopedTables = new ArrayList<>();
private Boolean _isNaturalizeInstalled = null;
private final Map<String, Map<String, List<ButtonConfigFactory>>> _queryButtons = new CaseInsensitiveHashMap<>();
private static final String BACKGROUND_USER_PROPNAME = "BackgroundAdminUser";
public LDKServiceImpl()
{
}
@Override
public TableCustomizer getDefaultTableCustomizer()
{
return new DefaultTableCustomizer();
}
@Override
public TableCustomizer getDefaultTableCustomizer(MultiValuedMap<String, String> props)
{
return new DefaultTableCustomizer(props);
}
@Override
public TableCustomizer getBuiltInColumnsCustomizer(boolean disableFacetingForNumericCols)
{
BuiltInColumnsCustomizer ret = new BuiltInColumnsCustomizer();
ret.setDisableFacetingForNumericCols(disableFacetingForNumericCols);
return ret;
}
@Override
public TableCustomizer getColumnsOrderCustomizer()
{
return new ColumnOrderCustomizer();
}
@Override
public Map<String, Object> getContainerSizeJson(Container c, User u, boolean includeAllRootTypes, boolean includeFileCount)
{
FileContentService svc = FileContentService.get();
Map<String, Object> json = c.toJSON(u);
JSONArray fileRoots = new JSONArray();
//primary root
File root = svc.getFileRoot(c);
if (root != null && root.exists())
{
fileRoots.put(getJSONForRoot(root, "root", includeFileCount));
}
//append children
if (includeAllRootTypes)
{
Set<File> paths = new HashSet<>();
for (FileContentService.ContentType type : FileContentService.ContentType.values())
{
File fileRoot = svc.getFileRoot(c, type);
if (fileRoot != null && fileRoot.exists())
{
if (paths.contains(fileRoot))
continue;
paths.add(fileRoot);
fileRoots.put(getJSONForRoot(fileRoot, type.name(), includeFileCount));
}
}
}
json.put("fileRoots", fileRoots);
return json;
}
private JSONObject getJSONForRoot(File fileRoot, String name, boolean includeFileCount)
{
try
{
JSONObject obj = new JSONObject();
obj.put("name", name);
obj.put("rootPath", fileRoot.getPath());
long size = FileUtils.sizeOfDirectory(fileRoot);
obj.put("rootSizeInt", size);
obj.put("rootSize", FileUtils.byteCountToDisplaySize(size));
if (includeFileCount)
obj.put("totalFiles", getFileCount(fileRoot));
return obj;
}
catch (IllegalArgumentException e)
{
//NOTE: this has been hit when there are bad symlinks under a file root
_log.error(e.getMessage(), e);
}
return new JSONObject();
}
private int getFileCount(File file)
{
if (!file.isDirectory())
{
return 1;
}
int count = 0;
File[] files = file.listFiles();
if (files != null)
{
for (File f: files)
{
count += getFileCount(f);
}
}
return count;
}
@Override
public void applyNaturalSort(AbstractTableInfo ti, String colName)
{
DefaultTableCustomizer.applyNaturalSort(ti, colName);
}
@Override
public void appendCalculatedDateColumns(AbstractTableInfo ti, String dateColName, String enddateColName)
{
DefaultTableCustomizer.appendCalculatedDateColumns(ti, dateColName, enddateColName);
}
@Override
public void registerSiteSummaryNotification(NotificationSection ns)
{
_summaryNotificationSections.add(ns);
}
public Set<NotificationSection> getSiteSummaryNotificationSections()
{
return Collections.unmodifiableSet(_summaryNotificationSections);
}
@Override
public boolean isNaturalizeInstalled()
{
if (_isNaturalizeInstalled != null)
{
return _isNaturalizeInstalled;
}
else
{
try
{
// Attempt to use the ldk.naturalize function. If this succeeds, we'll skip the install step.
SqlExecutor executor = new SqlExecutor(LDKSchema.getInstance().getSchema());
executor.setLogLevel(Level.OFF);
executor.execute("SELECT ldk.naturalize('Foo') FROM (SELECT 1 AS G) x");
_isNaturalizeInstalled = true;
}
catch (Exception e)
{
_isNaturalizeInstalled = false;
}
return _isNaturalizeInstalled;
}
}
@Override
public void registerContainerScopedTable(String dbSchemaName, String tableName, String pseudoPk)
{
_containerScopedTables.add(Arrays.asList(dbSchemaName, tableName, pseudoPk));
}
public List<String> validateContainerScopedTables(boolean onlyReportErrors)
{
final List<String> messages = new ArrayList<>();
for (List<String> values : _containerScopedTables)
{
DbSchema schema = DbSchema.get(values.get(0));
if (schema == null)
{
messages.add("Unknown schema: " + values.get(0));
continue;
}
TableInfo ti = schema.getTable(values.get(1));
if (ti == null)
{
messages.add("Unknown table: " + values.get(0) + "." + values.get(1));
continue;
}
final ColumnInfo pseudoPk = ti.getColumn(values.get(2));
if (pseudoPk == null)
{
messages.add("Unable to find column " + values.get(2) + " in table " + values.get(0) + "." + values.get(1));
continue;
}
// group data based on pseudoPK and effective container (ie. workbooks go with parent) and return duplicates
SQLFragment sql = new SQLFragment("SELECT ").append(pseudoPk.getValueSql("t")).append(" as keyField, count(*) as total FROM ").append(ti.getFromSQL("t")).append(
" LEFT JOIN core.containers c ON t.container = c.entityid " +
" GROUP BY ").append(pseudoPk.getValueSql("t")).append(", CASE WHEN c.type = 'workbook' THEN c.parent ELSE c.entityid END " +
" HAVING count(*) > 1");
SqlSelector ss = new SqlSelector(schema.getScope(), sql);
if (ss.exists())
{
messages.add("ERROR: duplicates found in: " + values.get(0) + "." + values.get(1));
ss.forEach(new Selector.ForEachBlock<>()
{
@Override
public void exec(ResultSet rs) throws SQLException
{
messages.add(pseudoPk.getName() + ": " + rs.getString("keyField") + ", total: " + rs.getInt("total"));
}
});
}
else
{
if (!onlyReportErrors)
messages.add("No duplicates: " + values.get(0) + "." + values.get(1));
}
}
return messages;
}
@Override
public void logPerfMetric(Container c, User u, String metricName, String comment, Double value)
{
PerfMetricModel model = new PerfMetricModel();
model.setMetricName(metricName);
model.setStringValue1(comment);
model.setNumericValue1(value);
logPerfMetric(c, u, model);
}
public void logPerfMetric(Container c, User u, PerfMetricModel model)
{
Map<String, Object> result = new HashMap<>();
TableInfo t = LDKSchema.getInstance().getSchema().getTable(LDKSchema.TABLE_METRICS);
if(model.getMetricName() == null)
{
throw new IllegalArgumentException("No metric name provided");
}
Map<String, Object> map = new HashMap<>();
map.put("container", c.getId());
map.put("created", new Date());
map.put("createdby", u.getUserId());
map.put("category", model.getCategory());
map.put("metric_name", model.getMetricName());
map.put("floatvalue1", model.getNumericValue1());
map.put("floatvalue2", model.getNumericValue2());
map.put("floatvalue3", model.getNumericValue3());
map.put("stringvalue1", model.getStringValue1());
map.put("stringvalue2", model.getStringValue2());
map.put("stringvalue3", model.getStringValue3());
map.put("referrerURL", model.getReferrerURL());
map.put("browser", model.getBrowser());
map.put("platform", model.getPlatform());
Table.insert(u, t, map);
}
@Override
public void registerQueryButton(ButtonConfigFactory btn, String schema, String query)
{
Map<String, List<ButtonConfigFactory>> schemaMap = _queryButtons.get(schema);
if (schemaMap == null)
schemaMap = new CaseInsensitiveHashMap<>();
List<ButtonConfigFactory> list = schemaMap.get(query);
if (list == null)
list = new ArrayList<>();
list.add(btn);
schemaMap.put(query, list);
_queryButtons.put(schema, schemaMap);
}
@Override
public List<ButtonConfigFactory> getQueryButtons(TableInfo ti)
{
List<ButtonConfigFactory> buttons = new ArrayList<>();
Map<String, List<ButtonConfigFactory>> factories = _queryButtons.get(ti.getPublicSchemaName());
if (factories == null)
return buttons;
List<ButtonConfigFactory> list = factories.get(ti.getPublicName());
if (list == null)
return buttons;
for (ButtonConfigFactory fact : list)
{
if (fact.isAvailable(ti))
buttons.add(fact);
}
return Collections.unmodifiableList(buttons);
}
@Override
public void customizeButtonBar(AbstractTableInfo ti, List<ButtonConfigFactory> buttons)
{
DefaultTableCustomizer.customizeButtonBar(ti, buttons);
}
public static class PerfMetricModel
{
String _category;
String _metricName;
String _stringValue1;
String _stringValue2;
String _stringValue3;
Double _numericValue1;
Double _numericValue2;
Double _numericValue3;
String _referrerURL;
String _platform;
String _browser;
public PerfMetricModel()
{
}
public String getCategory()
{
return _category;
}
public void setCategory(String category)
{
_category = category;
}
public String getMetricName()
{
return _metricName;
}
public void setMetricName(String metricName)
{
_metricName = metricName;
}
public String getStringValue1()
{
return _stringValue1;
}
public void setStringValue1(String stringValue1)
{
_stringValue1 = stringValue1;
}
public String getStringValue2()
{
return _stringValue2;
}
public void setStringValue2(String stringValue2)
{
_stringValue2 = stringValue2;
}
public String getStringValue3()
{
return _stringValue3;
}
public void setStringValue3(String stringValue3)
{
_stringValue3 = stringValue3;
}
public Double getNumericValue1()
{
return _numericValue1;
}
public void setNumericValue1(Double numericValue1)
{
_numericValue1 = numericValue1;
}
public Double getNumericValue2()
{
return _numericValue2;
}
public void setNumericValue2(Double numericValue2)
{
_numericValue2 = numericValue2;
}
public Double getNumericValue3()
{
return _numericValue3;
}
public void setNumericValue3(Double numericValue3)
{
_numericValue3 = numericValue3;
}
public String getReferrerURL()
{
return _referrerURL;
}
public void setReferrerURL(String referrerURL)
{
_referrerURL = referrerURL;
}
public String getPlatform()
{
return _platform;
}
public void setPlatform(String platform)
{
_platform = platform;
}
public String getBrowser()
{
return _browser;
}
public void setBrowser(String browser)
{
_browser = browser;
}
}
@Override
public User getBackgroundAdminUser()
{
Module m = ModuleLoader.getInstance().getModule(LDKModule.NAME);
ModuleProperty mp = m.getModuleProperties().get(BACKGROUND_USER_PROPNAME);
String username = StringUtils.trimToNull(mp.getValueContainerSpecific(ContainerManager.getRoot()));
if (username != null)
{
User u = UserManager.getUserByDisplayName(username);
if (u == null)
{
try
{
u = UserManager.getUser(new ValidEmail(username));
}
catch (ValidEmail.InvalidEmailException e)
{
//ignore
}
}
return u;
}
return null;
}
}