public MeasureFilterResult execute(Map<String, Object> filterMap, @Nullable Long userId) { StopWatch watch = profiling.start("measures", Level.BASIC); StopWatch sqlWatch = null; MeasureFilterResult result = new MeasureFilterResult(); MeasureFilterContext context = new MeasureFilterContext(); context.setUserId(userId); context.setData( String.format("{%s}", Joiner.on('|').withKeyValueSeparator("=").join(filterMap))); try { MeasureFilter filter = factory.create(filterMap); sqlWatch = profiling.start("sql", Level.FULL); List<MeasureFilterRow> rows = executor.execute(filter, context); result.setRows(rows); } catch (NumberFormatException e) { result.setError(MeasureFilterResult.Error.VALUE_SHOULD_BE_A_NUMBER); LOG.debug("Value selected for the metric should be a number: " + context); } catch (Exception e) { result.setError(MeasureFilterResult.Error.UNKNOWN); LOG.error("Fail to execute measure filter: " + context, e); } finally { if (sqlWatch != null) { sqlWatch.stop(context.getSql()); } watch.stop(log(context, result)); } return result; }
private String log(MeasureFilterContext context, MeasureFilterResult result) { StringBuilder log = new StringBuilder(); log.append(SystemUtils.LINE_SEPARATOR); log.append("request: ").append(context.getData()).append(SystemUtils.LINE_SEPARATOR); log.append(" result: ").append(result.toString()); return log.toString(); }
private void appendFavouritesCondition(StringBuilder sb) { if (filter.isOnFavourites()) { sb.append( " AND props.prop_key='favourite' AND props.resource_id IS NOT NULL AND props.user_id="); sb.append(context.getUserId()); sb.append(" "); } }
private void appendResourceBaseCondition(StringBuilder sb) { SnapshotDto baseSnapshot = context.getBaseSnapshot(); if (baseSnapshot != null) { if (filter.isOnBaseResourceChildren()) { sb.append(" AND s.parent_snapshot_id=").append(baseSnapshot.getId()); } else { Long rootSnapshotId = baseSnapshot.getRootId() != null ? baseSnapshot.getRootId() : baseSnapshot.getId(); sb.append(" AND s.root_snapshot_id=").append(rootSnapshotId); sb.append(" AND s.path LIKE '") .append(StringUtils.defaultString(baseSnapshot.getPath())) .append(baseSnapshot.getId()) .append(".%'"); } } }
private void appendResourceConditions(StringBuilder sb) { sb.append(" s.status='P' AND s.islast=").append(database.getDialect().getTrueSqlValue()); if (context.getBaseSnapshot() == null) { sb.append(" AND p.copy_resource_id IS NULL "); } if (!filter.getResourceQualifiers().isEmpty()) { sb.append(" AND s.qualifier IN "); appendInStatement(filter.getResourceQualifiers(), sb); } if (!filter.getResourceScopes().isEmpty()) { sb.append(" AND s.scope IN "); appendInStatement(filter.getResourceScopes(), sb); } appendDateConditions(sb); appendFavouritesCondition(sb); appendResourceNameCondition(sb); appendResourceKeyCondition(sb); appendResourceBaseCondition(sb); }