From d104ca20751117924dc7e74a9cf13445926b0acc Mon Sep 17 00:00:00 2001 From: Godin Date: Mon, 13 Dec 2010 21:55:49 +0000 Subject: SONAR-833: New Web Service to get history of measures (time machine) --- .../sonar/wsclient/services/TimeMachineData.java | 38 +++++++++++++--------- .../sonar/wsclient/services/TimeMachineQuery.java | 11 +++++++ .../unmarshallers/TimeMachineUnmarshaller.java | 34 ++++++++----------- 3 files changed, 47 insertions(+), 36 deletions(-) (limited to 'sonar-ws-client/src/main/java/org/sonar/wsclient') diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineData.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineData.java index 4c68d2c3ca1..e0d669d7c2e 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineData.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineData.java @@ -1,36 +1,42 @@ package org.sonar.wsclient.services; +import java.util.ArrayList; import java.util.Date; -import java.util.HashMap; import java.util.List; -import java.util.Map; public class TimeMachineData extends Model { + private Date date; /** * We use strings here in order to support measures with string value. */ - private Map> data = new HashMap>(); + private List values = new ArrayList(); - public Map> getData() { - return data; + public Date getDate() { + return date; } - public TimeMachineData setData(Map> data) { - this.data = data; + public TimeMachineData setDate(Date date) { + this.date = date; return this; } - public Double getValueAsDouble(Date date, int index) { - if (data.containsKey(date)) { - String valueStr = data.get(date).get(index); - try { - return valueStr == null ? null : Double.valueOf(valueStr); - } catch (NumberFormatException e) { - return null; - } + public List getValues() { + return values; + } + + public TimeMachineData setValues(List values) { + this.values = values; + return this; + } + + public Double getValueAsDouble(int index) { + String valueStr = values.get(index); + try { + return valueStr == null ? null : Double.valueOf(valueStr); + } catch (NumberFormatException e) { + return null; } - return null; } } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineQuery.java index 6d4fee7fa96..04f2019d490 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineQuery.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/TimeMachineQuery.java @@ -14,6 +14,9 @@ public class TimeMachineQuery extends Query { private Date from; private Date to; + private String model; + private String[] characteristicKeys; + public TimeMachineQuery(String resourceKeyOrId) { this.resourceKeyOrId = resourceKeyOrId; } @@ -45,6 +48,12 @@ public class TimeMachineQuery extends Query { return this; } + public TimeMachineQuery setCharacteristicKeys(String model, String... keys) { + this.model = model; + this.characteristicKeys = keys; + return this; + } + @Override public String getUrl() { StringBuilder url = new StringBuilder(BASE_URL); @@ -53,6 +62,8 @@ public class TimeMachineQuery extends Query { appendUrlParameter(url, "metrics", metrics); appendUrlParameter(url, "fromDateTime", from, true); appendUrlParameter(url, "toDateTime", to, true); + appendUrlParameter(url, "model", model); + appendUrlParameter(url, "characteristics", characteristicKeys); return url.toString(); } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshaller.java index dab000f069c..1289e63a5ae 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/TimeMachineUnmarshaller.java @@ -2,31 +2,25 @@ package org.sonar.wsclient.unmarshallers; import org.json.simple.JSONArray; import org.json.simple.JSONObject; -import org.json.simple.JSONValue; import org.sonar.wsclient.services.TimeMachineData; -import java.util.*; +import java.util.ArrayList; +import java.util.List; -public class TimeMachineUnmarshaller implements Unmarshaller { +public class TimeMachineUnmarshaller extends AbstractUnmarshaller { - public TimeMachineData toModel(String json) { - JSONObject map = (JSONObject) JSONValue.parse(json); - Map> data = new HashMap>(); - for (Object key : map.keySet()) { - JSONArray array = (JSONArray) map.get(key); - List measures = new ArrayList(); - for (int i = 0; i < array.size(); i++) { - Object elem = array.get(i); - String value = elem == null ? null : elem.toString(); - measures.add(value); - } - data.put(JsonUtils.parseDateTime((String) key), measures); + protected TimeMachineData parse(JSONObject json) { + String dateTimeStr = (String) json.keySet().iterator().next(); + JSONArray array = (JSONArray) json.get(dateTimeStr); + List measures = new ArrayList(); + for (int i = 0; i < array.size(); i++) { + Object elem = array.get(i); + String value = elem == null ? null : elem.toString(); + measures.add(value); } - return new TimeMachineData().setData(data); - } - - public List toModels(String json) { - throw new UnsupportedOperationException(); + return new TimeMachineData() + .setDate(JsonUtils.parseDateTime(dateTimeStr)) + .setValues(measures); } } -- cgit v1.2.3