Spaces:
Paused
Paused
File size: 16,691 Bytes
b70ff07 20adca1 b70ff07 20adca1 b70ff07 20adca1 1f8ac0c 20adca1 |
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
from datetime import datetime, timedelta
from sqlalchemy import func, and_, select, extract
from ..db.database import db
from ..utils.cache import cache
from ..db.models import StaffActivity, PerformanceMetric, User, ActivityType
from typing import Dict, List, Any, Optional
import numpy as np
from collections import defaultdict
class AnalyticsService:
@staticmethod
async def get_sales_analytics(start_date: datetime, end_date: datetime) -> Dict[str, Any]:
cache_key = f"sales_analytics:{start_date.date()}:{end_date.date()}"
cached_data = await cache.get_cache(cache_key)
if cached_data:
return cached_data
pipeline = [
{
"$match": {
"created_at": {
"$gte": start_date,
"$lte": end_date
},
"status": {"$in": ["completed", "delivered"]}
}
},
{
"$group": {
"_id": {"$dateToString": {"format": "%Y-%m-%d", "date": "$created_at"}},
"total_sales": {"$sum": "$total_amount"},
"order_count": {"$sum": 1}
}
},
{"$sort": {"_id": 1}}
]
sales_data = await db.db["orders"].aggregate(pipeline).to_list(None)
result = {
"daily_sales": sales_data,
"total_revenue": sum(day["total_sales"] for day in sales_data),
"total_orders": sum(day["order_count"] for day in sales_data),
"average_order_value": sum(day["total_sales"] for day in sales_data) /
(sum(day["order_count"] for day in sales_data) or 1)
}
await cache.set_cache(cache_key, result, expire=3600) # Cache for 1 hour
return result
@staticmethod
async def get_product_analytics() -> Dict[str, Any]:
cache_key = "product_analytics"
cached_data = await cache.get_cache(cache_key)
if cached_data:
return cached_data
pipeline = [
{
"$unwind": "$products"
},
{
"$group": {
"_id": "$products.product_id",
"total_quantity": {"$sum": "$products.quantity"},
"total_revenue": {
"$sum": {
"$multiply": ["$products.price", "$products.quantity"]
}
}
}
},
{
"$sort": {"total_revenue": -1}
},
{
"$limit": 10
}
]
top_products = await db.db["orders"].aggregate(pipeline).to_list(None)
# Get product details
for product in top_products:
product_detail = await db.db["products"].find_one({"_id": product["_id"]})
if product_detail:
product["name"] = product_detail["name"]
product["category"] = product_detail["category"]
result = {
"top_products": top_products,
"total_products": await db.db["products"].count_documents({}),
"low_stock_products": await db.db["products"].count_documents({"inventory_count": {"$lt": 10}})
}
await cache.set_cache(cache_key, result, expire=3600) # Cache for 1 hour
return result
@staticmethod
async def get_customer_analytics() -> Dict[str, Any]:
cache_key = "customer_analytics"
cached_data = await cache.get_cache(cache_key)
if cached_data:
return cached_data
pipeline = [
{
"$group": {
"_id": "$customer_id",
"total_orders": {"$sum": 1},
"total_spent": {"$sum": "$total_amount"},
"last_order": {"$max": "$created_at"}
}
},
{
"$sort": {"total_spent": -1}
}
]
customer_data = await db.db["orders"].aggregate(pipeline).to_list(None)
result = {
"total_customers": len(customer_data),
"top_customers": customer_data[:10],
"average_customer_value": sum(c["total_spent"] for c in customer_data) / (len(customer_data) or 1),
"customer_segments": {
"high_value": len([c for c in customer_data if c["total_spent"] > 1000]),
"medium_value": len([c for c in customer_data if 500 <= c["total_spent"] <= 1000]),
"low_value": len([c for c in customer_data if c["total_spent"] < 500])
}
}
await cache.set_cache(cache_key, result, expire=3600) # Cache for 1 hour
return result
class StaffAnalyticsService:
@staticmethod
async def record_activity(
user_id: int,
branch_id: int,
activity_type: ActivityType,
details: dict,
duration: Optional[float] = None
) -> StaffActivity:
"""Record a staff activity with performance scoring and notifications"""
# Calculate performance score based on activity type and details
score = None
if activity_type == ActivityType.SALE:
# Score based on sale amount and speed
amount = details.get('amount', 0)
duration = details.get('duration', 0) # duration in minutes
if duration > 0:
score = min((amount / 100) * (5 / duration), 10) # Max score of 10
elif activity_type == ActivityType.CUSTOMER_SERVICE:
# Score based on interaction quality
satisfaction = details.get('customer_satisfaction', 0)
resolution_time = details.get('resolution_time', 0)
if resolution_time > 0:
score = min((satisfaction * 2) * (10 / resolution_time), 10)
async with db() as session:
activity = StaffActivity(
user_id=user_id,
branch_id=branch_id,
activity_type=activity_type,
details=details,
duration=duration,
performance_score=score
)
session.add(activity)
await session.commit()
await session.refresh(activity)
# Get current metrics before update
prev_metrics = await StaffAnalyticsService._get_current_metrics(user_id, branch_id)
# Update daily performance metrics
new_metrics = await StaffAnalyticsService._update_performance_metrics(
user_id, branch_id, activity
)
return activity, prev_metrics, new_metrics
@staticmethod
async def _get_current_metrics(user_id: int, branch_id: int) -> Optional[Dict[str, Any]]:
"""Get current day's metrics for a user"""
today = datetime.utcnow().date()
async with db() as session:
stmt = select(PerformanceMetric).where(
and_(
PerformanceMetric.user_id == user_id,
PerformanceMetric.branch_id == branch_id,
func.date(PerformanceMetric.metric_date) == today
)
)
result = await session.execute(stmt)
metric = result.scalar_one_or_none()
if metric:
return {
"total_sales": metric.total_sales,
"transaction_count": metric.transaction_count,
"void_count": metric.void_count,
"efficiency_score": metric.efficiency_score,
"customer_interaction_count": metric.customer_interaction_count
}
return None
@staticmethod
async def _update_performance_metrics(
user_id: int,
branch_id: int,
activity: StaffActivity
) -> Dict[str, Any]:
"""Update daily performance metrics based on new activity"""
today = datetime.utcnow().date()
async with db() as session:
# Get or create today's metrics
stmt = select(PerformanceMetric).where(
and_(
PerformanceMetric.user_id == user_id,
PerformanceMetric.branch_id == branch_id,
func.date(PerformanceMetric.metric_date) == today
)
)
result = await session.execute(stmt)
metric = result.scalar_one_or_none()
if not metric:
metric = PerformanceMetric(
user_id=user_id,
branch_id=branch_id,
metric_date=datetime.utcnow()
)
session.add(metric)
# Update metrics based on activity type
if activity.activity_type == ActivityType.SALE:
metric.total_sales += activity.details.get('amount', 0)
metric.transaction_count += 1
metric.average_transaction_value = metric.total_sales / metric.transaction_count
elif activity.activity_type == ActivityType.VOID:
metric.void_count += 1
elif activity.activity_type == ActivityType.CUSTOMER_SERVICE:
metric.customer_interaction_count += 1
elif activity.activity_type in [ActivityType.LOGIN, ActivityType.LOGOUT]:
if activity.duration:
metric.login_time += activity.duration
# Calculate efficiency score
# Weight different factors in the score
weights = {
'sales': 0.4,
'speed': 0.2,
'accuracy': 0.2,
'customer_service': 0.2
}
# Calculate component scores
sales_score = min((metric.total_sales / 1000) * 10, 10) # Scale sales to 0-10
speed_score = 10 * (1 - (metric.login_time / (8 * 60))) # Assuming 8-hour day
accuracy_score = 10 * (1 - (metric.void_count / max(metric.transaction_count, 1)))
cs_score = min((metric.customer_interaction_count / 10) * 10, 10)
metric.efficiency_score = (
(sales_score * weights['sales']) +
(speed_score * weights['speed']) +
(accuracy_score * weights['accuracy']) +
(cs_score * weights['customer_service'])
)
await session.commit()
await session.refresh(metric)
return {
"total_sales": metric.total_sales,
"transaction_count": metric.transaction_count,
"average_transaction_value": metric.average_transaction_value,
"void_count": metric.void_count,
"customer_interaction_count": metric.customer_interaction_count,
"login_time": metric.login_time,
"efficiency_score": metric.efficiency_score
}
@staticmethod
async def get_staff_performance(
branch_id: Optional[int] = None,
user_id: Optional[int] = None,
start_date: Optional[datetime] = None,
end_date: Optional[datetime] = None
) -> Dict[str, Any]:
"""Get comprehensive staff performance metrics"""
if not start_date:
start_date = datetime.utcnow() - timedelta(days=30)
if not end_date:
end_date = datetime.utcnow()
cache_key = f"staff_performance:{branch_id}:{user_id}:{start_date.date()}:{end_date.date()}"
cached_data = await cache.get_cache(cache_key)
if cached_data:
return cached_data
async with db() as session:
# Base query conditions
conditions = [
PerformanceMetric.metric_date.between(start_date, end_date)
]
if branch_id:
conditions.append(PerformanceMetric.branch_id == branch_id)
if user_id:
conditions.append(PerformanceMetric.user_id == user_id)
# Get aggregated metrics
metrics_stmt = select(
PerformanceMetric.user_id,
func.sum(PerformanceMetric.total_sales).label('total_sales'),
func.sum(PerformanceMetric.transaction_count).label('transaction_count'),
func.avg(PerformanceMetric.average_transaction_value).label('avg_transaction_value'),
func.sum(PerformanceMetric.void_count).label('void_count'),
func.sum(PerformanceMetric.customer_interaction_count).label('customer_interactions'),
func.sum(PerformanceMetric.login_time).label('total_login_time'),
func.avg(PerformanceMetric.efficiency_score).label('avg_efficiency_score')
).where(
and_(*conditions)
).group_by(
PerformanceMetric.user_id
)
result = await session.execute(metrics_stmt)
metrics_data = result.all()
# Get user details
user_ids = [m.user_id for m in metrics_data]
users_stmt = select(User).where(User.id.in_(user_ids))
users = (await session.execute(users_stmt)).scalars().all()
users_dict = {u.id: u for u in users}
# Format response data
performance_data = []
for metric in metrics_data:
user = users_dict.get(metric.user_id)
if user:
performance_data.append({
"user_id": user.id,
"username": user.username,
"full_name": user.full_name,
"metrics": {
"total_sales": metric.total_sales,
"transaction_count": metric.transaction_count,
"average_transaction_value": metric.avg_transaction_value,
"void_count": metric.void_count,
"customer_interactions": metric.customer_interactions,
"total_login_time": metric.total_login_time,
"efficiency_score": metric.avg_efficiency_score
}
})
# Calculate branch averages if branch_id is specified
branch_averages = None
if branch_id:
avg_stmt = select(
func.avg(PerformanceMetric.total_sales).label('avg_sales'),
func.avg(PerformanceMetric.transaction_count).label('avg_transactions'),
func.avg(PerformanceMetric.average_transaction_value).label('avg_transaction_value'),
func.avg(PerformanceMetric.efficiency_score).label('avg_efficiency')
).where(
and_(
PerformanceMetric.branch_id == branch_id,
PerformanceMetric.metric_date.between(start_date, end_date)
)
)
avg_result = await session.execute(avg_stmt)
branch_avg = avg_result.one()
branch_averages = {
"average_daily_sales": branch_avg.avg_sales,
"average_daily_transactions": branch_avg.avg_transactions,
"average_transaction_value": branch_avg.avg_transaction_value,
"average_efficiency_score": branch_avg.avg_efficiency
}
response = {
"staff_performance": performance_data,
"date_range": {
"start_date": start_date.isoformat(),
"end_date": end_date.isoformat()
}
}
if branch_averages:
response["branch_averages"] = branch_averages
# Cache the response for 1 hour
await cache.set_cache(cache_key, response, expire=3600)
return response
analytics = AnalyticsService()
staff_analytics = StaffAnalyticsService() |