import pandas as pd import datetime import json # Creating the Dates used to rename the files at the end of the process today = datetime.date.today() first = today.replace(day=1) last_month = first - datetime.timedelta(days=1) year = last_month.strftime("%Y") month = last_month.strftime("%m") # Dictionary to map month numbers to month names months_dict = { '01': 'jan', '02': 'feb', '03': 'mar', '04': 'apr', '05': 'may', '06': 'jun', '07': 'jul', '08': 'aug', '09': 'sep', '10': 'oct', '11': 'nov', '12': 'dec' } # Update month name using the dictionary month_name = months_dict.get(month) # Retrieving DataFrame df = pd.read_csv('license_usage_report.csv') # Dropping last 5 rows df.drop(df.tail(5).index, inplace=True) # Convert "Time" column to datetime format with specified format df['Time'] = pd.to_datetime(df['Time'], format='%Y-%m-%d %H:%M:%S (%Z)') # Filter DataFrame based on year and month filtered_df = df[(df['Time'].dt.year == int(year)) & (df['Time'].dt.month == int(month))] # Initialize an empty list to store the formatted rows formatted_rows = [] # filtered_df.to_csv("license_usage_report_08_2024.csv", index=False) # Iterate over each row in the filtered DataFrame for index, row in filtered_df.iterrows(): # Extract the values from the columns time = row['Time'] user = row['Number of Users'] tenant = row['Tenant Name'] # Format the row as a JSON object with a comma at the end formatted_row = '\n{"time":"' + str(time) + '", "users":' + str(user) + ', "tenant_name":"' + str(tenant) + '"},' formatted_rows.append(formatted_row) # Remove the comma and newline from the last row if formatted_rows: formatted_rows[-1] = formatted_rows[-1][:-2] # Remove the last two characters, which are the comma and newline # Construct the JSON array json_data = '[' + ''.join(formatted_rows) + '}\n]' # Request user whether it's FK or SYD region = input('Is the region FK or SYD? ') list_region = ['FK', 'fk', 'SYD', 'syd'] if region not in list_region: print('Error ecountered, check script and fix the issue...') exit() else: match region: case 'FK': region = 'fk' case 'SYD': region = 'syd' case 'fk': region = 'fk' case 'syd': region = 'syd' # Save the .csv file filtered_df.to_csv('usage_' + month_name + '_' + year + '_' + region + '.csv', index=False) # Write the JSON data to a file with open('usage_' + month_name + '_' + year + '_' + region + '.json', 'w') as json_file: json_file.write(json_data) # Re-loads the file array = json.loads(json_data) # Setting variables t = '' sum = 0 max = [] # Saves a list of UNIQUE tenant names ten = [{"tenant": array[0]["tenant_name"], "check": False}] b = False for i in range(len(array)): for j in range(len(ten)): if ten[j]["tenant"] == array[i]["tenant_name"]: b = True if not b: tmp = {"tenant": array[i]["tenant_name"], "check": False} ten.append(tmp) b = False # Sums of the concurrent tenants per each timeframe, used for the next step to find the maximum licence usage for i in range(len(array)): if t == array[i]["time"]: for j in range(len(ten)): if ten[j]["tenant"] == array[i]["tenant_name"]: if not ten[j]["check"]: sum += array[i]["users"] ten[j]["check"] = True else: t = array[i]["time"] temp = {"users": sum, "time": array[i]["time"]} max.append(temp) for j in range(len(ten)): ten[j]["check"] = False for j in range(len(ten)): if ten[j]["tenant"] == array[i]["tenant_name"]: if not ten[j]["check"]: sum = array[i]["users"] ten[j]["check"] = True # This line will just find the maximum licence usage res = None tmp = 0 for i in range(len(max)): if tmp < max[i]["users"]: res = max[i] tmp = max[i]["users"] # Prints the maximum of connected licence on the platform print("This is the max --> ", res['users']) filtered_df.to_csv("usage.csv", index=False)