@@ -87,12 +87,13 @@ public class LiveMeasureDao implements Dao { | |||
* If Main Branch = 100 LOCs and the "largest long-lived branch" is 120 LOCs, I'm expecting to consider the value 120. | |||
* If Main Branch = 100 LOCs and the "largest long-lived branch" is 80 LOCs, I'm expecting to consider the value 100. | |||
*/ | |||
public long sumNclocOfBiggestLongLivingBranch(DbSession dbSession) { | |||
return sumNclocOfBiggestLongLivingBranch(dbSession, SumNclocDbQuery.builder().build()); | |||
public long sumNclocOfBiggestLongLivingBranch(DbSession dbSession, String organizationUuid) { | |||
return sumNclocOfBiggestLongLivingBranch(dbSession, SumNclocDbQuery.builder().setOrganizationUuid(organizationUuid).build()); | |||
} | |||
public long sumNclocOfBiggestLongLivingBranch(DbSession dbSession, SumNclocDbQuery dbQuery) { | |||
Long ncloc = mapper(dbSession).sumNclocOfBiggestLongLivingBranch(NCLOC_KEY, KeyType.BRANCH, BranchType.LONG, dbQuery.getProjectUuidToExclude()); | |||
Long ncloc = mapper(dbSession).sumNclocOfBiggestLongLivingBranch( | |||
NCLOC_KEY, KeyType.BRANCH, BranchType.LONG, dbQuery.getOrganizationUuid(), dbQuery.getProjectUuidToExclude()); | |||
return ncloc == null ? 0L : ncloc; | |||
} | |||
@@ -47,6 +47,7 @@ public interface LiveMeasureMapper { | |||
@Param("ncloc") String nclocKey, | |||
@Param("branch") KeyType branchOrPullRequest, | |||
@Param("branchType") BranchType branchType, | |||
@Param("organizationUuid") String organizationUuid, | |||
@Nullable @Param("projectUuidToExclude") String projectUuidToExclude); | |||
void insert( |
@@ -22,12 +22,16 @@ package org.sonar.db.measure; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
import static com.google.common.base.Preconditions.checkNotNull; | |||
public class SumNclocDbQuery { | |||
private final String projectUuidToExclude; | |||
private final String organizationUuid; | |||
public SumNclocDbQuery(Builder builder) { | |||
projectUuidToExclude = builder.projectUuidToExclude; | |||
organizationUuid = builder.organizationUuid; | |||
} | |||
@CheckForNull | |||
@@ -35,12 +39,17 @@ public class SumNclocDbQuery { | |||
return projectUuidToExclude; | |||
} | |||
public String getOrganizationUuid() { | |||
return organizationUuid; | |||
} | |||
public static Builder builder() { | |||
return new Builder(); | |||
} | |||
public static class Builder { | |||
private String projectUuidToExclude; | |||
private String organizationUuid; | |||
private Builder() { | |||
// to enforce use of builder() | |||
@@ -51,7 +60,13 @@ public class SumNclocDbQuery { | |||
return this; | |||
} | |||
public Builder setOrganizationUuid(String organizationUuid) { | |||
this.organizationUuid = organizationUuid; | |||
return this; | |||
} | |||
public SumNclocDbQuery build() { | |||
checkNotNull(organizationUuid); | |||
return new SumNclocDbQuery(this); | |||
} | |||
} |
@@ -36,24 +36,25 @@ | |||
<select id="sumNclocOfBiggestLongLivingBranch" parameterType="map" resultType="long"> | |||
select sum(sumncloc.maxncloc) from ( | |||
select b.project_uuid as projectUuid, max(lm.value) as maxncloc | |||
from live_measures lm | |||
inner join metrics m on m.id = lm.metric_id | |||
inner join projects p on p.uuid = lm.component_uuid | |||
inner join project_branches b on b.uuid = p.uuid | |||
<where> | |||
m.name = #{ncloc, jdbcType=VARCHAR} | |||
and p.enabled = ${_true} | |||
and p.scope = 'PRJ' | |||
and p.qualifier = 'TRK' | |||
and p.copy_component_uuid is null | |||
and b.branch_type = #{branchType, jdbcType=VARCHAR} | |||
and b.key_type = #{branch, jdbcType=VARCHAR} | |||
<if test="projectUuidToExclude != null"> | |||
and b.project_uuid <> #{projectUuidToExclude,jdbcType=VARCHAR} | |||
</if> | |||
</where> | |||
group by b.project_uuid | |||
select b.project_uuid as projectUuid, max(lm.value) as maxncloc | |||
from live_measures lm | |||
inner join metrics m on m.id = lm.metric_id | |||
inner join projects p on p.uuid = lm.component_uuid | |||
inner join project_branches b on b.uuid = p.uuid | |||
<where> | |||
m.name = #{ncloc, jdbcType=VARCHAR} | |||
and p.enabled = ${_true} | |||
and p.scope = 'PRJ' | |||
and p.qualifier = 'TRK' | |||
and p.copy_component_uuid is null | |||
and p.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} | |||
and b.branch_type = #{branchType, jdbcType=VARCHAR} | |||
and b.key_type = #{branch, jdbcType=VARCHAR} | |||
<if test="projectUuidToExclude != null"> | |||
and b.project_uuid <> #{projectUuidToExclude,jdbcType=VARCHAR} | |||
</if> | |||
</where> | |||
group by b.project_uuid | |||
) sumncloc | |||
</select> | |||
@@ -219,7 +219,7 @@ public class LiveMeasureDaoTest { | |||
db.measures().insertLiveMeasure(projectWithLinesButNoLoc, lines, m -> m.setValue(365d)); | |||
db.measures().insertLiveMeasure(projectWithLinesButNoLoc, ncloc, m -> m.setValue(0d)); | |||
long result = underTest.sumNclocOfBiggestLongLivingBranch(db.getSession()); | |||
long result = underTest.sumNclocOfBiggestLongLivingBranch(db.getSession(), organization.getUuid()); | |||
assertThat(result).isEqualTo(10L + 200L); | |||
} | |||
@@ -229,7 +229,7 @@ public class LiveMeasureDaoTest { | |||
db.measures().insertMetric(m -> m.setKey("ncloc").setValueType(INT.toString())); | |||
db.measures().insertMetric(m -> m.setKey("lines").setValueType(INT.toString())); | |||
long result = underTest.sumNclocOfBiggestLongLivingBranch(db.getSession()); | |||
long result = underTest.sumNclocOfBiggestLongLivingBranch(db.getSession(), db.getDefaultOrganization().getUuid()); | |||
assertThat(result).isEqualTo(0L); | |||
} | |||
@@ -252,7 +252,11 @@ public class LiveMeasureDaoTest { | |||
db.measures().insertLiveMeasure(projectToExclude, ncloc, m -> m.setValue(300d)); | |||
db.measures().insertLiveMeasure(projectToExcludeBranch, ncloc, m -> m.setValue(400d)); | |||
long result = underTest.sumNclocOfBiggestLongLivingBranch(db.getSession(), SumNclocDbQuery.builder().setProjectUuidToExclude(projectToExclude.uuid()).build()); | |||
SumNclocDbQuery query = SumNclocDbQuery.builder() | |||
.setOrganizationUuid(organization.getUuid()) | |||
.setProjectUuidToExclude(projectToExclude.uuid()) | |||
.build(); | |||
long result = underTest.sumNclocOfBiggestLongLivingBranch(db.getSession(), query); | |||
assertThat(result).isEqualTo(10L + 200L); | |||
} |
@@ -34,6 +34,7 @@ import org.sonar.db.DbSession; | |||
import org.sonar.server.es.SearchOptions; | |||
import org.sonar.server.measure.index.ProjectMeasuresIndex; | |||
import org.sonar.server.measure.index.ProjectMeasuresStatistics; | |||
import org.sonar.server.organization.DefaultOrganizationProvider; | |||
import org.sonar.server.telemetry.TelemetryData.Database; | |||
import org.sonar.server.user.index.UserIndex; | |||
import org.sonar.server.user.index.UserQuery; | |||
@@ -46,15 +47,17 @@ public class TelemetryDataLoader { | |||
private final UserIndex userIndex; | |||
private final ProjectMeasuresIndex projectMeasuresIndex; | |||
private final PlatformEditionProvider editionProvider; | |||
private final DefaultOrganizationProvider defaultOrganizationProvider; | |||
public TelemetryDataLoader(Server server, DbClient dbClient, PluginRepository pluginRepository, UserIndex userIndex, ProjectMeasuresIndex projectMeasuresIndex, | |||
PlatformEditionProvider editionProvider) { | |||
PlatformEditionProvider editionProvider, DefaultOrganizationProvider defaultOrganizationProvider) { | |||
this.server = server; | |||
this.dbClient = dbClient; | |||
this.pluginRepository = pluginRepository; | |||
this.userIndex = userIndex; | |||
this.projectMeasuresIndex = projectMeasuresIndex; | |||
this.editionProvider = editionProvider; | |||
this.defaultOrganizationProvider = defaultOrganizationProvider; | |||
} | |||
public TelemetryData load() { | |||
@@ -73,7 +76,7 @@ public class TelemetryDataLoader { | |||
try (DbSession dbSession = dbClient.openSession(false)) { | |||
data.setDatabase(loadDatabaseMetadata(dbSession)); | |||
data.setUsingBranches(dbClient.branchDao().hasNonMainBranches(dbSession)); | |||
data.setNcloc(dbClient.liveMeasureDao().sumNclocOfBiggestLongLivingBranch(dbSession)); | |||
data.setNcloc(dbClient.liveMeasureDao().sumNclocOfBiggestLongLivingBranch(dbSession, defaultOrganizationProvider.get().getUuid())); | |||
} | |||
return data.build(); |
@@ -25,6 +25,7 @@ import org.sonar.api.server.ws.Response; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.server.organization.DefaultOrganizationProvider; | |||
import org.sonar.server.user.UserSession; | |||
import org.sonarqube.ws.Navigation; | |||
@@ -34,11 +35,13 @@ public class MarketplaceAction implements NavigationWsAction { | |||
private final UserSession userSession; | |||
private final Server server; | |||
private final DbClient dbClient; | |||
private final DefaultOrganizationProvider defaultOrganizationProvider; | |||
public MarketplaceAction(UserSession userSession, Server server, DbClient dbClient) { | |||
public MarketplaceAction(UserSession userSession, Server server, DbClient dbClient, DefaultOrganizationProvider defaultOrganizationProvider) { | |||
this.userSession = userSession; | |||
this.server = server; | |||
this.dbClient = dbClient; | |||
this.defaultOrganizationProvider = defaultOrganizationProvider; | |||
} | |||
@Override | |||
@@ -67,7 +70,7 @@ public class MarketplaceAction implements NavigationWsAction { | |||
private long computeNcloc() { | |||
try (DbSession dbSession = dbClient.openSession(false)) { | |||
return dbClient.liveMeasureDao().sumNclocOfBiggestLongLivingBranch(dbSession); | |||
return dbClient.liveMeasureDao().sumNclocOfBiggestLongLivingBranch(dbSession, defaultOrganizationProvider.get().getUuid()); | |||
} | |||
} | |||
} |
@@ -45,6 +45,7 @@ import org.sonar.db.metric.MetricDto; | |||
import org.sonar.server.es.EsTester; | |||
import org.sonar.server.measure.index.ProjectMeasuresIndex; | |||
import org.sonar.server.measure.index.ProjectMeasuresIndexer; | |||
import org.sonar.server.organization.DefaultOrganizationProviderImpl; | |||
import org.sonar.server.property.InternalProperties; | |||
import org.sonar.server.property.MapInternalProperties; | |||
import org.sonar.server.tester.UserSessionRule; | |||
@@ -102,7 +103,7 @@ public class TelemetryDaemonTest { | |||
private PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class); | |||
private final TelemetryDataLoader dataLoader = new TelemetryDataLoader(server, db.getDbClient(), pluginRepository, new UserIndex(es.client(), system2), | |||
new ProjectMeasuresIndex(es.client(), null, system2), editionProvider); | |||
new ProjectMeasuresIndex(es.client(), null, system2), editionProvider, new DefaultOrganizationProviderImpl(db.getDbClient())); | |||
private TelemetryDaemon underTest = new TelemetryDaemon(dataLoader, client, settings.asConfig(), internalProperties, system2); | |||
@After |
@@ -32,6 +32,7 @@ import org.sonar.db.component.ComponentDto; | |||
import org.sonar.db.metric.MetricDto; | |||
import org.sonar.server.exceptions.ForbiddenException; | |||
import org.sonar.server.exceptions.UnauthorizedException; | |||
import org.sonar.server.organization.DefaultOrganizationProviderImpl; | |||
import org.sonar.server.tester.UserSessionRule; | |||
import org.sonar.server.ws.TestRequest; | |||
import org.sonar.server.ws.WsActionTester; | |||
@@ -57,7 +58,7 @@ public class MarketplaceActionTest { | |||
private Server server = mock(Server.class); | |||
private DbClient dbClient = db.getDbClient(); | |||
private MarketplaceAction underTest = new MarketplaceAction(userSessionRule, server, dbClient); | |||
private MarketplaceAction underTest = new MarketplaceAction(userSessionRule, server, dbClient, new DefaultOrganizationProviderImpl(dbClient)); | |||
private WsActionTester ws = new WsActionTester(underTest); | |||