Ejemplo n.º 1
0
    def save_premodel_dataset(self):
        '''

        This method saves the entire the dataset collection, as a json
        document, into the nosql implementation.

        '''

        # local variables
        entity = Entity()
        cursor = Collection()
        collection = self.premodel_data['properties']['collection']
        collection_adjusted = collection.lower().replace(' ', '_')
        collection_count = entity.get_collection_count(self.uid)
        document_count = cursor.query(collection_adjusted, 'count_documents')

        # enfore collection limit: oldest collection name is obtained from the
        #     sql database. Then, the corresponding collection (i.e. target) is
        #     removed from the nosql database.
        if (not self.uid and collection_count
                and collection_count['result'] >= self.max_collection
                and collection_adjusted):
            target = entity.get_collections(self.uid)['result'][0]
            cursor.query(target, 'drop_collection')
            entity.remove_entity(self.uid, target)
            collection_count = entity.get_collection_count(self.uid)
            document_count = cursor.query(collection_adjusted,
                                          'count_documents')

        # save dataset
        if (collection_adjusted and collection_count
                and collection_count['result'] < self.max_collection
                and document_count
                and document_count['result'] < self.max_document):
            current_utc = datetime.datetime.utcnow().strftime(
                "%Y-%m-%dT%H:%M:%S")
            self.premodel_data['properties']['datetime_saved'] = current_utc
            self.premodel_data['properties']['uid'] = self.uid
            document = {
                'properties': self.premodel_data['properties'],
                'dataset': self.dataset
            }

            response = cursor.query(collection_adjusted, 'insert_one',
                                    document)

        else:
            response = None

        # return result
        if response and response['error']:
            self.list_error.append(response['error'])
            return {'result': None, 'error': response['error']}

        elif response and response['result']:
            return {'result': response['result'], 'error': None}

        else:
            return {'result': None, 'error': 'no dataset provided'}
Ejemplo n.º 2
0
def remove_collection():
    '''

    This router function removes a collection, with respect to a database type.

    @collection, indicates a nosql implementation
    @entity, indicates a sql database

    '''

    if request.method == 'POST':
        # local variables
        response = None
        entity = Entity()
        collection = Collection()

        # programmatic-interface
        if request.get_json():
            r = request.get_json()
            uid = r['uid']
            type = r['type']
            cname = r['collection']

            if (cname and type == 'collection'):
                payload = {'properties.uid': uid}
                response = collection.query(cname, 'drop_collection', payload)

            elif (type == 'entity'):
                response = entity.remove_entity(uid, cname)

        # lastrowid returned must be greater than 0
        if response and response['result']:
            return json.dumps({'response': response['result']})
        else:
            return json.dumps({'response': response})
Ejemplo n.º 3
0
def remove_collection():
    '''

    This router function removes a collection, with respect to a database type.

    @collection, indicates a nosql implementation
    @entity, indicates a sql database

    '''

    if request.method == 'POST':
        # local variables
        response = None
        entity = Entity()
        collection = Collection()

        # programmatic-interface
        if request.get_json():
            r = request.get_json()
            uid = r['uid']
            type = r['type']
            cname = r['collection']

            if (cname and type == 'collection'):
                payload = {'properties.uid': uid}
                response = collection.query(cname, 'drop_collection', payload)

            elif (type == 'entity'):
                response = entity.remove_entity(uid, cname)

        # lastrowid returned must be greater than 0
        if response and response['result']:
            return json.dumps({'response': response['result']})
        else:
            return json.dumps({'response': response})
Ejemplo n.º 4
0
    def save_premodel_dataset(self):
        '''

        This method saves the entire the dataset collection, as a json
        document, into the nosql implementation.

        '''

        # local variables
        response = None
        entity = Entity()
        cursor = Collection()
        collection = self.premodel_data['properties']['collection']
        collection_adjusted = collection.lower().replace(' ', '_')
        collection_count = entity.get_collection_count(self.uid)
        document_count = cursor.query(collection_adjusted, 'count_documents')

        # enfore collection limit: oldest collection name is obtained from the
        #     sql database. Then, the corresponding collection (i.e. target) is
        #     removed from the nosql database.
        if (
            not self.uid and
            collection_count and
            collection_count['result'] >= self.max_collection and
            collection_adjusted
        ):
            target = entity.get_collections(self.uid)['result'][0]
            cursor.query(target, 'drop_collection')
            entity.remove_entity(self.uid, target)
            collection_count = entity.get_collection_count(self.uid)
            document_count = cursor.query(collection_adjusted, 'count_documents')

        # save dataset
        if (
            collection_adjusted and
            collection_count and
            collection_count['result'] < self.max_collection and
            document_count and
            document_count['result'] < self.max_document
        ):
            current_utc = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
            self.premodel_data['properties']['datetime_saved'] = current_utc

            if self.dataset:
                document = {
                    'properties': self.premodel_data['properties'],
                    'dataset': self.dataset
                }

                response = cursor.query(
                    collection_adjusted,
                    'insert_one',
                    document
                )

        else:
            response = None

        # return result
        if response and response['error']:
            self.list_error.append(response['error'])
            return {'result': None, 'error': response['error']}

        elif response and response['result']:
            return {'result': response['result'], 'error': None}

        else:
            return {'result': None, 'error': 'no dataset provided'}