cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

Sample Java utility to validate ThingWatcher 52.2 installation

No ratings

This Blog presents a simple Java utility to validate the deployment of ThingWatcher.
It is important to note that the utility used is not a real life situation, the intent was to keep it as simple as possible in order to achieve its aim: validation of the deployment.
An understanding of Java IDE (such as Eclipse) is necessary in order to run the utility with relevant dependency and classpath setup. Those are beyond the scope of this posting.

We will cover the following points:

Pre-requisites


A strict adherence to the ThingWatcher deployment guide is recommended in order to first deploy training and model microservices as well as to familiarize yourself with ThingWatcher APIs.

Prior to testing ThingWatcher, both the training and model microservices should be up and running

The media for ThingWatcher (including model and training micro-service) should be downloaded from PTC Software Download page .

The commands to deploy the micro-services will vary depending on the platform used and are presented in the ThingWatcher deployment guide.
As a reference example, on Windows the command will be similar to the following:

  1. Start Docker: Start > Program > Docker > Docker Quick Start Terminal
  2. Load model micro service tar

$ docker load < "D:\PTC\MED-61147-CD-522_F000_ThingWorx-Analytics-ThingWatcher-52-2\components\ModelService\ModelService\model-service.tar"

    3. Install model service:

$ docker run -d -p 8080:8080 -v '/d/TWatcherStorage/model:/data/models' -v '/d/TWatcherStorage/db:/tmp/' twxml/model-service:1.0 -Dfile.storage.path=/data/models -jar maven/model-1.0.jar server maven/standalone-evaluator.yml

    4. Load training micro service tar file

                        $ docker load < "D:\PTC\MED-61147-CD-522_F000_ThingWorx-Analytics-ThingWatcher-52-2\components\TrainingService\TrainingService\training-service.tar"

    5. Install training service

                        $ docker run -d -p 8090:8080  twxml/training-service:1.0.0  -Dmodel.destination.uri=model://192.168.99.100:8080/models -jar maven/training-standalone-1.0.0-bin.jar server /maven/training-standalone-single.yml

Note: the -Dmodel.destination.uri points here to the model micro-service host. To find the ip address, enter docker-machine ip on the model micro-service docker machine.

    6. Validate micro-services deployment:

Execute docker ps  and confirmed that both services are up, as in the following example:
CONTAINER ID        IMAGE                          COMMAND                      CREATED            STATUS              PORTS NAMES

5b6a29b95611        twxml/training-service:1.0.0  "java -Dmodel.destina"  13 days ago        Up 44 minutes      8081/tcp, 0.0.0.0:8090->8080/tcp  modest_albattani

8c13c0bc910e        twxml/model-service:1.0        "java -Dfile.storage."      2 weeks ago        Up 44 minutes      0.0.0.0:8080->8080/tcp, 8081/tcp  thirsty_ptolemy

 


Using the sample utility


  • Download the attachment Main.java
  • Import Main.java into Eclipse (or IDE of choice) with the ThingWatcher dependencies added in classpath.
  • Update the trainingBaseURI (see below) to points to the training micro-services.
  • The utility should be ready to execute.

Code walk through

The code declares a thingwatcher in the following snippet:

ThingWatcher thingwatcher = new ThingWatcherBuilder()

.certainty(90.0)

.trainingDataDuration(60)

.trainingDataDurationUnit(DurationUnit.SECOND)

.trainingBaseURI("http://192.168.99.100:8090/training")

.getThingWatcher();

  • In the above code it is important to update the trainingBaseURI argument with the correct ip address and port for the training micro-service host.
  • The code then loops 10000 times and sends a new value, which simulates the sensor data, at a simulated 100 ms interval.
    The value is computed as Math.sin(i) for the whole calibrating phase and most of the monitoring phase too.
  • We artificially introduce an anomaly by sending a value of Math.incremetExact(i) between the 9000th and 9900th iterations.
  • During the Monitoring phase, the code logs the value, the anomalous status and the thingwatcher state.

It is advised to save the output to a file in order to review the logging once the utility has run.

  • In Eclipse this can be done by selecting the Main.java with right mouse button > Run As… > Run Configuration > Common and tick Output File under the Standard Input and Output, and specify a location for the output file.

A review of the output log file will shows that somewhere between timestamp 900000 and 990000, the isAnomalousValue is true.

  • Note that this does not starts and ends exactly at 900000 and 990000, as ThingWatcher needs a few occurrences before reporting it as anomaly.
    Sample output indicating an anomalous state:


[main] INFO com.thingworx.analytics.demo.Main - Value = 901700,9017.0,-9016.403802019577

[main] INFO com.thingworx.analytics.demo.Main - isAnomalousValue = true

[main] INFO com.thingworx.analytics.demo.Main - ThingWatcherStat = MONITORING

As part of validating the successful deployment of ThingWatcher, it is recommended to validate the correct creation of a training and model job.

Validate training job creation


In order to validate the successful creation of a training job, execute a GET request to the training micro service : http://192.168.99.100:8090/training (update the ip address to the one on your system)
This should return a COMPLETED job whose body starts with something similar to:

Validate model job creation

In order to validate the successful creation of a model job, execute a GET request to http://192.168.99.100:8080/models (update the ip address to the one on your system) to see all the models that have been created. For example:

Alternatively, click (or use) the URI reported in the training job output, here http://192.168.99.100:8080/models/6/pmml.xml, to see the complete model definition. The output will be similar to:

When this sample test runs correctly, the ThingWatcher deployment has been validated.

Update for ThingWorx Analytics 8.0

To Note:

  • The utility provided is for testing purpose only.
  • The code does not represent any kind of best practice and is not meant to be a perfect java coding example.
  • It is provided as is with no guarantee.
Comments

Christophe

I set the Thingwatcher according to the above guide, edit trainingBaseUrl and run the java utility(main.java) to run Training.

I got an error like below.

What is the problem?

2월 19, 2017 10:41:53 오전 com.thingworx.analytics.thingwatcher.ThingWatcher <init>

INFO: ThingWatcher [ ID: a539027a-3ac7-48a6-b798-10edf188e710, Certainty: 90.0, MinimumTrainingDataDuration: 60, TrainingDataDurationUnit: SECOND, PMMLModelURI: null, TrainingURI: http://127.0.0.1:8090/training ]

2월 19, 2017 10:41:54 오전 com.thingworx.analytics.demo.Main main

INFO: Waiting for thingwatcher training to complete...

2월 19, 2017 10:41:59 오전 com.thingworx.analytics.demo.Main main

INFO: Waiting for thingwatcher training to complete...

2월 19, 2017 10:42:04 오전 com.thingworx.analytics.demo.Main main

INFO: Waiting for thingwatcher training to complete...

com.thingworx.analytics.thingwatcher.Trainer$TrainingJobErrorException: Training job with id 2 entered into an incomplete state [FAILED] unable to process models.  Error message: [NullPointerException: ]

  at com.thingworx.analytics.thingwatcher.Trainer.lambda$getPMMLModelUri$1(Trainer.java:45)

  at java.util.Optional.orElseThrow(Unknown Source)

  at com.thingworx.analytics.thingwatcher.Trainer.getPMMLModelUri(Trainer.java:45)

  at com.thingworx.analytics.thingwatcher.ThingWatcher.attemptTransitionToMonitoringState(ThingWatcher.java:338)

  at com.thingworx.analytics.thingwatcher.ThingWatcher.monitor(ThingWatcher.java:157)

  at com.thingworx.analytics.demo.Main.main(Main.java:55)

com.thingworx.analytics.thingwatcher.Trainer$TrainingJobErrorException: Training job with id 2 entered into an incomplete state [FAILED] unable to process models.  Error message: [NullPointerException: ]

  at com.thingworx.analytics.thingwatcher.Trainer.lambda$getPMMLModelUri$1(Trainer.java:45)

...........................................................

Hi Yong June Kim

From the output you pasted, the trainingBaseURI is set to http://127.0.0.1:8090/training .

If you follow the installation steps, the training model service runs inside docker so I would expect a docker ip address here and not the localhost one. Having said that it seems ThingWatcher does find the training micro service but it would seem you have not follow the setup described in the ThingWatcher guide.

It probably would be interesting to have the details of what exactly you have done.

The error reported here, though ,  is most likely due to the training microservice not finding the model micro service.

While initializing the training micro service (see command in step 5 of pre-requisite above) the  model.destination.uri property should point to the location of the model micro service ( model://192.168.99.100:8080/models in the example). I would think the URI was incorrect in your command.
As a troubleshooting step, you can try accessing this URI in a browser:  http://192.168.99.100:8080/models, you should get the list of models or {"values":[],"total":0,"next":null,"previous":null} if none have been created yet. If you get some error of type http 404, then the URI is not correct.

Once you have the correct URI, you will need to initialize a new training service with a similar command as in step 5 above but with correct model URI for you.

You will also need to stop and delete the current training microservice which points to the wrong URI.

Hope this helps.

Kind regards

Christophe

plu

Hi Christophe

I set the Thingwatcher according to the above guide, edit trainingBaseUrl and run the java utility(main.java) to run Training.

My code error like picture .

What is the problem?图1.jpg图2.jpg

Hi

What version are you using ?

The main.java was created with 52.2 version of ThingWatcher - I think it woudl work up to version 7.4.

For version 8.0, you would need to check the chapter Update for ThingWorx Analytics 8.0 in the blog

Hope this helps

Christophe

plu

Hi Christophe

I see  , I using version 8.0,   thanks.

Version history
Last update:
‎Jan 05, 2017 11:06 AM
Updated by:
Labels (3)
Attachments