@Override public Collection<Metric> findAll(List<String> metricKeys) { try (DbSession session = dbClient.openSession(false)) { List<MetricDto> dtos = dbClient.metricDao().selectByKeys(session, metricKeys); return from(dtos).filter(IsEnabled.INSTANCE).transform(ToMetric.INSTANCE).toList(); } }
@Override public Collection<Metric> findAll() { try (DbSession session = dbClient.openSession(false)) { List<MetricDto> dtos = dbClient.metricDao().selectEnabled(session); return from(dtos).transform(ToMetric.INSTANCE).toList(); } }
private MetricDto getNonNullMetric(DbSession dbSession, String metricKey) { MetricDto metric = dbClient.metricDao().selectByKey(dbSession, metricKey); if (metric == null) { throw new NotFoundException(format("There is no metric with key=%s", metricKey)); } return metric; }
private List<MetricDto> searchMetrics(DbSession dbSession, SearchHistoryRequest request) { List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, request.getMetrics()); if (request.getMetrics().size() > metrics.size()) { Set<String> requestedMetrics = request.getMetrics().stream().collect(MoreCollectors.toSet()); Set<String> foundMetrics = metrics.stream().map(MetricDto::getKey).collect(MoreCollectors.toSet()); Set<String> unfoundMetrics = Sets.difference(requestedMetrics, foundMetrics).immutableCopy(); throw new IllegalArgumentException(format("Metrics %s are not found", String.join(", ", unfoundMetrics))); } return metrics; }
private Map<String, LiveMeasureDto> loadMeasuresGroupedByMetricKey(ComponentDto component, DbSession dbSession) { List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, METRIC_KEYS); Map<Integer, MetricDto> metricsById = Maps.uniqueIndex(metrics, MetricDto::getId); List<LiveMeasureDto> measures = dbClient.liveMeasureDao() .selectByComponentUuidsAndMetricIds(dbSession, Collections.singletonList(component.uuid()), metricsById.keySet()); return Maps.uniqueIndex(measures, m -> metricsById.get(m.getMetricId()).getKey()); }
@Override public Metric findByKey(String key) { try (DbSession session = dbClient.openSession(false)) { MetricDto dto = dbClient.metricDao().selectByKey(session, key); if (dto != null && dto.isEnabled()) { return ToMetric.INSTANCE.apply(dto); } return null; } }
@Override public Metric findById(int id) { try (DbSession session = dbClient.openSession(false)) { MetricDto dto = dbClient.metricDao().selectById(session, id); if (dto != null && dto.isEnabled()) { return ToMetric.INSTANCE.apply(dto); } return null; } }
private void checkNoOtherMetricWithTargetKey(DbSession dbSession, MetricDto metricInDb, MetricDto template) { String targetKey = template.getKey(); MetricDto metricWithTargetKey = dbClient.metricDao().selectByKey(dbSession, targetKey); checkRequest(!isMetricFoundInDb(metricWithTargetKey) || metricInDb.getId().equals(metricWithTargetKey.getId()), "The key '%s' is already used by an existing metric.", targetKey); }
private void insertNewCustomMetric(String... ids) { for (String id : ids) { dbClient.metricDao().insert(dbSession, newCustomMetric(id)); } dbSession.commit(); }
@Override public void handle(Request request, Response response) throws Exception { try (DbSession dbSession = dbClient.openSession(false)) { List<String> domains = dbClient.metricDao().selectEnabledDomains(dbSession); JsonWriter json = response.newJsonWriter(); json.beginObject(); writeDomains(json, domains); json.endObject(); json.close(); } }
@Test public void delete_by_id() { loggedAsSystemAdministrator(); MetricDto metric = insertCustomMetric("custom-key"); TestResponse response = newRequest().setParam("ids", String.valueOf(metric.getId())).execute(); assertThat(db.getDbClient().metricDao().selectEnabled(db.getSession())).isEmpty(); assertThat(response.getStatus()).isEqualTo(204); }
@Test public void delete_by_keys() { loggedAsSystemAdministrator(); insertCustomEnabledMetrics("key-1", "key-2", "key-3"); newRequest().setParam("keys", "key-1, key-3").execute(); List<MetricDto> disabledMetrics = db.getDbClient().metricDao().selectByKeys(db.getSession(), asList("key-1", "key-3")); assertThat(disabledMetrics).extracting("enabled").containsOnly(false); assertThat(db.getDbClient().metricDao().selectByKey(db.getSession(), "key-2").isEnabled()).isTrue(); }
@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn().checkIsSystemAdministrator(); try (DbSession dbSession = dbClient.openSession(false)) { List<Integer> ids = loadIds(dbSession, request); dbClient.metricDao().disableCustomByIds(dbSession, ids); dbClient.customMeasureDao().deleteByMetricIds(dbSession, ids); dbClient.gateConditionDao().deleteConditionsWithInvalidMetrics(dbSession); dbSession.commit(); } response.noContent(); }
private MetricDto insertMetric(MetricDto metric) { db.getDbClient().metricDao().insert(db.getSession(), metric); db.getSession().commit(); return metric; }
private void updateMetric(DbSession dbSession, MetricDto metricInDb, MetricDto metricTemplate) { metricInDb .setShortName(metricTemplate.getShortName()) .setValueType(metricTemplate.getValueType()) .setDomain(metricTemplate.getDomain()) .setDescription(metricTemplate.getDescription()) .setEnabled(true); dbClient.metricDao().update(dbSession, metricInDb); dbSession.commit(); }
@Test public void findAll_enabled() { db.getDbClient().metricDao().insert(db.getSession(), newMetricDto()); db.getDbClient().metricDao().insert(db.getSession(), newMetricDto()); db.getDbClient().metricDao().insert(db.getSession(), newMetricDto().setEnabled(false)); db.commit(); assertThat(underTest.findAll()).hasSize(2); }
@Test public void findAll_by_keys() { db.getDbClient().metricDao().insert(db.getSession(), newMetricDto().setKey("ncloc")); db.getDbClient().metricDao().insert(db.getSession(), newMetricDto().setKey("foo")); db.getDbClient().metricDao().insert(db.getSession(), newMetricDto().setKey("coverage")); db.commit(); assertThat(underTest.findAll(Arrays.asList("ncloc", "foo"))).extracting(Metric::getKey).containsExactlyInAnyOrder("ncloc", "foo") .doesNotContain("coverage"); }
@Test public void findById() { MetricDto firstMetric = db.getDbClient().metricDao().insert(db.getSession(), newMetricDto()); MetricDto secondMetric = db.getDbClient().metricDao().insert(db.getSession(), newMetricDto()); db.commit(); assertThat(underTest.findById(firstMetric.getId())).extracting(Metric::getKey).containsExactly(firstMetric.getKey()); }
@Test public void list_metrics_active_and_custom_only() throws Exception { insertCustomMetric("metric-key-1"); dbClient.metricDao().insert(dbSession, newMetricDto().setEnabled(true).setUserManaged(false).setKey("metric-key-2")); dbClient.metricDao().insert(dbSession, newMetricDto().setEnabled(false).setUserManaged(true).setKey("metric-key-3")); dbSession.commit(); String response = newRequest().outputAsString(); assertThat(response).contains("metric-key-1") .doesNotContain("metric-key-2") .doesNotContain("metric-key-3"); }
private void insertMetrics() { dbClient.metricDao().insert(dbSession, newMetricDto().setKey(NEW_RELIABILITY_RATING_KEY).setValueType(INT.name()).setHidden(false).setDirection(0)); dbClient.metricDao().insert(dbSession, newMetricDto().setKey(NEW_SECURITY_RATING_KEY).setValueType(INT.name()).setHidden(false).setDirection(0)); dbClient.metricDao().insert(dbSession, newMetricDto().setKey(NEW_SECURITY_REMEDIATION_EFFORT_KEY).setValueType(INT.name()).setHidden(false).setDirection(0)); dbClient.metricDao().insert(dbSession, newMetricDto().setKey(NEW_MAINTAINABILITY_RATING_KEY).setValueType(PERCENT.name()).setHidden(false).setDirection(0)); dbClient.metricDao().insert(dbSession, newMetricDto().setKey(NEW_COVERAGE_KEY).setValueType(PERCENT.name()).setHidden(false).setDirection(0)); dbClient.metricDao().insert(dbSession, newMetricDto().setKey(NEW_DUPLICATED_LINES_DENSITY_KEY).setValueType(PERCENT.name()).setHidden(false).setDirection(0)); dbSession.commit(); }