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

Method GetStats

pkg/memory/logic/store_postgres.go:352–423  ·  view source on GitHub ↗

GetStats 获取统计信息

(ctx context.Context, namespace string)

Source from the content-addressed store, hash-verified

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