tdengine.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package db
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "log"
  6. "os"
  7. "time"
  8. _ "github.com/taosdata/driver-go/v3/taosRestful"
  9. )
  10. var TD *sql.DB
  11. func InitTDengine() {
  12. dsn := os.Getenv("TD_DSN")
  13. if dsn == "" {
  14. log.Println("TD_DSN not set, skipping TDengine initialization")
  15. return
  16. }
  17. // Connect to TDengine using RESTful driver (CGO_ENABLED=0 friendly)
  18. var err error
  19. TD, err = sql.Open("taosRestful", dsn)
  20. if err != nil {
  21. log.Printf("Failed to open TDengine connection: %v\n", err)
  22. return
  23. }
  24. if err := TD.Ping(); err != nil {
  25. log.Printf("Failed to ping TDengine: %v\n", err)
  26. return
  27. }
  28. log.Println("Connected to TDengine")
  29. // Initialize Schema
  30. initSchema()
  31. }
  32. func initSchema() {
  33. // Create Database
  34. _, err := TD.Exec("CREATE DATABASE IF NOT EXISTS power_db KEEP 365 DURATION 10 BUFFER 16")
  35. if err != nil {
  36. log.Printf("Failed to create database: %v\n", err)
  37. return
  38. }
  39. // Switch to database
  40. _, err = TD.Exec("USE power_db")
  41. if err != nil {
  42. log.Printf("Failed to use database: %v\n", err)
  43. return
  44. }
  45. // Create Super Table for standard metrics
  46. // Using a "Single Value" model for flexibility:
  47. // ts: timestamp
  48. // val: double value
  49. // Tags: device_id, metric (e.g., 'power', 'voltage'), location_id
  50. stSql := `CREATE STABLE IF NOT EXISTS readings (
  51. ts TIMESTAMP,
  52. val DOUBLE
  53. ) TAGS (
  54. device_id BINARY(64),
  55. metric BINARY(32),
  56. location_id BINARY(64)
  57. )`
  58. _, err = TD.Exec(stSql)
  59. if err != nil {
  60. log.Printf("Failed to create stable readings: %v\n", err)
  61. } else {
  62. log.Println("TDengine schema initialized")
  63. }
  64. }
  65. // InsertReading inserts a single reading
  66. func InsertReading(deviceID string, metric string, val float64, locationID string, ts time.Time) error {
  67. if TD == nil {
  68. return fmt.Errorf("TDengine not initialized")
  69. }
  70. // Sanitize table name: d_{device_id}_{metric}
  71. // We need to ensure device_id and metric are safe for table names.
  72. // UUIDs are safe (with hyphens replaced by underscores).
  73. // Metrics should be alphanumeric.
  74. // Actually, using automatic table creation via INSERT ... USING is best.
  75. // Table name: t_{device_id}_{metric} (hash or sanitized)
  76. // But simple string concatenation is risky if not sanitized.
  77. // Let's rely on prepared statements or careful construction.
  78. // Construct table name
  79. // deviceID: usually UUID
  80. // metric: e.g. "power", "voltage"
  81. // locationID: UUID or empty
  82. // Safe table name construction
  83. // Replace '-' with '_'
  84. safeDID := fmt.Sprintf("d_%s_%s", deviceID, metric)
  85. // Remove special chars if any (simplified)
  86. // SQL: INSERT INTO {table_name} USING readings TAGS (...) VALUES (?, ?)
  87. // Note: taosSql driver might not support prepared statement for Table Name or Tags in USING clause well in all versions.
  88. // Standard approach: fmt.Sprintf for the SQL structure.
  89. query := fmt.Sprintf("INSERT INTO `%s` USING readings TAGS ('%s', '%s', '%s') VALUES (?, ?)",
  90. safeDID, deviceID, metric, locationID)
  91. _, err := TD.Exec(query, ts, val)
  92. return err
  93. }
  94. // ReadingHistory represents aggregated historical data
  95. type ReadingHistory struct {
  96. Ts string `json:"ts"`
  97. Open float64 `json:"open"`
  98. High float64 `json:"high"`
  99. Low float64 `json:"low"`
  100. Close float64 `json:"close"`
  101. Avg float64 `json:"avg"`
  102. DeviceID string `json:"device_id"`
  103. }
  104. // GetReadings fetches aggregated historical data
  105. func GetReadings(deviceIDs []string, metric string, start, end time.Time, interval string) ([]ReadingHistory, error) {
  106. if TD == nil {
  107. return nil, fmt.Errorf("TDengine not initialized")
  108. }
  109. if len(deviceIDs) == 0 {
  110. return []ReadingHistory{}, nil
  111. }
  112. // Construct device_id IN clause
  113. // device_id is BINARY in TDengine, so we quote them
  114. inClause := ""
  115. for i, id := range deviceIDs {
  116. if i > 0 {
  117. inClause += ", "
  118. }
  119. inClause += fmt.Sprintf("'%s'", id)
  120. }
  121. // Validate Interval (simple check)
  122. // e.g. 1m, 1h, 1d
  123. if interval == "" {
  124. interval = "1h"
  125. }
  126. // TDengine might not support '1y' directly in all versions, map to days
  127. if interval == "1y" {
  128. interval = "365d"
  129. }
  130. // Query
  131. // Note: ts output format depends on driver, usually time.Time or string.
  132. // Using generic sql driver, it might be time.Time.
  133. // Group by device_id to separate lines.
  134. query := fmt.Sprintf(`SELECT
  135. FIRST(val), MAX(val), MIN(val), LAST(val), AVG(val), device_id
  136. FROM readings
  137. WHERE device_id IN (%s) AND metric = '%s' AND ts >= '%s' AND ts <= '%s'
  138. INTERVAL(%s)
  139. GROUP BY device_id
  140. ORDER BY ts ASC`,
  141. inClause, metric, start.Format("2006-01-02 15:04:05"), end.Format("2006-01-02 15:04:05"), interval)
  142. rows, err := TD.Query(query)
  143. if err != nil {
  144. log.Printf("TDengine Query Error: %v\nQuery: %s", err, query)
  145. return nil, err
  146. }
  147. defer rows.Close()
  148. var results []ReadingHistory
  149. for rows.Next() {
  150. var r ReadingHistory
  151. var ts time.Time
  152. var did sql.NullString // device_id might be grouped?
  153. // Note: TDengine RESTful driver behavior on INTERVAL + GROUP BY:
  154. // Result columns: ts, first, max, min, last, avg, device_id
  155. if err := rows.Scan(&ts, &r.Open, &r.High, &r.Low, &r.Close, &r.Avg, &did); err != nil {
  156. log.Printf("Scan error: %v", err)
  157. continue
  158. }
  159. r.Ts = ts.Format("2006-01-02 15:04:05")
  160. if did.Valid {
  161. r.DeviceID = did.String
  162. }
  163. results = append(results, r)
  164. }
  165. return results, nil
  166. }