-
Notifications
You must be signed in to change notification settings - Fork 12
/
testApi.py
105 lines (88 loc) · 2.8 KB
/
testApi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#! /usr/bin/python
import csv
import os
import requests
import json
from datetime import datetime
from output.streamPlot import streamDetectionPlot
from output.multiStreamPlot import multiStreamDetectionPlot
def getData():
# get the data.
filePath = os.path.join(os.getcwd(), "test/data/heartBeat.csv")
file = open(filePath)
allData = csv.reader(file)
# skip the first three lines.
allData.next()
allData.next()
allData.next()
inputData = [x for x in allData]
return inputData
def convertData(dataList):
transformedData = []
# convert the data into json type.
for line in dataList:
transformedData.append(
json.dumps(
{"timestamp": line[0], "actualValue": line[1]}
)
)
return transformedData
def convertMultiData(dataList):
transformedData = []
# convert the data into json type.
for line in dataList:
transformedData.append(
json.dumps(
{
"A": {"timestamp": line[0], "actualValue": line[1]},
"B": {"timestamp": line[0], "actualValue": line[2]},
"C": {"timestamp": line[0], "actualValue": line[3]}
}
)
)
return transformedData
def requestsHTMApi(streamData):
# requests HTM api.
inputData = {"streamData": streamData}
url = "http://127.0.0.1:5000/api/HTM/v1.0/anomalyDetection/1"
r = requests.post(url, inputData)
return r.json()
def requestMultiHTMApi(streamData):
inputData = {"streamData": streamData}
url = "http://127.0.0.1:5000/api/HTM/v1.0/multiAnomalyDetection/1"
r = requests.post(url, inputData)
return r.json()
def run():
# read the data from /output/data/heartBeat.csv, convert it into json type,
# and initial the graph.
data = convertData(getData())
graph = streamDetectionPlot()
graph.initPlot()
for line in data:
# requests the one field api.
requestsData = requestsHTMApi(line)
print requestsData
# plot the data
graph.anomalyDetectionPlot(
requestsData["timestamp"],
requestsData["actualValue"],
requestsData["predictValue"],
requestsData["anomalyScore"]
)
graph.close()
def runMulti():
# read the data from /output/data/heartBeat.csv, convert it into json type,
# and initial the graph.
data = convertMultiData(getData())
graph2 = multiStreamDetectionPlot()
graph2.initPlot()
for line in data:
# requests the one field api.
requestsData = requestMultiHTMApi(line)
# print requestsData
# print requestsData["A"]["timestamp"]
# plot the data
graph2.anomalyDetectionPlot(requestsData)
graph2.close()
if __name__ == "__main__":
run()