Beispiel #1
0
def main(args):
    # Initialize Trace Context
    global trace_context
    trace_context = TraceContext(
        trace_id=args.get('__OW_TRACE_ID',
                          os.environ.get('__OW_TRACE_ID', generate_id())),
        service_name='fetchProductImage',
        transaction_id=os.environ.get('__OW_TRANSACTION_ID', ''),
        tracer_endpoint='192.168.178.62',
        parent_id=args.get('__PARENT_TRACE_ID', ''),
        action_trace_id=os.environ.get('__OW_TRACE_ID', ''))
    # Initialize parameters
    image_urls = args.get("imageUrls", [])
    shop_key = args.get("shopKey", "771d87188d568ddd")
    with Span(span_name='fetch_images',
              trace_context=trace_context) as wrapper_context:
        thumbnails = []
        for image_url in image_urls:
            with Span(span_name='fetch_image',
                      trace_context=trace_context,
                      parent_id=wrapper_context.id) as wrapper_image_context:
                global _WRAPPER_PARENT_SPAN_ID_
                _WRAPPER_PARENT_SPAN_ID_ = wrapper_image_context.id
                image_file = fetch_image_from_url(image_url.get("imageUrl"))
                filename = save_file_in_minio(
                    image_file, shop_key, image_url.get("externalProductId"),
                    image_url.get("order"))
                if int(image_url.get("order")) == 0:
                    thumbnails.append(filename)
        call_thumbnail_generator(thumbnails)
    return {"message": "hi"}
Beispiel #2
0
def save_file_in_minio(csv_file, shop_key):
    """
    creates a file in the minio stores 'productstore' bucket
    :param shop_key: A string that is used to specify a shop
    :param csv_file: A object that is an instance of CsvFile
    :return:
    """
    with Span(span_name='save_file_in_minio', trace_context=trace_context):
        filename = "{}:{}".format(generate_id(), shop_key)
        minio_client = Minio('{}'.format(_INTERNAL_DATA_STORE_ENDPOINT_),
                             access_key=_MINIO_ACCESS_KEY_,
                             secret_key=_MINIO_SECRET_KEY_,
                             secure=False)
        try:
            with io.BytesIO(csv_file.file) as file:
                minio_client.put_object('productstore',
                                        filename,
                                        file,
                                        csv_file.size,
                                        content_type='application/csv')
                print("published file: {}".format(filename))
                return filename
        except ResponseError as err:
            print(err)
            return "ERROR"
Beispiel #3
0
def main(args):
    try:
        # Initialize trace context
        global trace_context
        trace_context = TraceContext(
            trace_id=args.get('__OW_TRACE_ID',
                              os.environ.get('__OW_TRACE_ID', generate_id())),
            service_name='unifyFormat',
            transaction_id=os.environ.get('__OW_TRANSACTION_ID', ''),
            tracer_endpoint=_TRACER_ENDPOINT_,
            parent_id=args.get('__PARENT_TRACE_ID', ''),
            action_trace_id=os.environ.get('__OW_TRACE_ID', ''))
        # initialize parameters
        filename = str(args.get("filename"))
        shop_key = str(args.get("shopKey"))

        csv_file = get_csv_file(filename)
        products = process_products(csv_file.csv_lines(), shop_key)
        # TODO: Logic for failures (check status)
        with Span(span_name='invoke_productsApi', trace_context=trace_context):
            invoke_action("productsApi",
                          os.environ.get('__OW_API_HOST',
                                         _OPENWHISK_HOST_ENDPOINT_),
                          os.environ.get('__OW_API_KEY', _OPENWHISK_KEY_),
                          data={
                              '__OW_TRACE_ID': trace_context.trace_id,
                              'products': products
                          },
                          ignore_certs=True)
        return {
            '__OW_TRACE_ID': trace_context.trace_id,
            'products': products.get('products', [])
        }
    except Exception as e:
        return {"error": "{}".format(e)}
Beispiel #4
0
def main(args):
    try:
        # Initialize trace context
        global trace_context
        trace_context = TraceContext(
            trace_id=args.get('__OW_TRACE_ID',
                              os.environ.get('__OW_TRACE_ID', generate_id())),
            service_name='fetchCSV',
            transaction_id=os.environ.get('__OW_TRANSACTION_ID', ''),
            tracer_endpoint=_TRACER_ENDPOINT_,
            parent_id=args.get('__PARENT_TRACE_ID', ''),
            action_trace_id=os.environ.get('__OW_TRACE_ID', ''))
        # Initialize parameters
        csv_url = args.get(
            "csvUrl", "http://{}/productdata/products.csv".format(
                _EXTERNAL_DATA_STORE_ENDPOINT_))
        shop_key = args.get("shopKey", "771d87188d568ddd")
        # get csv file (CsvFile instance)
        file = fetch_csv_file(csv_url)
        # save csv file in minio and return filename
        minio_save_result = save_file_in_minio(file, shop_key)

        call_fetch_product_image_in_batches(file.csv_lines(), 2, shop_key)
        print(minio_save_result)
        return {
            "filename": str(minio_save_result),
            "shopKey": shop_key,
            "__OW_TRACE_ID": trace_context.trace_id
        }
    except Exception as e:
        print(e)
        return {'error': "could not fetch data properly {}".format(e)}
Beispiel #5
0
def main(args):
    trace_context = TraceContext(
        trace_id=args.get('__OW_TRACE_ID',
                          os.environ.get('__OW_TRACE_ID', generate_id())),
        service_name='productsApi',
        transaction_id=os.environ.get('__OW_TRANSACTION_ID', ''),
        tracer_endpoint=_TRACER_ENDPOINT_,
        parent_id=args.get('__PARENT_TRACE_ID', ''),
        action_trace_id=os.environ.get('__OW_TRACE_ID', ''))
    with Span(span_name='publish_products', trace_context=trace_context):
        result = {'message': 'Hello World!'}
    return result