package main import ( "context" "fmt" "log" "time" "git.pyer.club/kingecg/gotidb/pkg/engine" "git.pyer.club/kingecg/gotidb/pkg/engine/memory" ) func main() { // 创建引擎注册表 registry := engine.NewEngineRegistry() // 注册内存引擎 memory.Register(registry) // 创建引擎配置 config := engine.NewEngineConfig(). WithMaxRetention(24 * time.Hour). WithMaxPoints(1000) // 创建内存引擎实例 eng, err := registry.Create("memory", config) if err != nil { log.Fatal("Failed to create engine:", err) } // 打开引擎 if err := eng.Open(); err != nil { log.Fatal("Failed to open engine:", err) } defer eng.Close() // 写入一些测试数据 deviceID := "device001" metricCode := "temperature" now := time.Now() // 写入单个数据点 point := engine.DataPoint{ DeviceID: deviceID, MetricCode: metricCode, Labels: map[string]string{ "location": "room1", "floor": "1st", }, Value: 25.5, Timestamp: now.UnixNano(), } if err := eng.WritePoint(context.Background(), point); err != nil { log.Fatal("Failed to write point:", err) } // 写入一批数据点 var points []engine.DataPoint for i := 0; i < 10; i++ { points = append(points, engine.DataPoint{ DeviceID: deviceID, MetricCode: metricCode, Labels: map[string]string{ "location": "room1", "floor": "1st", }, Value: 25.5 + float64(i), Timestamp: now.Add(time.Duration(i) * time.Second).UnixNano(), }) } if err := eng.WriteBatch(context.Background(), points); err != nil { log.Fatal("Failed to write batch:", err) } // 查询最新数据 latestQuery := engine.NewQueryBuilder(). ForMetric(metricCode). WithTag("location", engine.OpEqual, "room1"). Build() latestQuery.Type = engine.QueryTypeLatest result, err := eng.Query(context.Background(), latestQuery) if err != nil { log.Fatal("Failed to query latest data:", err) } if tsResult, ok := result.(*engine.TimeSeriesResult); ok { fmt.Println("\nLatest data:") for _, p := range tsResult.Points { fmt.Printf("Time: %v, Value: %.2f\n", time.Unix(0, p.Timestamp).Format(time.RFC3339), p.Value) } } // 查询原始数据 rawQuery := engine.NewQueryBuilder(). ForMetric(metricCode). WithTimeRange(now.Add(-1*time.Hour).UnixNano(), now.UnixNano()). WithTag("location", engine.OpEqual, "room1"). Build() result, err = eng.Query(context.Background(), rawQuery) if err != nil { log.Fatal("Failed to query raw data:", err) } if tsResult, ok := result.(*engine.TimeSeriesResult); ok { fmt.Println("\nRaw data:") for _, p := range tsResult.Points { fmt.Printf("Time: %v, Value: %.2f\n", time.Unix(0, p.Timestamp).Format(time.RFC3339), p.Value) } } // 查询聚合数据 aggQuery := engine.NewQueryBuilder(). ForMetric(metricCode). WithTimeRange(now.Add(-1*time.Hour).UnixNano(), now.UnixNano()). WithTag("location", engine.OpEqual, "room1"). WithAggregation(engine.AggAvg, 5*time.Minute). Build() result, err = eng.Query(context.Background(), aggQuery) if err != nil { log.Fatal("Failed to query aggregate data:", err) } if aggResult, ok := result.(*engine.AggregateResult); ok { fmt.Println("\nAggregate data (5-minute averages):") for _, g := range aggResult.Groups { fmt.Printf("Time range: %v - %v, Average: %.2f, Count: %d\n", time.Unix(0, g.StartTime).Format(time.RFC3339), time.Unix(0, g.EndTime).Format(time.RFC3339), g.Value, g.Count) } } // 打印引擎统计信息 stats := eng.Stats() fmt.Printf("\nEngine stats:\n") fmt.Printf("Points count: %d\n", stats.PointsCount) fmt.Printf("Last write time: %v\n", stats.LastWriteTime.Format(time.RFC3339)) // 打印引擎能力 caps := eng.Capabilities() fmt.Printf("\nEngine capabilities:\n") fmt.Printf("Supports compression: %v\n", caps.SupportsCompression) fmt.Printf("Supports persistence: %v\n", caps.SupportsPersistence) fmt.Printf("Supports replication: %v\n", caps.SupportsReplication) fmt.Printf("Max concurrent writes: %d\n", caps.MaxConcurrentWrites) }