Spaces:
Sleeping
Sleeping
Komalpreet Kaur commited on
Commit ·
ccfe044
1
Parent(s): 35870e7
feat: implement responsive, theme-adaptive visitor analytics modal dialog box
Browse files- .env.example +1 -0
- app/api/endpoints.py +25 -0
- app/core/config.py +1 -0
- app/services/analytics.py +135 -0
- app/services/neocortex.py +32 -10
- frontend/src/App.css +16 -0
- frontend/src/App.jsx +45 -1
- frontend/src/components/VisitorAnalytics.css +576 -0
- frontend/src/components/VisitorAnalytics.jsx +154 -0
- tests/test_analytics.py +63 -0
.env.example
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
OPENAI_API_KEY=your_openai_api_key_here
|
| 2 |
REDIS_HOST=localhost
|
| 3 |
REDIS_PORT=6379
|
|
|
|
| 4 |
CHROMA_DB_PATH=./chroma_db
|
| 5 |
NEO4J_URI=bolt://localhost:7687
|
| 6 |
NEO4J_USER=neo4j
|
|
|
|
| 1 |
OPENAI_API_KEY=your_openai_api_key_here
|
| 2 |
REDIS_HOST=localhost
|
| 3 |
REDIS_PORT=6379
|
| 4 |
+
REDIS_URL=rediss://default:your-password@your-endpoint.upstash.io:port
|
| 5 |
CHROMA_DB_PATH=./chroma_db
|
| 6 |
NEO4J_URI=bolt://localhost:7687
|
| 7 |
NEO4J_USER=neo4j
|
app/api/endpoints.py
CHANGED
|
@@ -551,3 +551,28 @@ async def fetch_chat_history(current_user: str = Depends(get_current_user)):
|
|
| 551 |
return {"messages": history}
|
| 552 |
except Exception as e:
|
| 553 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 551 |
return {"messages": history}
|
| 552 |
except Exception as e:
|
| 553 |
raise HTTPException(status_code=500, detail=str(e))
|
| 554 |
+
|
| 555 |
+
|
| 556 |
+
# ── Visitor Analytics ─────────────────────────────────────────────
|
| 557 |
+
|
| 558 |
+
class HitRequest(BaseModel):
|
| 559 |
+
visitor_id: str
|
| 560 |
+
|
| 561 |
+
@router.post("/analytics/hit")
|
| 562 |
+
async def record_visitor_hit(request: HitRequest, current_user: str = Depends(get_current_user)):
|
| 563 |
+
try:
|
| 564 |
+
from app.services.analytics import analytics_manager
|
| 565 |
+
success = analytics_manager.record_hit(request.visitor_id)
|
| 566 |
+
return {"success": success, "message": "Hit recorded successfully" if success else "Hit recorded in local simulation fallback."}
|
| 567 |
+
except Exception as e:
|
| 568 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 569 |
+
|
| 570 |
+
|
| 571 |
+
@router.get("/analytics/stats")
|
| 572 |
+
async def get_visitor_stats(current_user: str = Depends(get_current_user)):
|
| 573 |
+
try:
|
| 574 |
+
from app.services.analytics import analytics_manager
|
| 575 |
+
stats = analytics_manager.get_stats()
|
| 576 |
+
return stats
|
| 577 |
+
except Exception as e:
|
| 578 |
+
raise HTTPException(status_code=500, detail=str(e))
|
app/core/config.py
CHANGED
|
@@ -19,6 +19,7 @@ class Settings(BaseSettings):
|
|
| 19 |
# Working Memory (Redis)
|
| 20 |
REDIS_HOST: str = os.getenv("REDIS_HOST", "localhost")
|
| 21 |
REDIS_PORT: int = int(os.getenv("REDIS_PORT", 6379))
|
|
|
|
| 22 |
|
| 23 |
# Episodic Memory (ChromaDB)
|
| 24 |
CHROMA_DB_PATH: str = os.getenv("CHROMA_DB_PATH", os.path.join(os.getcwd(), "data", "chroma_db"))
|
|
|
|
| 19 |
# Working Memory (Redis)
|
| 20 |
REDIS_HOST: str = os.getenv("REDIS_HOST", "localhost")
|
| 21 |
REDIS_PORT: int = int(os.getenv("REDIS_PORT", 6379))
|
| 22 |
+
REDIS_URL: str = os.getenv("REDIS_URL", "")
|
| 23 |
|
| 24 |
# Episodic Memory (ChromaDB)
|
| 25 |
CHROMA_DB_PATH: str = os.getenv("CHROMA_DB_PATH", os.path.join(os.getcwd(), "data", "chroma_db"))
|
app/services/analytics.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import redis
|
| 2 |
+
from app.core.config import settings
|
| 3 |
+
import logging
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
logger = logging.getLogger("soma.analytics")
|
| 7 |
+
|
| 8 |
+
class AnalyticsManager:
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self._client = None
|
| 11 |
+
self._connected = False
|
| 12 |
+
self._last_attempt = 0.0
|
| 13 |
+
self._cooldown = 30.0 # seconds to wait before trying to reconnect if offline
|
| 14 |
+
|
| 15 |
+
def get_client(self):
|
| 16 |
+
# Return existing connection if still healthy
|
| 17 |
+
if self._client is not None:
|
| 18 |
+
try:
|
| 19 |
+
self._client.ping()
|
| 20 |
+
self._connected = True
|
| 21 |
+
return self._client
|
| 22 |
+
except Exception:
|
| 23 |
+
logger.warning("Redis connection lost, re-establishing...")
|
| 24 |
+
self._client = None
|
| 25 |
+
self._connected = False
|
| 26 |
+
|
| 27 |
+
# If we failed to connect recently, don't block the request; return None (fallback) fast.
|
| 28 |
+
now = time.time()
|
| 29 |
+
if now - self._last_attempt < self._cooldown:
|
| 30 |
+
return None
|
| 31 |
+
|
| 32 |
+
self._last_attempt = now
|
| 33 |
+
|
| 34 |
+
# 1. Try REDIS_URL first (Upstash URL format)
|
| 35 |
+
if settings.REDIS_URL:
|
| 36 |
+
try:
|
| 37 |
+
print(f"Analytics: Connecting to Upstash Redis...")
|
| 38 |
+
# Note: socket_timeout/socket_connect_timeout are small to prevent FastAPI from hanging
|
| 39 |
+
self._client = redis.Redis.from_url(
|
| 40 |
+
settings.REDIS_URL,
|
| 41 |
+
decode_responses=True,
|
| 42 |
+
socket_timeout=3.0,
|
| 43 |
+
socket_connect_timeout=3.0,
|
| 44 |
+
retry_on_timeout=False
|
| 45 |
+
)
|
| 46 |
+
self._client.ping()
|
| 47 |
+
self._connected = True
|
| 48 |
+
print("Analytics: Connected to Upstash Redis successfully!")
|
| 49 |
+
return self._client
|
| 50 |
+
except Exception as e:
|
| 51 |
+
print(f"Analytics: Failed to connect to Upstash via REDIS_URL: {e}")
|
| 52 |
+
self._client = None
|
| 53 |
+
self._connected = False
|
| 54 |
+
|
| 55 |
+
# 2. Try default REDIS_HOST / REDIS_PORT (Local fallback)
|
| 56 |
+
if settings.REDIS_HOST:
|
| 57 |
+
try:
|
| 58 |
+
print(f"Analytics: Connecting to local Redis at {settings.REDIS_HOST}:{settings.REDIS_PORT}...")
|
| 59 |
+
self._client = redis.Redis(
|
| 60 |
+
host=settings.REDIS_HOST,
|
| 61 |
+
port=settings.REDIS_PORT,
|
| 62 |
+
decode_responses=True,
|
| 63 |
+
socket_timeout=2.0,
|
| 64 |
+
socket_connect_timeout=2.0,
|
| 65 |
+
retry_on_timeout=False
|
| 66 |
+
)
|
| 67 |
+
self._client.ping()
|
| 68 |
+
self._connected = True
|
| 69 |
+
print("Analytics: Connected to local Redis successfully!")
|
| 70 |
+
return self._client
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"Analytics: Failed to connect to local Redis: {e}")
|
| 73 |
+
self._client = None
|
| 74 |
+
self._connected = False
|
| 75 |
+
|
| 76 |
+
return None
|
| 77 |
+
|
| 78 |
+
def record_hit(self, visitor_id: str) -> bool:
|
| 79 |
+
"""
|
| 80 |
+
Record a visit hit.
|
| 81 |
+
Increments the total visits count and adds the visitor_id to the unique set.
|
| 82 |
+
Optimized for Upstash Free Tier limits by being called exactly once per session.
|
| 83 |
+
"""
|
| 84 |
+
client = self.get_client()
|
| 85 |
+
if client is None:
|
| 86 |
+
print("Analytics: Redis is offline, skipping hit.")
|
| 87 |
+
return False
|
| 88 |
+
|
| 89 |
+
try:
|
| 90 |
+
# 1. Increment total visits
|
| 91 |
+
client.incr("soma:analytics:total_visits")
|
| 92 |
+
|
| 93 |
+
# 2. Add visitor_id to unique visitors set
|
| 94 |
+
client.sadd("soma:analytics:unique_visitors", visitor_id)
|
| 95 |
+
return True
|
| 96 |
+
except Exception as e:
|
| 97 |
+
print(f"Analytics: Failed to record hit in Redis: {e}")
|
| 98 |
+
return False
|
| 99 |
+
|
| 100 |
+
def get_stats(self) -> dict:
|
| 101 |
+
"""
|
| 102 |
+
Retrieve stats (total visitors, unique visitors) from Redis.
|
| 103 |
+
Returns mock/offline status if Redis connection fails, ensuring zero crashes.
|
| 104 |
+
"""
|
| 105 |
+
client = self.get_client()
|
| 106 |
+
if client is None or not self._connected:
|
| 107 |
+
return {
|
| 108 |
+
"total_visitors": 0,
|
| 109 |
+
"unique_visitors": 0,
|
| 110 |
+
"status": "offline"
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
try:
|
| 114 |
+
# Fetch total visits
|
| 115 |
+
total_val = client.get("soma:analytics:total_visits")
|
| 116 |
+
total = int(total_val) if total_val else 0
|
| 117 |
+
|
| 118 |
+
# Fetch cardinality of unique set
|
| 119 |
+
unique = client.scard("soma:analytics:unique_visitors") or 0
|
| 120 |
+
|
| 121 |
+
return {
|
| 122 |
+
"total_visitors": total,
|
| 123 |
+
"unique_visitors": unique,
|
| 124 |
+
"status": "online"
|
| 125 |
+
}
|
| 126 |
+
except Exception as e:
|
| 127 |
+
print(f"Analytics: Failed to query stats: {e}")
|
| 128 |
+
return {
|
| 129 |
+
"total_visitors": 0,
|
| 130 |
+
"unique_visitors": 0,
|
| 131 |
+
"status": "error",
|
| 132 |
+
"detail": str(e)
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
analytics_manager = AnalyticsManager()
|
app/services/neocortex.py
CHANGED
|
@@ -105,9 +105,6 @@ def extract_and_store_knowledge(text: str, user_id: str = "default_user"):
|
|
| 105 |
|
| 106 |
owner = user_id.upper()
|
| 107 |
|
| 108 |
-
# Bind the structured Pydantic model to the Groq LLM
|
| 109 |
-
structured_llm = llm.with_structured_output(KnowledgeGraphExtraction)
|
| 110 |
-
|
| 111 |
prompt = f"""You are a child's brain learning about the world. Read the text and pick out SIMPLE facts as connections between concepts.
|
| 112 |
|
| 113 |
Think like a child drawing a mind-map:
|
|
@@ -123,20 +120,45 @@ RULES:
|
|
| 123 |
5. If the text is just greetings or small talk with zero factual content, return an empty triples list.
|
| 124 |
|
| 125 |
Text:
|
| 126 |
-
{clean}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
try:
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
print("Neocortex: No triples extracted.")
|
| 132 |
return 0
|
| 133 |
|
| 134 |
stored_count = 0
|
| 135 |
|
| 136 |
-
for t in
|
| 137 |
-
subj = str(t.subject).strip().upper()
|
| 138 |
-
rel = _sanitize_relation(str(t.relation))
|
| 139 |
-
obj = str(t.object).strip().upper()
|
| 140 |
|
| 141 |
# Validate both nodes
|
| 142 |
if not _is_valid_node(subj) or not _is_valid_node(obj):
|
|
|
|
| 105 |
|
| 106 |
owner = user_id.upper()
|
| 107 |
|
|
|
|
|
|
|
|
|
|
| 108 |
prompt = f"""You are a child's brain learning about the world. Read the text and pick out SIMPLE facts as connections between concepts.
|
| 109 |
|
| 110 |
Think like a child drawing a mind-map:
|
|
|
|
| 120 |
5. If the text is just greetings or small talk with zero factual content, return an empty triples list.
|
| 121 |
|
| 122 |
Text:
|
| 123 |
+
{clean}
|
| 124 |
+
|
| 125 |
+
Return the extracted facts ONLY as a valid JSON block in this exact format:
|
| 126 |
+
{{
|
| 127 |
+
"triples": [
|
| 128 |
+
{{"subject": "SUBJECT", "relation": "RELATION", "object": "OBJECT"}}
|
| 129 |
+
]
|
| 130 |
+
}}
|
| 131 |
+
Do not write any other explanation or thoughts outside the JSON block. If there are no facts, return: {{"triples": []}}"""
|
| 132 |
|
| 133 |
try:
|
| 134 |
+
response = llm.invoke([HumanMessage(content=prompt)])
|
| 135 |
+
content = response.content.strip()
|
| 136 |
+
|
| 137 |
+
# Robustly extract the JSON block
|
| 138 |
+
triples_data = []
|
| 139 |
+
match = re.search(r'\{.*\}', content, re.DOTALL)
|
| 140 |
+
if match:
|
| 141 |
+
json_str = match.group(0)
|
| 142 |
+
try:
|
| 143 |
+
data = json.loads(json_str)
|
| 144 |
+
triples_data = data.get("triples", [])
|
| 145 |
+
except Exception as e:
|
| 146 |
+
print(f"Neocortex: Failed to parse JSON block: {e}")
|
| 147 |
+
return 0
|
| 148 |
+
else:
|
| 149 |
+
print("Neocortex: No JSON block found in LLM response.")
|
| 150 |
+
return 0
|
| 151 |
+
|
| 152 |
+
if not triples_data:
|
| 153 |
print("Neocortex: No triples extracted.")
|
| 154 |
return 0
|
| 155 |
|
| 156 |
stored_count = 0
|
| 157 |
|
| 158 |
+
for t in triples_data:
|
| 159 |
+
subj = str(t.get("subject", "")).strip().upper()
|
| 160 |
+
rel = _sanitize_relation(str(t.get("relation", "")))
|
| 161 |
+
obj = str(t.get("object", "")).strip().upper()
|
| 162 |
|
| 163 |
# Validate both nodes
|
| 164 |
if not _is_valid_node(subj) or not _is_valid_node(obj):
|
frontend/src/App.css
CHANGED
|
@@ -124,6 +124,16 @@
|
|
| 124 |
color: #ff6b35;
|
| 125 |
}
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
.sidebar-footer {
|
| 128 |
margin-top: auto;
|
| 129 |
}
|
|
@@ -576,6 +586,12 @@ body.dark-theme .modal-header button:hover {
|
|
| 576 |
border-left-color: rgba(255,255,255,0.05) !important;
|
| 577 |
}
|
| 578 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 579 |
.sidebar-footer {
|
| 580 |
margin-top: 0 !important;
|
| 581 |
margin-left: 16px !important;
|
|
|
|
| 124 |
color: #ff6b35;
|
| 125 |
}
|
| 126 |
|
| 127 |
+
.sidebar-link.visitors-btn {
|
| 128 |
+
margin-top: 16px;
|
| 129 |
+
border-top: 1px solid rgba(0, 0, 0, 0.03);
|
| 130 |
+
padding-top: 16px !important;
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
body.dark-theme .sidebar-link.visitors-btn {
|
| 134 |
+
border-top-color: rgba(255, 255, 255, 0.05);
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
.sidebar-footer {
|
| 138 |
margin-top: auto;
|
| 139 |
}
|
|
|
|
| 586 |
border-left-color: rgba(255,255,255,0.05) !important;
|
| 587 |
}
|
| 588 |
|
| 589 |
+
.visitors-btn {
|
| 590 |
+
margin-top: 0 !important;
|
| 591 |
+
border-top: none !important;
|
| 592 |
+
padding-top: 0 !important;
|
| 593 |
+
}
|
| 594 |
+
|
| 595 |
.sidebar-footer {
|
| 596 |
margin-top: 0 !important;
|
| 597 |
margin-left: 16px !important;
|
frontend/src/App.jsx
CHANGED
|
@@ -9,6 +9,7 @@ import CognitiveDashboard from './components/CognitiveDashboard';
|
|
| 9 |
import KnowledgeInput from './components/KnowledgeInput';
|
| 10 |
import { SleepProgress } from './components/DreamSequence';
|
| 11 |
import AuthScreen from './components/AuthScreen';
|
|
|
|
| 12 |
import { apiFetch } from './api';
|
| 13 |
import './App.css';
|
| 14 |
|
|
@@ -43,6 +44,7 @@ function App() {
|
|
| 43 |
const [sleepSummary, setSleepSummary] = useState(null);
|
| 44 |
const [showStatus, setShowStatus] = useState(false);
|
| 45 |
const [darkMode, setDarkMode] = useState(localStorage.getItem('soma_dark') === 'true');
|
|
|
|
| 46 |
|
| 47 |
useEffect(() => {
|
| 48 |
if (darkMode) {
|
|
@@ -62,6 +64,36 @@ function App() {
|
|
| 62 |
return () => clearInterval(interval);
|
| 63 |
}, [username]);
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
const fetchHistory = async () => {
|
| 66 |
try {
|
| 67 |
const res = await apiFetch('/api/v1/history');
|
|
@@ -252,10 +284,18 @@ function App() {
|
|
| 252 |
</button>
|
| 253 |
))}
|
| 254 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
<button
|
| 256 |
className="sidebar-link theme-toggle-btn"
|
| 257 |
onClick={() => setDarkMode(!darkMode)}
|
| 258 |
-
style={{marginTop: '
|
| 259 |
title="Toggle Dark/Light Mode"
|
| 260 |
>
|
| 261 |
<span className="material-icons">{darkMode ? 'light_mode' : 'dark_mode'}</span>
|
|
@@ -410,6 +450,10 @@ function App() {
|
|
| 410 |
/>
|
| 411 |
)}
|
| 412 |
</main>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
</div>
|
| 414 |
);
|
| 415 |
}
|
|
|
|
| 9 |
import KnowledgeInput from './components/KnowledgeInput';
|
| 10 |
import { SleepProgress } from './components/DreamSequence';
|
| 11 |
import AuthScreen from './components/AuthScreen';
|
| 12 |
+
import VisitorAnalytics from './components/VisitorAnalytics';
|
| 13 |
import { apiFetch } from './api';
|
| 14 |
import './App.css';
|
| 15 |
|
|
|
|
| 44 |
const [sleepSummary, setSleepSummary] = useState(null);
|
| 45 |
const [showStatus, setShowStatus] = useState(false);
|
| 46 |
const [darkMode, setDarkMode] = useState(localStorage.getItem('soma_dark') === 'true');
|
| 47 |
+
const [showAnalyticsModal, setShowAnalyticsModal] = useState(false);
|
| 48 |
|
| 49 |
useEffect(() => {
|
| 50 |
if (darkMode) {
|
|
|
|
| 64 |
return () => clearInterval(interval);
|
| 65 |
}, [username]);
|
| 66 |
|
| 67 |
+
useEffect(() => {
|
| 68 |
+
if (!username) return;
|
| 69 |
+
|
| 70 |
+
// Verify if session hit is already registered to protect Upstash limits
|
| 71 |
+
if (sessionStorage.getItem('soma_hit_registered') === 'true') return;
|
| 72 |
+
|
| 73 |
+
// Get or create unique persistent visitor ID
|
| 74 |
+
let visitorId = localStorage.getItem('soma_visitor_id');
|
| 75 |
+
if (!visitorId) {
|
| 76 |
+
visitorId = 'visitor_' + Math.random().toString(36).substring(2, 15) + Date.now().toString(36);
|
| 77 |
+
localStorage.setItem('soma_visitor_id', visitorId);
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
const registerHit = async () => {
|
| 81 |
+
try {
|
| 82 |
+
const res = await apiFetch('/api/v1/analytics/hit', {
|
| 83 |
+
method: 'POST',
|
| 84 |
+
body: JSON.stringify({ visitor_id: visitorId })
|
| 85 |
+
});
|
| 86 |
+
if (res.ok) {
|
| 87 |
+
sessionStorage.setItem('soma_hit_registered', 'true');
|
| 88 |
+
}
|
| 89 |
+
} catch (error) {
|
| 90 |
+
console.error("Failed to register visitor telemetry hit:", error);
|
| 91 |
+
}
|
| 92 |
+
};
|
| 93 |
+
|
| 94 |
+
registerHit();
|
| 95 |
+
}, [username]);
|
| 96 |
+
|
| 97 |
const fetchHistory = async () => {
|
| 98 |
try {
|
| 99 |
const res = await apiFetch('/api/v1/history');
|
|
|
|
| 284 |
</button>
|
| 285 |
))}
|
| 286 |
|
| 287 |
+
<button
|
| 288 |
+
className={`sidebar-link visitors-btn ${showAnalyticsModal ? 'active' : ''}`}
|
| 289 |
+
onClick={() => setShowAnalyticsModal(true)}
|
| 290 |
+
>
|
| 291 |
+
<span className="material-icons">analytics</span>
|
| 292 |
+
<span>Visitors</span>
|
| 293 |
+
</button>
|
| 294 |
+
|
| 295 |
<button
|
| 296 |
className="sidebar-link theme-toggle-btn"
|
| 297 |
onClick={() => setDarkMode(!darkMode)}
|
| 298 |
+
style={{marginTop: '8px'}}
|
| 299 |
title="Toggle Dark/Light Mode"
|
| 300 |
>
|
| 301 |
<span className="material-icons">{darkMode ? 'light_mode' : 'dark_mode'}</span>
|
|
|
|
| 450 |
/>
|
| 451 |
)}
|
| 452 |
</main>
|
| 453 |
+
|
| 454 |
+
{showAnalyticsModal && (
|
| 455 |
+
<VisitorAnalytics onClose={() => setShowAnalyticsModal(false)} />
|
| 456 |
+
)}
|
| 457 |
</div>
|
| 458 |
);
|
| 459 |
}
|
frontend/src/components/VisitorAnalytics.css
ADDED
|
@@ -0,0 +1,576 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* VisitorAnalytics.css - Premium Adaptive Neural Gloss Traffic Diagnostics */
|
| 2 |
+
|
| 3 |
+
.analytics-stage {
|
| 4 |
+
display: flex;
|
| 5 |
+
flex-direction: column;
|
| 6 |
+
gap: 16px;
|
| 7 |
+
width: 100%;
|
| 8 |
+
color: var(--text-main);
|
| 9 |
+
transition: color 0.3s ease;
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
.analytics-header-row {
|
| 13 |
+
display: flex;
|
| 14 |
+
justify-content: space-between;
|
| 15 |
+
align-items: center;
|
| 16 |
+
flex-wrap: wrap;
|
| 17 |
+
gap: 12px;
|
| 18 |
+
border-bottom: 1px solid rgba(0, 0, 0, 0.05);
|
| 19 |
+
padding-bottom: 12px;
|
| 20 |
+
transition: border-color 0.3s ease;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
body.dark-theme .analytics-header-row {
|
| 24 |
+
border-bottom-color: rgba(255, 255, 255, 0.05);
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
.analytics-title-area h3 {
|
| 28 |
+
font-family: var(--font-display);
|
| 29 |
+
font-size: 1.2rem;
|
| 30 |
+
font-weight: 700;
|
| 31 |
+
color: #ff6b35;
|
| 32 |
+
margin: 0 0 4px 0;
|
| 33 |
+
letter-spacing: -0.01em;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
.analytics-title-area p {
|
| 37 |
+
font-size: 0.75rem;
|
| 38 |
+
color: var(--text-muted);
|
| 39 |
+
margin: 0;
|
| 40 |
+
transition: color 0.3s ease;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
.analytics-db-status .status-badge {
|
| 44 |
+
display: inline-flex;
|
| 45 |
+
align-items: center;
|
| 46 |
+
gap: 8px;
|
| 47 |
+
font-size: 0.75rem;
|
| 48 |
+
font-weight: 700;
|
| 49 |
+
text-transform: uppercase;
|
| 50 |
+
letter-spacing: 0.05em;
|
| 51 |
+
padding: 6px 14px;
|
| 52 |
+
border-radius: 20px;
|
| 53 |
+
background: rgba(0, 0, 0, 0.02);
|
| 54 |
+
border: 1px solid rgba(0, 0, 0, 0.05);
|
| 55 |
+
transition: all 0.3s ease;
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
body.dark-theme .analytics-db-status .status-badge {
|
| 59 |
+
background: rgba(255, 255, 255, 0.02);
|
| 60 |
+
border-color: rgba(255, 255, 255, 0.08);
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
.analytics-db-status .status-badge.success {
|
| 64 |
+
color: #ff6b35;
|
| 65 |
+
border-color: rgba(255, 107, 53, 0.2);
|
| 66 |
+
background: rgba(255, 107, 53, 0.03);
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
.analytics-db-status .status-badge.success .dot {
|
| 70 |
+
background: #ff6b35;
|
| 71 |
+
box-shadow: 0 0 8px #ff6b35;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
.analytics-db-status .status-badge.warning {
|
| 75 |
+
color: #f59e0b;
|
| 76 |
+
border-color: rgba(245, 158, 11, 0.2);
|
| 77 |
+
background: rgba(245, 158, 11, 0.03);
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
.analytics-db-status .status-badge.warning .dot {
|
| 81 |
+
background: #f59e0b;
|
| 82 |
+
box-shadow: 0 0 8px #f59e0b;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
.analytics-db-status .status-badge.error {
|
| 86 |
+
color: var(--text-muted);
|
| 87 |
+
border-color: var(--border-card);
|
| 88 |
+
background: rgba(0, 0, 0, 0.02);
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
body.dark-theme .analytics-db-status .status-badge.error {
|
| 92 |
+
background: rgba(255, 255, 255, 0.02);
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
.analytics-db-status .status-badge.error .dot {
|
| 96 |
+
background: #737373;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
.analytics-db-status .dot {
|
| 100 |
+
width: 6px;
|
| 101 |
+
height: 6px;
|
| 102 |
+
border-radius: 50%;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
/* Metrics Grid - Adjusted column wrapping to fit beautifully in modal viewport */
|
| 106 |
+
.analytics-grid {
|
| 107 |
+
display: grid;
|
| 108 |
+
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
|
| 109 |
+
gap: 20px;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
.metric-card {
|
| 113 |
+
position: relative;
|
| 114 |
+
display: flex;
|
| 115 |
+
align-items: center;
|
| 116 |
+
gap: 16px;
|
| 117 |
+
padding: 16px 20px;
|
| 118 |
+
border-radius: 16px;
|
| 119 |
+
background: rgba(255, 255, 255, 0.7);
|
| 120 |
+
border: 1px solid rgba(0, 0, 0, 0.04);
|
| 121 |
+
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.02);
|
| 122 |
+
backdrop-filter: blur(12px);
|
| 123 |
+
overflow: hidden;
|
| 124 |
+
transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
|
| 125 |
+
color: var(--text-main);
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
body.dark-theme .metric-card {
|
| 129 |
+
background: rgba(10, 10, 10, 0.6);
|
| 130 |
+
border-color: rgba(255, 255, 255, 0.03);
|
| 131 |
+
box-shadow: none;
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
.metric-card:hover {
|
| 135 |
+
transform: translateY(-4px);
|
| 136 |
+
border-color: rgba(0, 0, 0, 0.1);
|
| 137 |
+
box-shadow: 0 12px 30px rgba(0, 0, 0, 0.06);
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
body.dark-theme .metric-card:hover {
|
| 141 |
+
border-color: rgba(255, 255, 255, 0.08);
|
| 142 |
+
box-shadow: none;
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
.metric-card.glow-orange:hover,
|
| 146 |
+
.metric-card.glow-green:hover {
|
| 147 |
+
box-shadow: 0 12px 40px -10px rgba(255, 107, 53, 0.15);
|
| 148 |
+
border-color: rgba(255, 107, 53, 0.25);
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
.metric-icon {
|
| 152 |
+
display: flex;
|
| 153 |
+
align-items: center;
|
| 154 |
+
justify-content: center;
|
| 155 |
+
width: 56px;
|
| 156 |
+
height: 56px;
|
| 157 |
+
border-radius: 12px;
|
| 158 |
+
background: rgba(0, 0, 0, 0.02);
|
| 159 |
+
border: 1px solid rgba(0, 0, 0, 0.05);
|
| 160 |
+
transition: all 0.3s ease;
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
body.dark-theme .metric-icon {
|
| 164 |
+
background: rgba(255, 255, 255, 0.02);
|
| 165 |
+
border-color: rgba(255, 255, 255, 0.05);
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
.glow-orange .metric-icon,
|
| 169 |
+
.glow-green .metric-icon {
|
| 170 |
+
color: #ff6b35;
|
| 171 |
+
background: rgba(255, 107, 53, 0.04);
|
| 172 |
+
border-color: rgba(255, 107, 53, 0.15);
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
.metric-icon .material-icons {
|
| 176 |
+
font-size: 26px;
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
.metric-data {
|
| 180 |
+
display: flex;
|
| 181 |
+
flex-direction: column;
|
| 182 |
+
z-index: 1;
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
.metric-number {
|
| 186 |
+
font-size: 1.5rem;
|
| 187 |
+
font-weight: 800;
|
| 188 |
+
line-height: 1.1;
|
| 189 |
+
letter-spacing: -0.03em;
|
| 190 |
+
margin-bottom: 4px;
|
| 191 |
+
color: #ff6b35;
|
| 192 |
+
text-shadow: 0 0 20px rgba(255, 107, 53, 0.15);
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
.metric-label {
|
| 196 |
+
font-size: 0.65rem;
|
| 197 |
+
font-weight: 700;
|
| 198 |
+
letter-spacing: 0.1em;
|
| 199 |
+
text-transform: uppercase;
|
| 200 |
+
color: var(--text-muted);
|
| 201 |
+
margin-bottom: 4px;
|
| 202 |
+
transition: color 0.3s ease;
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
.metric-subtext {
|
| 206 |
+
font-size: 0.75rem;
|
| 207 |
+
color: var(--text-muted);
|
| 208 |
+
transition: color 0.3s ease;
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
/* Mini graphic background in cards */
|
| 212 |
+
.metric-graph-bg {
|
| 213 |
+
position: absolute;
|
| 214 |
+
bottom: 0;
|
| 215 |
+
right: 0;
|
| 216 |
+
width: 140px;
|
| 217 |
+
height: 60px;
|
| 218 |
+
opacity: 0.07;
|
| 219 |
+
pointer-events: none;
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
.mini-wave {
|
| 223 |
+
width: 100%;
|
| 224 |
+
height: 100%;
|
| 225 |
+
background: linear-gradient(180deg, transparent 40%, #ff6b35 100%);
|
| 226 |
+
clip-path: polygon(0 80%, 20% 60%, 40% 75%, 60% 40%, 80% 55%, 100% 20%, 100% 100%, 0 100%);
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
.wave-green {
|
| 230 |
+
background: linear-gradient(180deg, transparent 40%, #ff6b35 100%);
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
/* Insights Dashboard Grid */
|
| 234 |
+
.analytics-insights-grid {
|
| 235 |
+
display: grid;
|
| 236 |
+
grid-template-columns: 1fr;
|
| 237 |
+
gap: 20px;
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
.insights-panel {
|
| 241 |
+
padding: 16px 20px;
|
| 242 |
+
border-radius: 16px;
|
| 243 |
+
background: rgba(255, 255, 255, 0.5);
|
| 244 |
+
border: 1px solid rgba(0, 0, 0, 0.03);
|
| 245 |
+
backdrop-filter: blur(8px);
|
| 246 |
+
transition: all 0.3s ease;
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
body.dark-theme .insights-panel {
|
| 250 |
+
background: rgba(10, 10, 10, 0.4);
|
| 251 |
+
border-color: rgba(255, 255, 255, 0.02);
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
.insights-panel h4 {
|
| 255 |
+
font-size: 0.65rem;
|
| 256 |
+
font-weight: 800;
|
| 257 |
+
text-transform: uppercase;
|
| 258 |
+
color: var(--text-light);
|
| 259 |
+
letter-spacing: 0.1em;
|
| 260 |
+
margin: 0 0 12px 0;
|
| 261 |
+
transition: color 0.3s ease;
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
/* Ratio Bar */
|
| 265 |
+
.ratio-bar-container {
|
| 266 |
+
display: flex;
|
| 267 |
+
flex-direction: column;
|
| 268 |
+
gap: 12px;
|
| 269 |
+
margin-bottom: 16px;
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
.ratio-bar {
|
| 273 |
+
display: flex;
|
| 274 |
+
height: 8px;
|
| 275 |
+
border-radius: 4px;
|
| 276 |
+
overflow: hidden;
|
| 277 |
+
background: rgba(0, 0, 0, 0.05);
|
| 278 |
+
transition: background 0.3s ease;
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
body.dark-theme .ratio-bar {
|
| 282 |
+
background: rgba(255, 255, 255, 0.05);
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
.ratio-fill {
|
| 286 |
+
height: 100%;
|
| 287 |
+
transition: width 0.6s ease;
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
.unique-fill {
|
| 291 |
+
background: #ff6b35;
|
| 292 |
+
box-shadow: 0 0 10px rgba(255, 107, 53, 0.3);
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
.returning-fill {
|
| 296 |
+
background: #a34018;
|
| 297 |
+
box-shadow: 0 0 10px rgba(163, 64, 24, 0.3);
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
.ratio-legend {
|
| 301 |
+
display: flex;
|
| 302 |
+
gap: 16px;
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
.legend-item {
|
| 306 |
+
display: flex;
|
| 307 |
+
align-items: center;
|
| 308 |
+
gap: 8px;
|
| 309 |
+
font-size: 0.7rem;
|
| 310 |
+
color: var(--text-muted);
|
| 311 |
+
transition: color 0.3s ease;
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
.indicator-dot {
|
| 315 |
+
width: 8px;
|
| 316 |
+
height: 8px;
|
| 317 |
+
border-radius: 50%;
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
.unique-dot {
|
| 321 |
+
background: #ff6b35;
|
| 322 |
+
}
|
| 323 |
+
|
| 324 |
+
.returning-dot {
|
| 325 |
+
background: #a34018;
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
.retention-score-block {
|
| 329 |
+
display: flex;
|
| 330 |
+
align-items: center;
|
| 331 |
+
gap: 16px;
|
| 332 |
+
border-top: 1px solid rgba(0, 0, 0, 0.05);
|
| 333 |
+
padding-top: 12px;
|
| 334 |
+
transition: border-color 0.3s ease;
|
| 335 |
+
}
|
| 336 |
+
|
| 337 |
+
body.dark-theme .retention-score-block {
|
| 338 |
+
border-top-color: rgba(255, 255, 255, 0.03);
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
.retention-score {
|
| 342 |
+
font-size: 1.5rem;
|
| 343 |
+
font-weight: 800;
|
| 344 |
+
color: #ff6b35;
|
| 345 |
+
line-height: 1;
|
| 346 |
+
}
|
| 347 |
+
|
| 348 |
+
.retention-copy h5 {
|
| 349 |
+
font-size: 0.75rem;
|
| 350 |
+
font-weight: 700;
|
| 351 |
+
margin: 0 0 2px 0;
|
| 352 |
+
color: var(--text-main);
|
| 353 |
+
transition: color 0.3s ease;
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
.retention-copy p {
|
| 357 |
+
font-size: 0.75rem;
|
| 358 |
+
color: var(--text-muted);
|
| 359 |
+
margin: 0;
|
| 360 |
+
transition: color 0.3s ease;
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
/* Sync telemetries btn */
|
| 364 |
+
.analytics-action-row {
|
| 365 |
+
display: flex;
|
| 366 |
+
justify-content: flex-end;
|
| 367 |
+
}
|
| 368 |
+
|
| 369 |
+
.analytics-sync-btn {
|
| 370 |
+
display: flex;
|
| 371 |
+
align-items: center;
|
| 372 |
+
gap: 8px;
|
| 373 |
+
font-size: 0.8rem;
|
| 374 |
+
font-weight: 700;
|
| 375 |
+
color: #ff6b35;
|
| 376 |
+
background: rgba(255, 107, 53, 0.05);
|
| 377 |
+
border: 1px solid rgba(255, 107, 53, 0.15);
|
| 378 |
+
padding: 10px 18px;
|
| 379 |
+
border-radius: 8px;
|
| 380 |
+
cursor: pointer;
|
| 381 |
+
transition: all 0.2s ease;
|
| 382 |
+
}
|
| 383 |
+
|
| 384 |
+
.analytics-sync-btn:hover:not(:disabled) {
|
| 385 |
+
background: rgba(255, 107, 53, 0.1);
|
| 386 |
+
border-color: #ff6b35;
|
| 387 |
+
box-shadow: 0 0 16px rgba(255, 107, 53, 0.15);
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
.analytics-sync-btn:disabled {
|
| 391 |
+
opacity: 0.6;
|
| 392 |
+
cursor: not-allowed;
|
| 393 |
+
}
|
| 394 |
+
|
| 395 |
+
@keyframes pulse {
|
| 396 |
+
0% { transform: scale(1); opacity: 1; }
|
| 397 |
+
50% { transform: scale(1.1); opacity: 0.7; }
|
| 398 |
+
100% { transform: scale(1); opacity: 1; }
|
| 399 |
+
}
|
| 400 |
+
|
| 401 |
+
.pulse {
|
| 402 |
+
animation: pulse 2s infinite ease-in-out;
|
| 403 |
+
}
|
| 404 |
+
|
| 405 |
+
/* Modal Backdrop Overlay */
|
| 406 |
+
.analytics-modal-backdrop {
|
| 407 |
+
position: fixed;
|
| 408 |
+
top: 0;
|
| 409 |
+
left: 0;
|
| 410 |
+
width: 100vw;
|
| 411 |
+
height: 100vh;
|
| 412 |
+
background: rgba(0, 0, 0, 0.25);
|
| 413 |
+
backdrop-filter: blur(8px);
|
| 414 |
+
z-index: 9999;
|
| 415 |
+
display: flex;
|
| 416 |
+
justify-content: center;
|
| 417 |
+
align-items: center;
|
| 418 |
+
animation: fadeIn 0.2s cubic-bezier(0.16, 1, 0.3, 1) forwards;
|
| 419 |
+
transition: background-color 0.3s ease;
|
| 420 |
+
}
|
| 421 |
+
|
| 422 |
+
body.dark-theme .analytics-modal-backdrop {
|
| 423 |
+
background: rgba(0, 0, 0, 0.7);
|
| 424 |
+
}
|
| 425 |
+
|
| 426 |
+
/* Modal Content Container */
|
| 427 |
+
.analytics-modal-content {
|
| 428 |
+
background: rgba(255, 255, 255, 0.85);
|
| 429 |
+
border: 1px solid rgba(0, 0, 0, 0.05);
|
| 430 |
+
box-shadow: 0 24px 60px rgba(0, 0, 0, 0.1), 0 0 40px rgba(255, 107, 53, 0.02);
|
| 431 |
+
border-radius: 20px;
|
| 432 |
+
width: 90%;
|
| 433 |
+
max-width: 620px;
|
| 434 |
+
padding: 20px 24px;
|
| 435 |
+
position: relative;
|
| 436 |
+
backdrop-filter: blur(25px);
|
| 437 |
+
max-height: 90vh;
|
| 438 |
+
overflow-y: auto;
|
| 439 |
+
scrollbar-width: thin;
|
| 440 |
+
scrollbar-color: rgba(255, 107, 53, 0.25) transparent;
|
| 441 |
+
animation: slideUp 0.25s cubic-bezier(0.16, 1, 0.3, 1) forwards;
|
| 442 |
+
transition: background-color 0.3s ease, border-color 0.3s ease, box-shadow 0.3s ease;
|
| 443 |
+
}
|
| 444 |
+
|
| 445 |
+
body.dark-theme .analytics-modal-content {
|
| 446 |
+
background: rgba(13, 13, 13, 0.85);
|
| 447 |
+
border: 1px solid rgba(255, 255, 255, 0.08);
|
| 448 |
+
box-shadow: 0 24px 60px rgba(0, 0, 0, 0.8), 0 0 40px rgba(255, 107, 53, 0.05);
|
| 449 |
+
}
|
| 450 |
+
|
| 451 |
+
.analytics-modal-content::-webkit-scrollbar {
|
| 452 |
+
width: 4px;
|
| 453 |
+
}
|
| 454 |
+
.analytics-modal-content::-webkit-scrollbar-track {
|
| 455 |
+
background: transparent;
|
| 456 |
+
}
|
| 457 |
+
.analytics-modal-content::-webkit-scrollbar-thumb {
|
| 458 |
+
background: rgba(255, 107, 53, 0.25);
|
| 459 |
+
border-radius: 4px;
|
| 460 |
+
}
|
| 461 |
+
.analytics-modal-content::-webkit-scrollbar-thumb:hover {
|
| 462 |
+
background: rgba(255, 107, 53, 0.45);
|
| 463 |
+
}
|
| 464 |
+
|
| 465 |
+
/* Modal Close Button */
|
| 466 |
+
.modal-close-btn {
|
| 467 |
+
position: absolute;
|
| 468 |
+
top: 16px;
|
| 469 |
+
right: 16px;
|
| 470 |
+
background: transparent;
|
| 471 |
+
border: none;
|
| 472 |
+
color: var(--text-muted);
|
| 473 |
+
cursor: pointer;
|
| 474 |
+
display: flex;
|
| 475 |
+
align-items: center;
|
| 476 |
+
justify-content: center;
|
| 477 |
+
width: 32px;
|
| 478 |
+
height: 32px;
|
| 479 |
+
border-radius: 50%;
|
| 480 |
+
transition: all 0.2s ease;
|
| 481 |
+
z-index: 10;
|
| 482 |
+
}
|
| 483 |
+
|
| 484 |
+
.modal-close-btn:hover {
|
| 485 |
+
background: rgba(0, 0, 0, 0.05);
|
| 486 |
+
color: var(--text-main);
|
| 487 |
+
}
|
| 488 |
+
|
| 489 |
+
body.dark-theme .modal-close-btn:hover {
|
| 490 |
+
background: rgba(255, 255, 255, 0.05);
|
| 491 |
+
}
|
| 492 |
+
|
| 493 |
+
.modal-close-btn .material-icons {
|
| 494 |
+
font-size: 20px;
|
| 495 |
+
}
|
| 496 |
+
|
| 497 |
+
/* Animations */
|
| 498 |
+
@keyframes fadeIn {
|
| 499 |
+
from { opacity: 0; }
|
| 500 |
+
to { opacity: 1; }
|
| 501 |
+
}
|
| 502 |
+
|
| 503 |
+
@keyframes slideUp {
|
| 504 |
+
from { transform: translateY(20px); opacity: 0; }
|
| 505 |
+
to { transform: translateY(0); opacity: 1; }
|
| 506 |
+
}
|
| 507 |
+
|
| 508 |
+
/* Responsive Adaptive Viewports - Ultra Responsive stack */
|
| 509 |
+
@media (max-width: 600px) {
|
| 510 |
+
.analytics-modal-content {
|
| 511 |
+
width: 95%;
|
| 512 |
+
padding: 16px;
|
| 513 |
+
max-height: 95vh;
|
| 514 |
+
}
|
| 515 |
+
|
| 516 |
+
.analytics-stage {
|
| 517 |
+
gap: 12px;
|
| 518 |
+
}
|
| 519 |
+
|
| 520 |
+
.analytics-header-row {
|
| 521 |
+
flex-direction: column;
|
| 522 |
+
align-items: flex-start;
|
| 523 |
+
gap: 10px;
|
| 524 |
+
padding-bottom: 10px;
|
| 525 |
+
}
|
| 526 |
+
|
| 527 |
+
.analytics-db-status {
|
| 528 |
+
align-self: flex-start;
|
| 529 |
+
}
|
| 530 |
+
|
| 531 |
+
.analytics-grid {
|
| 532 |
+
grid-template-columns: 1fr;
|
| 533 |
+
gap: 12px;
|
| 534 |
+
}
|
| 535 |
+
|
| 536 |
+
.metric-card {
|
| 537 |
+
padding: 12px 16px;
|
| 538 |
+
gap: 12px;
|
| 539 |
+
}
|
| 540 |
+
|
| 541 |
+
.metric-number {
|
| 542 |
+
font-size: 1.8rem;
|
| 543 |
+
}
|
| 544 |
+
|
| 545 |
+
.metric-icon {
|
| 546 |
+
width: 48px;
|
| 547 |
+
height: 48px;
|
| 548 |
+
}
|
| 549 |
+
|
| 550 |
+
.metric-icon .material-icons {
|
| 551 |
+
font-size: 22px;
|
| 552 |
+
}
|
| 553 |
+
|
| 554 |
+
.insights-panel {
|
| 555 |
+
padding: 12px 16px;
|
| 556 |
+
}
|
| 557 |
+
|
| 558 |
+
.ratio-legend {
|
| 559 |
+
flex-direction: column;
|
| 560 |
+
gap: 8px;
|
| 561 |
+
}
|
| 562 |
+
|
| 563 |
+
.retention-score-block {
|
| 564 |
+
gap: 12px;
|
| 565 |
+
padding-top: 10px;
|
| 566 |
+
}
|
| 567 |
+
|
| 568 |
+
.retention-score {
|
| 569 |
+
font-size: 1.5rem;
|
| 570 |
+
}
|
| 571 |
+
|
| 572 |
+
.modal-close-btn {
|
| 573 |
+
top: 12px;
|
| 574 |
+
right: 12px;
|
| 575 |
+
}
|
| 576 |
+
}
|
frontend/src/components/VisitorAnalytics.jsx
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useEffect, useState } from 'react';
|
| 2 |
+
import { apiFetch } from '../api';
|
| 3 |
+
import './VisitorAnalytics.css';
|
| 4 |
+
|
| 5 |
+
function VisitorAnalytics({ onClose }) {
|
| 6 |
+
const [stats, setStats] = useState({ total_visitors: 0, unique_visitors: 0, status: 'connecting' });
|
| 7 |
+
const [loading, setLoading] = useState(true);
|
| 8 |
+
|
| 9 |
+
const fetchStats = async () => {
|
| 10 |
+
try {
|
| 11 |
+
setLoading(true);
|
| 12 |
+
const res = await apiFetch('/api/v1/analytics/stats');
|
| 13 |
+
if (res.ok) {
|
| 14 |
+
const data = await res.json();
|
| 15 |
+
setStats(data);
|
| 16 |
+
} else {
|
| 17 |
+
setStats({ total_visitors: 0, unique_visitors: 0, status: 'offline' });
|
| 18 |
+
}
|
| 19 |
+
} catch (error) {
|
| 20 |
+
console.error("Failed to fetch visitor stats:", error);
|
| 21 |
+
setStats({ total_visitors: 0, unique_visitors: 0, status: 'offline' });
|
| 22 |
+
} finally {
|
| 23 |
+
setLoading(false);
|
| 24 |
+
}
|
| 25 |
+
};
|
| 26 |
+
|
| 27 |
+
useEffect(() => {
|
| 28 |
+
fetchStats();
|
| 29 |
+
}, []);
|
| 30 |
+
|
| 31 |
+
const returningCount = Math.max(0, stats.total_visitors - stats.unique_visitors);
|
| 32 |
+
const returningPercentage = stats.total_visitors > 0
|
| 33 |
+
? Math.round((returningCount / stats.total_visitors) * 100)
|
| 34 |
+
: 0;
|
| 35 |
+
const uniquePercentage = stats.total_visitors > 0
|
| 36 |
+
? Math.round((stats.unique_visitors / stats.total_visitors) * 100)
|
| 37 |
+
: 100;
|
| 38 |
+
|
| 39 |
+
return (
|
| 40 |
+
<div className="analytics-modal-backdrop" onClick={onClose}>
|
| 41 |
+
<div className="analytics-modal-content" onClick={(e) => e.stopPropagation()}>
|
| 42 |
+
<button className="modal-close-btn" onClick={onClose} title="Close Diagnostics">
|
| 43 |
+
<span className="material-icons">close</span>
|
| 44 |
+
</button>
|
| 45 |
+
|
| 46 |
+
<div className="analytics-stage">
|
| 47 |
+
<div className="analytics-header-row">
|
| 48 |
+
<div className="analytics-title-area">
|
| 49 |
+
<h3>Visitor Insights</h3>
|
| 50 |
+
<p>Real-time website traffic metrics.</p>
|
| 51 |
+
</div>
|
| 52 |
+
<div className="analytics-db-status">
|
| 53 |
+
{stats.status === 'online' && (
|
| 54 |
+
<span className="status-badge success">
|
| 55 |
+
<span className="dot pulse" />
|
| 56 |
+
Live Tracking
|
| 57 |
+
</span>
|
| 58 |
+
)}
|
| 59 |
+
{stats.status === 'offline' && (
|
| 60 |
+
<span className="status-badge error">
|
| 61 |
+
<span className="dot" />
|
| 62 |
+
Simulated
|
| 63 |
+
</span>
|
| 64 |
+
)}
|
| 65 |
+
{stats.status === 'connecting' && (
|
| 66 |
+
<span className="status-badge warning pulse">
|
| 67 |
+
<span className="dot" />
|
| 68 |
+
Connecting...
|
| 69 |
+
</span>
|
| 70 |
+
)}
|
| 71 |
+
</div>
|
| 72 |
+
</div>
|
| 73 |
+
|
| 74 |
+
<div className="analytics-grid">
|
| 75 |
+
{/* Metric 1: Total Hits */}
|
| 76 |
+
<div className="metric-card total-hits glow-orange">
|
| 77 |
+
<div className="metric-icon">
|
| 78 |
+
<span className="material-icons">visibility</span>
|
| 79 |
+
</div>
|
| 80 |
+
<div className="metric-data">
|
| 81 |
+
<span className="metric-number">{stats.total_visitors}</span>
|
| 82 |
+
<span className="metric-label">Total Visits</span>
|
| 83 |
+
</div>
|
| 84 |
+
<div className="metric-graph-bg">
|
| 85 |
+
<div className="mini-wave" />
|
| 86 |
+
</div>
|
| 87 |
+
</div>
|
| 88 |
+
|
| 89 |
+
{/* Metric 2: Unique Hits */}
|
| 90 |
+
<div className="metric-card unique-hits glow-green">
|
| 91 |
+
<div className="metric-icon">
|
| 92 |
+
<span className="material-icons">fingerprint</span>
|
| 93 |
+
</div>
|
| 94 |
+
<div className="metric-data">
|
| 95 |
+
<span className="metric-number">{stats.unique_visitors}</span>
|
| 96 |
+
<span className="metric-label">Unique Visitors</span>
|
| 97 |
+
</div>
|
| 98 |
+
<div className="metric-graph-bg">
|
| 99 |
+
<div className="mini-wave wave-green" />
|
| 100 |
+
</div>
|
| 101 |
+
</div>
|
| 102 |
+
</div>
|
| 103 |
+
|
| 104 |
+
<div className="analytics-insights-grid" style={{ gridTemplateColumns: '1fr' }}>
|
| 105 |
+
{/* Traffic breakdown panel */}
|
| 106 |
+
<div className="insights-panel ratio-panel">
|
| 107 |
+
<h4>Visitor Retention Breakdown</h4>
|
| 108 |
+
<div className="ratio-bar-container">
|
| 109 |
+
<div className="ratio-bar">
|
| 110 |
+
<div
|
| 111 |
+
className="ratio-fill unique-fill"
|
| 112 |
+
style={{ width: `${uniquePercentage}%` }}
|
| 113 |
+
title={`Unique: ${uniquePercentage}%`}
|
| 114 |
+
/>
|
| 115 |
+
<div
|
| 116 |
+
className="ratio-fill returning-fill"
|
| 117 |
+
style={{ width: `${returningPercentage}%` }}
|
| 118 |
+
title={`Returning: ${returningPercentage}%`}
|
| 119 |
+
/>
|
| 120 |
+
</div>
|
| 121 |
+
<div className="ratio-legend">
|
| 122 |
+
<div className="legend-item">
|
| 123 |
+
<span className="indicator-dot unique-dot" />
|
| 124 |
+
<span>New ({uniquePercentage}%)</span>
|
| 125 |
+
</div>
|
| 126 |
+
<div className="legend-item">
|
| 127 |
+
<span className="indicator-dot returning-dot" />
|
| 128 |
+
<span>Returning ({returningPercentage}%)</span>
|
| 129 |
+
</div>
|
| 130 |
+
</div>
|
| 131 |
+
</div>
|
| 132 |
+
|
| 133 |
+
<div className="retention-score-block">
|
| 134 |
+
<span className="retention-score">{returningCount}</span>
|
| 135 |
+
<div className="retention-copy">
|
| 136 |
+
<h5>Repeat Visits</h5>
|
| 137 |
+
</div>
|
| 138 |
+
</div>
|
| 139 |
+
</div>
|
| 140 |
+
</div>
|
| 141 |
+
|
| 142 |
+
<div className="analytics-action-row">
|
| 143 |
+
<button className="analytics-sync-btn" onClick={fetchStats} disabled={loading}>
|
| 144 |
+
<span className="material-icons">sync</span>
|
| 145 |
+
<span>{loading ? 'Refreshing...' : 'Refresh'}</span>
|
| 146 |
+
</button>
|
| 147 |
+
</div>
|
| 148 |
+
</div>
|
| 149 |
+
</div>
|
| 150 |
+
</div>
|
| 151 |
+
);
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
export default VisitorAnalytics;
|
tests/test_analytics.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# Add the project root directory to python path
|
| 5 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 6 |
+
|
| 7 |
+
from app.services.analytics import AnalyticsManager
|
| 8 |
+
|
| 9 |
+
def test_analytics_fallback():
|
| 10 |
+
print("Testing AnalyticsManager local fallback/simulation mechanism...")
|
| 11 |
+
|
| 12 |
+
# Instantiate a clean manager
|
| 13 |
+
manager = AnalyticsManager()
|
| 14 |
+
|
| 15 |
+
# 1. Assert initial state is not connected
|
| 16 |
+
assert manager._connected is False, "Initially manager should not be marked connected"
|
| 17 |
+
|
| 18 |
+
# 2. Get client (should try to connect and if REDIS_URL/REDIS_HOST are invalid or offline, fail gracefully returning None)
|
| 19 |
+
client = manager.get_client()
|
| 20 |
+
|
| 21 |
+
# If client is None, it means fallback to offline/simulation is active
|
| 22 |
+
if client is None:
|
| 23 |
+
print("[Offline Check] No active Redis client found (expected fallback mode).")
|
| 24 |
+
assert manager._connected is False, "State should be marked disconnected"
|
| 25 |
+
|
| 26 |
+
# Test record_hit in offline state (should return False and print offline skip, but NOT crash)
|
| 27 |
+
success = manager.record_hit("test_visitor_123")
|
| 28 |
+
assert success is False, "Hit record should be False when offline"
|
| 29 |
+
|
| 30 |
+
# Test get_stats in offline state (should return status offline with 0s)
|
| 31 |
+
stats = manager.get_stats()
|
| 32 |
+
assert stats["status"] == "offline", "Status should be offline"
|
| 33 |
+
assert stats["total_visitors"] == 0
|
| 34 |
+
assert stats["unique_visitors"] == 0
|
| 35 |
+
print("[Offline Check] Fallback works perfectly with zero crashes.")
|
| 36 |
+
else:
|
| 37 |
+
print("[Online Check] Active Redis client found! Testing live operations...")
|
| 38 |
+
assert manager._connected is True, "State should be marked connected"
|
| 39 |
+
|
| 40 |
+
# We have an active connection, so let's run actual Redis commands
|
| 41 |
+
visitor_id = "test_visitor_789"
|
| 42 |
+
|
| 43 |
+
# Record hit
|
| 44 |
+
success = manager.record_hit(visitor_id)
|
| 45 |
+
assert success is True, "Hit should be recorded successfully"
|
| 46 |
+
|
| 47 |
+
# Retrieve stats
|
| 48 |
+
stats = manager.get_stats()
|
| 49 |
+
assert stats["status"] == "online", "Status should be online"
|
| 50 |
+
assert stats["total_visitors"] >= 1, "Total visitors should be incremented"
|
| 51 |
+
assert stats["unique_visitors"] >= 1, "Unique visitors set size should be at least 1"
|
| 52 |
+
print("[Online Check] Real Redis connection and commands work perfectly!")
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
try:
|
| 56 |
+
test_analytics_fallback()
|
| 57 |
+
print("\nSUCCESS: All visitor analytics unit/integration tests passed successfully!")
|
| 58 |
+
except AssertionError as e:
|
| 59 |
+
print(f"\nFAILURE: Test assertion failed: {e}")
|
| 60 |
+
sys.exit(1)
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"\nFAILURE: Unexpected error occurred: {e}")
|
| 63 |
+
sys.exit(1)
|