-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStats.py
66 lines (54 loc) · 2.2 KB
/
Stats.py
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
62
63
64
65
66
import json
import pymongo
from helpers.Utils import get_month_from_sec
from fastapi import APIRouter
from pymongo import MongoClient
from config import settings
stats = APIRouter()
try:
client = MongoClient(f"mongodb+srv://{settings.MongoDB_username}:{settings.MongoDB_password}@{settings.MongoDB_id}/?retryWrites=true&w=majority")
db = client.test
except:
print("Connection to database failed")
@stats.get("/Profiles")
async def get_count():
return {"FreeResumeCount": db.resumes.count_documents({}), "FormattedResumeCount": db.formatted_resumes.count_documents({})}
@stats.get("/Transactions")
async def get_transactions(n):
transactions = db.transactions.find().sort("date", -1).limit(int(n))
transactions = list(transactions)
for transaction in transactions:
transaction["_id"] = str(transaction["_id"])
return {"transactions": json.dumps(list(transactions))}
@stats.get("/getStats")
async def get_stats():
try:
transactions = db.transactions.find({}).sort("date", pymongo.DESCENDING).limit(100)
lookup_year = [0]*12
insertion_year = [0]*12
list_of_transactions = []
for transaction in transactions:
transaction["_id"] = str(transaction["_id"])
list_of_transactions.append(transaction)
if transaction["type"] == "Profile Lookup":
lookup_year[get_month_from_sec(transaction["date"])] += 1
else:
insertion_year[get_month_from_sec(transaction["date"])] += 1
for l in list_of_transactions:
l["_id"] = str(l["_id"])
transactionscnt = len(list_of_transactions)
freecnt = db.resumes.count_documents({})
fillablecnt = db.formatted_resumes.count_documents({})
stats = {
"Cvscnt": freecnt+fillablecnt,
"transactionscnt": transactionscnt,
"lookup_year": lookup_year,
"insertion_year": insertion_year,
"freecnt": freecnt,
"fillablecnt": fillablecnt,
"transactions": json.dumps(list_of_transactions),
}
except Exception as e:
print(str(e))
return {"message": "Internal error", "status": 500}
return json.dumps(stats)