Ejemplo n.º 1
0
def get_all_concept_instances(
    db: PartitionedDatabase,
    concept_id_or_name: str,
    limit: int,
    offset: int,
    order_by: Optional[str] = None,
    ascending: Optional[bool] = None,
) -> List[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)
        results = db.get_all_records_offset_tx(
            tx,
            model=model,
            limit=limit,
            offset=offset,
            fill_missing=True,
            order_by=None
            if order_by is None and ascending is None else OrderByField(
                name="created_at" if order_by is None else order_by,
                ascending=True if ascending is None else ascending,
            ),
        )

        x_bf_trace_id = AuditLogger.trace_id_header()
        record_ids = []
        instances = []
        for record in results:
            record_ids.append(str(record.id))
            instances.append(to_concept_instance(record, model, properties))

        AuditLogger.get().message().append("records",
                                           *record_ids).log(x_bf_trace_id)

        return instances
Ejemplo n.º 2
0
def publish_records_of_model(db: PartitionedDatabase, tx: Transaction,
                             model: Model, config, s3) -> FileManifest:
    """
    Export the records of a specific model.
    """
    log.info(f"Writing records for model '{model.name}'")

    output_file: OutputFile = OutputFile.csv_for_model(model.name).with_prefix(
        os.path.join(config.s3_publish_key, METADATA))

    model_properties: List[ModelProperty] = db.get_properties_tx(tx, model)

    linked_properties: List[ModelRelationship] = sorted(
        db.get_outgoing_model_relationships_tx(tx,
                                               from_model=model,
                                               one_to_many=False),
        key=lambda r: r.index or sys.maxsize,
    )

    # Construct the header list for a model:
    headers: List[str] = record_headers(model_properties, linked_properties)

    with s3_csv_writer(s3, config.s3_bucket, str(output_file),
                       headers) as writer:
        for r in db.get_all_records_offset_tx(
                tx=tx,
                model=model,
                embed_linked=True,
                fill_missing=True,
                limit=None,
        ):
            writer.writerow(record_row(r, model_properties, linked_properties))

    return output_file.with_prefix(METADATA).as_manifest(
        size_of(s3, config.s3_bucket, output_file))