Exemple #1
0
def get_all_channels():
    """ Restish return all channels information """

    dao = DAO()
    if request.method == 'GET':

        session = dao.get_session()

        channel_table = dao.get_table("channels")

        the_channels = {"channels": []}

        for channel in session.query(Channel).order_by(
                channel_table.c.chan_id):
            LOGGER.info("channel = %s" % (channel))
            if channel:
                the_channels["channels"].append(channel.jsonize())

        session.close()

        return jsonify(the_channels)

    elif request.method == 'POST':
        data = request.json
        return jsonify(result=_add_jsonized_products(dao.get_session(), data))
Exemple #2
0
def get_all_files_for_product(uid):
    """ manage files in a product. POST add a new file, PUT update an existing one, GET get a file """

    dao = DAO()
    #get products
    if request.method == 'GET':
        result = {"files": []}

        session = dao.get_session()

        product = session.query(Product).filter_by(internal_id=uid).first()

        if product:

            for t_file in product.file_infos:
                result['files'].append(t_file.jsonize())

        session.close()
        return jsonify(result)

    #insert new products
    elif request.method == 'POST':
        data = request.json
        session = dao.get_session()
        res = _add_new_file_product(dao.get_session(), uid, data)

        return jsonify(result=res)
    #update existing products
    elif request.method == 'PUT':
        data = request.json

        res = _update_files_in_product(dao.get_session(), uid, data)

        return jsonify(result=res)
Exemple #3
0
def get_all_servicedirs():
    """ Restish return all servicedirs information """

    dao = DAO()
    if request.method == 'GET':

        session = dao.get_session()

        servicedirs_table = dao.get_table("service_dirs")

        the_result = {"service_dirs": []}

        probe_t1 = time.time()
        for servdir in session.query(ServiceDir).order_by(
                servicedirs_table.c.serv_id).options(joinedload('channel')):
            the_result["service_dirs"].append(servdir.jsonize())
        probe_t2 = time.time()

        LOGGER.info("sql request and jsonizing time %f\n" %
                    (probe_t2 - probe_t1))

        session.close()

        return jsonify(the_result)

    elif request.method == 'POST':
        data = request.json
        return jsonify(result=_add_jsonized_products(dao.get_session(), data))
Exemple #4
0
def func_update_jsonised_products():
    """ update jsonized products """
    dao = DAO()

    session = dao.get_session()

    f = open('/homespace/gaubert/ecli-workspace/rodd/etc/json/product_example')

    product_dict = json.loads(f.read())

    #_add_jsonized_products(session, product_dict)

    # update product
    for prod in product_dict.get('products', []):

        prod['name'] = 'title with time %s' % (datetime.datetime.now())
        prod['description'] = 'my description with time %s' % (
            datetime.datetime.now())

        retrieved_prod = session.query(Product).filter_by(
            internal_id='TEST:EO:EUM:DAT:METOP:ASCSZR1B').first()

        retrieved_prod.update(prod)

        #update the product
        if retrieved_prod:
            retrieved_prod.internal_id = 'TEST:EO:EUM:DAT:METOP:ASCSZRIB'
            retrieved_prod.title = prod['name']
            retrieved_prod.description = prod['description']

            print("retrieved prod modified = %s\n" % (retrieved_prod))

            session.add(retrieved_prod)
            session.commit()
Exemple #5
0
def manager_servicedir_with(name):
    """ Restish get_channels per name """

    dao = DAO()
    if request.method == 'GET':
        # show the user profile for that user
        session = dao.get_session()

        servicedirs_table = dao.get_table("service_dirs")

        the_result = {"service_dirs": []}

        #look for stars in uid and replace them with % for a like sql operation
        if name.find('*'):
            if len(name) == 1:
                #get everything because user asked for *
                for servdir in session.query(ServiceDir).order_by(
                        servicedirs_table.c.serv_id):
                    the_result["service_dirs"].append(servdir.jsonize())
            else:
                #restrict to the wildcard matching string
                name = name.replace('*', '%')
                for servdir in session.query(ServiceDir).filter(
                        ServiceDir.name.like(name)).order_by(
                            servicedirs_table.c.serv_id):
                    the_result["service_dirs"].append(servdir.jsonize())
        else:
            servdir = session.query(ServiceDir).filter_by(name=name).first()
            if servdir:
                the_result["service_dirs"].append(servdir.jsonize())

        return jsonify(the_result)

    elif request.method == 'DELETE':
        session = dao.get_session()
        servdir = session.query(ServiceDir).filter_by(name=name).first()

        if servdir:
            session.delete(servdir)
            session.commit()
            result = {
                "status": "OK",
                "messages": "service_dir %s deleted" % (name)
            }
        else:
            result = {
                "status": "KO",
                "messages": "service_dir %s not in database" % (name)
            }

        return jsonify(result)
Exemple #6
0
def manage_channel_with(name):
    """ Restish get_channels per name """

    dao = DAO()
    if request.method == 'GET':
        # show the user profile for that user

        session = dao.get_session()

        the_result = {"channels": []}

        #look for stars in uid and replace them with % for a like sql operation
        if name.find('*'):
            if len(name) == 1:
                channel_table = dao.get_table("channels")
                #get everything because user asked for *
                for channel in session.query(Channel).order_by(
                        channel_table.c.rodd_id):
                    the_result["channels"].append(channel.jsonize())
            else:
                #restrict to the wildcard matching string
                name = name.replace('*', '%')
                for channel in session.query(Channel).filter(
                        Channel.name.like(name)):
                    the_result["channels"].append(channel.jsonize())
        else:
            channel = session.query(Channel).filter_by(name=name).first()
            if channel:
                the_result["channels"].append(channel.jsonize())

        return jsonify(the_result)

    elif request.method == 'DELETE':
        session = dao.get_session()
        channel = session.query(Channel).filter_by(name=name).first()

        if channel:
            session.delete(channel)
            session.commit()
            result = {
                "status": "OK",
                "messages": ["channel %s deleted" % (name)]
            }
        else:
            result = {
                "status": "KO",
                "messages": ["channel %s not in database" % (name)]
            }

        return jsonify(result)
Exemple #7
0
def manage_product_with(uid):
    """ Restish get_product per uid """

    dao = DAO()
    if request.method == 'GET':
        # show the user profile for that user

        session = dao.get_session()

        the_products = {"products": []}

        #look for stars in uid and replace them with % for a like sql operation
        if uid.find('*'):
            if len(uid) == 1:
                product_table = dao.get_table("products")
                #get everything because user asked for *
                for product in session.query(Product).order_by(
                        product_table.c.rodd_id):
                    the_products["products"].append(product.jsonize())
            else:
                #restrict to the wildcard matching string
                uid = uid.replace('*', '%')
                for product in session.query(Product).filter(
                        Product.internal_id.like(uid)):
                    the_products["products"].append(product.jsonize())
        else:
            product = session.query(Product).filter_by(internal_id=uid).first()
            if product:
                the_products["products"].append(product.jsonize())

        return jsonify(the_products)

    elif request.method == 'DELETE':
        session = dao.get_session()
        product = session.query(Product).filter_by(internal_id=uid).first()

        if product:
            session.delete(product)
            session.commit()
            result = {
                "status": "OK",
                "messages": ["product %s deleted" % (uid)]
            }
        else:
            result = {
                "status": "KO",
                "messages": ["product %s not in database" % (uid)]
            }

        return jsonify(result)
Exemple #8
0
def get_all_products():
    """ Restish return all products information """

    #get products
    dao = DAO()
    if request.method == 'GET':
        session = dao.get_session()

        product_table = dao.get_table("products")

        the_products = {"products": []}

        probe_t1 = time.time()
        res = session.query(Product).order_by(product_table.c.rodd_id).options(
            joinedload('file_infos'))
        probe_t2 = time.time()

        LOGGER.info("#### sql request %f\n" % (probe_t2 - probe_t1))

        probe_t1 = time.time()
        for product in res:
            the_products["products"].append(product.jsonize())

        probe_t2 = time.time()

        LOGGER.info("#### creating products %f\n" % (probe_t2 - probe_t1))

        session.close()
        probe_t1 = time.time()
        json_res = jsonify(the_products)
        probe_t2 = time.time()

        LOGGER.info("#### jsonify %f\n" % (probe_t2 - probe_t1))

        return json_res
    #insert new products
    elif request.method == 'POST':
        data = request.json
        session = dao.get_session()
        res = _add_jsonized_products(session, data)

        LOGGER.info("products = %s" % (res))

        return jsonify(result=res)
    #update existing products: the policy is delete current object and add the new one
    elif request.method == 'PUT':
        data = request.json
        return jsonify(
            result=_update_jsonized_products(dao.get_session(), data))
Exemple #9
0
def func_return_jsonised_products():
    """ return a product that has been jsonised """

    # query all products
    dao = DAO()

    session = dao.get_session()

    product_table = dao.get_table("products")

    products = {"products": []}
    for product in session.query(Product).order_by(product_table.c.rodd_id):
        products["products"].append(product.jsonize())

    print("products = %s\n" % (products))

    session.close()
Exemple #10
0
def main():

    dao = DAO()

    session = dao.get_session()

    #result = session.query(Channel).filter_by(name='EPS-4').first()

    ch = Channel("MyName1", "124.124.123.2", 5, 34, "myFunction")

    if not session.query(Channel).filter_by(name='MyName1').first():
        session.add(ch)

        session.commit()

    result = session.query(Channel).filter_by(name='MyName1').first()

    print(result)
Exemple #11
0
def func_get_product_index_test():

    dao = DAO()

    session = dao.get_session()
    product_table = dao.get_table("products")
    retrieved_prod = session.query(Product).filter_by(
        internal_id='TEST:EO:EUM:DAT:METOP:ASCSZRIB').first()

    print(retrieved_prod.jsonize())

    tuple = retrieved_prod.contains_file('ascat_bufr')
    if tuple:
        grp_list, file = tuple

        file.reg_expr = 'alalalla'
        file.size = '900KB'
        print(file)
        #session.add(file)
        session.commit()
Exemple #12
0
def delete_files_for_product(uid, name):
    """ delete files for a specific product """
    dao = DAO()
    session = dao.get_session()

    product = session.query(Product).filter_by(internal_id=uid).first()

    messages = []

    if product:
        # be sure that the file is within the product
        if product.contains_file(name):
            finfo = session.query(FileInfo).filter_by(name=name).first()

            if finfo:

                session.delete(finfo)
                session.commit()

                return jsonify({
                    "status":
                    "OK",
                    "messages":
                    "FileInfo %s removed from the database" % (name)
                })
            else:
                messages.append("FileInfo %s doesn't exist" % (name))

        else:
            messages.append("Product %s doesn't contain the file info %s" %
                            (uid, name))

    else:
        messages.append("Product %s doesn't exist" % (uid))

    return jsonify({"status": "KO", "messages": messages})
Exemple #13
0
def func_jsonised_test():
    """ func test """

    f = open('/homespace/gaubert/ecli-workspace/rodd/etc/json/product_example')

    product2 = f.read()

    prod_dir = json.loads(product2)

    a_dao = DAO()

    session = a_dao.get_session()

    #add channels if there are any
    channels = prod_dir.get('channels', [])

    for chan in channels:
        #if it doesn't exist create it
        if not session.query(Channel).filter_by(name=chan['name']).first():
            session.add(
                Channel(chan['name'], chan['multicast_address'],
                        chan['min_rate'], chan['max_rate'],
                        chan['channel_function']))

    service_dirs = prod_dir.get('service_dirs', [])

    for serv_dir in service_dirs:
        if not session.query(ServiceDir).filter_by(
                name=serv_dir['name']).first():
            ch = session.query(Channel).filter_by(
                name=serv_dir['channel']).first()
            session.add(ServiceDir(serv_dir['name'], ch))

    products = prod_dir.get('products', [])

    for prod in products:
        if not session.query(Product).filter_by(
                internal_id='EO:EUM:DAT:METOP:ASCSZR1B').first():
            product = Product(prod['name'], prod['uid'], prod['description'],
                              True if prod['distribution'] else False,
                              "Operational")

            file_dict = {}

            for a_file in prod['eumetcast-info']['files']:

                #create file object
                finfo = FileInfo(  a_file["name"], \
                                   a_file.get("regexpr", ""), \
                                   a_file["size"], \
                                   a_file["type"])

                #add serviceDirs if there are any
                serv_dir_names = a_file.get("service_dir", None)

                for serv_dir_name in serv_dir_names:
                    service_d = session.query(ServiceDir).filter_by(
                        name=serv_dir_name).first()
                    finfo.service_dirs.append(service_d)

                product.eumetcast_infos.append(finfo)

                file_dict[finfo.name] = finfo

            for a_file in prod['gts-info']['files']:

                #look for existing file-info
                finfo = session.query(FileInfo).filter_by(
                    name=a_file['name']).first()
                if not finfo:
                    finfo = file_dict.get(a_file['name'], None)
                    if not finfo:
                        #create file object
                        finfo = FileInfo(a_file["name"], \
                                           a_file.get("regexpr", ""), \
                                           a_file["size"], \
                                           a_file["type"])

                product.gts_infos.append(finfo)

            for a_file in prod['data-centre-info']['files']:

                #look for existing file-info
                finfo = session.query(FileInfo).filter_by(
                    name=a_file['name']).first()
                if not finfo:
                    finfo = file_dict.get(a_file['name'], None)
                    if not finfo:
                        #create file object
                        finfo = FileInfo(  a_file["name"], \
                                           a_file.get("regexpr", ""), \
                                           a_file["size"], \
                                           a_file["type"])

                product.data_centre_infos.append(finfo)

            session.add(product)

    session.commit()

    session.close()