| 1 |
# Copyright (c) Microsoft Corporation. |
| 2 |
# Licensed under the MIT License |
| 3 |
import json |
| 4 |
import uuid |
| 5 |
from datetime import datetime |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
|
| 8 |
if TYPE_CHECKING: |
| 9 |
from azure.kusto.ingest import BlobDescriptor, IngestionProperties |
| 10 |
|
| 11 |
|
| 12 |
class IngestionBlobInfo: |
| 13 |
def __init__( |
| 14 |
self, |
| 15 |
blob_descriptor: "BlobDescriptor", |
| 16 |
ingestion_properties: "IngestionProperties", |
| 17 |
auth_context=None, |
| 18 |
application_for_tracing=None, |
| 19 |
client_version_for_tracing=None, |
| 20 |
): |
| 21 |
self.properties = dict() |
| 22 |
self.properties["BlobPath"] = blob_descriptor.path |
| 23 |
if blob_descriptor.size: |
| 24 |
self.properties["RawDataSize"] = blob_descriptor.size |
| 25 |
self.properties["DatabaseName"] = ingestion_properties.database |
| 26 |
self.properties["TableName"] = ingestion_properties.table |
| 27 |
self.properties["RetainBlobOnSuccess"] = True |
| 28 |
self.properties["FlushImmediately"] = ingestion_properties.flush_immediately |
| 29 |
self.properties["IgnoreSizeLimit"] = False |
| 30 |
self.properties["ReportLevel"] = ingestion_properties.report_level.value |
| 31 |
self.properties["ReportMethod"] = ingestion_properties.report_method.value |
| 32 |
self.properties["SourceMessageCreationTime"] = datetime.utcnow().isoformat() |
| 33 |
self.properties["Id"] = str(blob_descriptor.source_id) |
| 34 |
self.properties["ApplicationForTracing"] = application_for_tracing |
| 35 |
self.properties["ClientVersionForTracing"] = client_version_for_tracing |
| 36 |
|
| 37 |
additional_properties = ingestion_properties.additional_properties or {} |
| 38 |
additional_properties["authorizationContext"] = auth_context |
| 39 |
|
| 40 |
tags = [] |
| 41 |
if ingestion_properties.additional_tags: |
| 42 |
tags.extend(ingestion_properties.additional_tags) |
| 43 |
if ingestion_properties.drop_by_tags: |
| 44 |
tags.extend(["drop-by:" + drop for drop in ingestion_properties.drop_by_tags]) |
| 45 |
if ingestion_properties.ingest_by_tags: |
| 46 |
tags.extend(["ingest-by:" + ingest for ingest in ingestion_properties.ingest_by_tags]) |
| 47 |
if tags: |
| 48 |
additional_properties["tags"] = _convert_list_to_json(tags) |
| 49 |
if ingestion_properties.ingest_if_not_exists: |
| 50 |
additional_properties["ingestIfNotExists"] = _convert_list_to_json(ingestion_properties.ingest_if_not_exists) |
| 51 |
if ingestion_properties.ingestion_mapping: |
| 52 |
json_string = _convert_dict_to_json(ingestion_properties.ingestion_mapping) |
| 53 |
additional_properties["ingestionMapping"] = json_string |
| 54 |
|
| 55 |
if ingestion_properties.ingestion_mapping_reference: |
| 56 |
additional_properties["ingestionMappingReference"] = ingestion_properties.ingestion_mapping_reference |
| 57 |
if ingestion_properties.ingestion_mapping_type: |
| 58 |
additional_properties["ingestionMappingType"] = ingestion_properties.ingestion_mapping_type.value |
| 59 |
if ingestion_properties.validation_policy: |
| 60 |
additional_properties["ValidationPolicy"] = _convert_dict_to_json(ingestion_properties.validation_policy) |
| 61 |
if ingestion_properties.format: |
| 62 |
additional_properties["format"] = ingestion_properties.format.kusto_value |
| 63 |
if ingestion_properties.ignore_first_record: |
| 64 |
additional_properties["ignoreFirstRecord"] = ingestion_properties.ignore_first_record |
| 65 |
|
| 66 |
if additional_properties: |
| 67 |
self.properties["AdditionalProperties"] = additional_properties |
| 68 |
|
| 69 |
def to_json(self): |
| 70 |
"""Converts this object to a json string""" |
| 71 |
return _convert_list_to_json(self.properties) |
| 72 |
|
| 73 |
|
| 74 |
def _convert_list_to_json(array): |
| 75 |
"""Converts array to a json string""" |
| 76 |
return json.dumps(array, skipkeys=False, allow_nan=False, indent=None, separators=(",", ":")) |
| 77 |
|
| 78 |
|
| 79 |
def _convert_dict_to_json(array): |
| 80 |
"""Converts array to a json string""" |
| 81 |
return json.dumps(array, skipkeys=False, allow_nan=False, indent=None, separators=(",", ":"), sort_keys=True, default=lambda o: o.__dict__) |