public PastMeasuresLoader(DatabaseSession session, MetricFinder metricFinder) { this(session, metricFinder.findAll()); }
private QualityGate fetch(String qualityGateSetting, ServerClient client, MetricFinder metricFinder) { String jsonText = null; try { long qGateId = Long.parseLong(qualityGateSetting); jsonText = client.request(SHOW_URL + "?id=" + qGateId, false); } catch (NumberFormatException configIsNameInsteadOfId) { jsonText = client.request(SHOW_URL + "?name=" + qualityGateSetting, false); } JsonParser parser = new JsonParser(); JsonObject root = parser.parse(jsonText).getAsJsonObject(); QualityGate configuredGate = new QualityGate(root.get("name").getAsString()); if (root.has(ATTRIBUTE_CONDITIONS)) { for (JsonElement condition : root.get(ATTRIBUTE_CONDITIONS).getAsJsonArray()) { JsonObject conditionObject = condition.getAsJsonObject(); configuredGate.add(new ResolvedCondition(conditionObject, metricFinder.findByKey(conditionObject.get("metric").getAsString()))); } } return configuredGate; } }
private Measure copy(ManualMeasure manualMeasure) { Metric metric = metricFinder.findById(manualMeasure.getMetricId()); checkState(metric != null, "Unable to find manual metric with id: " + manualMeasure.getMetricId()); Measure measure = new Measure(metric); measure.setValue(manualMeasure.getValue(), 5); measure.setData(manualMeasure.getTextValue()); measure.setDescription(manualMeasure.getDescription()); return measure; }
@Override public DecoratorContext saveMeasure(Measure measure) { checkReadOnly(SAVE_MEASURE_METHOD); Metric metric = metricFinder.findByKey(measure.getMetricKey()); if (metric == null) { throw new SonarException("Unknown metric: " + measure.getMetricKey()); } measure.setMetric(metric); if (coverageFilter.accept(resource, measure)) { List<Measure> metricMeasures = measuresByMetric.get(measure.getMetricKey()); boolean add = true; if (metricMeasures != null) { int index = metricMeasures.indexOf(measure); if (index > -1) { if (metricMeasures.get(index) == measure) { add = false; } else { throw new SonarException("Can not add twice the same measure on " + resource + ": " + measure); } } } if (add) { measuresByMetric.put(measure.getMetricKey(), measure); } } return this; }
public Map<Integer, Metric> getMetricsById(TimeMachineQuery query) { Collection<Metric> metrics = metricFinder.findAll(query.getMetricKeys()); Map<Integer, Metric> result = Maps.newHashMap(); for (Metric metric : metrics) { result.put(metric.getId(), metric); } return result; }
@Override public void persist() { try (DbSession session = mybatis.openSession(true)) { MeasureMapper mapper = session.getMapper(MeasureMapper.class); for (Entry<Measure> entry : measureCache.entries()) { String effectiveKey = entry.key()[0].toString(); Measure measure = entry.value(); BatchResource batchResource = resourceCache.get(effectiveKey); // Reload Metric to have all Hibernate fields populated measure.setMetric(metricFinder.findByKey(measure.getMetricKey())); if (shouldPersistMeasure(batchResource.resource(), measure)) { MeasureModel measureModel = model(measure, ruleFinder).setSnapshotId(batchResource.snapshotId()); mapper.insert(measureModel); } } session.commit(); } catch (Exception e) { throw new IllegalStateException("Unable to save some measures", e); } }
public TendencyDecorator(TimeMachine timeMachine, MetricFinder metricFinder) { this.timeMachine = timeMachine; this.analyser = new TendencyAnalyser(); this.metrics = Lists.newLinkedList(); for (Metric metric : metricFinder.findAll()) { if (metric.isNumericType()) { metrics.add(metric); } } }
Metric metric = metricFinder.findByKey(measure.getMetric().getKey()); if (metric == null) { throw new IllegalStateException("Unknow metric with key: " + measure.getMetric().getKey());
@Override public void persist() { // Don't use batch insert for duplications since keeping all data in memory can produce OOM try (DbSession session = mybatis.openSession(false)) { MeasureMapper mapper = session.getMapper(MeasureMapper.class); Metric duplicationMetricWithId = metricFinder.findByKey(CoreMetrics.DUPLICATIONS_DATA_KEY); for (String effectiveKey : duplicationCache.componentKeys()) { Iterable<DefaultDuplication> dups = duplicationCache.byComponent(effectiveKey); Measure measure = new Measure(duplicationMetricWithId, DuplicationUtils.toXml(dups)).setPersistenceMode(PersistenceMode.DATABASE); BatchResource batchResource = resourceCache.get(effectiveKey); if (MeasurePersister.shouldPersistMeasure(batchResource.resource(), measure)) { MeasureModel measureModel = MeasurePersister.model(measure, ruleFinder).setSnapshotId(batchResource.snapshotId()); mapper.insert(measureModel); session.commit(); } } } catch (Exception e) { throw new IllegalStateException("Unable to save some measures", e); } }