def wip(action, drink): global total, client, progress span = trace.newspan(tracer, trace.getparentspanfromheader(tracer, request), 'wip ' + action + ' ' + drink) print('wip ' + action + ' ' + drink + ' ' + str(1 if action == 'sup' else -1)) progress[drink] += 1 if action == 'sup' else -1 if progress[drink] < 0: progress[drink] = 0 response = app.response_class(response=json.dumps('OK'), status=200, mimetype='application/json') json_body = [{ "measurement": "wip", "tags": { "type": drink }, "fields": { "resource": progress[drink] } }] client_influx.write_points(json_body) span.finish() return response
def buy_drink(drink, payment, name): span = trace.newspan(tracer, None, 'transaction') span.set_tag('drink', drink) span.set_tag('customer', name) span.set_tag('payment', payment) res = trace.inject_in_header(tracer, span, {}) span.finish() if True: print(name + ' buys ' + drink) API.call_api(data_server, data_port, '/wip/add/' + drink, res) API.call_api(data_server, data_port, '/add/client', res) r = API.call_api(produce_server, produce_port, '/produce/' + drink, res) if r.status_code == 200: r = API.call_api(bill_server, bill_port, '/bill/' + payment + '/' + name, res) if r.status_code == 200: span.set_tag('error', False) else: span.set_tag('error', True) span.set_tag('bill', r.status_code) else: span.set_tag('error', True) response = app.response_class(response=json.dumps(res), status=r.status_code, mimetype='application/json') API.call_api(data_server, data_port, '/wip/sup/' + drink, res) return response
def add_client(): global total, client span = trace.newspan(tracer, trace.getparentspanfromheader(tracer, request), 'add client') res = {} print('add client') client += 1 json_body = [{ "measurement": "rt", "tags": { "type": "client" }, "fields": { "resource": client, "movement": 1 } }] client_influx.write_points(json_body) response = app.response_class(response=json.dumps('OK'), status=200, mimetype='application/json') span.finish() return response
def bill(payment, name): span = trace.newspan(tracer, trace.getparentspanfromheader(tracer, request), 'bill') span.set_tag('customer', name) span.set_tag('payment', payment) res = trace.inject_in_header(tracer, span, {}) res = API.call_api(data_server, data_port, '/cashin/' + payment, res) span.finish() sspan = trace.newspan(tracer, span.context, payment) time.sleep(random.randint(0, 200) / 1000) response = app.response_class(response=json.dumps({}), status=200, mimetype='application/json') sspan.finish() return response
def add_resource(resource): print('add ' + resource) global fill, resources, progress span = trace.newspan(tracer, trace.getparentspanfromheader(tracer, request), 'add' + resource) res = {} cf = False cw = False if not fill[resource]: fill[resource] = True logging.info('filling ' + resource + ' tank') if resource == 'coffee': t2s = random.randint(2500, 3500) / 1000 add = 40 elif resource == 'tea': t2s = random.randint(1500, 2500) / 1000 add = 20 else: t2s = random.randint(4500, 5500) / 1000 add = 100 time.sleep(t2s) resources[resource] += add json_body = [{ "measurement": "rt", "tags": { "type": resource }, "fields": { "resource": resources[resource], "movement": add } }] client_influx.write_points(json_body) fill[resource] = False logging.info('filled ' + resource + ' tank') cf = True while fill[resource]: logging.info('waiting end of ' + resource + ' filling') time.sleep(1) cw = True res = {'cw': str(cw), 'cf': str(cf)} response = app.response_class(response=json.dumps(res), status=200, mimetype='application/json') span.finish() return response
def produce_drink(drink): print('produce '+drink) response = app.response_class( status=404, mimetype='application/json' ) span = trace.newspan( tracer, trace.getparentspanfromheader(tracer, request), drink ) res = trace.inject_in_header(tracer, span, {}) result = API.call_api(data_server, data_port, '/state', res) decoded = result.content.decode() _state = json.loads(decoded) error = False if True: if _state['water'] <= 0: API.call_api(data_server, data_port, '/resource/add/water', res) else: if drink == 'coffee' and _state['coffee_bean'] <= 0: API.call_api(data_server, data_port, '/resource/add/' + drink, res) error = True span.set_tag('reason', 'need ' + drink) elif drink == 'tea' and _state['tea_leaf'] <= 0: API.call_api(data_server, data_port, '/resource/add/' + drink, res) error = True span.set_tag('reason', 'need '+drink) else: r = API.call_api(data_server, data_port, '/consume/water', res) if r.status_code == 200: API.call_api(data_server, data_port, '/consume/'+drink, res) time.sleep(random.randint(0, 500) / 1000) response = app.response_class( status=200, mimetype='application/json' ) print(drink + ' produced') # span.status = status.Status( response.status,'TODO') span.set_tag('error', error) span.finish() return response
def cash_in(payment): global total span = trace.newspan(tracer, trace.getparentspanfromheader(tracer, request), 'cash_in ' + payment) # parent_span = trace.getparent_spanfromheader(tracer,request) # span = trace.newspan(tracer, parent_span, 'cash in') print('cash_in ' + payment) total += 1 print('total ' + str(total) + ' $') response = app.response_class(response=json.dumps('OK'), status=200, mimetype='application/json') span.finish() return response
def consume(r): global tea_leaf, coffee_bean, water s = 200 print('consume ' + r) span = trace.newspan(tracer, trace.getparentspanfromheader(tracer, request), 'consume ' + r) res = trace.inject_in_header(tracer, span, {}) error = False if resources[r] <= 0: API.call_api( data_server, data_port, '/resource/add/' + r, res, ) s = 404 error = True else: resources[r] += -1 json_body = [{ "measurement": "rt", "tags": { "type": r }, "fields": { "resource": resources[r], "movement": -1 } }] client_influx.write_points(json_body) time.sleep(random.randint(0, 50) / 1000) print(r + ' ' + str(resources[r])) span.set_tag('error', error) res = {} response = app.response_class(response=json.dumps(res), status=s, mimetype='application/json') span.finish() return response
def state(): global total, client global fill, resources print('state') span = trace.newspan(tracer, trace.getparentspanfromheader(tracer, request), 'state') res = { 'tea_leaf': resources['tea'], 'coffee_bean': resources['coffee'], 'water': resources['water'], 'total': total, 'client': client, 'coffee_in_progress': progress['coffee'], 'tea_in_progress': progress['tea'], 'fill_coffee_in_progress': fill['coffee'], 'fill_water_in_progress': fill['water'], 'fill_tea_in_progress': fill['tea'] } response = app.response_class(response=json.dumps(res), status=200, mimetype='application/json') span.finish() return response
influx_user = os.environ["influx_user"] influx_pwd = os.environ["influx_pwd"] influx_db = os.environ["influx_db"] tracer = trace.init_tracer('states', tracer_server, tracer_port) client_influx = InfluxDBClient(influx_server, influx_port, influx_user, influx_pwd, influx_db) client_influx.create_database(influx_db) # app = Flask(__name__) # CORS(app) i = 0 while True: if i == 0: root = trace.newspan(tracer, None, 'root') i += 1 if i == 10: root.finish() i = 0 r = None try: # span = trace.newspan(tracer, root.context, 'pushdata') span = trace.newspan(tracer, None, 'pushdata') sspan = trace.newspan(tracer, span.context, 'ask data') res = trace.inject_in_header(tracer, span, {}) r = API.call_api(data_server, data_port, '/state', res) # print(r.content) j = json.loads(r.content.decode()) sspan.finish() sspan = trace.newspan(tracer, sspan.context, 'push tea')