Skip to content

Commit 8a0cb11

Browse files
authored
Merge pull request #9 from chitcommit/copilot/add-analytics-and-monitoring
Add analytics and monitoring via Cloudflare Analytics Engine
2 parents 082aaf1 + fe544a2 commit 8a0cb11

5 files changed

Lines changed: 638 additions & 10 deletions

File tree

ANALYTICS.md

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
# Analytics & Monitoring
2+
3+
Billy Bullshit now tracks usage and performance metrics using Cloudflare Analytics Engine.
4+
5+
## Overview
6+
7+
The analytics system tracks three main categories of metrics:
8+
9+
### 1. Usage Metrics
10+
- **API call volume** by endpoint (review, chat, roast, analyze, debate)
11+
- **Language distribution** in code reviews (JavaScript, Python, Go, etc.)
12+
- **BS score distribution** (1-10 rating of code quality)
13+
- **Code smell frequency** (CRITICAL, MAJOR, BS, WTAF categories)
14+
15+
### 2. Performance Metrics
16+
- **Response times** per endpoint (milliseconds)
17+
- **AI model usage** (Workers AI, Anthropic Claude, OpenAI)
18+
- **Error rates** by error type
19+
- **Fallback usage** tracking when primary models fail
20+
21+
### 3. Quality Metrics
22+
- **User feedback** (thumbs up/down via `/feedback` endpoint)
23+
- **Review success rate** (successful vs. failed reviews)
24+
25+
## Architecture
26+
27+
### AnalyticsService (`src/analytics.ts`)
28+
29+
The `AnalyticsService` class handles all analytics tracking:
30+
31+
```typescript
32+
const analytics = new AnalyticsService(env.ANALYTICS);
33+
34+
// Track a code review
35+
await analytics.trackReview({
36+
language: 'javascript',
37+
bsScore: 7,
38+
responseTime: 1234,
39+
aiModel: 'workers-ai',
40+
success: true,
41+
codeSmells: ['critical', 'major']
42+
});
43+
44+
// Track user feedback
45+
await analytics.trackFeedback('/review', 'up', sessionId);
46+
47+
// Track an error
48+
await analytics.trackError('/review', 'timeout', 1500);
49+
```
50+
51+
### Data Points
52+
53+
Each analytics event includes:
54+
- **Blobs**: endpoint, language, AI model, error type (text data)
55+
- **Doubles**: BS score, response time (numeric data)
56+
- **Indexes**: endpoint (for efficient querying)
57+
58+
## Endpoints
59+
60+
### POST `/feedback`
61+
62+
Submit user feedback on Billy's responses.
63+
64+
**Request:**
65+
```json
66+
{
67+
"endpoint": "/review",
68+
"feedback": "up",
69+
"sessionId": "billy_1234567890"
70+
}
71+
```
72+
73+
**Response:**
74+
```json
75+
{
76+
"message": "Feedback recorded. Thanks for your honesty.",
77+
"feedback": "up",
78+
"billy_says": "👍 Glad you appreciate brutal honesty."
79+
}
80+
```
81+
82+
### GET `/analytics`
83+
84+
View information about tracked metrics.
85+
86+
**Response:**
87+
```json
88+
{
89+
"message": "Analytics are being tracked via Cloudflare Analytics Engine",
90+
"metrics_tracked": {
91+
"usage": [...],
92+
"performance": [...],
93+
"quality": [...]
94+
},
95+
"query_instructions": {
96+
"note": "Use Cloudflare GraphQL API to query Analytics Engine dataset",
97+
"dataset": "ANALYTICS binding",
98+
"documentation": "https://developers.cloudflare.com/analytics/analytics-engine/"
99+
}
100+
}
101+
```
102+
103+
## Querying Analytics Data
104+
105+
Analytics data is stored in Cloudflare Analytics Engine and can be queried using the GraphQL API.
106+
107+
### Example GraphQL Query
108+
109+
```graphql
110+
query {
111+
viewer {
112+
accounts(filter: {accountTag: "YOUR_ACCOUNT_ID"}) {
113+
analyticsEngineDatasets(filter: {datasetTag: "ANALYTICS"}) {
114+
data(
115+
filter: {
116+
timestamp_geq: "2024-01-01T00:00:00Z"
117+
timestamp_lt: "2024-12-31T23:59:59Z"
118+
}
119+
orderBy: [timestamp_DESC]
120+
limit: 1000
121+
) {
122+
blob1 # endpoint
123+
blob2 # language
124+
blob3 # AI model
125+
blob4 # error type
126+
double1 # BS score
127+
double2 # response time
128+
}
129+
}
130+
}
131+
}
132+
}
133+
```
134+
135+
### Using wrangler
136+
137+
You can also query analytics using the Wrangler CLI:
138+
139+
```bash
140+
# Query recent data
141+
wrangler analytics-engine query ANALYTICS \
142+
--start 2024-01-01T00:00:00Z \
143+
--end 2024-12-31T23:59:59Z
144+
145+
# Export to CSV
146+
wrangler analytics-engine query ANALYTICS --format csv > analytics.csv
147+
```
148+
149+
## Configuration
150+
151+
### wrangler.toml
152+
153+
The Analytics Engine binding is configured in `wrangler.toml`:
154+
155+
```toml
156+
[[analytics_engine_datasets]]
157+
binding = "ANALYTICS"
158+
```
159+
160+
### Environment Variables
161+
162+
No additional environment variables are required. The analytics service gracefully handles missing bindings.
163+
164+
## Deployment
165+
166+
### Prerequisites
167+
168+
1. Ensure your Cloudflare account has Analytics Engine enabled
169+
2. The ANALYTICS binding is automatically created during deployment
170+
171+
### Deploy
172+
173+
```bash
174+
# Deploy to production
175+
npm run deploy:production
176+
```
177+
178+
## Data Privacy
179+
180+
- No sensitive data (API keys, secrets, user credentials) is logged
181+
- Session IDs are optional and only tracked if provided
182+
- All data is retained according to Cloudflare's Analytics Engine retention policy (typically 31 days)
183+
184+
## Monitoring Dashboard
185+
186+
While Billy provides basic analytics info via the `/analytics` endpoint, you can build custom dashboards using:
187+
188+
1. **Cloudflare Dashboard**: View basic metrics in the Workers Analytics section
189+
2. **GraphQL API**: Build custom queries for detailed analysis
190+
3. **External Tools**: Export data to tools like Grafana, Datadog, or custom dashboards
191+
192+
## Metrics Examples
193+
194+
### Average Response Time by Endpoint
195+
196+
Query the `double2` (response time) field grouped by `blob1` (endpoint).
197+
198+
### BS Score Distribution
199+
200+
Query `double1` (BS score) to see how much BS Billy is finding in code:
201+
- 1-2: Clean code (rare!)
202+
- 3-4: Minor issues
203+
- 5-6: Moderate BS
204+
- 7-8: Significant problems
205+
- 9-10: Complete disaster
206+
207+
### Most Common Languages Reviewed
208+
209+
Query `blob2` (language) to see which programming languages are being reviewed most often.
210+
211+
### Error Rate Analysis
212+
213+
Query `blob4` (error type) where not 'none' to track error patterns and troubleshoot issues.
214+
215+
## Future Enhancements
216+
217+
Potential improvements to the analytics system:
218+
219+
- Real-time dashboard UI
220+
- Automated alerts for high error rates
221+
- Trend analysis and anomaly detection
222+
- Code smell tracking over time
223+
- A/B testing different AI models
224+
- User journey tracking across sessions
225+
226+
---
227+
228+
**Billy says:** "📊 Every bit of your BS is being tracked. Can't hide from the data."

README.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ No sugarcoating. No corporate BS. Just brutal, honest code review that you actua
2222
-**Lightning Fast** - Runs on Cloudflare Workers AI
2323
- 🌍 **Global** - Deployed at the edge, worldwide
2424
- 💾 **Conversation Memory** - Billy remembers your conversation
25+
- 📊 **Analytics** - Track usage, performance, and BS scores
2526

2627
## Quick Start
2728

@@ -191,6 +192,50 @@ curl -X POST https://billy.chitty.cc/stream \
191192
--no-buffer
192193
```
193194

195+
### POST `/feedback`
196+
Submit feedback on Billy's responses (thumbs up/down)
197+
198+
```bash
199+
curl -X POST https://billy.chitty.cc/feedback \
200+
-H "Content-Type: application/json" \
201+
-d '{
202+
"endpoint": "/review",
203+
"feedback": "up",
204+
"sessionId": "optional-session-id"
205+
}'
206+
```
207+
208+
**Response:**
209+
```json
210+
{
211+
"message": "Feedback recorded. Thanks for your honesty.",
212+
"feedback": "up",
213+
"billy_says": "👍 Glad you appreciate brutal honesty."
214+
}
215+
```
216+
217+
### GET `/analytics`
218+
View what metrics Billy is tracking
219+
220+
```bash
221+
curl https://billy.chitty.cc/analytics
222+
```
223+
224+
**Response:**
225+
```json
226+
{
227+
"message": "Analytics are being tracked via Cloudflare Analytics Engine",
228+
"metrics_tracked": {
229+
"usage": ["API call volume by endpoint", "Language distribution", "BS score distribution"],
230+
"performance": ["Response times", "AI model usage", "Error rates"],
231+
"quality": ["User feedback", "Review success rate"]
232+
},
233+
"billy_says": "📊 All your BS is being tracked. Every. Single. Call."
234+
}
235+
```
236+
237+
See [ANALYTICS.md](ANALYTICS.md) for detailed analytics documentation.
238+
194239
## Configuration
195240

196241
### Environment Variables
@@ -310,10 +355,12 @@ billy-bullshit/
310355
├── src/
311356
│ ├── index.ts # Main app + routes
312357
│ ├── billy-agent.ts # Billy's personality + AI
313-
│ └── conversation-store.ts # KV conversation management
358+
│ ├── conversation-store.ts # KV conversation management
359+
│ └── analytics.ts # Analytics tracking
314360
├── wrangler.toml # Cloudflare configuration
315361
├── package.json # Dependencies
316-
└── tsconfig.json # TypeScript config
362+
├── tsconfig.json # TypeScript config
363+
└── ANALYTICS.md # Analytics documentation
317364
```
318365

319366
### Testing

0 commit comments

Comments
 (0)