@jwdobeutechsolutions/dobeutech-claude-code-custom 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +174 -0
- package/CONTRIBUTING.md +191 -0
- package/README.md +345 -0
- package/agents/accessibility-auditor.md +315 -0
- package/agents/api-designer.md +265 -0
- package/agents/architect.md +211 -0
- package/agents/build-error-resolver.md +532 -0
- package/agents/ci-cd-generator.md +318 -0
- package/agents/code-reviewer.md +104 -0
- package/agents/database-migrator.md +258 -0
- package/agents/deployment-manager.md +296 -0
- package/agents/doc-updater.md +452 -0
- package/agents/docker-specialist.md +293 -0
- package/agents/e2e-runner.md +708 -0
- package/agents/fullstack-architect.md +293 -0
- package/agents/infrastructure-engineer.md +297 -0
- package/agents/integration-tester.md +320 -0
- package/agents/performance-tester.md +243 -0
- package/agents/planner.md +119 -0
- package/agents/refactor-cleaner.md +306 -0
- package/agents/security-reviewer.md +545 -0
- package/agents/tdd-guide.md +280 -0
- package/agents/unit-test-generator.md +290 -0
- package/bin/claude-config.js +290 -0
- package/commands/api-design.md +55 -0
- package/commands/audit-accessibility.md +37 -0
- package/commands/audit-performance.md +38 -0
- package/commands/audit-security.md +43 -0
- package/commands/build-fix.md +29 -0
- package/commands/changelog.md +31 -0
- package/commands/code-review.md +40 -0
- package/commands/deploy.md +51 -0
- package/commands/docs-api.md +41 -0
- package/commands/e2e.md +363 -0
- package/commands/plan.md +113 -0
- package/commands/refactor-clean.md +28 -0
- package/commands/tdd.md +326 -0
- package/commands/test-coverage.md +27 -0
- package/commands/update-codemaps.md +17 -0
- package/commands/update-docs.md +31 -0
- package/hooks/hooks.json +121 -0
- package/mcp-configs/mcp-servers.json +163 -0
- package/package.json +53 -0
- package/rules/agents.md +49 -0
- package/rules/coding-style.md +70 -0
- package/rules/git-workflow.md +45 -0
- package/rules/hooks.md +46 -0
- package/rules/patterns.md +55 -0
- package/rules/performance.md +47 -0
- package/rules/security.md +36 -0
- package/rules/testing.md +30 -0
- package/scripts/install.js +254 -0
- package/skills/backend-patterns.md +582 -0
- package/skills/clickhouse-io.md +429 -0
- package/skills/coding-standards.md +520 -0
- package/skills/frontend-patterns.md +631 -0
- package/skills/project-guidelines-example.md +345 -0
- package/skills/security-review/SKILL.md +494 -0
- package/skills/tdd-workflow/SKILL.md +409 -0
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: clickhouse-io
|
|
3
|
+
description: ClickHouse database patterns, query optimization, analytics, and data engineering best practices for high-performance analytical workloads.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# ClickHouse Analytics Patterns
|
|
7
|
+
|
|
8
|
+
ClickHouse-specific patterns for high-performance analytics and data engineering.
|
|
9
|
+
|
|
10
|
+
## Overview
|
|
11
|
+
|
|
12
|
+
ClickHouse is a column-oriented database management system (DBMS) for online analytical processing (OLAP). It's optimized for fast analytical queries on large datasets.
|
|
13
|
+
|
|
14
|
+
**Key Features:**
|
|
15
|
+
- Column-oriented storage
|
|
16
|
+
- Data compression
|
|
17
|
+
- Parallel query execution
|
|
18
|
+
- Distributed queries
|
|
19
|
+
- Real-time analytics
|
|
20
|
+
|
|
21
|
+
## Table Design Patterns
|
|
22
|
+
|
|
23
|
+
### MergeTree Engine (Most Common)
|
|
24
|
+
|
|
25
|
+
```sql
|
|
26
|
+
CREATE TABLE markets_analytics (
|
|
27
|
+
date Date,
|
|
28
|
+
market_id String,
|
|
29
|
+
market_name String,
|
|
30
|
+
volume UInt64,
|
|
31
|
+
trades UInt32,
|
|
32
|
+
unique_traders UInt32,
|
|
33
|
+
avg_trade_size Float64,
|
|
34
|
+
created_at DateTime
|
|
35
|
+
) ENGINE = MergeTree()
|
|
36
|
+
PARTITION BY toYYYYMM(date)
|
|
37
|
+
ORDER BY (date, market_id)
|
|
38
|
+
SETTINGS index_granularity = 8192;
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### ReplacingMergeTree (Deduplication)
|
|
42
|
+
|
|
43
|
+
```sql
|
|
44
|
+
-- For data that may have duplicates (e.g., from multiple sources)
|
|
45
|
+
CREATE TABLE user_events (
|
|
46
|
+
event_id String,
|
|
47
|
+
user_id String,
|
|
48
|
+
event_type String,
|
|
49
|
+
timestamp DateTime,
|
|
50
|
+
properties String
|
|
51
|
+
) ENGINE = ReplacingMergeTree()
|
|
52
|
+
PARTITION BY toYYYYMM(timestamp)
|
|
53
|
+
ORDER BY (user_id, event_id, timestamp)
|
|
54
|
+
PRIMARY KEY (user_id, event_id);
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### AggregatingMergeTree (Pre-aggregation)
|
|
58
|
+
|
|
59
|
+
```sql
|
|
60
|
+
-- For maintaining aggregated metrics
|
|
61
|
+
CREATE TABLE market_stats_hourly (
|
|
62
|
+
hour DateTime,
|
|
63
|
+
market_id String,
|
|
64
|
+
total_volume AggregateFunction(sum, UInt64),
|
|
65
|
+
total_trades AggregateFunction(count, UInt32),
|
|
66
|
+
unique_users AggregateFunction(uniq, String)
|
|
67
|
+
) ENGINE = AggregatingMergeTree()
|
|
68
|
+
PARTITION BY toYYYYMM(hour)
|
|
69
|
+
ORDER BY (hour, market_id);
|
|
70
|
+
|
|
71
|
+
-- Query aggregated data
|
|
72
|
+
SELECT
|
|
73
|
+
hour,
|
|
74
|
+
market_id,
|
|
75
|
+
sumMerge(total_volume) AS volume,
|
|
76
|
+
countMerge(total_trades) AS trades,
|
|
77
|
+
uniqMerge(unique_users) AS users
|
|
78
|
+
FROM market_stats_hourly
|
|
79
|
+
WHERE hour >= toStartOfHour(now() - INTERVAL 24 HOUR)
|
|
80
|
+
GROUP BY hour, market_id
|
|
81
|
+
ORDER BY hour DESC;
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Query Optimization Patterns
|
|
85
|
+
|
|
86
|
+
### Efficient Filtering
|
|
87
|
+
|
|
88
|
+
```sql
|
|
89
|
+
-- ✅ GOOD: Use indexed columns first
|
|
90
|
+
SELECT *
|
|
91
|
+
FROM markets_analytics
|
|
92
|
+
WHERE date >= '2025-01-01'
|
|
93
|
+
AND market_id = 'market-123'
|
|
94
|
+
AND volume > 1000
|
|
95
|
+
ORDER BY date DESC
|
|
96
|
+
LIMIT 100;
|
|
97
|
+
|
|
98
|
+
-- ❌ BAD: Filter on non-indexed columns first
|
|
99
|
+
SELECT *
|
|
100
|
+
FROM markets_analytics
|
|
101
|
+
WHERE volume > 1000
|
|
102
|
+
AND market_name LIKE '%election%'
|
|
103
|
+
AND date >= '2025-01-01';
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Aggregations
|
|
107
|
+
|
|
108
|
+
```sql
|
|
109
|
+
-- ✅ GOOD: Use ClickHouse-specific aggregation functions
|
|
110
|
+
SELECT
|
|
111
|
+
toStartOfDay(created_at) AS day,
|
|
112
|
+
market_id,
|
|
113
|
+
sum(volume) AS total_volume,
|
|
114
|
+
count() AS total_trades,
|
|
115
|
+
uniq(trader_id) AS unique_traders,
|
|
116
|
+
avg(trade_size) AS avg_size
|
|
117
|
+
FROM trades
|
|
118
|
+
WHERE created_at >= today() - INTERVAL 7 DAY
|
|
119
|
+
GROUP BY day, market_id
|
|
120
|
+
ORDER BY day DESC, total_volume DESC;
|
|
121
|
+
|
|
122
|
+
-- ✅ Use quantile for percentiles (more efficient than percentile)
|
|
123
|
+
SELECT
|
|
124
|
+
quantile(0.50)(trade_size) AS median,
|
|
125
|
+
quantile(0.95)(trade_size) AS p95,
|
|
126
|
+
quantile(0.99)(trade_size) AS p99
|
|
127
|
+
FROM trades
|
|
128
|
+
WHERE created_at >= now() - INTERVAL 1 HOUR;
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Window Functions
|
|
132
|
+
|
|
133
|
+
```sql
|
|
134
|
+
-- Calculate running totals
|
|
135
|
+
SELECT
|
|
136
|
+
date,
|
|
137
|
+
market_id,
|
|
138
|
+
volume,
|
|
139
|
+
sum(volume) OVER (
|
|
140
|
+
PARTITION BY market_id
|
|
141
|
+
ORDER BY date
|
|
142
|
+
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
|
|
143
|
+
) AS cumulative_volume
|
|
144
|
+
FROM markets_analytics
|
|
145
|
+
WHERE date >= today() - INTERVAL 30 DAY
|
|
146
|
+
ORDER BY market_id, date;
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Data Insertion Patterns
|
|
150
|
+
|
|
151
|
+
### Bulk Insert (Recommended)
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
import { ClickHouse } from 'clickhouse'
|
|
155
|
+
|
|
156
|
+
const clickhouse = new ClickHouse({
|
|
157
|
+
url: process.env.CLICKHOUSE_URL,
|
|
158
|
+
port: 8123,
|
|
159
|
+
basicAuth: {
|
|
160
|
+
username: process.env.CLICKHOUSE_USER,
|
|
161
|
+
password: process.env.CLICKHOUSE_PASSWORD
|
|
162
|
+
}
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
// ✅ Batch insert (efficient)
|
|
166
|
+
async function bulkInsertTrades(trades: Trade[]) {
|
|
167
|
+
const values = trades.map(trade => `(
|
|
168
|
+
'${trade.id}',
|
|
169
|
+
'${trade.market_id}',
|
|
170
|
+
'${trade.user_id}',
|
|
171
|
+
${trade.amount},
|
|
172
|
+
'${trade.timestamp.toISOString()}'
|
|
173
|
+
)`).join(',')
|
|
174
|
+
|
|
175
|
+
await clickhouse.query(`
|
|
176
|
+
INSERT INTO trades (id, market_id, user_id, amount, timestamp)
|
|
177
|
+
VALUES ${values}
|
|
178
|
+
`).toPromise()
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// ❌ Individual inserts (slow)
|
|
182
|
+
async function insertTrade(trade: Trade) {
|
|
183
|
+
// Don't do this in a loop!
|
|
184
|
+
await clickhouse.query(`
|
|
185
|
+
INSERT INTO trades VALUES ('${trade.id}', ...)
|
|
186
|
+
`).toPromise()
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### Streaming Insert
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
// For continuous data ingestion
|
|
194
|
+
import { createWriteStream } from 'fs'
|
|
195
|
+
import { pipeline } from 'stream/promises'
|
|
196
|
+
|
|
197
|
+
async function streamInserts() {
|
|
198
|
+
const stream = clickhouse.insert('trades').stream()
|
|
199
|
+
|
|
200
|
+
for await (const batch of dataSource) {
|
|
201
|
+
stream.write(batch)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
await stream.end()
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## Materialized Views
|
|
209
|
+
|
|
210
|
+
### Real-time Aggregations
|
|
211
|
+
|
|
212
|
+
```sql
|
|
213
|
+
-- Create materialized view for hourly stats
|
|
214
|
+
CREATE MATERIALIZED VIEW market_stats_hourly_mv
|
|
215
|
+
TO market_stats_hourly
|
|
216
|
+
AS SELECT
|
|
217
|
+
toStartOfHour(timestamp) AS hour,
|
|
218
|
+
market_id,
|
|
219
|
+
sumState(amount) AS total_volume,
|
|
220
|
+
countState() AS total_trades,
|
|
221
|
+
uniqState(user_id) AS unique_users
|
|
222
|
+
FROM trades
|
|
223
|
+
GROUP BY hour, market_id;
|
|
224
|
+
|
|
225
|
+
-- Query the materialized view
|
|
226
|
+
SELECT
|
|
227
|
+
hour,
|
|
228
|
+
market_id,
|
|
229
|
+
sumMerge(total_volume) AS volume,
|
|
230
|
+
countMerge(total_trades) AS trades,
|
|
231
|
+
uniqMerge(unique_users) AS users
|
|
232
|
+
FROM market_stats_hourly
|
|
233
|
+
WHERE hour >= now() - INTERVAL 24 HOUR
|
|
234
|
+
GROUP BY hour, market_id;
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Performance Monitoring
|
|
238
|
+
|
|
239
|
+
### Query Performance
|
|
240
|
+
|
|
241
|
+
```sql
|
|
242
|
+
-- Check slow queries
|
|
243
|
+
SELECT
|
|
244
|
+
query_id,
|
|
245
|
+
user,
|
|
246
|
+
query,
|
|
247
|
+
query_duration_ms,
|
|
248
|
+
read_rows,
|
|
249
|
+
read_bytes,
|
|
250
|
+
memory_usage
|
|
251
|
+
FROM system.query_log
|
|
252
|
+
WHERE type = 'QueryFinish'
|
|
253
|
+
AND query_duration_ms > 1000
|
|
254
|
+
AND event_time >= now() - INTERVAL 1 HOUR
|
|
255
|
+
ORDER BY query_duration_ms DESC
|
|
256
|
+
LIMIT 10;
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Table Statistics
|
|
260
|
+
|
|
261
|
+
```sql
|
|
262
|
+
-- Check table sizes
|
|
263
|
+
SELECT
|
|
264
|
+
database,
|
|
265
|
+
table,
|
|
266
|
+
formatReadableSize(sum(bytes)) AS size,
|
|
267
|
+
sum(rows) AS rows,
|
|
268
|
+
max(modification_time) AS latest_modification
|
|
269
|
+
FROM system.parts
|
|
270
|
+
WHERE active
|
|
271
|
+
GROUP BY database, table
|
|
272
|
+
ORDER BY sum(bytes) DESC;
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## Common Analytics Queries
|
|
276
|
+
|
|
277
|
+
### Time Series Analysis
|
|
278
|
+
|
|
279
|
+
```sql
|
|
280
|
+
-- Daily active users
|
|
281
|
+
SELECT
|
|
282
|
+
toDate(timestamp) AS date,
|
|
283
|
+
uniq(user_id) AS daily_active_users
|
|
284
|
+
FROM events
|
|
285
|
+
WHERE timestamp >= today() - INTERVAL 30 DAY
|
|
286
|
+
GROUP BY date
|
|
287
|
+
ORDER BY date;
|
|
288
|
+
|
|
289
|
+
-- Retention analysis
|
|
290
|
+
SELECT
|
|
291
|
+
signup_date,
|
|
292
|
+
countIf(days_since_signup = 0) AS day_0,
|
|
293
|
+
countIf(days_since_signup = 1) AS day_1,
|
|
294
|
+
countIf(days_since_signup = 7) AS day_7,
|
|
295
|
+
countIf(days_since_signup = 30) AS day_30
|
|
296
|
+
FROM (
|
|
297
|
+
SELECT
|
|
298
|
+
user_id,
|
|
299
|
+
min(toDate(timestamp)) AS signup_date,
|
|
300
|
+
toDate(timestamp) AS activity_date,
|
|
301
|
+
dateDiff('day', signup_date, activity_date) AS days_since_signup
|
|
302
|
+
FROM events
|
|
303
|
+
GROUP BY user_id, activity_date
|
|
304
|
+
)
|
|
305
|
+
GROUP BY signup_date
|
|
306
|
+
ORDER BY signup_date DESC;
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### Funnel Analysis
|
|
310
|
+
|
|
311
|
+
```sql
|
|
312
|
+
-- Conversion funnel
|
|
313
|
+
SELECT
|
|
314
|
+
countIf(step = 'viewed_market') AS viewed,
|
|
315
|
+
countIf(step = 'clicked_trade') AS clicked,
|
|
316
|
+
countIf(step = 'completed_trade') AS completed,
|
|
317
|
+
round(clicked / viewed * 100, 2) AS view_to_click_rate,
|
|
318
|
+
round(completed / clicked * 100, 2) AS click_to_completion_rate
|
|
319
|
+
FROM (
|
|
320
|
+
SELECT
|
|
321
|
+
user_id,
|
|
322
|
+
session_id,
|
|
323
|
+
event_type AS step
|
|
324
|
+
FROM events
|
|
325
|
+
WHERE event_date = today()
|
|
326
|
+
)
|
|
327
|
+
GROUP BY session_id;
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
### Cohort Analysis
|
|
331
|
+
|
|
332
|
+
```sql
|
|
333
|
+
-- User cohorts by signup month
|
|
334
|
+
SELECT
|
|
335
|
+
toStartOfMonth(signup_date) AS cohort,
|
|
336
|
+
toStartOfMonth(activity_date) AS month,
|
|
337
|
+
dateDiff('month', cohort, month) AS months_since_signup,
|
|
338
|
+
count(DISTINCT user_id) AS active_users
|
|
339
|
+
FROM (
|
|
340
|
+
SELECT
|
|
341
|
+
user_id,
|
|
342
|
+
min(toDate(timestamp)) OVER (PARTITION BY user_id) AS signup_date,
|
|
343
|
+
toDate(timestamp) AS activity_date
|
|
344
|
+
FROM events
|
|
345
|
+
)
|
|
346
|
+
GROUP BY cohort, month, months_since_signup
|
|
347
|
+
ORDER BY cohort, months_since_signup;
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
## Data Pipeline Patterns
|
|
351
|
+
|
|
352
|
+
### ETL Pattern
|
|
353
|
+
|
|
354
|
+
```typescript
|
|
355
|
+
// Extract, Transform, Load
|
|
356
|
+
async function etlPipeline() {
|
|
357
|
+
// 1. Extract from source
|
|
358
|
+
const rawData = await extractFromPostgres()
|
|
359
|
+
|
|
360
|
+
// 2. Transform
|
|
361
|
+
const transformed = rawData.map(row => ({
|
|
362
|
+
date: new Date(row.created_at).toISOString().split('T')[0],
|
|
363
|
+
market_id: row.market_slug,
|
|
364
|
+
volume: parseFloat(row.total_volume),
|
|
365
|
+
trades: parseInt(row.trade_count)
|
|
366
|
+
}))
|
|
367
|
+
|
|
368
|
+
// 3. Load to ClickHouse
|
|
369
|
+
await bulkInsertToClickHouse(transformed)
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// Run periodically
|
|
373
|
+
setInterval(etlPipeline, 60 * 60 * 1000) // Every hour
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
### Change Data Capture (CDC)
|
|
377
|
+
|
|
378
|
+
```typescript
|
|
379
|
+
// Listen to PostgreSQL changes and sync to ClickHouse
|
|
380
|
+
import { Client } from 'pg'
|
|
381
|
+
|
|
382
|
+
const pgClient = new Client({ connectionString: process.env.DATABASE_URL })
|
|
383
|
+
|
|
384
|
+
pgClient.query('LISTEN market_updates')
|
|
385
|
+
|
|
386
|
+
pgClient.on('notification', async (msg) => {
|
|
387
|
+
const update = JSON.parse(msg.payload)
|
|
388
|
+
|
|
389
|
+
await clickhouse.insert('market_updates', [
|
|
390
|
+
{
|
|
391
|
+
market_id: update.id,
|
|
392
|
+
event_type: update.operation, // INSERT, UPDATE, DELETE
|
|
393
|
+
timestamp: new Date(),
|
|
394
|
+
data: JSON.stringify(update.new_data)
|
|
395
|
+
}
|
|
396
|
+
])
|
|
397
|
+
})
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
## Best Practices
|
|
401
|
+
|
|
402
|
+
### 1. Partitioning Strategy
|
|
403
|
+
- Partition by time (usually month or day)
|
|
404
|
+
- Avoid too many partitions (performance impact)
|
|
405
|
+
- Use DATE type for partition key
|
|
406
|
+
|
|
407
|
+
### 2. Ordering Key
|
|
408
|
+
- Put most frequently filtered columns first
|
|
409
|
+
- Consider cardinality (high cardinality first)
|
|
410
|
+
- Order impacts compression
|
|
411
|
+
|
|
412
|
+
### 3. Data Types
|
|
413
|
+
- Use smallest appropriate type (UInt32 vs UInt64)
|
|
414
|
+
- Use LowCardinality for repeated strings
|
|
415
|
+
- Use Enum for categorical data
|
|
416
|
+
|
|
417
|
+
### 4. Avoid
|
|
418
|
+
- SELECT * (specify columns)
|
|
419
|
+
- FINAL (merge data before query instead)
|
|
420
|
+
- Too many JOINs (denormalize for analytics)
|
|
421
|
+
- Small frequent inserts (batch instead)
|
|
422
|
+
|
|
423
|
+
### 5. Monitoring
|
|
424
|
+
- Track query performance
|
|
425
|
+
- Monitor disk usage
|
|
426
|
+
- Check merge operations
|
|
427
|
+
- Review slow query log
|
|
428
|
+
|
|
429
|
+
**Remember**: ClickHouse excels at analytical workloads. Design tables for your query patterns, batch inserts, and leverage materialized views for real-time aggregations.
|