tdengine.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package db
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strings"
  8. "time"
  9. _ "github.com/taosdata/driver-go/v3/taosRestful"
  10. )
  11. var TD *sql.DB
  12. func InitTDengine() {
  13. dsn := os.Getenv("TD_DSN")
  14. if dsn == "" {
  15. log.Println("TD_DSN not set, skipping TDengine initialization")
  16. return
  17. }
  18. // Connect to TDengine using RESTful driver (CGO_ENABLED=0 friendly)
  19. var err error
  20. TD, err = sql.Open("taosRestful", dsn)
  21. if err != nil {
  22. log.Printf("Failed to open TDengine connection: %v\n", err)
  23. return
  24. }
  25. if err := TD.Ping(); err != nil {
  26. log.Printf("Failed to ping TDengine: %v\n", err)
  27. return
  28. }
  29. log.Println("Connected to TDengine")
  30. // Initialize Schema
  31. initSchema()
  32. }
  33. func initSchema() {
  34. // Create Database
  35. _, err := TD.Exec("CREATE DATABASE IF NOT EXISTS power_db KEEP 365 DURATION 10 BUFFER 16")
  36. if err != nil {
  37. log.Printf("Failed to create database: %v\n", err)
  38. return
  39. }
  40. // Switch to database
  41. _, err = TD.Exec("USE power_db")
  42. if err != nil {
  43. log.Printf("Failed to use database: %v\n", err)
  44. return
  45. }
  46. // Create Super Table for standard metrics
  47. // Using a "Single Value" model for flexibility:
  48. // ts: timestamp
  49. // val: double value
  50. // Tags: device_id, metric (e.g., 'power', 'voltage'), location_id
  51. stSql := `CREATE STABLE IF NOT EXISTS readings (
  52. ts TIMESTAMP,
  53. val DOUBLE
  54. ) TAGS (
  55. device_id BINARY(64),
  56. metric BINARY(256),
  57. location_id BINARY(64)
  58. )`
  59. _, err = TD.Exec(stSql)
  60. if err != nil {
  61. log.Printf("Failed to create stable readings: %v\n", err)
  62. } else {
  63. log.Println("TDengine schema initialized")
  64. }
  65. // Create Super Table for switch logs
  66. stSwitchSql := `CREATE STABLE IF NOT EXISTS switch_logs (
  67. ts TIMESTAMP,
  68. val BOOL
  69. ) TAGS (
  70. device_id BINARY(64),
  71. metric BINARY(256),
  72. location_id BINARY(64)
  73. )`
  74. _, err = TD.Exec(stSwitchSql)
  75. if err != nil {
  76. log.Printf("Failed to create stable switch_logs: %v\n", err)
  77. }
  78. // Upgrade schema for existing tables (ignore error if redundant)
  79. _, _ = TD.Exec("ALTER STABLE readings MODIFY TAG metric BINARY(256)")
  80. }
  81. // InsertReading inserts a single reading
  82. func InsertReading(deviceID string, metric string, val float64, locationID string, ts time.Time) error {
  83. if TD == nil {
  84. return fmt.Errorf("TDengine not initialized")
  85. }
  86. // Sanitize table name: d_{device_id}_{metric}
  87. // We need to ensure device_id and metric are safe for table names.
  88. // UUIDs are safe (with hyphens replaced by underscores).
  89. // Metrics should be alphanumeric.
  90. // Actually, using automatic table creation via INSERT ... USING is best.
  91. // Table name: t_{device_id}_{metric} (hash or sanitized)
  92. // But simple string concatenation is risky if not sanitized.
  93. // Let's rely on prepared statements or careful construction.
  94. // Construct table name
  95. // deviceID: usually UUID
  96. // metric: e.g. "power", "voltage"
  97. // locationID: UUID or empty
  98. // Safe table name construction
  99. // Replace '-' with '_' and '.' with '_' to ensure valid table name
  100. safeMetric := metric
  101. safeMetric = strings.ReplaceAll(safeMetric, ".", "_")
  102. safeMetric = strings.ReplaceAll(safeMetric, "-", "_")
  103. safeDID := fmt.Sprintf("d_%s_%s", strings.ReplaceAll(deviceID, "-", "_"), safeMetric)
  104. // Remove special chars if any (simplified)
  105. // SQL: INSERT INTO {table_name} USING readings TAGS (...) VALUES (?, ?)
  106. // Note: taosSql driver might not support prepared statement for Table Name or Tags in USING clause well in all versions.
  107. // Standard approach: fmt.Sprintf for the SQL structure.
  108. query := fmt.Sprintf("INSERT INTO `%s` USING readings TAGS ('%s', '%s', '%s') VALUES (?, ?)",
  109. safeDID, deviceID, metric, locationID)
  110. _, err := TD.Exec(query, ts, val)
  111. return err
  112. }
  113. // InsertSwitchLog inserts a switch status log
  114. func InsertSwitchLog(deviceID string, metric string, val bool, locationID string, ts time.Time) error {
  115. if TD == nil {
  116. return fmt.Errorf("TDengine not initialized")
  117. }
  118. safeMetric := metric
  119. safeMetric = strings.ReplaceAll(safeMetric, ".", "_")
  120. safeMetric = strings.ReplaceAll(safeMetric, "-", "_")
  121. // Use 's_' prefix for switch tables
  122. safeDID := fmt.Sprintf("s_%s_%s", strings.ReplaceAll(deviceID, "-", "_"), safeMetric)
  123. query := fmt.Sprintf("INSERT INTO `%s` USING switch_logs TAGS ('%s', '%s', '%s') VALUES (?, ?)",
  124. safeDID, deviceID, metric, locationID)
  125. _, err := TD.Exec(query, ts, val)
  126. return err
  127. }
  128. // ReadingHistory represents aggregated historical data
  129. type ReadingHistory struct {
  130. Ts string `json:"ts"`
  131. Open float64 `json:"open"`
  132. High float64 `json:"high"`
  133. Low float64 `json:"low"`
  134. Close float64 `json:"close"`
  135. Avg float64 `json:"avg"`
  136. DeviceID string `json:"device_id"`
  137. }
  138. // GetReadings fetches aggregated historical data
  139. func GetReadings(deviceIDs []string, metric string, start, end time.Time, interval string) ([]ReadingHistory, error) {
  140. if TD == nil {
  141. return nil, fmt.Errorf("TDengine not initialized")
  142. }
  143. if len(deviceIDs) == 0 {
  144. return []ReadingHistory{}, nil
  145. }
  146. // Construct device_id IN clause
  147. // device_id is BINARY in TDengine, so we quote them
  148. inClause := ""
  149. for i, id := range deviceIDs {
  150. if i > 0 {
  151. inClause += ", "
  152. }
  153. inClause += fmt.Sprintf("'%s'", id)
  154. }
  155. // Metric Filter
  156. metricFilter := ""
  157. if metric != "" {
  158. metricFilter = fmt.Sprintf("AND metric = '%s'", metric)
  159. }
  160. var query string
  161. // 如果 interval 为 "raw",查询原始数据(不聚合)
  162. // 这有助于在数据量少时直接查看,或者调试
  163. if interval == "raw" {
  164. // 限制 2000 条以防止数据量过大
  165. // 选择 val 重复 5 次是为了复用 Scan 逻辑 (Open, High, Low, Close, Avg)
  166. query = fmt.Sprintf(`SELECT
  167. ts, val, val, val, val, val, device_id
  168. FROM readings
  169. WHERE device_id IN (%s) %s AND ts >= '%s' AND ts <= '%s'
  170. ORDER BY ts ASC LIMIT 2000`,
  171. inClause, metricFilter, start.Format("2006-01-02 15:04:05"), end.Format("2006-01-02 15:04:05"))
  172. } else {
  173. // 聚合查询
  174. if interval == "" {
  175. interval = "1h"
  176. }
  177. if interval == "1y" {
  178. interval = "365d"
  179. }
  180. // TDengine Query: Simplified to avoid GROUP BY + INTERVAL conflict
  181. query = fmt.Sprintf(`SELECT
  182. _wstart, FIRST(val), MAX(val), MIN(val), LAST(val), AVG(val)
  183. FROM readings
  184. WHERE device_id IN (%s) %s AND ts >= '%s' AND ts <= '%s'
  185. INTERVAL(%s)
  186. ORDER BY _wstart ASC`,
  187. inClause, metricFilter, start.Format("2006-01-02 15:04:05"), end.Format("2006-01-02 15:04:05"), interval)
  188. }
  189. rows, err := TD.Query(query)
  190. if err != nil {
  191. log.Printf("TDengine Query Error: %v\nQuery: %s", err, query)
  192. return nil, err
  193. }
  194. defer rows.Close()
  195. var results []ReadingHistory
  196. for rows.Next() {
  197. var r ReadingHistory
  198. var ts time.Time
  199. // var did sql.NullString
  200. // Result columns: ts, open, high, low, close, avg
  201. if err := rows.Scan(&ts, &r.Open, &r.High, &r.Low, &r.Close, &r.Avg); err != nil {
  202. log.Printf("Scan error: %v", err)
  203. continue
  204. }
  205. r.Ts = ts.Format("2006-01-02 15:04:05")
  206. // If we only queried one device, we can fill it here
  207. if len(deviceIDs) == 1 {
  208. r.DeviceID = deviceIDs[0]
  209. }
  210. results = append(results, r)
  211. }
  212. return results, nil
  213. }