예제 #1
0
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
예제 #2
0
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
예제 #3
0
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
예제 #4
0
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
예제 #5
0
# 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')
        json_body = [{
            "measurement": "coffeetea",
            "tags": {
                "type": "tea",
                "fill": j['fill_tea_in_progress'],
            },
            "fields": {
                "resource": j['tea_leaf'],
                "refills": 1.0 if j['fill_tea_in_progress'] else 0.0,
                "wip_produce": j['tea_in_progress'],