base_from_kpler.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_data_analysis/models"
  5. "eta/eta_data_analysis/services/kpler"
  6. "eta/eta_data_analysis/utils"
  7. "fmt"
  8. "time"
  9. "github.com/patrickmn/go-cache"
  10. )
  11. func ExcelDataWatch() {
  12. fmt.Println("kplerExcelWatch start")
  13. utils.FileLog.Info("kplerExcelWatch start")
  14. var err error
  15. defer func() {
  16. if err != nil {
  17. fmt.Println("kplerExcelDataWatch Err:" + err.Error())
  18. utils.FileLog.Info(fmt.Sprintf("kplerExcelDataWatch, Err: %s", err))
  19. }
  20. }()
  21. var cacheClient *cache.Cache
  22. if cacheClient == nil {
  23. cacheClient = cache.New(365*24*time.Hour, 365*24*time.Hour)
  24. }
  25. /*err = filepath.Walk(utils.KplerExcelFilePath, func(path string, info fs.FileInfo, err error) error {
  26. if err != nil {
  27. return err
  28. }
  29. if !info.IsDir() {
  30. fileInfo, e := os.Stat(path)
  31. if e != nil {
  32. err = e
  33. fmt.Println("os.Stat:", err.Error())
  34. utils.FileLog.Info(fmt.Sprintf("os.Stat, Err: %s", err))
  35. return err
  36. }
  37. winFileAttr := fileInfo.Sys().(*syscall.Win32FileAttributeData)
  38. modifyTimeStr := utils.SecondToTime(winFileAttr.LastWriteTime.Nanoseconds() / 1e9).Format(utils.FormatDateTime)
  39. existModifyTime, ok := cacheClient.Get(path)
  40. if ok {
  41. existModifyTimeStr := existModifyTime.(string)
  42. if existModifyTimeStr != modifyTimeStr {
  43. _, err = GetKplerDataByExcel(path)
  44. }
  45. } else {
  46. _, err = GetKplerDataByExcel(path)
  47. }
  48. cacheClient.Delete(path)
  49. cacheClient.Set(path, modifyTimeStr, 24*time.Hour)
  50. }
  51. return nil
  52. })*/
  53. }
  54. // Main function for standalone testing
  55. func GetKplerDataByExcel(filePath string) {
  56. filePath = "services/kpler/crude.xlsx"
  57. fmt.Println("Starting Kpler data processing...")
  58. // Process the Excel data
  59. indexData, err :=kpler.ProcessKplerData(filePath)
  60. if err != nil {
  61. fmt.Printf("Error processing Excel data: %v\n", err)
  62. return
  63. }
  64. indexList := make([]*models.HandleKplerExcelData, 0)
  65. // Print the processed data
  66. for k, index := range indexData {
  67. // 解析请求参数
  68. if index.Request != "" {
  69. flowsRequestItem, err := kpler.ParseSpecificKplerFormulaV2(index.Request)
  70. if err != nil {
  71. fmt.Printf("Error parsing formula: %v\n", err)
  72. continue
  73. }
  74. indexName := fmt.Sprintf("%s_%s", index.Title, index.Name)
  75. unit := flowsRequestItem.Unit
  76. sort := k
  77. productNameSlice := flowsRequestItem.Products
  78. productNames := ""
  79. if len(productNameSlice) > 0 {
  80. for _, productName := range productNameSlice {
  81. productNames += productName.Name + ","
  82. }
  83. }
  84. fromZoneNameSlice := flowsRequestItem.Origins
  85. fromZoneNames := ""
  86. if len(fromZoneNameSlice) > 0 {
  87. for _, fromZoneName := range fromZoneNameSlice {
  88. fromZoneNames += fromZoneName.Name + ","
  89. }
  90. }
  91. toZoneNames := ""
  92. toZoneNameSlice := flowsRequestItem.Destinations
  93. if len(toZoneNameSlice) > 0 {
  94. for _, toZoneName := range toZoneNameSlice {
  95. toZoneNames += toZoneName.Name + ","
  96. }
  97. }
  98. flowDirection := flowsRequestItem.FlowDirection
  99. granularity := flowsRequestItem.Granularity
  100. split := flowsRequestItem.Split
  101. excelDataMap := make(map[string]string)
  102. if len(index.DataPoints) > 0 {
  103. for _, dataPoint := range index.DataPoints {
  104. excelDataMap[dataPoint.EndDate] = dataPoint.Value
  105. }
  106. }
  107. tmp := models.HandleKplerExcelData{
  108. IndexName: indexName,
  109. Unit: unit,
  110. Sort: sort,
  111. ProductNames: productNames,
  112. FromZoneNames: fromZoneNames,
  113. ToZoneNames: toZoneNames,
  114. FlowDirection: flowDirection,
  115. Granularity: granularity,
  116. Split: split,
  117. ExcelDataMap: excelDataMap,
  118. }
  119. indexList = append(indexList, &tmp)
  120. }
  121. }
  122. if len(indexList) > 0 {
  123. params := make(map[string]interface{})
  124. params["List"] = indexList
  125. params["TerminalCode"] = ""
  126. result, e := PostEdbLib(params, utils.LIB_ROUTE_KPLER_DATA)
  127. if e != nil {
  128. b, _ := json.Marshal(params)
  129. utils.FileLog.Info(fmt.Sprintf("sheet :GetKplerDataByExcel PostEdbLib err: %s, params: %s", e.Error(), string(b)))
  130. return
  131. }
  132. resp := new(models.BaseEdbLibResponse)
  133. if e := json.Unmarshal(result, &resp); e != nil {
  134. utils.FileLog.Info(fmt.Sprintf("sheet :GetKplerDataByExcel json.Unmarshal err: %s", e))
  135. return
  136. }
  137. if resp.Ret != 200 {
  138. utils.FileLog.Info(fmt.Sprintf("sheet :GetKplerDataByExcel Msg: %s, ErrMsg: %s", resp.Msg, resp.ErrMsg))
  139. return
  140. }
  141. }
  142. // 传递list给指标服务
  143. fmt.Println("GetKplerDataByExcel completed successfully!")
  144. }