diff --git a/.gitignore b/.gitignore index 3e759b7..cbde30a 100644 --- a/.gitignore +++ b/.gitignore @@ -328,3 +328,15 @@ ASALocalRun/ # MFractors (Xamarin productivity tool) working folder .mfractor/ + + +#eclipse +.project +.classpath +.settings/ + +#IntelliJ Extesions +*.iml + +#visual code +.vscode/ diff --git a/other-samples/java-sample/.gitignore b/other-samples/java-sample/.gitignore index 6143e53..1a4c78e 100644 --- a/other-samples/java-sample/.gitignore +++ b/other-samples/java-sample/.gitignore @@ -20,3 +20,6 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + + +.vscode \ No newline at end of file diff --git a/other-samples/java-sample/anomalydetction.iml b/other-samples/java-sample/anomalydetction.iml deleted file mode 100644 index 74f3f13..0000000 --- a/other-samples/java-sample/anomalydetction.iml +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/other-samples/java-sample/pom.xml b/other-samples/java-sample/pom.xml index 62dcaa1..a0efe43 100644 --- a/other-samples/java-sample/pom.xml +++ b/other-samples/java-sample/pom.xml @@ -1,7 +1,5 @@ - - + + 4.0.0 com.microsoft.cognitiveservice @@ -19,6 +17,23 @@ json 20180130 + + + org.apache.commons + commons-lang3 + 3.9 + + + com.google.code.gson + gson + 2.8.6 + + + + commons-io + commons-io + 2.6 + \ No newline at end of file diff --git a/other-samples/java-sample/src/main/java/com/microsoft/cognitiveservice/anomalydetection/AnomalyDetectorRequest.java b/other-samples/java-sample/src/main/java/com/microsoft/cognitiveservice/anomalydetection/AnomalyDetectorRequest.java new file mode 100644 index 0000000..743308c --- /dev/null +++ b/other-samples/java-sample/src/main/java/com/microsoft/cognitiveservice/anomalydetection/AnomalyDetectorRequest.java @@ -0,0 +1,26 @@ +package com.microsoft.cognitiveservice.anomalydetection; + +import java.util.Collection; + +public class AnomalyDetectorRequest{ + public AnomalyDetectorRequest(Collection points,String granularity){ + this.series = points; + this.granularity = granularity; + } + private String granularity; + + private Collection series; + + public void setGranularity(String granularity){ + this.granularity = granularity; + } + public String getGranularity(){ + return this.granularity; + } + public void setSeries(Collection series){ + this.series = series; + } + public Collection getSeries(){ + return this.series; + } +} \ No newline at end of file diff --git a/other-samples/java-sample/src/main/java/com/microsoft/cognitiveservice/anomalydetection/Main.java b/other-samples/java-sample/src/main/java/com/microsoft/cognitiveservice/anomalydetection/Main.java index 8e7a5eb..cc36583 100644 --- a/other-samples/java-sample/src/main/java/com/microsoft/cognitiveservice/anomalydetection/Main.java +++ b/other-samples/java-sample/src/main/java/com/microsoft/cognitiveservice/anomalydetection/Main.java @@ -18,13 +18,15 @@ public class Main { // ********************************************** // Replace the subscriptionKey string value with your valid subscription key. + private static final String subscriptionKey = ""; + // Choose which anomaly detection way you want to use and change the uriBase's second part private static final String rootUrl = "https://westus2.api.cognitive.microsoft.com/anomalydetector/v1.0"; private static final String lastDetect = "/timeseries/last/detect"; private static final String entireDetect = "/timeseries/entire/detect"; - private static final String uriBase = rootUrl + lastDetect; + private static final String uriBase = rootUrl + entireDetect; public static void main(String[] args) throws FileNotFoundException { String resourceName = "/request-data.json"; diff --git a/other-samples/java-sample/src/main/java/com/microsoft/cognitiveservice/anomalydetection/Point.java b/other-samples/java-sample/src/main/java/com/microsoft/cognitiveservice/anomalydetection/Point.java new file mode 100644 index 0000000..8b234f8 --- /dev/null +++ b/other-samples/java-sample/src/main/java/com/microsoft/cognitiveservice/anomalydetection/Point.java @@ -0,0 +1,35 @@ +package com.microsoft.cognitiveservice.anomalydetection; + +import org.apache.commons.lang3.StringUtils; + +public class Point implements Comparable { + private String timestamp; + + private long value; + + public void setTimestamp(String timestamp) { + this.timestamp = timestamp; + } + + public String getTimestamp() { + return this.timestamp; + } + + public void setValue(long value) { + this.value = value; + } + + public long getValue() { + return this.value; + } + + @Override + public int compareTo(Point o) { + if (o == null || StringUtils.isEmpty(o.getTimestamp())) + return 1; + else if (StringUtils.isEmpty(this.getTimestamp())) + return -1; + + return this.getTimestamp().compareTo(o.timestamp); + } +} diff --git a/other-samples/java-sample/src/main/java/com/microsoft/cognitiveservice/anomalydetection/StreamMain.java b/other-samples/java-sample/src/main/java/com/microsoft/cognitiveservice/anomalydetection/StreamMain.java new file mode 100644 index 0000000..a662df1 --- /dev/null +++ b/other-samples/java-sample/src/main/java/com/microsoft/cognitiveservice/anomalydetection/StreamMain.java @@ -0,0 +1,87 @@ +package com.microsoft.cognitiveservice.anomalydetection; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collection; + +import com.google.gson.Gson; + +import org.apache.commons.io.IOUtils; + +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; + +public class StreamMain{ + + private static final String subscriptionKey = "xxxxx"; + + // Choose which anomaly detection way you want to use and change the uriBase's second part + private static final String rootUrl = "http://localhost:5000/anomalydetector/v1.0"; + private static final String lastDetect = "/timeseries/last/detect"; + private static final String uriBase = rootUrl + lastDetect; + + public static void main(String[] args) throws Exception{ + String resourceName = "/request-data.json"; + String rawRequest = IOUtils.toString(StreamMain.class.getResourceAsStream(resourceName),StandardCharsets.UTF_8); + AnomalyDetectorRequest anomalyDetectorRequest = new Gson().fromJson(rawRequest, AnomalyDetectorRequest.class); + Collection pointCollection = anomalyDetectorRequest.getSeries(); + int i = 0; + AnomalyDetectorRequest request = new AnomalyDetectorRequest(new ArrayList(), anomalyDetectorRequest.getGranularity()); + for(Point p : pointCollection){ + + request.getSeries().add(p); + if(++i == 12){ + doRequest(request); + request = new AnomalyDetectorRequest(new ArrayList(), anomalyDetectorRequest.getGranularity()); + i = 0; + } + + } + } + + private static void doRequest(AnomalyDetectorRequest anomalyDetectorRequest){ + CloseableHttpClient client = HttpClients.createDefault(); + HttpPost request = new HttpPost(uriBase); + + // Request headers. + request.setHeader("Content-Type", "application/json"); + request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey); + + try { + StringEntity params = new StringEntity(new Gson().toJson(anomalyDetectorRequest)); + request.setEntity(params); + + CloseableHttpResponse response = client.execute(request); + try { + HttpEntity respEntity = response.getEntity(); + if (respEntity != null) { + System.out.println("----------"); + System.out.println(response.getStatusLine()); + System.out.println("Response content is :\n"); + System.out.println(EntityUtils.toString(respEntity, "utf-8")); + System.out.println("----------"); + } + } catch (Exception respEx) { + respEx.printStackTrace(); + } finally { + response.close(); + } + + } catch (Exception ex) { + System.err.println("Exception on Anomaly Detection: " + ex.getMessage()); + ex.printStackTrace(); + } finally { + try { + client.close(); + } catch (Exception e) { + System.err.println("Exception on closing HttpClient: " + e.getMessage()); + e.printStackTrace(); + } + } + } +} \ No newline at end of file diff --git a/other-samples/java-sample/src/main/java/com/microsoft/cognitiveservice/anomalydetection/StreamResponse.java b/other-samples/java-sample/src/main/java/com/microsoft/cognitiveservice/anomalydetection/StreamResponse.java new file mode 100644 index 0000000..015891b --- /dev/null +++ b/other-samples/java-sample/src/main/java/com/microsoft/cognitiveservice/anomalydetection/StreamResponse.java @@ -0,0 +1,80 @@ +package com.microsoft.cognitiveservice.anomalydetection; + +public class StreamResponse { + private float expectedValue; + private boolean isAnomaly; + private boolean isNegativeAnomaly; + private boolean isPositiveAnomaly; + private float lowerMargin; + private float period; + private float suggestedWindow; + private float upperMargin; + + // Getter Methods + + public float getExpectedValue() { + return expectedValue; + } + + public boolean getIsAnomaly() { + return isAnomaly; + } + + public boolean getIsNegativeAnomaly() { + return isNegativeAnomaly; + } + + public boolean getIsPositiveAnomaly() { + return isPositiveAnomaly; + } + + public float getLowerMargin() { + return lowerMargin; + } + + public float getPeriod() { + return period; + } + + public float getSuggestedWindow() { + return suggestedWindow; + } + + public float getUpperMargin() { + return upperMargin; + } + + // Setter Methods + + public void setExpectedValue(float expectedValue) { + this.expectedValue = expectedValue; + } + + public void setIsAnomaly(boolean isAnomaly) { + this.isAnomaly = isAnomaly; + } + + public void setIsNegativeAnomaly(boolean isNegativeAnomaly) { + this.isNegativeAnomaly = isNegativeAnomaly; + } + + public void setIsPositiveAnomaly(boolean isPositiveAnomaly) { + this.isPositiveAnomaly = isPositiveAnomaly; + } + + public void setLowerMargin(float lowerMargin) { + this.lowerMargin = lowerMargin; + } + + public void setPeriod(float period) { + this.period = period; + } + + public void setSuggestedWindow(float suggestedWindow) { + this.suggestedWindow = suggestedWindow; + } + + public void setUpperMargin(float upperMargin) { + this.upperMargin = upperMargin; + } +} \ No newline at end of file