Update main.py
Browse files
main.py
CHANGED
|
@@ -1,87 +1,65 @@
|
|
| 1 |
-
from fastapi import FastAPI,
|
| 2 |
-
from fastapi.responses import HTMLResponse
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
import requests
|
| 7 |
-
import time
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
<form action="/upload/" enctype="multipart/form-data" method="post">
|
| 17 |
-
<input name="file" type="file">
|
| 18 |
-
<input type="submit">
|
| 19 |
-
</form>
|
| 20 |
-
</body>
|
| 21 |
-
</html>
|
| 22 |
-
"""
|
| 23 |
|
| 24 |
@app.post("/upload/")
|
| 25 |
-
async def
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
async def view_result():
|
| 76 |
-
try:
|
| 77 |
-
with open("result.html", "r") as html_file:
|
| 78 |
-
content = html_file.read()
|
| 79 |
-
return HTMLResponse(content=content)
|
| 80 |
-
except FileNotFoundError:
|
| 81 |
-
return HTMLResponse(content="Result HTML not found")
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import HTMLResponse # Import HTMLResponse class
|
| 3 |
+
import csv
|
| 4 |
+
from typing import List
|
| 5 |
+
from io import StringIO # Import StringIO to create an in-memory CSV file
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# Dictionaries to store URLs, API counts, and associated names
|
| 10 |
+
url_counts = {}
|
| 11 |
+
api_counts = {}
|
| 12 |
+
url_names = {}
|
| 13 |
+
api_names = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
@app.post("/upload/")
|
| 16 |
+
async def upload_csv(file: UploadFile):
|
| 17 |
+
if file.content_type == "text/csv":
|
| 18 |
+
content = await file.read()
|
| 19 |
+
content = content.decode('utf-8').splitlines()
|
| 20 |
+
total_records = 0
|
| 21 |
+
|
| 22 |
+
csvreader = csv.reader(content)
|
| 23 |
+
header = next(csvreader) # Skip the header row
|
| 24 |
+
for row in csvreader:
|
| 25 |
+
url = row[3] # Assuming the URL is in the 4th column (index 3)
|
| 26 |
+
api = row[4] # Assuming the API is in the 5th column (index 4)
|
| 27 |
+
url_name = row[2] # Assuming the name associated with URLs is in the 3rd column (index 2)
|
| 28 |
+
api_name = row[6] # Assuming the name associated with APIs is in the 7th column (index 6)
|
| 29 |
+
total_records += 1
|
| 30 |
+
|
| 31 |
+
# Count URLs and associated names
|
| 32 |
+
if url in url_counts:
|
| 33 |
+
url_counts[url] += 1
|
| 34 |
+
else:
|
| 35 |
+
url_counts[url] = 1
|
| 36 |
+
url_names[url] = url_name
|
| 37 |
+
|
| 38 |
+
# Count APIs and associated names
|
| 39 |
+
if api in api_counts:
|
| 40 |
+
api_counts[api] += 1
|
| 41 |
+
else:
|
| 42 |
+
api_counts[api] = 1
|
| 43 |
+
api_names[api] = api_name
|
| 44 |
+
|
| 45 |
+
# Find redundant URLs and APIs with counts greater than 1
|
| 46 |
+
redundant_urls = [url for url, count in url_counts.items() if count > 1]
|
| 47 |
+
redundant_apis = [api for api, count in api_counts.items() if count > 1]
|
| 48 |
+
|
| 49 |
+
# Calculate the percentage of redundant URLs
|
| 50 |
+
percentage_redundant_urls = (len(redundant_urls) / total_records) * 100 if total_records > 0 else 0
|
| 51 |
+
|
| 52 |
+
# Create an HTML table
|
| 53 |
+
html_table = "<table>"
|
| 54 |
+
html_table += "<tr><th>GET Request</th><th>Count of Repetition</th></tr>"
|
| 55 |
+
for url, count in url_counts.items():
|
| 56 |
+
html_table += f"<tr><td>{url}</td><td>{count}</td></tr>"
|
| 57 |
+
html_table += "</table>"
|
| 58 |
+
|
| 59 |
+
# Return both JSON and HTML responses
|
| 60 |
+
return HTMLResponse(content=f"<h2>Analysis Results:</h2>{html_table}<br>"
|
| 61 |
+
f"<b>Total GET Records:</b> {total_records}<br>"
|
| 62 |
+
f"<b>% of Redundant GET Requests:</b> {round(percentage_redundant_urls, 2)}%",
|
| 63 |
+
status_code=200)
|
| 64 |
+
else:
|
| 65 |
+
return {"error": "Invalid file format. Please upload a CSV file."}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|