Skip to content

Commit

Permalink
Merge pull request #721 from cliveseldon/request_logging
Browse files Browse the repository at this point in the history
update request logger
  • Loading branch information
ukclivecox authored Jul 24, 2019
2 parents 5a05a5c + c2ebf60 commit 2758b2b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
40 changes: 33 additions & 7 deletions seldon-request-logger/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import sys
import dict_digger
import json
from seldon_core.utils import json_to_seldon_message, extract_request_parts
from seldon_core.utils import json_to_seldon_message, extract_request_parts, array_to_grpc_datadef, seldon_message_to_json
from seldon_core.proto import prediction_pb2
import numpy as np

app = Flask(__name__)
Expand All @@ -23,7 +24,7 @@ def index():
if "date" in requestCopy:
del requestCopy["date"]
requestMsg = json_to_seldon_message(requestCopy)
(req_features, _, req_datadef, _) = extract_request_parts(requestMsg)
(req_features, _, req_datadef, req_datatype) = extract_request_parts(requestMsg)
req_elements = createElelmentsArray(req_features,list(req_datadef.names))

responsePart = dict_digger.dig(content,'response')
Expand All @@ -33,22 +34,30 @@ def index():
if "date" in responseCopy:
del responseCopy["date"]
responseMsg = json_to_seldon_message(responseCopy)
(res_features, _, res_datadef, _) = extract_request_parts(responseMsg)
(res_features, _, res_datadef, res_datatype) = extract_request_parts(responseMsg)
res_elements = createElelmentsArray(res_features,list(res_datadef.names))

if not req_elements is None and not res_elements is None:
for (a,b) in zip(req_elements,res_elements):
for i,(a,b) in enumerate(zip(req_elements,res_elements)):
merged = {**a, **b}
content["elements"] = merged
reqJson = extractRow(i, requestMsg, req_datatype, req_features, req_datadef)
resJson = extractRow(i, responseMsg, res_datatype, res_features, res_datadef)
content["request"] = reqJson
content["response"] = resJson
#log formatted json to stdout for fluentd collection
print(str(json.dumps(content)))
elif not req_elements is None:
for e in req_elements:
for i,e in enumerate(req_elements):
content["elements"] = e
reqJson = extractRow(i, requestMsg, req_datatype, req_features, req_datadef)
content["request"] = reqJson
print(str(json.dumps(content)))
elif not res_elements is None:
for e in res_elements:
for i,e in enumerate(res_elements):
content["elements"] = e
resJson = extractRow(i, responseMsg, res_datatype, res_features, res_datadef)
content["response"] = resJson
print(str(json.dumps(content)))
else:
print(str(json.dumps(content)))
Expand All @@ -60,15 +69,32 @@ def index():
# print(e, file=sys.stderr)
# return 'Error processing input'


def extractRow(i:int,requestMsg: prediction_pb2.SeldonMessage,req_datatype: str,req_features: np.ndarray,req_datadef: prediction_pb2.SeldonMessage.data):
if req_datatype == "data":
datatyReq = requestMsg.data.WhichOneof("data_oneof")
else:
datatyReq = "ndarray"
if len(req_features.shape) == 2:
dataReq = array_to_grpc_datadef(datatyReq, np.expand_dims(req_features[i], axis=0), req_datadef.names)
else:
dataReq = array_to_grpc_datadef(datatyReq, req_features, req_datadef.names)
requestMsg2 = prediction_pb2.SeldonMessage(data=dataReq, meta=requestMsg.meta)
reqJson = seldon_message_to_json(requestMsg2)
return reqJson


def createElelmentsArray(X: np.ndarray,names: list):
results = []
results = None
if isinstance(X,np.ndarray):
if len(X.shape) == 1:
results = []
d = {}
for num, name in enumerate(names, start=0):
d[name] = X[num]
results.append(d)
elif len(X.shape) == 2:
results = []
for i in range(X.shape[0]):
d = {}
for num, name in enumerate(names, start=0):
Expand Down
18 changes: 18 additions & 0 deletions seldon-request-logger/app/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
curl 0.0.0.0:8080 -d '{"request":{"data":{"names":["a","b"],"tensor":{"shape":[2,2],"values":[1,2,3,4]}}},"response":{"data":{"names":["c"],"tensor":{"shape":[2,1],"values":[5,6]}}}}' -H "Content-Type: application/json"


curl 0.0.0.0:8080 -d '{"request":{"data":{"names":["a","b"],"ndarray":[[1,2],[3,4]]}},"response":{"data":{"names":["c"],"ndarray":[[7],[8]]}}}' -H "Content-Type: application/json"


curl 0.0.0.0:8080 -d '{"request":{"data":{"names":["a","b"],"tensor":{"shape":[2,2],"values":[1,2,3,4]}}}}' -H "Content-Type: application/json"

curl 0.0.0.0:8080 -d '{"response":{"data":{"names":["c"],"tensor":{"shape":[2,1],"values":[5,6]}}}}' -H "Content-Type: application/json"

curl 0.0.0.0:8080 -d '{"request":{"data":{"names":["a","b"],"tensor":{"shape":[2,2,1],"values":[1,2,3,4]}}},"response":{"data":{"names":["c"],"tensor":{"shape":[2,1],"values":[5,6]}}}}' -H "Content-Type: application/json"







0 comments on commit 2758b2b

Please sign in to comment.