Ejemplo n.º 1
0
def create_concept_instance(db: PartitionedDatabase, concept_id_or_name: str,
                            body: JsonDict):
    with db.transaction() as tx:
        model = db.get_model_tx(tx, concept_id_or_name)
        properties = db.get_properties_tx(tx, concept_id_or_name)
        record = to_record(properties, body["values"])
        records = db.create_records_tx(tx,
                                       concept_id_or_name, [record],
                                       fill_missing=True)

        if not records:
            raise BadRequest(
                f"Could not create concept instance [{concept_id_or_name}]")
        record = records[0]

        # Log the created concept instance:
        x_bf_trace_id = AuditLogger.trace_id_header()

        # Emit "CreateRecord" event:
        PennsieveJobsClient.get().send_changelog_event(
            organization_id=db.organization_id,
            dataset_id=db.dataset_id,
            user_id=db.user_id,
            event=CreateRecord(id=record.id,
                               name=record.name,
                               model_id=model.id),
            trace_id=TraceId(x_bf_trace_id),
        )

        AuditLogger.get().message().append("records",
                                           str(record.id)).log(x_bf_trace_id)

        return to_concept_instance(record, model, properties), 201
Ejemplo n.º 2
0
def create_record(db: PartitionedDatabase, model_id_or_name: str,
                  body: JsonDict) -> Tuple[JsonDict, int]:
    record_values = [body["values"]]
    x_bf_trace_id = AuditLogger.trace_id_header()
    record = db.create_records(model_id_or_name, record_values)[0]

    model = db.get_model(model_id_or_name)
    if model is None:
        raise NotFound(f"Model {model_id_or_name}")

    # Emit "CreateRecord" event:
    PennsieveJobsClient.get().send_changelog_event(
        organization_id=db.organization_id,
        dataset_id=db.dataset_id,
        user_id=db.user_id,
        event=CreateRecord(id=record.id, name=record.name, model_id=model.id),
        trace_id=TraceId(x_bf_trace_id),
    )
    return record.to_dict(), 201
Ejemplo n.º 3
0
def create_records(
    db: PartitionedDatabase, model_id_or_name: str, body: List[Dict]
) -> Tuple[List[JsonDict], int]:
    x_bf_trace_id = AuditLogger.trace_id_header()
    record_values = [r["values"] for r in body]
    records = db.create_records(model_id_or_name, records=record_values)

    model = db.get_model(model_id_or_name)
    if model is None:
        raise NotFound(f"Model {model_id_or_name}")

    # Emit "CreateRecord" event:
    events = [CreateRecord(id=r.id, name=r.name, model_id=model.id) for r in records]
    PennsieveJobsClient.get().send_changelog_events(
        organization_id=db.organization_id,
        dataset_id=db.dataset_id,
        user_id=db.user_id,
        events=events,
        trace_id=TraceId(x_bf_trace_id),
    )
    return [record.to_dict() for record in records], 201
Ejemplo n.º 4
0
def create_concept_instance_batch(db: PartitionedDatabase,
                                  concept_id_or_name: str, body: JsonDict):
    with db.transaction() as tx:
        model = db.get_model_tx(tx, concept_id_or_name)
        properties = db.get_properties_tx(tx, concept_id_or_name)
        requests = [to_record(properties, req["values"]) for req in body]
        records = db.create_records_tx(tx,
                                       concept_id_or_name,
                                       requests,
                                       fill_missing=True)
        instances = [
            to_concept_instance(r, model, properties) for r in records
        ]
        if not instances:
            raise BadRequest(
                f"Could not create concept instances for [{concept_id_or_name}]"
            )

        # Log the created concept instance:
        x_bf_trace_id = AuditLogger.trace_id_header()

        # Emit "CreateRecord" events:
        PennsieveJobsClient.get().send_changelog_events(
            organization_id=db.organization_id,
            dataset_id=db.dataset_id,
            user_id=db.user_id,
            events=[
                CreateRecord(id=r.id, name=r.name, model_id=model.id)
                for r in records
            ],
            trace_id=TraceId(x_bf_trace_id),
        )

        AuditLogger.get().message().append("records",
                                           *[str(r.id) for r in records
                                             ]).log(x_bf_trace_id)

        return instances