redundancy / main.py
MFF212's picture
Update main.py
a49b336
from fastapi import FastAPI, File, UploadFile
import csv
from typing import List
app = FastAPI()
# Dictionaries to store URLs, API counts, and associated names
url_counts = {}
api_counts = {}
url_names = {}
api_names = {}
@app.post("/upload/")
async def upload_csv(file: UploadFile):
if file.content_type == "text/csv":
content = await file.read()
content = content.decode('utf-8').splitlines()
total_records = 0
csvreader = csv.reader(content)
header = next(csvreader) # Skip the header row
for row in csvreader:
url = row[3] # Assuming the URL is in the 4th column (index 3)
api = row[4] # Assuming the API is in the 5th column (index 4)
url_name = row[2] # Assuming the name associated with URLs is in the 3rd column (index 2)
api_name = row[6] # Assuming the name associated with APIs is in the 7th column (index 6)
total_records += 1
# Count URLs and associated names
if url in url_counts:
url_counts[url] += 1
else:
url_counts[url] = 1
url_names[url] = url_name
# Count APIs and associated names
if api in api_counts:
api_counts[api] += 1
else:
api_counts[api] = 1
api_names[api] = api_name
# Find redundant URLs and APIs with counts greater than 1
redundant_urls = [url for url, count in url_counts.items() if count > 1]
redundant_apis = [api for api, count in api_counts.items() if count > 1]
# Calculate the percentage of redundant URLs and APIs
percentage_redundant_urls = (len(redundant_urls) / total_records) * 100 if total_records > 0 else 0
return {
"Total GET Records": total_records,
"% of Redundant GET Requests": round(percentage_redundant_urls, 2)
}
else:
return {"error": "Invalid file format. Please upload a CSV file."}