File size: 2,012 Bytes
02848dc
1364580
02848dc
6e52221
 
b2a551b
 
 
02848dc
 
 
 
b2a551b
 
02848dc
 
 
 
 
 
 
6e52221
02848dc
6e52221
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02848dc
 
 
 
 
1364580
c9f0af5
4cd0cca
c9f0af5
 
 
2b07ff3
 
c9f0af5
 
77cda31
c9f0af5
77cda31
02848dc
1364580
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
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."}