Spaces:
Running

File size: 8,051 Bytes
7e33b90
1b99aeb
eaf22dd
1b99aeb
 
 
 
 
1c2c4a2
1b99aeb
 
 
 
 
 
 
 
f127889
e83163e
1b99aeb
1c2c4a2
1b99aeb
1c2c4a2
 
1b99aeb
 
 
 
1c2c4a2
1b99aeb
 
 
 
 
 
 
 
a7e3546
 
 
 
 
1b99aeb
 
 
a7e3546
 
 
eaf22dd
 
 
a7e3546
 
eaf22dd
 
 
 
 
 
1c2c4a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7e3546
1b99aeb
a7e3546
 
 
1b99aeb
 
 
 
 
 
 
 
 
 
 
a7e3546
 
 
 
 
 
 
 
 
 
 
1b99aeb
 
 
 
 
 
 
a7e3546
 
 
1b99aeb
 
 
a7e3546
 
1b99aeb
a7e3546
1b99aeb
 
a7e3546
 
 
 
 
 
 
 
 
 
 
1b99aeb
 
 
 
 
 
 
 
 
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

import { useState, useEffect } from 'react'
import { BarChart, Bar, XAxis, YAxis, Tooltip, ResponsiveContainer, LineChart, Line, CartesianGrid, AreaChart, Area } from 'recharts'
import { Activity, DollarSign, Users, TrendingUp } from 'lucide-react'

export default function AnalyticsTab() {
    const [revenueData, setRevenueData] = useState<any>(null)
    const [growthData, setGrowthData] = useState<any[]>([])
    const [aiPulse, setAiPulse] = useState<any>(null)
    const [loading, setLoading] = useState(true)

    useEffect(() => {
        loadData()
    }, [])

    const loadData = async () => {
        try {
            const token = localStorage.getItem('auth_token')
            const headers = { 'Authorization': 'Bearer ' + token }

            const [revRes, growthRes, pulseRes] = await Promise.all([
                fetch('/api/v1/admin/analytics/revenue', { headers }),
                fetch('/api/v1/admin/analytics/growth', { headers }),
                fetch('/api/v1/analytics/admin/ai-pulse', { headers })
            ])

            if (revRes.ok) setRevenueData(await revRes.json())
            if (growthRes.ok) setGrowthData(await growthRes.json())
            if (pulseRes.ok) setAiPulse(await pulseRes.json())

        } catch (error) {
            console.error('Analytics load error', error)
        } finally {
            setLoading(false)
        }
    }

    if (loading) return (
        <div className="p-8 text-center text-slate-400">
            <div className="animate-pulse">Загрузка аналитики...</div>
        </div>
    )

    return (
        <div className="space-y-6">
            {/* Info Card - Aurora Style */}
            <div className="bg-gradient-to-r from-indigo-500/10 to-purple-500/10 border border-indigo-500/20 p-4 rounded-xl flex items-start gap-3">
                <div className="p-2 bg-indigo-500/20 rounded-lg text-indigo-400">
                    <TrendingUp className="w-5 h-5" />
                </div>
                <div>
                    <h3 className="font-bold text-white text-sm">Аналитика и Доходы</h3>
                    <p className="text-xs text-slate-400 mt-1 leading-relaxed">
                        Обзор ключевых метрик платформы: рост пользователей и финансовые показатели (MRR).
                        Данные обновляются в реальном времени на основе активных подписок.
                    </p>
                </div>
            </div>

            {/* AI Pulse Row (NEW) */}
            <div className="grid grid-cols-1 md:grid-cols-4 gap-4">
                <div className="bg-slate-900/50 backdrop-blur-sm p-4 rounded-xl border border-indigo-500/20">
                    <p className="text-[10px] text-slate-500 font-bold uppercase mb-1">Global Tokens (24h)</p>
                    <p className="text-xl font-mono font-bold text-white">{(aiPulse?.total_tokens_24h || 0).toLocaleString()}</p>
                </div>
                <div className="bg-slate-900/50 backdrop-blur-sm p-4 rounded-xl border border-emerald-500/20">
                    <p className="text-[10px] text-slate-500 font-bold uppercase mb-1">Global AI Cost</p>
                    <p className="text-xl font-mono font-bold text-emerald-400">${(aiPulse?.total_cost_usd || 0).toFixed(2)}</p>
                </div>
                <div className="bg-slate-900/50 backdrop-blur-sm p-4 rounded-xl border border-purple-500/20">
                    <p className="text-[10px] text-slate-500 font-bold uppercase mb-1">Active Convs (1h)</p>
                    <p className="text-xl font-mono font-bold text-purple-400">{aiPulse?.active_conversations_1h || 0}</p>
                </div>
                <div className="bg-slate-900/50 backdrop-blur-sm p-4 rounded-xl border border-red-500/20">
                    <p className="text-[10px] text-slate-500 font-bold uppercase mb-1">Spam Alerts (24h)</p>
                    <p className="text-xl font-mono font-bold text-red-400">{aiPulse?.spam_alerts_24h || 0}</p>
                </div>
            </div>

            <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
                {/* Growth Chart */}
                <div className="bg-slate-900/50 backdrop-blur-sm p-6 rounded-2xl border border-white/10">
                    <h3 className="text-lg font-bold text-white mb-4 flex items-center gap-2">
                        <Users className="w-5 h-5 text-indigo-400" />
                        Рост пользователей
                    </h3>
                    <div className="h-64">
                        <ResponsiveContainer width="100%" height="100%">
                            <AreaChart data={growthData}>
                                <defs>
                                    <linearGradient id="colorCount" x1="0" y1="0" x2="0" y2="1">
                                        <stop offset="5%" stopColor="#6366f1" stopOpacity={0.8} />
                                        <stop offset="95%" stopColor="#6366f1" stopOpacity={0} />
                                    </linearGradient>
                                </defs>
                                <CartesianGrid strokeDasharray="3 3" vertical={false} stroke="#334155" />
                                <XAxis dataKey="date" tickFormatter={(str) => str.slice(5)} stroke="#64748b" />
                                <YAxis stroke="#64748b" />
                                <Tooltip
                                    contentStyle={{
                                        backgroundColor: '#1e293b',
                                        border: '1px solid #334155',
                                        borderRadius: '8px',
                                        color: '#fff'
                                    }}
                                />
                                <Area type="monotone" dataKey="count" stroke="#6366f1" fillOpacity={1} fill="url(#colorCount)" />
                            </AreaChart>
                        </ResponsiveContainer>
                    </div>
                </div>

                {/* Revenue Chart */}
                <div className="bg-slate-900/50 backdrop-blur-sm p-6 rounded-2xl border border-white/10">
                    <h3 className="text-lg font-bold text-white mb-4 flex items-center gap-2">
                        <DollarSign className="w-5 h-5 text-emerald-400" />
                        Выручка (MRR)
                    </h3>
                    <div className="mb-4">
                        <span className="text-3xl font-bold text-white">&#36;{revenueData?.current_mrr || 0}</span>
                        <span className="text-sm text-slate-400 ml-2">текущий MRR</span>
                    </div>
                    <div className="h-52">
                        <ResponsiveContainer width="100%" height="100%">
                            <LineChart data={revenueData?.history || []}>
                                <CartesianGrid strokeDasharray="3 3" vertical={false} stroke="#334155" />
                                <XAxis dataKey="date" tickFormatter={(str) => str.slice(5)} stroke="#64748b" />
                                <YAxis stroke="#64748b" />
                                <Tooltip
                                    contentStyle={{
                                        backgroundColor: '#1e293b',
                                        border: '1px solid #334155',
                                        borderRadius: '8px',
                                        color: '#fff'
                                    }}
                                />
                                <Line type="monotone" dataKey="amount" stroke="#10b981" strokeWidth={3} dot={false} />
                            </LineChart>
                        </ResponsiveContainer>
                    </div>
                </div>
            </div>
        </div>
    )
}