Skip to content

Commit 3410b9b

Browse files
committed
Initial commit.
0 parents  commit 3410b9b

File tree

29 files changed

+888
-0
lines changed

29 files changed

+888
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# generated files
2+
.idea
3+
*.iml
4+
**/target
5+
*.log
6+
.classpath
7+
.project
8+
**/org.eclipse.jdt.core.prefs
9+
**/org.eclipse.core.resources.prefs
10+
**/org.eclipse.m2e.core.prefs

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Predix Analytics Samples
2+
3+
A collection of samples for use with Predix Analytics services.
4+
5+
## Sample Analytics
6+
7+
These analytics are implementations of the same logic (adding 2 numbers together and returning the result) developed in different languages.
8+
9+
1. **[demo-adder-java](demo-adder-java)** - sample adder analytic in Java
10+
2. **[demo-adder-matlab](demo-adder-matlab)** - sample adder analytic in Matlab
11+
3. **[demo-adder-py](demo-adder-py)** - sample adder analytic in Python
12+
13+
The expected JSON input data format is as follows:
14+
15+
`{"number1": 123, "number2": 456}`
16+
17+
For more information on developing analytics for use with Predix Analytics, see "Analytic Development" in the Predix Analytics Services documentation on Predix IO.
18+
19+
## Sample Orchestration Workflows
20+
21+
**[Sample Orchestration Workflows](orchestrations)**
22+
23+
For more information on running orchestrations in Predix Analytics, see "Using the Analytics Runtime Service" in the Predix Analytics Services documentation on Predix IO.

demo-adder-java/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#demo-adder-java
2+
3+
A java-based sample analytic for Predix Analytics.
4+
5+
## Compiled binaries
6+
Refer to the Releases page for compiled binaries you can upload directly to Predix Analytics.
7+
8+
## Pre-requisites
9+
To build and run this analytic, you will need to have the following:
10+
- JDK 1.7+
11+
- Maven 3+
12+
13+
## Building, deploying and running the analytic
14+
1. From the demo-analytics/demo-adder-java directory, run the `mvn clean install` command to build and perform the component test.
15+
2. Create an analytic in Analytics Catalog with the name "Demo Adder Java" and the version "v1".
16+
3. Upload the jar file demo-adder-java-1.0.0-SNAPSHOT.jar from the demo-adder-java/target directory and attach it to the created analytic entry.
17+
4. Deploy and test the analytic on Predix Analytics platform.
18+
19+
## Input format
20+
The expected JSON input data format is as follows:
21+
`{"number1": 123, "number2": 456}`
22+
23+
## Output format
24+
The JSON output format from the analytic is as follows:
25+
`{"result":579}`
26+
27+
## Testing the analytic locally
28+
1. From the demo-analytics/demo-adder-java directory, run the `mvn spring-boot:run` command to run the analytic locally.
29+
2. The analytic should come up using the TCP port defined in demo-analytics/demo-adder-java/src/main/resource/application.properties file.
30+
3. Using Chrome POSTMAN or curl, POST the REST request to http://localhost:9090/api/v1/demo_adder_java/execution. Set "Accept" header to "application/json". Set "Content-Type" header to "application/json". Specify the request body as defined in "Input format" section.
31+
32+
## Developing a java-based analytic
33+
Define the REST API with the URL of api/{version}/{analytic_name}/execution where
34+
- "api" is the URL prefix expected by Predix Analytics
35+
- {version} is based on the version of the analytic to be defined in the Analytic Catalog
36+
- {analytic_name} is based on the name of the analytic to be defined in the Analytic Catalog
37+
- "execution" is the REST resource name expected by Predix Analytics
38+
39+
40+
Note: The version of the analytic to be included in the URL should match that defined in the Analytic Catalog with the following modifications
41+
42+
- Substitute dots '.' with underscores '_'
43+
- Convert all letters to lower case
44+
45+
46+
Note: The name of the analytic to be included in the URL should match that defined in the Analytic Catalog with the following modifications
47+
48+
- Substitute spaces ' ' with underscores '_'
49+
- Convert all letters to lower case
50+
51+
52+
For example, suppose you defined the following analytic in the analytic catalog
53+
54+
- Name: Anomaly Detection
55+
- Version: v1.0
56+
57+
Then define the REST URL as api/v1_0/anomaly_detection/execution
58+
59+
60+
For more information, see "Analytic Development" in the Predix Analytics Services documentation on Predix IO.

demo-adder-java/pom.xml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.ge.predix.demo</groupId>
7+
<artifactId>demo-adder-java</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>demo-adder-java</name>
12+
<description>Demo adder java analytic microservice</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.2.1.RELEASE</version>
18+
</parent>
19+
20+
<properties>
21+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22+
<java.version>1.7</java.version>
23+
<commons-io.version>1.4</commons-io.version>
24+
<maven-failsafe-plugin.version>2.18.1</maven-failsafe-plugin.version>
25+
<rest-assured.version>2.4.0</rest-assured.version>
26+
<start-class>com.ge.predix.demo.DemoAnalyticApplication</start-class>
27+
</properties>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-web</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-test</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>commons-io</groupId>
41+
<artifactId>commons-io</artifactId>
42+
<version>${commons-io.version}</version>
43+
<scope>test</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>com.jayway.restassured</groupId>
47+
<artifactId>rest-assured</artifactId>
48+
<version>${rest-assured.version}</version>
49+
<scope>test</scope>
50+
</dependency>
51+
</dependencies>
52+
53+
<build>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-maven-plugin</artifactId>
58+
<configuration>
59+
<mainClass>${start-class}</mainClass>
60+
</configuration>
61+
<executions>
62+
<execution>
63+
<goals>
64+
<goal>repackage</goal>
65+
</goals>
66+
</execution>
67+
</executions>
68+
</plugin>
69+
<plugin>
70+
<groupId>org.apache.maven.plugins</groupId>
71+
<artifactId>maven-failsafe-plugin</artifactId>
72+
<version>${maven-failsafe-plugin.version}</version>
73+
<executions>
74+
<execution>
75+
<goals>
76+
<goal>integration-test</goal>
77+
<goal>verify</goal>
78+
</goals>
79+
<configuration>
80+
<includes>
81+
<include>**/IT*.java</include>
82+
<include>**/CT*.java</include>
83+
</includes>
84+
</configuration>
85+
</execution>
86+
</executions>
87+
</plugin>
88+
</plugins>
89+
</build>
90+
91+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2015 General Electric Company. All rights reserved.
3+
*
4+
* The copyright to the computer software herein is the property of
5+
* General Electric Company. The software may be used and/or copied only
6+
* with the written permission of General Electric Company or in accordance
7+
* with the terms and conditions stipulated in the agreement/contract
8+
* under which the software has been supplied.
9+
*/
10+
package com.ge.predix.demo;
11+
12+
import org.springframework.boot.SpringApplication;
13+
import org.springframework.boot.autoconfigure.SpringBootApplication;
14+
import org.springframework.context.annotation.PropertySource;
15+
16+
@SpringBootApplication
17+
@PropertySource("application.properties")
18+
public class DemoAnalyticApplication {
19+
20+
public static void main(String[] args) {
21+
SpringApplication.run(DemoAnalyticApplication.class, args);
22+
}
23+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2015 General Electric Company. All rights reserved.
3+
*
4+
* The copyright to the computer software herein is the property of
5+
* General Electric Company. The software may be used and/or copied only
6+
* with the written permission of General Electric Company or in accordance
7+
* with the terms and conditions stipulated in the agreement/contract
8+
* under which the software has been supplied.
9+
*/
10+
package com.ge.predix.demo;
11+
12+
public class DemoAnalyticRequest {
13+
14+
protected long number1;
15+
protected long number2;
16+
17+
/**
18+
* Gets the value of the number1 property.
19+
*
20+
*/
21+
public long getNumber1() {
22+
return number1;
23+
}
24+
25+
/**
26+
* Sets the value of the number1 property.
27+
*
28+
*/
29+
public void setNumber1(long value) {
30+
this.number1 = value;
31+
}
32+
33+
/**
34+
* Gets the value of the number2 property.
35+
*
36+
*/
37+
public long getNumber2() {
38+
return number2;
39+
}
40+
41+
/**
42+
* Sets the value of the number2 property.
43+
*
44+
*/
45+
public void setNumber2(long value) {
46+
this.number2 = value;
47+
}
48+
49+
@Override public String toString() {
50+
return "DemoAnalyticRequest{" +
51+
"number1=" + number1 +
52+
", number2=" + number2 +
53+
'}';
54+
}
55+
56+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2015 General Electric Company. All rights reserved.
3+
*
4+
* The copyright to the computer software herein is the property of
5+
* General Electric Company. The software may be used and/or copied only
6+
* with the written permission of General Electric Company or in accordance
7+
* with the terms and conditions stipulated in the agreement/contract
8+
* under which the software has been supplied.
9+
*/
10+
package com.ge.predix.demo;
11+
12+
public class DemoAnalyticResponse {
13+
14+
protected Long result;
15+
16+
/**
17+
* Gets the value of the result property.
18+
*
19+
20+
* @return
21+
* possible object is
22+
* {@link Long }
23+
*
24+
*/
25+
public Long getResult() {
26+
return result;
27+
}
28+
29+
/**
30+
* Sets the value of the result property.
31+
*
32+
* @param value
33+
* allowed object is
34+
* {@link Long }
35+
*
36+
*/
37+
public void setResult(Long value) {
38+
this.result = value;
39+
}
40+
41+
@Override public String toString() {
42+
return "DemoAnalyticResponse{" +
43+
"result=" + result +
44+
'}';
45+
}
46+
47+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2015 General Electric Company. All rights reserved.
3+
*
4+
* The copyright to the computer software herein is the property of
5+
* General Electric Company. The software may be used and/or copied only
6+
* with the written permission of General Electric Company or in accordance
7+
* with the terms and conditions stipulated in the agreement/contract
8+
* under which the software has been supplied.
9+
*/
10+
package com.ge.predix.demo;
11+
12+
import org.springframework.http.HttpStatus;
13+
import org.springframework.http.ResponseEntity;
14+
import org.springframework.web.bind.annotation.RequestBody;
15+
import org.springframework.web.bind.annotation.RequestMapping;
16+
import org.springframework.web.bind.annotation.RequestMethod;
17+
import org.springframework.web.bind.annotation.RestController;
18+
19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
21+
/**
22+
* REST API with the URL of api/v1/demoadder/execution where
23+
* "api" is the URL prefix expected by Predix Analytics
24+
* "v1" is the version of the analytic defined in the analytic catalog
25+
* "demoadder" is the name of the analytic defined in the analytic catalog
26+
* "execution" is the REST resource name expected by Predix Analytics
27+
*
28+
* Note: The version of the analytic defined in the analytic catalog is transformed into URL friendly name by
29+
* - Substituting dots '.' with underscores '_'
30+
* - Converting to all lower cases
31+
*
32+
* Note: The name of the analytic defined in the analytic catalog is transformed into URL friendly name by
33+
* - Substituting spaces ' ' with underscores '_'
34+
* - Converting to all lower cases
35+
*
36+
* For example, suppose you defined the following analytic in the analytic catalog
37+
* Name: Anomaly Detection
38+
* Version: v1.0
39+
* Then define the REST URL as api/v1_0/anomaly_detection/execution
40+
*/
41+
@RestController
42+
@RequestMapping(value = DemoAnalyticService.ANALYTIC_REST_URL)
43+
public class DemoAnalyticService {
44+
45+
public static final String ANALYTIC_NAME = "demo_adder_java";
46+
public static final String ANALYTIC_VERSION = "v1";
47+
public static final String ANALYTIC_REST_URL_PREFIX = "api";
48+
public static final String ANALYTIC_REST_RESOURCE = "execution";
49+
public static final String ANALYTIC_REST_URL = ANALYTIC_REST_URL_PREFIX + "/" + ANALYTIC_VERSION + "/" + ANALYTIC_NAME;
50+
51+
// api/v1/demo_analytic_java/execution
52+
public static final String ANALYTIC_REST_RESOURCE_URL = ANALYTIC_REST_URL + "/" + ANALYTIC_REST_RESOURCE;
53+
54+
@RequestMapping(value = ANALYTIC_REST_RESOURCE, method = RequestMethod.POST)
55+
public ResponseEntity<String> execute(@RequestBody String input) {
56+
try {
57+
return new ResponseEntity<>(run(input), HttpStatus.OK);
58+
} catch (Exception ex) {
59+
throw new RuntimeException(ex);
60+
}
61+
}
62+
63+
public String run(String input) throws Exception {
64+
ObjectMapper mapper = new ObjectMapper();
65+
DemoAnalyticRequest request = mapper.readValue(input, DemoAnalyticRequest.class);
66+
DemoAnalyticResponse response = new DemoAnalyticResponse();
67+
response.setResult(request.getNumber1() + request.getNumber2());
68+
return mapper.writeValueAsString(response);
69+
}
70+
71+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
server.port=9090

0 commit comments

Comments
 (0)