Skip to content

Commit

Permalink
Merge branch 'EMC-311-metrics-report-incorrect-data' into 'develop'
Browse files Browse the repository at this point in the history
Fix metrics report showing incorrect data

Closes EMC-311

See merge request eip/catalogue!744
  • Loading branch information
hkhan38 committed Oct 24, 2024
2 parents 77a567c + e6549ef commit 6e23a8c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
Expand All @@ -15,6 +16,8 @@
import java.time.Instant;
import java.util.*;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;

@Profile("metrics")
@Slf4j
Expand Down Expand Up @@ -145,68 +148,63 @@ private void syncDBHelper(Map<String, Set<String>> tableMap, SimpleJdbcInsert in
}

public List<Map<String,String>> getMetricsReport(Instant startDate, Instant endDate, String orderBy, String ordering, List<String> recordType, String docId, Integer noOfRecords) {
String sql = "select t.DOCUMENT, ifnull(t.DOC_TITLE, '') as \"DOC_TITLE\", ifnull(t.RECORD_TYPE, '') as \"RECORD_TYPE\", sum(t.VIEWS) as \"VIEWS\", sum(t.DOWNLOADS) as \"DOWNLOADS\" " +
"from (select DOCUMENT, DOC_TITLE, RECORD_TYPE, AMOUNT as \"DOWNLOADS\", 0 as \"VIEWS\" from downloads where %s " +
"union select DOCUMENT, DOC_TITLE, RECORD_TYPE, 0 as \"DOWNLOADS\", AMOUNT as \"VIEWS\" from views where %s) t " +
"group by DOCUMENT,DOC_TITLE,RECORD_TYPE";
String sql = """
SELECT t.document, coalesce(t.doc_title, '') AS doc_title, coalesce(t.record_type, '') AS record_type, sum(t.views) AS views, sum(t.downloads) AS downloads
FROM (
SELECT document, doc_title, record_type, amount AS downloads, 0 AS views FROM downloads WHERE %s
UNION ALL
SELECT document, doc_title, record_type, 0 AS downloads, amount AS views FROM views WHERE %s
) t
GROUP BY document, doc_title, record_type
""";

ArrayList<String> whereVal = new ArrayList<>();
StringBuilder where = new StringBuilder(" 1=1");
StringBuilder where = new StringBuilder("1=1");
if (startDate != null) {
String start = Long.toString(startDate.getEpochSecond());
whereVal.add(start);
where.append(" and START_TIMESTAMP>=?");
where.append(" AND start_timestamp >= ?");
}
if (endDate != null) {
String end = Long.toString(endDate.getEpochSecond());
whereVal.add(end);
where.append(" and END_TIMESTAMP<=?");
where.append(" AND end_timestamp <= ?");
}
if (docId != null && !docId.isBlank()) {
whereVal.add(String.format("%%%s%%", docId));
where.append(" and DOCUMENT like ?");
whereVal.add("%" + docId + "%");
where.append(" AND document LIKE ?");
}
if (recordType != null && !recordType.isEmpty()) {
where.append(" and RECORD_TYPE in (");
for (String type: recordType) {
where.append(" AND record_type IN (");
for (String type : recordType) {
whereVal.add(type);
where.append("?,");
}
where.deleteCharAt(where.length() - 1).append(")");
where.setCharAt(where.length() - 1, ')');
}

StringBuilder sqlBuilder = new StringBuilder(sql.formatted(where, where));
if (orderBy != null && !orderBy.isBlank()) {
switch (orderBy) {
case "views":
sqlBuilder.append(" order by VIEWS");
break;
case "downloads":
sqlBuilder.append(" order by DOWNLOADS");
break;
default:
sqlBuilder.append(" order by DOCUMENT");
break;
}
if (ordering != null && !ordering.isBlank()) {
if (ordering.equals("descending")) {
sqlBuilder.append(" desc");
}
sqlBuilder.append(" ORDER BY ");
sqlBuilder.append(switch (orderBy) {
case "views", "downloads" -> orderBy;
default -> "document";
});
if (ordering != null && ordering.equals("descending")) {
sqlBuilder.append(" DESC");
}
}
if (noOfRecords != null && noOfRecords >= 0) {
sqlBuilder.append(" limit ").append(noOfRecords);
} else {
sqlBuilder.append(" limit 100");
}

sqlBuilder.append(" LIMIT ");
sqlBuilder.append(noOfRecords != null && noOfRecords >= 0 ? noOfRecords : 100);

log.info("Metrics report sql: {}", sqlBuilder);

return jdbcTemplate.query(
sqlBuilder.toString(), preparedStatement -> {
int index = 1;
int valSize = whereVal.size();
for (String val: whereVal) {
for (String val : whereVal) {
preparedStatement.setString(index, val);
preparedStatement.setString(valSize + index, val);
index++;
Expand All @@ -215,4 +213,17 @@ public List<Map<String,String>> getMetricsReport(Instant startDate, Instant endD
new ReportMapper()
);
}

static class ReportMapper implements RowMapper<Map<String, String>> {
@Override
public Map<String, String> mapRow(ResultSet rs, int map) throws SQLException {
LinkedHashMap<String, String> row = new LinkedHashMap<>();
row.put("document", rs.getString("document"));
row.put("docTitle", rs.getString("doc_title"));
row.put("recordType", rs.getString("record_type"));
row.put("views", String.valueOf(rs.getInt("views")));
row.put("downloads", String.valueOf(rs.getInt("downloads")));
return row;
}
}
}

This file was deleted.

4 changes: 2 additions & 2 deletions templates/html/metrics/metrics.ftlh
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@
<label><input type="checkbox" name="recordType" value="dataset">dataset</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="recordType" value="datasetNonGeographic">dataset (non geographic)</label>
<label><input type="checkbox" name="recordType" value="nonGeographicDataset">dataset (non geographic)</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="recordType" value="modelCodeApplication">model code (application)</label>
<label><input type="checkbox" name="recordType" value="application">model code (application)</label>
</div>
</div>
</div>
Expand Down

0 comments on commit 6e23a8c

Please sign in to comment.