|
from fastapi import FastAPI, File, UploadFile |
|
from fastapi.responses import HTMLResponse |
|
import csv |
|
from typing import List |
|
from io import StringIO |
|
|
|
app = FastAPI() |
|
|
|
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) |
|
for row in csvreader: |
|
url = row[3] |
|
api = row[4] |
|
url_name = row[2] |
|
api_name = row[6] |
|
total_records += 1 |
|
|
|
if url in url_counts: |
|
url_counts[url] += 1 |
|
else: |
|
url_counts[url] = 1 |
|
url_names[url] = url_name |
|
|
|
if api in api_counts: |
|
api_counts[api] += 1 |
|
else: |
|
api_counts[api] = 1 |
|
api_names[api] = api_name |
|
|
|
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] |
|
|
|
percentage_redundant_urls = (len(redundant_urls) / total_records) * 100 if total_records > 0 else 0 |
|
|
|
summary_string = f"<b>Total GET Records:</b> {total_records}<br>" \ |
|
f"<b>% of Redundant GET Requests,you can save :</b> {round(percentage_redundant_urls, 2)}%<br>" |
|
|
|
html_table = "<table>" |
|
html_table += "<tr><th>GET Request</th><th>Count of Repetition</th></tr>" |
|
for url in redundant_urls: |
|
count = url_counts[url] |
|
html_table += f"<tr><td>{url}</td><td>{count}</td></tr>" |
|
html_table += "</table>" |
|
|
|
return HTMLResponse(content=f"<h2>Analysis Results:</h2>{summary_string}{html_table}", |
|
status_code=200) |
|
else: |
|
return {"error": "Invalid file format. Please upload a CSV file."} |
|
|