我是声纳中的新手并尝试编写插件。虽然尝试使用可用的标准示例插件,但我可以打印存储在“随机”指标中的值,但“消息”指标的值是在仪表板小部件上不可见。此指标的关键还在于SampleMetrics中的“message_key”和小部件erb文件中的“message”。我将它们改为相同,但仍然不可见。但是,如果我尝试显示两个指标的描述,则两者都是可见的。 这是必需的代码:
在SampleMetrics文件中:
public static final Metric MESSAGE = new Metric("message", "Message",
"This is a metric to store a well known message", Metric.ValueType.STRING, -1, false,
CoreMetrics.DOMAIN_GENERAL);
public static final Metric RANDOM = new Metric("random", "Random",
"Random value", Metric.ValueType.FLOAT, Metric.DIRECTION_BETTER, false,
CoreMetrics.DOMAIN_GENERAL);
*
在SampleSensor文件中:
public class SampleSensor implements Sensor {
public boolean shouldExecuteOnProject(Project project) {
// this sensor is executed on any type of project
return true;
}
public void analyse(Project project, SensorContext sensorContext) {
saveLabelMeasure(sensorContext);
saveNumericMeasure(sensorContext);
}
private void saveNumericMeasure(SensorContext context) {
// Sonar API includes many libraries like commons-lang and google-collections
context.saveMeasure(SampleMetrics.RANDOM, RandomUtils.nextDouble());
}
private void saveLabelMeasure(SensorContext context) {
Measure measure = new Measure(SampleMetrics.MESSAGE, "Hello World!");
context.saveMeasure(measure);
}
}
在sample_dashboard_widget.html.erb文件中:
<div class="dashbox">
<h3><span><%= metric('message').description -%></span></h3>
<h3><span><%= metric('random').description -%></span></h3>
<p>
<span class="big">
<%= format_measure('random') -%> <%= tendency_icon(measure('random')) %>
</span>
</p>
<p><%= format_measure('message') -%></p>
</div>
有人可以建议我在获取小部件上打印的消息指标的度量值时缺少哪一步? 谢谢!
答案 0 :(得分:0)
我用我自己的测试小部件尝试了这个并且完全没有问题:
<p><%= format_measure('my_key') -%></p>
显示指标的字符串值。我在代码示例中找不到任何错误。你确定指标是写的吗? 查看日志并添加一些日志输出。
您还可以使用以下方法显示指标值:
<%= @snapshot.measure('my_key').formatted_value -%>