示例#1
0
def upload_data(data):
    formatted_version = '_'.join(factorio_version().split('.'))
    storage_filename = 'factorio-data/v{}.json.gz'.format(formatted_version)

    data_blob = Blob(storage_filename, storage.bucket(app=actuario_app))
    data_blob.content_encoding = 'gzip'

    data_gz_bytes = gzip.compress(json.dumps(data).encode())
    with io.BytesIO(data_gz_bytes) as data_stream:
        data_blob.upload_from_file(data_stream,
                                   content_type='application/json')
    def _set_format_metadata(formatted: OptimizedMetricRepresentation,
                             blob: storage.Blob,
                             should_compress: bool = False) -> None:
        """Sets metadata on the Cloud Storage blob that can be used to retrieve data points from the optimized
        representation.

        This includes the ordered dimension manifest, the ordered list of value keys, and the total
        number of data points to effectively "unflatten" the flattened matrix. Also sets the 'Content-Encoding: gzip'
        header if the content is going to be compressed.
        """
        total_data_points = len(
            formatted.value_matrix[0]) if formatted.value_matrix else 0
        metadata = {
            'dimension_manifest': json.dumps(formatted.dimension_manifest),
            'value_keys': json.dumps(formatted.value_keys),
            'total_data_points': total_data_points,
        }
        blob.metadata = metadata

        if should_compress:
            blob.content_encoding = 'gzip'