File size: 10,068 Bytes
1d2a779
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adf80ee
1d2a779
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adf80ee
1d2a779
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adf80ee
1d2a779
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adf80ee
1d2a779
adf80ee
 
 
 
 
 
 
 
 
 
 
 
 
 
1d2a779
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adf80ee
 
 
 
1d2a779
 
 
 
 
 
adf80ee
1d2a779
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import { useEffect, useState } from "react";
import { useTenant } from "@/contexts/TenantContext";

type HeatmapData = {
  hour: number;
  day: number; // 0-6 (Sunday-Saturday)
  count: number;
};

type ToolUsageTrend = {
  tool: string;
  count: number;
  trend: "up" | "down" | "stable";
};

type TenantHeatmapProps = {
  days?: number;
};

const API_BASE =
  process.env.NEXT_PUBLIC_API_URL?.replace(/\/$/, "") || "http://localhost:8000";

const DAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const HOURS = Array.from({ length: 24 }, (_, i) => i);

export function TenantHeatmap({ days = 7 }: TenantHeatmapProps) {
  const { tenantId, role } = useTenant();
  const [loading, setLoading] = useState(false);
  const [heatmapData, setHeatmapData] = useState<HeatmapData[]>([]);
  const [toolTrends, setToolTrends] = useState<ToolUsageTrend[]>([]);
  const [maxCount, setMaxCount] = useState(1);

  useEffect(() => {
    async function fetchHeatmapData() {
      if (!tenantId) return;

      setLoading(true);
      try {
        // Fetch activity data
        const activityRes = await fetch(
          `${API_BASE}/analytics/activity?days=${days}`,
          {
            headers: {
              "x-tenant-id": tenantId,
              "x-user-role": role,
            },
          },
        );

        if (!activityRes.ok) {
          throw new Error(`Activity endpoint returned ${activityRes.status}`);
        }

        const activityData = await activityRes.json();

        // Fetch tool usage stats
        const toolRes = await fetch(
          `${API_BASE}/analytics/tool-usage?days=${days}`,
          {
            headers: {
              "x-tenant-id": tenantId,
              "x-user-role": role,
            },
          },
        );

        if (!toolRes.ok) {
          throw new Error(`Tool usage endpoint returned ${toolRes.status}`);
        }

        const toolData = await toolRes.json();

        // Process activity data into heatmap format
        const heatmap: HeatmapData[] = [];
        const activityByHour: Record<string, number> = {};

        // Group activities by hour and day
        if (activityData.activities && Array.isArray(activityData.activities)) {
          activityData.activities.forEach((activity: any) => {
            try {
              const timestamp = new Date(activity.timestamp || activity.created_at);
              // Check if date is valid
              if (!isNaN(timestamp.getTime())) {
                const hour = timestamp.getHours();
                const day = timestamp.getDay();
                const key = `${day}-${hour}`;

                activityByHour[key] = (activityByHour[key] || 0) + 1;
              }
            } catch (e) {
              // Skip invalid timestamps
              console.warn("Invalid timestamp in activity:", activity);
            }
          });
        }

        // Build heatmap data
        for (let day = 0; day < 7; day++) {
          for (let hour = 0; hour < 24; hour++) {
            const key = `${day}-${hour}`;
            const count = activityByHour[key] || 0;
            heatmap.push({ hour, day, count });
          }
        }

        setHeatmapData(heatmap);
        setMaxCount(Math.max(...heatmap.map((h) => h.count), 1));

        // Process tool trends
        const trends: ToolUsageTrend[] = [];
        if (toolData.tool_usage) {
          Object.entries(toolData.tool_usage).forEach(([tool, stats]: [string, any]) => {
            trends.push({
              tool,
              count: stats.count || 0,
              trend: "stable", // Could be calculated from historical data
            });
          });
        }

        // Sort by count descending
        trends.sort((a, b) => b.count - a.count);
        setToolTrends(trends.slice(0, 10)); // Top 10 tools
      } catch (err) {
        console.error("Failed to fetch heatmap data:", err);
        // Set empty data on error to show empty state
        setHeatmapData([]);
        setToolTrends([]);
        setMaxCount(1);
      } finally {
        setLoading(false);
      }
    }

    fetchHeatmapData();
  }, [tenantId, role, days]);

  const getIntensity = (count: number) => {
    if (maxCount === 0) return 0;
    const ratio = count / maxCount;
    if (ratio === 0) return 0;
    if (ratio < 0.2) return 1;
    if (ratio < 0.4) return 2;
    if (ratio < 0.6) return 3;
    if (ratio < 0.8) return 4;
    return 5;
  };

  const getColorClass = (intensity: number) => {
    const colors = [
      "bg-slate-800/30 border-slate-700/30", // 0
      "bg-cyan-500/20 border-cyan-500/30", // 1
      "bg-cyan-500/40 border-cyan-500/50", // 2
      "bg-cyan-500/60 border-cyan-500/70", // 3
      "bg-cyan-500/80 border-cyan-500/90", // 4
      "bg-cyan-500 border-cyan-400", // 5
    ];
    return colors[intensity] || colors[0];
  };

  return (
    <div className="space-y-6">
      {/* Query Heatmap */}
      <div className="rounded-2xl border border-white/10 bg-slate-950/40 p-6">
        <div className="mb-4 flex items-center justify-between">
          <h3 className="text-lg font-semibold text-white">Query Activity Heatmap</h3>
          <span className="text-xs text-slate-400">Last {days} days</span>
        </div>

        {loading ? (
          <div className="flex h-64 items-center justify-center">
            <span className="text-slate-400">Loading heatmap data...</span>
          </div>
        ) : (
          <div className="overflow-x-auto">
            <div className="inline-block min-w-full">
              {/* Hour labels */}
              <div className="mb-2 flex">
                <div className="w-12 shrink-0" />
                {HOURS.map((hour) => (
                  <div
                    key={hour}
                    className="flex-1 text-center text-[10px] text-slate-500"
                  >
                    {hour % 6 === 0 ? hour : ""}
                  </div>
                ))}
              </div>

              {/* Heatmap grid */}
              <div className="space-y-1">
                {DAYS.map((dayName, dayIdx) => {
                  const dayData = heatmapData.filter((d) => d.day === dayIdx);
                  return (
                    <div key={dayIdx} className="flex items-center gap-2">
                      <div className="w-12 shrink-0 text-xs text-slate-400">
                        {dayName}
                      </div>
                      <div className="flex flex-1 gap-0.5">
                        {HOURS.map((hour) => {
                          const data = dayData.find((d) => d.hour === hour);
                          const count = data?.count || 0;
                          const intensity = getIntensity(count);

                          return (
                            <div
                              key={hour}
                              className={`h-4 flex-1 rounded border transition-all hover:scale-110 ${getColorClass(
                                intensity,
                              )}`}
                              title={`${dayName} ${hour}:00 - ${count} queries`}
                            />
                          );
                        })}
                      </div>
                    </div>
                  );
                })}
              </div>

              {/* Legend */}
              <div className="mt-4 flex items-center justify-center gap-4 text-xs text-slate-400">
                <span>Less</span>
                <div className="flex gap-0.5">
                  {[0, 1, 2, 3, 4, 5].map((intensity) => (
                    <div
                      key={intensity}
                      className={`h-3 w-3 rounded border ${getColorClass(intensity)}`}
                    />
                  ))}
                </div>
                <span>More</span>
              </div>
            </div>
          </div>
        )}
      </div>

      {/* Tool Usage Trends */}
      <div className="rounded-2xl border border-white/10 bg-slate-950/40 p-6">
        <div className="mb-4 flex items-center justify-between">
          <h3 className="text-lg font-semibold text-white">Per-Tool Usage Trends</h3>
          <span className="text-xs text-slate-400">Top 10 tools</span>
        </div>

        {loading ? (
          <div className="flex h-32 items-center justify-center">
            <span className="text-slate-400">Loading tool trends...</span>
          </div>
        ) : toolTrends.length === 0 ? (
          <div className="flex h-32 items-center justify-center">
            <span className="text-slate-400">No tool usage data available</span>
          </div>
        ) : (
          <div className="space-y-3">
            {toolTrends.map((trend, idx) => {
              const maxToolCount = Math.max(...toolTrends.map((t) => t.count), 1);
              const widthPercent = (trend.count / maxToolCount) * 100;

              return (
                <div key={idx} className="space-y-1">
                  <div className="flex items-center justify-between text-sm">
                    <div className="flex items-center gap-2">
                      <span className="font-medium text-white">{trend.tool}</span>
                      {trend.trend === "up" && (
                        <span className="text-xs text-emerald-400"></span>
                      )}
                      {trend.trend === "down" && (
                        <span className="text-xs text-red-400"></span>
                      )}
                    </div>
                    <span className="text-slate-300">{trend.count}</span>
                  </div>
                  <div className="h-2 overflow-hidden rounded-full bg-slate-800">
                    <div
                      className="h-full bg-gradient-to-r from-cyan-500 to-cyan-400 transition-all"
                      style={{ width: `${widthPercent}%` }}
                    />
                  </div>
                </div>
              );
            })}
          </div>
        )}
      </div>
    </div>
  );
}