MCPcopy Create free account
hub / github.com/astercloud/aster / GetStats

Method GetStats

pkg/memory/logic/store_mysql.go:342–413  ·  view source on GitHub ↗

GetStats 获取统计信息

(ctx context.Context, namespace string)

Source from the content-addressed store, hash-verified

340
341// GetStats 获取统计信息
342func (s *MySQLStore) GetStats(ctx context.Context, namespace string) (*MemoryStats, error) {
343 if s.closed {
344 return nil, ErrStoreClosed
345 }
346
347 stats := &MemoryStats{
348 CountByType: make(map[string]int),
349 CountByScope: make(map[MemoryScope]int),
350 }
351
352 // 总数和平均置信度
353 query := fmt.Sprintf(`
354 SELECT COUNT(*), COALESCE(AVG(confidence), 0), COALESCE(MAX(updated_at), NOW())
355 FROM %s
356 WHERE (? = '' OR namespace = ?)
357 `, s.tableName)
358
359 var lastUpdated time.Time
360 err := s.db.QueryRowContext(ctx, query, namespace, namespace).Scan(&stats.TotalCount, &stats.AverageConfidence, &lastUpdated)
361 if err != nil {
362 return nil, NewStoreError("QUERY_ERROR", "failed to get stats", err)
363 }
364 stats.LastUpdated = lastUpdated
365
366 // 按类型统计
367 query = fmt.Sprintf(`
368 SELECT type, COUNT(*)
369 FROM %s
370 WHERE (? = '' OR namespace = ?)
371 GROUP BY type
372 `, s.tableName)
373
374 rows, err := s.db.QueryContext(ctx, query, namespace, namespace)
375 if err != nil {
376 return nil, NewStoreError("QUERY_ERROR", "failed to get type stats", err)
377 }
378 defer func() { _ = rows.Close() }()
379
380 for rows.Next() {
381 var memType string
382 var count int
383 if err := rows.Scan(&memType, &count); err != nil {
384 continue
385 }
386 stats.CountByType[memType] = count
387 }
388
389 // 按作用域统计
390 query = fmt.Sprintf(`
391 SELECT scope, COUNT(*)
392 FROM %s
393 WHERE (? = '' OR namespace = ?)
394 GROUP BY scope
395 `, s.tableName)
396
397 rows, err = s.db.QueryContext(ctx, query, namespace, namespace)
398 if err != nil {
399 return nil, NewStoreError("QUERY_ERROR", "failed to get scope stats", err)

Callers

nothing calls this directly

Calls 3

NewStoreErrorFunction · 0.85
MemoryScopeTypeAlias · 0.70
CloseMethod · 0.65

Tested by

no test coverage detected