Пример #1
0
    def resolve_files(root, info, **args):
        t0 = dt.utcnow()
        if not root.files:
            res = []

        elif (len(info.field_asts[0].selection_set.selections) == 1
              and info.field_asts[0].selection_set.selections[0].name.value
              == 'total_count'):
            #OPTIMISE return list of Nones, if total count is the ONLY field selection
            res = FileRelationConnection(
                edges=[None for x in range(len(root.files))])
            db_metrics.put_duration(__name__, 'resolve_files[total_count]',
                                    dt.utcnow() - t0)

        elif isinstance(root.files[0], dict):
            #new form, files is list of objects
            res = [
                get_data_manager().file_relation.build_one(
                    file['file_id'], root.id, file['file_role'])
                for file in root.files
            ]
            db_metrics.put_duration(__name__, 'resolve_files[optimised]',
                                    dt.utcnow() - t0)

        else:
            #old form, files is list of strings
            res = [
                get_data_manager().file_relation.get_one(_id)
                for _id in root.files
            ]
            db_metrics.put_duration(__name__, 'resolve_files[legacy]',
                                    dt.utcnow() - t0)

        return res
Пример #2
0
    def resolve_relations(root, info, **args):
        # Transform the instance thing_ids into real instances
        t0 = dt.utcnow()
        if not root.relations:
            res = []

        elif (len(info.field_asts[0].selection_set.selections) == 1
              and info.field_asts[0].selection_set.selections[0].name.value
              == 'total_count'):
            from graphql_api.schema.file_relation import FileRelationConnection
            res = FileRelationConnection(
                edges=[None for x in range(len(root.relations))])
            db_metrics.put_duration(__name__, 'resolve_files[total_count]',
                                    dt.utcnow() - t0)

        elif isinstance(root.relations[0], dict):
            #new form, files is list of objects
            res = [
                get_data_manager().file_relation.build_one(
                    root.id, relation['id'], relation['role'])
                for relation in root.relations
            ]
            db_metrics.put_duration(__name__, 'resolve_relations[optimised]',
                                    dt.utcnow() - t0)

        else:
            #old form, files is list of strings
            res = [
                get_data_manager().file_relation.get_one(_id)
                for _id in root.relations
            ]
            db_metrics.put_duration(__name__, 'resolve_relations[legacy]',
                                    dt.utcnow() - t0)

        return res
    def mutate_and_get_payload(cls, root, info, **kwargs):
        t0 = dt.utcnow()
        type, nid = from_global_id(kwargs.get('id'))
        inversion_solution = get_data_manager().file.get_one_raw(nid)

        #inversion_solution = InversionSolution(inversion_solution)
        #TODO this is schema migration , need a cleaner way
        if not inversion_solution.get('clazz_name') == 'InversionSolution':
            print(
                f"Upgrading {inversion_solution.get('clazz_name')} to InversionSolution"
            )
            inversion_solution['clazz_name'] = 'InversionSolution'
        if not inversion_solution.get('tables'):
            inversion_solution['tables'] = []

        ##do table updates...
        for table in kwargs['tables']:
            table_relation = copy.copy(table)
            table_relation['identity'] = str(uuid.uuid4())
            table_relation['created'] = dt.now(
                datetime.timezone.utc).isoformat()
            inversion_solution['tables'].append(table_relation)

        inversion_solution = get_data_manager().file.update(
            nid, inversion_solution)
        print('inversion_solution', inversion_solution)
        db_metrics.put_duration(
            __name__, 'AppendInversionSolutionTables.mutate_and_get_payload',
            dt.utcnow() - t0)
        return AppendInversionSolutionTables(inversion_solution, ok=True)
Пример #4
0
    def resolve_inversion_solution(root, info, **args):

        if not len(root.files):
            return
        if not root.task_type == TaskSubType.INVERSION.value:
            return

        t0 = dt.utcnow()
        res = None

        # TODO this is an ugly hack....
        #  - It gets the inversion solution by traversing the file_relations until it finds an InversionSolution.
        #  - Instead this attribute needs to be a first-class one-to-one relationship
        for file_id in root.files:
            if isinstance(file_id, dict): #new form, files is list of objects
                if not file_id['file_role'] == FileRole.WRITE.value:
                    continue
                file_relation = get_data_manager().file_relation.build_one(file_id['file_id'], root.id, file_id['file_role'])
            else: #old form, files is list of strings
                file_relation = get_data_manager().file_relation.get_one(file_id)
                if not file_relation.role == FileRole.WRITE.value:
                    continue
            file = get_data_manager().file.get_one(file_relation.file_id)
            if file.__class__ == InversionSolution:
                res = file
                break
        return res
        db_metrics.put_duration(__name__, 'AutomationTask.resolve_inversion_solution' , dt.utcnow()-t0)
def resolve_node(root, info, id_field, dm_type):
    """
    Optimisation function, looks at the query and avoids a fetch if
    we only want to resolve the id field.
    """
    t0 = dt.utcnow()
    assert dm_type in ["table", "thing"]

    node_id = getattr(root, id_field)
    if not node_id:
        return

    type, nid = from_global_id(node_id)

    if len(info.field_asts[0].selection_set.selections)==1 and \
        (info.field_asts[0].selection_set.selections[0].name.value == 'id'):

        #create an instance with just it's id attribute set
        clazz = getattr(import_module('graphql_api.schema'), type)
        res = clazz(id=nid)
    else:
        res = getattr(get_data_manager(), dm_type).get_one(nid)

    db_metrics.put_duration(__name__, 'resolve_node', dt.utcnow() - t0)
    return res
Пример #6
0
 def mutate(cls, root, info, input):
     t0 = dt.utcnow()
     print("mutate: ", input)
     thing_id = input.pop('task_id')
     task_result = get_data_manager().thing.update('AutomationTask', thing_id, **input)
     db_metrics.put_duration(__name__, 'UpdateAutomationTask.mutate' , dt.utcnow()-t0)
     return UpdateAutomationTask(task_result=task_result)
Пример #7
0
 def mutate_and_get_payload(cls, root, info, **kwargs):
     t0 = dt.utcnow()
     print("mutate_and_get_payload: ", kwargs)
     table = get_data_manager().table.create('Table', **kwargs)
     db_metrics.put_duration(__name__, 'CreateFile.mutate',
                             dt.utcnow() - t0)
     return CreateTable(table=table)
Пример #8
0
 def mutate_and_get_payload(cls, root, info, **kwargs):
     t0 = dt.utcnow()
     #print("mutate_and_get_payload: ", kwargs)
     thing_id = kwargs.pop('task_id')
     general_task = get_data_manager().thing.update('GeneralTask', thing_id, **kwargs)
     #print("general_task", general_task.created)
     db_metrics.put_duration(__name__, 'UpdateGeneralTask.mutate_and_get_payload' , dt.utcnow()-t0)
     return UpdateGeneralTask(general_task=general_task, ok=True)
 def mutate_and_get_payload(cls, root, info, **kwargs):
     t0 = dt.utcnow()
     inversion_solution = get_data_manager().file.create(
         'InversionSolution', **kwargs)
     db_metrics.put_duration(
         __name__, 'CreateInversionSolution.mutate_and_get_payload',
         dt.utcnow() - t0)
     return CreateInversionSolution(inversion_solution=inversion_solution,
                                    ok=True)
 def mutate(cls, root, info, input):
     t0 = dt.utcnow()
     print("payload: ", input)
     task_result = get_data_manager().thing.create('RuptureGenerationTask',
                                                   **input)
     db_metrics.put_duration(
         __name__, 'CreateRuptureGenerationTask.mutate_and_get_payload',
         dt.utcnow() - t0)
     return CreateRuptureGenerationTask(task_result=task_result)
Пример #11
0
    def mutate(self, info, **kwargs):
        t0 = dt.utcnow()
        print("CreateTaskTaskRelation.mutate: ", kwargs)
        ftype, parent_id = from_global_id(kwargs.pop('parent_id'))
        ttype, child_id = from_global_id(kwargs.pop('child_id'))

        thing_relation = get_data_manager().thing_relation.create(
            'TaskTaskRelation', parent_id, child_id, **kwargs)
        db_metrics.put_duration(__name__, 'CreateTaskTaskRelation.mutate',
                                dt.utcnow() - t0)
        return CreateTaskTaskRelation(ok=True, thing_relation=thing_relation)
Пример #12
0
 def resolve_parents(self, info, **args):
     t0 = dt.utcnow()
     if not self.parents:
         res = []
     elif (len(info.field_asts[0].selection_set.selections)==1 and
         info.field_asts[0].selection_set.selections[0].name.value == 'total_count'):
         from graphql_api.schema.task_task_relation import TaskTaskRelationConnection
         res = TaskTaskRelationConnection(edges= [None for x in range(len(self.parents))])
     else:
         res =  [get_data_manager().thing_relation.get_one(_id) for _id in self.parents]
     db_metrics.put_duration(__name__, 'GeneralTask.resolve_parents' , dt.utcnow()-t0)
     return res
Пример #13
0
    def mutate(self, info, **kwargs):
        t0 = dt.utcnow()
        print("CreateFileRelation.mutate: ", kwargs)
        ftype, file_id = from_global_id(kwargs.pop('file_id'))
        #file = db_root.file.get_one(file_id)
        ttype, thing_id = from_global_id(kwargs.pop('thing_id'))
        #thing = db_root.thing.get_one(kwargs.pop('strong_motion_station_id'))

        file_relation = get_data_manager().file_relation.create(
            'FileRelation', thing_id, file_id, **kwargs)
        db_metrics.put_duration(__name__, 'CreateFileRelation.mutate',
                                dt.utcnow() - t0)
        return CreateFileRelation(ok=True, file_relation=file_relation)
 def get_node(cls, info, _id):
     node = get_data_manager().file.get_one(_id, "InversionSolution")
     return node
Пример #15
0
 def resolve_thing(root, info, *args, **kwargs):
     # print("THING", root.thing_id)
     return get_data_manager().thing.get_one(root.thing_id)
Пример #16
0
 def resolve_file(root, info, *args, **kwargs):
     # print("FILE", root.file_id)
     return get_data_manager().file.get_one(root.file_id)
 def get_node(cls, info, id):
     return get_data_manager().thing.get_one(id)
 def mutate_and_get_payload(cls, root, info, **kwargs):
     print("mutate_and_get_payload: ", kwargs)
     strong_motion_station = get_data_manager().thing.create(
         'StrongMotionStation', **kwargs)
     return CreateStrongMotionStation(
         strong_motion_station=strong_motion_station)
Пример #19
0
 def mutate(self, info, **kwargs):
     t0 = dt.utcnow()
     file_result = get_data_manager().file.create('File', **kwargs)
     db_metrics.put_duration(__name__, 'CreateFile.mutate',
                             dt.utcnow() - t0)
     return CreateFile(ok=True, file_result=file_result)
Пример #20
0
 def get_node(cls, info, _id):
     t0 = dt.utcnow()
     res = get_data_manager().table.get_one(_id)
     db_metrics.put_duration(__name__, 'Table.get_node', dt.utcnow() - t0)
     return res
Пример #21
0
 def get_node(cls, info, _id):
     #t0 = dt.utcnow()
     node = get_data_manager().file.get_one(_id)
     #db_metrics.put_duration(__name__, 'CreateFile.mutate' , dt.utcnow()-t0)
     return node
Пример #22
0
 def mutate_and_get_payload(cls, root, info, **kwargs):
     print("mutate_and_get_payload: ", kwargs)
     general_task = get_data_manager().thing.create('GeneralTask', **kwargs)
     return CreateGeneralTask(general_task=general_task)
 def mutate(self, info, **kwargs):
     # print("CreateFile.mutate: ", file_in, kwargs)
     file_result = get_data_manager().file.create('SmsFile', **kwargs)
     return CreateSmsFile(ok=True, file_result=file_result)
Пример #24
0
 def resolve_file_url(self, info, **args):
     return get_data_manager().file.get_presigned_url(self.id)