tower.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package model
  4. import (
  5. "tower-monitor/consts"
  6. "tower-monitor/errors"
  7. "encoding/json"
  8. "fmt"
  9. "strings"
  10. "time"
  11. "github.com/jaryhe/gopkgs/logger"
  12. "go.uber.org/zap"
  13. iclient "github.com/influxdata/influxdb/client/v2"
  14. "github.com/jaryhe/gopkgs/influxdb"
  15. )
  16. func WriteTowerData(sn string, tags map[string]string, fields map[string]interface{}, t time.Time, cmdType string) error {
  17. dbName := "tower_monitor_" + cmdType
  18. fmt.Printf("xxxxxxxx:%v,%v\n", dbName,sn)
  19. bp, err := iclient.NewBatchPoints(iclient.BatchPointsConfig{
  20. Database: dbName,
  21. Precision: "ns",
  22. })
  23. if err != nil {
  24. logger.Error("func",
  25. zap.String("call", "NewBatchPoints"),
  26. zap.String("args", dbName),
  27. zap.String("error", err.Error()))
  28. return errors.DataBaseError
  29. }
  30. pt, err := iclient.NewPoint(
  31. sn,
  32. tags,
  33. fields,
  34. t,
  35. )
  36. if err != nil {
  37. logger.Error("func",
  38. zap.String("call", "NewPoint"),
  39. zap.String("args", sn),
  40. zap.String("error", err.Error()))
  41. return errors.DataBaseError
  42. }
  43. bp.AddPoint(pt)
  44. err = influxdb.InfluxCli.Write(bp)
  45. if err != nil {
  46. logger.Error("func",
  47. zap.String("call", "InfluxCli.Write"),
  48. zap.String("args", sn),
  49. zap.String("error", err.Error()))
  50. if strings.Contains(err.Error(), consts.DatabaseNotFound) {
  51. Create(influxdb.InfluxCli, dbName)
  52. err = influxdb.InfluxCli.Write(bp)
  53. if err != nil {
  54. return errors.DataBaseError
  55. }
  56. return nil
  57. }
  58. return errors.DataBaseError
  59. }
  60. return nil
  61. }
  62. func Query(sql string, db string, result interface{}) ([]map[string]interface{}, error) {
  63. qt := iclient.Query{
  64. Database: db,
  65. Command: sql,
  66. }
  67. r, err := influxdb.InfluxCli.Query(qt)
  68. if err != nil {
  69. logger.Error("func",
  70. zap.String("call", "Query "+ db),
  71. zap.String("args", sql),
  72. zap.String("error", err.Error()))
  73. return nil, err
  74. }
  75. if r == nil {
  76. return nil, nil
  77. }
  78. if len(r.Results) == 0 {
  79. return nil, nil
  80. }
  81. if len(r.Results[0].Series) == 0 {
  82. return nil, nil
  83. }
  84. colNames := r.Results[0].Series[0].Columns
  85. var marray = make([]map[string]interface{}, len(r.Results[0].Series[0].Values))
  86. for i, row := range r.Results[0].Series[0].Values {
  87. item := map[string]interface{}{}
  88. for j, v := range row {
  89. if colNames[j] == "time" {
  90. t, _ := time.Parse(time.RFC3339, v.(string))
  91. v = t.Format("2006-01-02 15:04:05")
  92. }
  93. item[colNames[j]] = v
  94. }
  95. marray[i] = item
  96. }
  97. if result != nil {
  98. bytes, _ := json.Marshal(marray)
  99. err = json.Unmarshal(bytes, result)
  100. if err != nil {
  101. return nil, err
  102. }
  103. }
  104. return marray, nil
  105. }
  106. func Create(conn iclient.Client, db string) error {
  107. qt := iclient.Query{
  108. Command: "create database " + db,
  109. }
  110. _, err := conn.Query(qt)
  111. if err != nil {
  112. return err
  113. }
  114. return nil
  115. }