From b252bea18bbbf44452c05b1e569d530f369dede6 Mon Sep 17 00:00:00 2001 From: Belen Pruvost Date: Wed, 15 Jun 2022 12:28:31 +0200 Subject: SONAR-13915 - ncloc_language_distribution is taken from biggest branch --- .../db/measure/ProjectMeasuresIndexerIterator.java | 147 +++++++++++++++++++-- .../ProjectMeasuresIndexerIteratorTest.java | 15 ++- 2 files changed, 149 insertions(+), 13 deletions(-) (limited to 'server/sonar-db-dao/src') diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectMeasuresIndexerIterator.java b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectMeasuresIndexerIterator.java index 6261483715d..37126bf775b 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectMeasuresIndexerIterator.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectMeasuresIndexerIterator.java @@ -61,8 +61,8 @@ public class ProjectMeasuresIndexerIterator extends CloseableIterator projects; - private ProjectMeasuresIndexerIterator(PreparedStatement measuresStatement, List projects) { + private ProjectMeasuresIndexerIterator(DbSession dbSession, PreparedStatement measuresStatement, List projects) { + this.dbSession = dbSession; this.measuresStatement = measuresStatement; this.projects = projects.iterator(); } @@ -102,13 +125,13 @@ public class ProjectMeasuresIndexerIterator extends CloseableIterator projects = selectProjects(session, projectUuid); PreparedStatement projectsStatement = createMeasuresStatement(session); - return new ProjectMeasuresIndexerIterator(projectsStatement, projects); + return new ProjectMeasuresIndexerIterator(session, projectsStatement, projects); } private static List selectProjects(DbSession session, @Nullable String projectUuid) { List projects = new ArrayList<>(); try (PreparedStatement stmt = createProjectsStatement(session, projectUuid); - ResultSet rs = stmt.executeQuery()) { + ResultSet rs = stmt.executeQuery()) { while (rs.next()) { String uuid = rs.getString(1); String key = rs.getString(2); @@ -146,7 +169,9 @@ public class ProjectMeasuresIndexerIterator extends CloseableIterator "?").collect(Collectors.joining(",")); + String metricNameQuestionMarks = METRIC_KEYS.stream() + .filter(m -> !m.equals(NCLOC_LANGUAGE_DISTRIBUTION_KEY)) + .map(x -> "?").collect(Collectors.joining(",")); String sql = StringUtils.replace(SQL_MEASURES, "{metricNames}", metricNameQuestionMarks); return session.getConnection().prepareStatement(sql); } catch (SQLException e) { @@ -169,14 +194,23 @@ public class ProjectMeasuresIndexerIterator extends CloseableIterator projectBranches = selectProjectBranches(dbSession, projectUuid); + long biggestNcloc = selectProjectBiggestNcloc(dbSession, projectBranches); + String biggestBranch = selectProjectBiggestBranch(dbSession, projectBranches, biggestNcloc); + PreparedStatement prepareNclocByLanguageStatement = prepareNclocByLanguageStatement(dbSession, biggestBranch); + rs = prepareNclocByLanguageStatement.executeQuery(); + + if (rs.next()) { + readMeasure(rs, measures); + } + return measures; } catch (Exception e) { throw new IllegalStateException(String.format("Fail to execute request to select measures of project %s", projectUuid), e); @@ -185,6 +219,101 @@ public class ProjectMeasuresIndexerIterator extends CloseableIterator !m.equals(NCLOC_LANGUAGE_DISTRIBUTION_KEY)) + .forEach(DatabaseUtils.setStrings(measuresStatement, index::getAndIncrement)); + measuresStatement.setBoolean(index.getAndIncrement(), ENABLED); + } + + private static PreparedStatement prepareNclocByLanguageStatement(DbSession session, String projectUuid) { + try { + PreparedStatement stmt = session.getConnection().prepareStatement(SQL_NCLOC_LANGUAGE_DISTRIBUTION); + AtomicInteger index = new AtomicInteger(1); + stmt.setString(index.getAndIncrement(), projectUuid); + stmt.setString(index.getAndIncrement(), NCLOC_LANGUAGE_DISTRIBUTION_KEY); + stmt.setBoolean(index.getAndIncrement(), ENABLED); + return stmt; + } catch (SQLException e) { + throw new IllegalStateException("Fail to execute request to select ncloc_language_distribution measure", e); + } + } + + private static List selectProjectBranches(DbSession session, @Nullable String projectUuid) { + ResultSet rs = null; + List projectBranches = new ArrayList<>(); + try (PreparedStatement stmt = session.getConnection().prepareStatement(SQL_PROJECT_BRANCHES)){ + AtomicInteger index = new AtomicInteger(1); + stmt.setString(index.getAndIncrement(), projectUuid); + + rs = stmt.executeQuery(); + + while (rs.next()) { + String uuid = rs.getString(1); + projectBranches.add(uuid); + } + return projectBranches; + } catch (SQLException e) { + throw new IllegalStateException("Fail to execute request to select all project branches", e); + } finally { + DatabaseUtils.closeQuietly(rs); + } + } + + private static long selectProjectBiggestNcloc(DbSession session, List projectBranches) { + try { + long ncloc = 0; + + AtomicInteger index = new AtomicInteger(1); + PreparedStatement nclocStatement = getPreparedStatement(projectBranches, SQL_BIGGEST_NCLOC_VALUE, session); + projectBranches.forEach(DatabaseUtils.setStrings(nclocStatement, index::getAndIncrement)); + + nclocStatement.setString(index.getAndIncrement(), CoreMetrics.NCLOC_KEY); + nclocStatement.setBoolean(index.getAndIncrement(), ENABLED); + + ResultSet rs = nclocStatement.executeQuery(); + + if (rs.next()) { + ncloc = rs.getLong(1); + } + return ncloc; + } catch (SQLException e) { + throw new IllegalStateException("Fail to execute request to select the project biggest ncloc", e); + } + } + + private static String selectProjectBiggestBranch(DbSession session, List projectBranches, long ncloc) { + try { + String biggestBranchUuid = ""; + + AtomicInteger index = new AtomicInteger(1); + PreparedStatement nclocStatement = getPreparedStatement(projectBranches, SQL_BIGGEST_BRANCH, session); + projectBranches.forEach(DatabaseUtils.setStrings(nclocStatement, index::getAndIncrement)); + + nclocStatement.setString(index.getAndIncrement(), CoreMetrics.NCLOC_KEY); + nclocStatement.setLong(index.getAndIncrement(), ncloc); + nclocStatement.setBoolean(index.getAndIncrement(), ENABLED); + + ResultSet rs = nclocStatement.executeQuery(); + + if (rs.next()) { + biggestBranchUuid = rs.getString(1); + } + return biggestBranchUuid; + } catch (SQLException e) { + throw new IllegalStateException("Fail to execute request to select the project biggest branch", e); + } + } + + private static PreparedStatement getPreparedStatement(List projectBranches, String replacement, DbSession session) throws SQLException { + String projectBranchesQuestionMarks = projectBranches.stream().map(x -> "?").collect(Collectors.joining(",")); + String sql = StringUtils.replace(replacement, "{projectBranches}", projectBranchesQuestionMarks); + return session.getConnection().prepareStatement(sql); + } + private static void readMeasure(ResultSet rs, Measures measures) throws SQLException { String metricKey = rs.getString(FIELD_METRIC_NAME); Optional value = metricKey.startsWith("new_") ? getDouble(rs, FIELD_MEASURE_VARIATION) : getDouble(rs, FIELD_MEASURE_VALUE); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorTest.java index 0f013d975da..15d9976d6c2 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorTest.java @@ -141,15 +141,22 @@ public class ProjectMeasuresIndexerIteratorTest { } @Test - public void return_language_distribution_measure() { + public void return_language_distribution_measure_from_biggest_branch() { ComponentDto project = dbTester.components().insertPrivateProject(); - MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(DATA.name()).setKey("ncloc_language_distribution")); - dbTester.measures().insertLiveMeasure(project, metric, m -> m.setValue(null).setData("=2;java=6;xoo=18")); + MetricDto languagesDistributionMetric = dbTester.measures().insertMetric(m -> m.setValueType(DATA.name()).setKey("ncloc_language_distribution")); + MetricDto nclocMetric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()).setKey("ncloc")); + + dbTester.measures().insertLiveMeasure(project, languagesDistributionMetric, m -> m.setValue(null).setData("=2;java=6;xoo=18")); + dbTester.measures().insertLiveMeasure(project, nclocMetric, m -> m.setValue(26d)); + + ComponentDto branch = dbTester.components().insertProjectBranch(project); + dbTester.measures().insertLiveMeasure(branch, languagesDistributionMetric, m -> m.setValue(null).setData("=4;java=12;xoo=36")); + dbTester.measures().insertLiveMeasure(branch, nclocMetric, m -> m.setValue(52d)); Map docsById = createResultSetAndReturnDocsById(); assertThat(docsById.get(project.uuid()).getMeasures().getNclocByLanguages()) - .containsOnly(entry("", 2), entry("java", 6), entry("xoo", 18)); + .containsOnly(entry("", 4), entry("java", 12), entry("xoo", 36)); } @Test -- cgit v1.2.3