dust_day.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package timetask
  2. import (
  3. "dust-monitor/errors"
  4. "dust-monitor/model"
  5. "dust-monitor/util"
  6. "encoding/json"
  7. "fmt"
  8. "time"
  9. )
  10. // 获取天表中已有的最大时间
  11. func getLastMaxDay(sn string) (string, error) {
  12. sql := fmt.Sprintf("select * from \"%s\" order by time desc limit 1", sn)
  13. dbname := "dust_monitor_2031"
  14. array := []DustInfo{}
  15. _, err := model.Query(sql, dbname, &array)
  16. if err != nil {
  17. return "", errors.DataBaseError
  18. }
  19. if len(array) == 0 {
  20. return "", nil
  21. }
  22. t := util.ParseTime(array[0].Time).Add(8*time.Hour)
  23. ret := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location())
  24. return ret.Format(layout), nil
  25. }
  26. // 获取未入库的天数据
  27. func getUnwritedDayData(start string, end string, sn string) ([]DustInfo, error) {
  28. sql := fmt.Sprintf("SELECT mean(*) FROM \"%s\"", sn)
  29. whereArray := []string{}
  30. if start != "" {
  31. whereArray = append(whereArray, fmt.Sprintf("time >= '%s'", start))
  32. }
  33. if end != "" {
  34. whereArray = append(whereArray, fmt.Sprintf("time < '%s'", end))
  35. }
  36. where := ""
  37. for _, v := range whereArray {
  38. if where == "" {
  39. where = fmt.Sprintf("where %s", v)
  40. continue
  41. }
  42. where = fmt.Sprintf("%s and %s", where, v)
  43. }
  44. sql = fmt.Sprintf("%s %s GROUP BY time(1d) tz('Asia/Shanghai')", sql, where)
  45. array := []DustInfoMean{}
  46. dbname := "dust_monitor_2011"
  47. _, err := model.Query(sql, dbname, &array)
  48. if err != nil {
  49. return nil, errors.DataBaseError
  50. }
  51. if len(array) == 0 {
  52. return nil, nil
  53. }
  54. ret := make([]DustInfo, len(array))
  55. for i, v := range array {
  56. item := DustInfo{
  57. Time:v.Time,
  58. Pm25Avg:v.Pm25Avg,
  59. Pm10Avg:v.Pm10Avg,
  60. W01Avg:v.W01Avg,
  61. W03:v.W03,
  62. W02Avg:v.W02Avg,
  63. P01Avg:v.P01Avg,
  64. TspAvg:v.TspAvg,
  65. T01Avg:v.T01Avg,
  66. H01Avg:v.H01Avg,
  67. R01Avg:v.R01Avg,
  68. Lat:v.Lat,
  69. Lng:v.Lng,
  70. B03Avg:v.B03Avg,
  71. }
  72. ret[i] = item
  73. }
  74. return ret, nil
  75. }
  76. // 写入天数据
  77. func writeDayData(sn string, data []DustInfo) error {
  78. if len(data) == 0 {
  79. return nil
  80. }
  81. bytes, _ := json.Marshal(data)
  82. array := []map[string]interface{}{}
  83. json.Unmarshal(bytes, &array)
  84. tarray := make([]time.Time, len(array))
  85. for i, v := range array {
  86. ti, ok := v["time"]
  87. if !ok {
  88. return errors.ParamsError
  89. }
  90. str, ok := ti.(string)
  91. if !ok {
  92. return errors.ParamsError
  93. }
  94. t := util.ParseTime(str)
  95. tarray[i] = t
  96. delete(array[i], "time")
  97. }
  98. tags := map[string]string{
  99. "sn":sn,
  100. }
  101. return model.WriteDustDatas(sn, tags, array, tarray, "2031")
  102. }
  103. func handleDustDay(start string, end string, sn string) {
  104. lastMaxTime, err := getLastMaxDay(sn)
  105. if err != nil {
  106. return
  107. }
  108. if lastMaxTime == "" {
  109. start = ""
  110. } else {
  111. tmpStart := util.ParseTime(lastMaxTime)
  112. tmpStart = tmpStart.Add(24 *time.Hour)
  113. if tmpStart.Unix() > util.ParseTime(start).Unix() {
  114. return
  115. }
  116. start = tmpStart.Format(layout)
  117. }
  118. datas, err := getUnwritedDayData(start, end, sn)
  119. if err != nil {
  120. return
  121. }
  122. writeDayData(sn, datas)
  123. }