import json import os import sys from datetime import datetime import boto3 from sme_logger import get_logger from aws_utils import AwsUtils from const import APP_NAME, ENV, aws, chartmetric from utility import Utilities # - - - - Variables for Performance Logs - - - - # process_type = "Historical File Split" last_run = Utilities().get_last_run(process_type) parent_id = Utilities().get_last_run("Fan Metrics") start_time = datetime.today().strftime("%Y-%m-%d %H:%M:%S") today = datetime.today().strftime("%Y-%m-%d") # - - - - Variables for Performance Logs - - - - # logger = get_logger(APP_NAME, os.environ.get("ENVIRONMENT", ENV).lower() != ENV) logger.debug(f"Running FanMetrics in Environment :{ENV}") logger.debug("Bucket used : {0}".format( aws.get("s3", None).get("BUCKET_HISTORICAL_PRE_QUARANTINE", None))) if len(sys.argv) > 1: report_date = sys.argv[1] prefix = f"{aws.get('s3', None).get('HISTORICAL_BUCKET_PATH', None).format(chartmetric.get('CHARTMETRIC_VERSION', None),report_date, chartmetric.get('REPORT_LICENSOR', None))}" else: prefix = f"{aws.get('s3', None).get('BUCKET_PATH', None)}/" class HistoricalFileSplit(object): def __init__(self): self.s3 = boto3.resource("s3") self.bucket = self.s3.Bucket( aws.get("s3", None).get("BUCKET_HISTORICAL_PRE_QUARANTINE", None)) try: self.files_list = AwsUtils().get_s3_files( bucket=aws.get("s3", None).get("BUCKET_HISTORICAL_PRE_QUARANTINE", None), prefix=prefix, ) except Exception as e: logger.error(f"Prefix not available in bucket: {e}") self.files_list = [] self.my_data = {} self.date_stamp = "" def split_files(self): if len(self.files_list ) == 0: # Check if there is any file at all in Bucket logger.warning(f"No files found in bucket to be split") return for obj in self.bucket.objects.filter(Prefix=prefix): logger.info(f"Currently working on : {obj}") for line in obj.get()["Body"]._raw_stream: json_content = json.loads(line) if f"{json_content['timestp']}" not in self.my_data: self.my_data[f"{json_content['timestp']}"] = [] self.my_data[f"{json_content['timestp']}"].append(json_content) # Filter the Key, Values in data based on timestamps to create seperate files / folders for key, value in self.my_data.items(): date_now = datetime.today().strftime("%H-%M-%S") folder = str( key ) # Folder name to be set based on chartmetric timestamp for items in value: new_items = json.dumps(items) self.date_stamp += f"{new_items}\n" # String needs to be converted into bytes to be dumped in S3 main_data = bytes(self.date_stamp, "utf-8") self.s3.Object( aws.get("s3", None).get("BUCKET_QUARANTINE", None), f"chartmetric/{chartmetric.get('CHARTMETRIC_VERSION', None)}/" f"report_date={folder}/" f"report_licensor={chartmetric.get('REPORT_LICENSOR', None)}/{ENV}-delphi-chartmetric_stream_{folder}_{date_now}.json", ).put(Body=main_data) self.date_stamp = "" logger.info(f"File split completed for: {folder}") self.my_data.clear() client = boto3.client("s3") copy_source = { "Bucket": f"{ENV}-delphi-chartmetric-hist-data-decompressed", "Key": obj.key, } client.copy_object( CopySource=copy_source, Bucket=f"{ENV}-delphi-chartmetric-hist-data-decompressed", Key=f"processed_files/{obj.key}", ) client.delete_object( Bucket=f"{ENV}-delphi-chartmetric-hist-data-decompressed", Key=obj.key) logger.info(f"File moved to processed files: {obj.key}") if __name__ == "__main__": HistoricalFileSplit().split_files() end_time = datetime.today().strftime("%Y-%m-%d %H:%M:%S") Utilities().insert_performance_logs(process_type, f"{last_run} + 1", start_time, end_time, parent_id) time_delta_format = "%Y-%m-%d %H:%M:%S" time_delta = datetime.strptime(end_time, time_delta_format) - datetime.strptime( start_time, time_delta_format) logger.info( f"{process_type}: Start time: {start_time}, End Time: {end_time}, Total Execution Time: {time_delta}" )