Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Main.java #22

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,15 @@ ASALocalRun/

# MFractors (Xamarin productivity tool) working folder
.mfractor/


#eclipse
.project
.classpath
.settings/

#IntelliJ Extesions
*.iml

#visual code
.vscode/
3 changes: 3 additions & 0 deletions other-samples/java-sample/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*


.vscode
2 changes: 0 additions & 2 deletions other-samples/java-sample/anomalydetction.iml

This file was deleted.

23 changes: 19 additions & 4 deletions other-samples/java-sample/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.microsoft.cognitiveservice</groupId>
Expand All @@ -19,6 +17,23 @@
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.microsoft.cognitiveservice.anomalydetection;

import java.util.Collection;

public class AnomalyDetectorRequest{
public AnomalyDetectorRequest(Collection<Point> points,String granularity){
this.series = points;
this.granularity = granularity;
}
private String granularity;

private Collection<Point> series;

public void setGranularity(String granularity){
this.granularity = granularity;
}
public String getGranularity(){
return this.granularity;
}
public void setSeries(Collection<Point> series){
this.series = series;
}
public Collection<Point> getSeries(){
return this.series;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ public class Main {
// **********************************************

// Replace the subscriptionKey string value with your valid subscription key.

private static final String subscriptionKey = "<Subscription Key>";


// 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";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.microsoft.cognitiveservice.anomalydetection;

import org.apache.commons.lang3.StringUtils;

public class Point implements Comparable<Point> {
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);
}
}
Original file line number Diff line number Diff line change
@@ -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<Point> pointCollection = anomalyDetectorRequest.getSeries();
int i = 0;
AnomalyDetectorRequest request = new AnomalyDetectorRequest(new ArrayList<Point>(), anomalyDetectorRequest.getGranularity());
for(Point p : pointCollection){

request.getSeries().add(p);
if(++i == 12){
doRequest(request);
request = new AnomalyDetectorRequest(new ArrayList<Point>(), 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();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}