MFF212 commited on
Commit
6e52221
·
1 Parent(s): c9f0af5

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +20 -9
main.py CHANGED
@@ -1,10 +1,11 @@
1
  from fastapi import FastAPI, File, UploadFile
2
  from fastapi.responses import HTMLResponse
3
  import csv
 
 
4
 
5
  app = FastAPI()
6
 
7
- # Dictionaries to store URLs, API counts, and associated names
8
  url_counts = {}
9
  api_counts = {}
10
  url_names = {}
@@ -18,22 +19,34 @@ async def upload_csv(file: UploadFile):
18
  total_records = 0
19
 
20
  csvreader = csv.reader(content)
21
- header = next(csvreader) # Skip the header row
22
  for row in csvreader:
23
- # ... (rest of the processing code remains the same)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- # Find redundant URLs and APIs with counts greater than 1
26
  redundant_urls = [url for url, count in url_counts.items() if count > 1]
27
  redundant_apis = [api for api, count in api_counts.items() if count > 1]
28
 
29
- # Calculate the percentage of redundant URLs
30
  percentage_redundant_urls = (len(redundant_urls) / total_records) * 100 if total_records > 0 else 0
31
 
32
- # Construct the summary string
33
  summary_string = f"<b>Total GET Records:</b> {total_records}<br>" \
34
  f"<b>% of Redundant GET Requests:</b> {round(percentage_redundant_urls, 2)}%<br>"
35
 
36
- # Create an HTML table in the desired format
37
  html_table = "<table>"
38
  html_table += "<tr><th>GET Request</th><th>Count of Repetition</th></tr>"
39
  for url in redundant_urls:
@@ -41,9 +54,7 @@ async def upload_csv(file: UploadFile):
41
  html_table += f"<tr><td>{url}</td><td>{count}</td></tr>"
42
  html_table += "</table>"
43
 
44
- # Return both JSON and HTML responses
45
  return HTMLResponse(content=f"<h2>Analysis Results:</h2>{summary_string}{html_table}",
46
  status_code=200)
47
  else:
48
  return {"error": "Invalid file format. Please upload a CSV file."}
49
-
 
1
  from fastapi import FastAPI, File, UploadFile
2
  from fastapi.responses import HTMLResponse
3
  import csv
4
+ from typing import List
5
+ from io import StringIO
6
 
7
  app = FastAPI()
8
 
 
9
  url_counts = {}
10
  api_counts = {}
11
  url_names = {}
 
19
  total_records = 0
20
 
21
  csvreader = csv.reader(content)
22
+ header = next(csvreader)
23
  for row in csvreader:
24
+ url = row[3]
25
+ api = row[4]
26
+ url_name = row[2]
27
+ api_name = row[6]
28
+ total_records += 1
29
+
30
+ if url in url_counts:
31
+ url_counts[url] += 1
32
+ else:
33
+ url_counts[url] = 1
34
+ url_names[url] = url_name
35
+
36
+ if api in api_counts:
37
+ api_counts[api] += 1
38
+ else:
39
+ api_counts[api] = 1
40
+ api_names[api] = api_name
41
 
 
42
  redundant_urls = [url for url, count in url_counts.items() if count > 1]
43
  redundant_apis = [api for api, count in api_counts.items() if count > 1]
44
 
 
45
  percentage_redundant_urls = (len(redundant_urls) / total_records) * 100 if total_records > 0 else 0
46
 
 
47
  summary_string = f"<b>Total GET Records:</b> {total_records}<br>" \
48
  f"<b>% of Redundant GET Requests:</b> {round(percentage_redundant_urls, 2)}%<br>"
49
 
 
50
  html_table = "<table>"
51
  html_table += "<tr><th>GET Request</th><th>Count of Repetition</th></tr>"
52
  for url in redundant_urls:
 
54
  html_table += f"<tr><td>{url}</td><td>{count}</td></tr>"
55
  html_table += "</table>"
56
 
 
57
  return HTMLResponse(content=f"<h2>Analysis Results:</h2>{summary_string}{html_table}",
58
  status_code=200)
59
  else:
60
  return {"error": "Invalid file format. Please upload a CSV file."}