Spaces:
Running
Running
put in the customer rate
Browse files- gamification/logic.py +74 -1
- gamification/objects.py +25 -1
- gamification/routes.py +8 -0
- utils.py +2 -0
gamification/logic.py
CHANGED
@@ -1,11 +1,47 @@
|
|
1 |
|
2 |
-
from gamification.objects import UserFeedback, UserLevel, UserPoints
|
3 |
from bson import ObjectId
|
4 |
from pymongo import MongoClient
|
5 |
import os
|
6 |
from typing import List
|
7 |
from dotenv import load_dotenv
|
|
|
8 |
load_dotenv()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
MONGO_URI = os.getenv("MONGO_URI")
|
10 |
|
11 |
|
@@ -208,3 +244,40 @@ def get_all_feedback_func() -> List[UserFeedback]:
|
|
208 |
|
209 |
|
210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
+
from gamification.objects import UserFeedback, UserLevel, UserPoints,CustomerInfo,Customer
|
3 |
from bson import ObjectId
|
4 |
from pymongo import MongoClient
|
5 |
import os
|
6 |
from typing import List
|
7 |
from dotenv import load_dotenv
|
8 |
+
from datetime import datetime,timedelta
|
9 |
load_dotenv()
|
10 |
+
|
11 |
+
# Normal Math
|
12 |
+
def caculate_rate_change_func(c0,c1,days_ago):
|
13 |
+
if c0 == 0:
|
14 |
+
if c1 > 0:
|
15 |
+
# If there are customers now, but no customers initially (c0 is 0),
|
16 |
+
# we consider it as infinite growth or 100% growth
|
17 |
+
return {"daysAgo":days_ago,"totalCustomers": c1, "GrowthRate": 100, "GrowthRateType": "positive"}
|
18 |
+
elif c1 == 0:
|
19 |
+
# If both c0 and c1 are zero, there is no change
|
20 |
+
return {"daysAgo":days_ago,"totalCustomers": c1, "GrowthRate": 0.0, "GrowthRateType": "neutral"}
|
21 |
+
else:
|
22 |
+
# This case is for when c1 < 0, but it's unlikely in a customer count scenario.
|
23 |
+
return {"daysAgo":days_ago,"totalCustomers": c1, "GrowthRate": -100, "GrowthRateType": "negative"}
|
24 |
+
|
25 |
+
elif c1 > c0:
|
26 |
+
# Positive growth rate: c1 > c0
|
27 |
+
e = c1 - c0
|
28 |
+
b = e / c0
|
29 |
+
d = b * 100
|
30 |
+
return {"daysAgo":days_ago,"totalCustomers": c1, "GrowthRate": d, "GrowthRateType": "positive"}
|
31 |
+
|
32 |
+
elif c1 < c0:
|
33 |
+
# Negative growth rate: c1 < c0
|
34 |
+
e = c0 - c1
|
35 |
+
b = e / c0
|
36 |
+
d = b * 100
|
37 |
+
return {"daysAgo":days_ago,"totalCustomers": c1, "GrowthRate": d, "GrowthRateType": "negative"}
|
38 |
+
|
39 |
+
elif c1 == c0:
|
40 |
+
# No change: c1 == c0
|
41 |
+
return {"daysAgo":days_ago,"totalCustomers": c1, "GrowthRate": 0.0, "GrowthRateType": "neutral"}
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
MONGO_URI = os.getenv("MONGO_URI")
|
46 |
|
47 |
|
|
|
244 |
|
245 |
|
246 |
|
247 |
+
|
248 |
+
def get_all_customer_info()->List[CustomerInfo]:
|
249 |
+
db_uri = "mongodb+srv://groupcresearchseminar:[email protected]/?retryWrites=true&w=majority&appName=Cluster0"
|
250 |
+
db_name = "crayonics"
|
251 |
+
collection_name = "users"
|
252 |
+
client = MongoClient(db_uri)
|
253 |
+
db = client[db_name]
|
254 |
+
collection = db[collection_name]
|
255 |
+
|
256 |
+
# Fetch all documents from the collection
|
257 |
+
customer_cursor = collection.find() # This returns a cursor to the documents
|
258 |
+
|
259 |
+
# Convert the cursor to a list of Customer objects, setting a default date_Joined if it's missing
|
260 |
+
customers = []
|
261 |
+
customer_info = []
|
262 |
+
for customer in customer_cursor:
|
263 |
+
# If date_Joined is missing, add the default value (current datetime)
|
264 |
+
if 'date_Joined' not in customer:
|
265 |
+
print("adding a new date")
|
266 |
+
customer['date_Joined'] = datetime.now()
|
267 |
+
collection.update_one(filter={"_id":ObjectId(customer['_id'])},update={"$set":customer})
|
268 |
+
|
269 |
+
customers.append(Customer(**customer)) # Create Customer model with the filled data
|
270 |
+
dt_now = datetime.now()
|
271 |
+
thirty_days_ago = dt_now - timedelta(days=30)
|
272 |
+
sixty_days_ago = dt_now - timedelta(days=60)
|
273 |
+
ninty_days_ago = dt_now - timedelta(days=90)
|
274 |
+
all_customer_from_30_days_ago = [customer for customer in customers if customer.date_Joined <=thirty_days_ago]
|
275 |
+
all_customer_from_60_days_ago = [customer for customer in customers if customer.date_Joined <=sixty_days_ago]
|
276 |
+
all_customer_from_90_days_ago = [customer for customer in customers if customer.date_Joined <=ninty_days_ago]
|
277 |
+
rate_30_days = caculate_rate_change_func(c0=len(all_customer_from_30_days_ago),c1=len(customers),days_ago=30)
|
278 |
+
rate_60_days = caculate_rate_change_func(c0=len(all_customer_from_60_days_ago),c1=len(customers),days_ago=60)
|
279 |
+
rate_90_days = caculate_rate_change_func(c0=len(all_customer_from_90_days_ago),c1=len(customers),days_ago=90)
|
280 |
+
list_of_rates= [rate_30_days,rate_60_days,rate_90_days]
|
281 |
+
for rate in list_of_rates:
|
282 |
+
customer_info.append(CustomerInfo(**rate))
|
283 |
+
return customer_info
|
gamification/objects.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
from pydantic import BaseModel
|
2 |
from bson import ObjectId
|
3 |
class UserLevel(BaseModel):
|
@@ -29,7 +30,30 @@ class UserFeedback(BaseModel):
|
|
29 |
json_encoders = {
|
30 |
ObjectId: str
|
31 |
}
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
class IndividualUserLevel(BaseModel):
|
34 |
totalpoints:int
|
35 |
userId:str
|
|
|
1 |
+
from datetime import datetime
|
2 |
from pydantic import BaseModel
|
3 |
from bson import ObjectId
|
4 |
class UserLevel(BaseModel):
|
|
|
30 |
json_encoders = {
|
31 |
ObjectId: str
|
32 |
}
|
33 |
+
|
34 |
+
class Customer(BaseModel):
|
35 |
+
email:str
|
36 |
+
first_name:str
|
37 |
+
last_name:str
|
38 |
+
date_Joined: datetime
|
39 |
+
class Config:
|
40 |
+
json_encoders = {
|
41 |
+
ObjectId: str
|
42 |
+
}
|
43 |
+
|
44 |
+
|
45 |
+
|
46 |
+
class CustomerInfo(BaseModel):
|
47 |
+
totalCustomers:int
|
48 |
+
GrowthRate: float
|
49 |
+
GrowthRateType:str
|
50 |
+
daysAgo:int
|
51 |
+
class Config:
|
52 |
+
json_encoders = {
|
53 |
+
ObjectId: str
|
54 |
+
}
|
55 |
+
|
56 |
+
|
57 |
class IndividualUserLevel(BaseModel):
|
58 |
totalpoints:int
|
59 |
userId:str
|
gamification/routes.py
CHANGED
@@ -36,6 +36,14 @@ def create_feedback(user_feedback:UserFeedback)->bool:
|
|
36 |
def get_leaderboard_details():
|
37 |
pass
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
@gamification.get("/get-feedback",tags=["admin"])
|
41 |
def get_feedback()->List[UserFeedback]:
|
|
|
36 |
def get_leaderboard_details():
|
37 |
pass
|
38 |
|
39 |
+
@gamification.get("/get-customer-info",tags=["admin"])
|
40 |
+
def get_customer_information():
|
41 |
+
try:
|
42 |
+
result = get_all_customer_info()
|
43 |
+
return result
|
44 |
+
except Exception as e:
|
45 |
+
raise HTTPException(status_code=500,detail=f"{e}")
|
46 |
+
|
47 |
|
48 |
@gamification.get("/get-feedback",tags=["admin"])
|
49 |
def get_feedback()->List[UserFeedback]:
|
utils.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
from bson import ObjectId
|
2 |
import json
|
|
|
3 |
import requests
|
4 |
from pymongo import MongoClient
|
5 |
from password import *
|
@@ -137,6 +138,7 @@ def create_user(db_uri: str, db_name: str, collection_name: str, document: dict)
|
|
137 |
s = collection.find_one({"email":document.get('email')})
|
138 |
password = hash_password(document.get('password'))
|
139 |
document['password']= password
|
|
|
140 |
if s==None:
|
141 |
result = collection.insert_one(document)
|
142 |
|
|
|
1 |
from bson import ObjectId
|
2 |
import json
|
3 |
+
from datetime import datetime
|
4 |
import requests
|
5 |
from pymongo import MongoClient
|
6 |
from password import *
|
|
|
138 |
s = collection.find_one({"email":document.get('email')})
|
139 |
password = hash_password(document.get('password'))
|
140 |
document['password']= password
|
141 |
+
document['date_Joined'] = datetime.now()
|
142 |
if s==None:
|
143 |
result = collection.insert_one(document)
|
144 |
|