Open BVishal-Work opened 1 month ago
@rykahsay Could you please give me more idea on what is request_count_total
and request_count_frontend
mean in the CSV file (api_request_stats_monthly.csv)?
Additionally, are you capturing old users using any cookies from your script?
@jeet-vora I have developed a code which can separate frontend, backend, frontend_without_bots, Unique_ip's (Which tells the users based on the recurrent ip's).
Code:
import json import csv from collections import defaultdict
def read_json_and_generate_csv(open_data_path, output_csv_path): with open(open_data_path, 'r') as json_file: json_data = json.load(json_file) # Load the JSON data from the file
# Dictionary to store the counts
api_counts = defaultdict(lambda: {'backend_hits': 0, 'frontend_hits': 0, 'frontend_hits_no_bots': 0, 'frontend_hits_no_bots_unique_ips': set()})
for obj in json_data:
if isinstance(obj, dict) and 'api' in obj:
api = obj['api']
origin = obj['headers'].get('origin', None)
is_bot = obj['headers'].get('is_bot', False)
ip = obj['headers'].get('ip', None)
if origin is None:
api_counts[api]['backend_hits'] += 1
else:
api_counts[api]['frontend_hits'] += 1
if not is_bot:
api_counts[api]['frontend_hits_no_bots'] += 1
if ip:
api_counts[api]['frontend_hits_no_bots_unique_ips'].add(ip)
# Write the counts to a CSV file
with open(output_csv_path, 'w', newline='') as csvfile:
fieldnames = ['api', 'backend_hits', 'frontend_hits', 'frontend_hits_no_bots', 'frontend_hits_no_bots_unique_ips']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for api, counts in api_counts.items():
writer.writerow(
{
'api': api,
'backend_hits': counts['backend_hits'],
'frontend_hits': counts['frontend_hits'],
'frontend_hits_no_bots': counts['frontend_hits_no_bots'],
'frontend_hits_no_bots_unique_ips': len(counts['frontend_hits_no_bots_unique_ips'])
}
)
open_data_path = "/Users/vishal/Desktop/logs/src/json/c_request-2024-07-21.json" # Paste the JSON file path from your local
output_csv_path = "/Users/vishal/Desktop/logs/src/json/api_hits_unique_ip.csv" # Path to the output CSV file
read_json_and_generate_csv(open_data_path, output_csv_path)
@jeet-vora I am successful in accessing the server and downloading the CSV's. Any clue what are we exactly capturing request_count_total and request_count_frontend ?