123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- package services
- import (
- "encoding/json"
- "eta/eta_data_analysis/models"
- "eta/eta_data_analysis/services/kpler"
- "eta/eta_data_analysis/utils"
- "fmt"
- "time"
- "github.com/patrickmn/go-cache"
- )
- func ExcelDataWatch() {
- fmt.Println("kplerExcelWatch start")
- utils.FileLog.Info("kplerExcelWatch start")
- var err error
- defer func() {
- if err != nil {
- fmt.Println("kplerExcelDataWatch Err:" + err.Error())
- utils.FileLog.Info(fmt.Sprintf("kplerExcelDataWatch, Err: %s", err))
- }
- }()
- var cacheClient *cache.Cache
- if cacheClient == nil {
- cacheClient = cache.New(365*24*time.Hour, 365*24*time.Hour)
- }
- /*err = filepath.Walk(utils.KplerExcelFilePath, func(path string, info fs.FileInfo, err error) error {
- if err != nil {
- return err
- }
- if !info.IsDir() {
- fileInfo, e := os.Stat(path)
- if e != nil {
- err = e
- fmt.Println("os.Stat:", err.Error())
- utils.FileLog.Info(fmt.Sprintf("os.Stat, Err: %s", err))
- return err
- }
- winFileAttr := fileInfo.Sys().(*syscall.Win32FileAttributeData)
- modifyTimeStr := utils.SecondToTime(winFileAttr.LastWriteTime.Nanoseconds() / 1e9).Format(utils.FormatDateTime)
- existModifyTime, ok := cacheClient.Get(path)
- if ok {
- existModifyTimeStr := existModifyTime.(string)
- if existModifyTimeStr != modifyTimeStr {
- _, err = GetKplerDataByExcel(path)
- }
- } else {
- _, err = GetKplerDataByExcel(path)
- }
- cacheClient.Delete(path)
- cacheClient.Set(path, modifyTimeStr, 24*time.Hour)
- }
- return nil
- })*/
- }
- // Main function for standalone testing
- func GetKplerDataByExcel(filePath string) {
- filePath = "services/kpler/crude.xlsx"
- fmt.Println("Starting Kpler data processing...")
- // Process the Excel data
- indexData, err :=kpler.ProcessKplerData(filePath)
- if err != nil {
- fmt.Printf("Error processing Excel data: %v\n", err)
- return
- }
- indexList := make([]*models.HandleKplerExcelData, 0)
- // Print the processed data
- for k, index := range indexData {
- // 解析请求参数
- if index.Request != "" {
- flowsRequestItem, err := kpler.ParseSpecificKplerFormulaV2(index.Request)
- if err != nil {
- fmt.Printf("Error parsing formula: %v\n", err)
- continue
- }
-
- indexName := fmt.Sprintf("%s_%s", index.Title, index.Name)
- unit := flowsRequestItem.Unit
- sort := k
- productNameSlice := flowsRequestItem.Products
- productNames := ""
- if len(productNameSlice) > 0 {
- for _, productName := range productNameSlice {
- productNames += productName.Name + ","
- }
- }
- fromZoneNameSlice := flowsRequestItem.Origins
- fromZoneNames := ""
- if len(fromZoneNameSlice) > 0 {
- for _, fromZoneName := range fromZoneNameSlice {
- fromZoneNames += fromZoneName.Name + ","
- }
- }
- toZoneNames := ""
- toZoneNameSlice := flowsRequestItem.Destinations
- if len(toZoneNameSlice) > 0 {
- for _, toZoneName := range toZoneNameSlice {
- toZoneNames += toZoneName.Name + ","
- }
- }
- flowDirection := flowsRequestItem.FlowDirection
- granularity := flowsRequestItem.Granularity
- split := flowsRequestItem.Split
- excelDataMap := make(map[string]string)
- if len(index.DataPoints) > 0 {
- for _, dataPoint := range index.DataPoints {
- excelDataMap[dataPoint.EndDate] = dataPoint.Value
- }
- }
- tmp := models.HandleKplerExcelData{
- IndexName: indexName,
- Unit: unit,
- Sort: sort,
- ProductNames: productNames,
- FromZoneNames: fromZoneNames,
- ToZoneNames: toZoneNames,
- FlowDirection: flowDirection,
- Granularity: granularity,
- Split: split,
- ExcelDataMap: excelDataMap,
- }
- indexList = append(indexList, &tmp)
- }
- }
- if len(indexList) > 0 {
- params := make(map[string]interface{})
- params["List"] = indexList
- params["TerminalCode"] = ""
- result, e := PostEdbLib(params, utils.LIB_ROUTE_KPLER_DATA)
- if e != nil {
- b, _ := json.Marshal(params)
- utils.FileLog.Info(fmt.Sprintf("sheet :GetKplerDataByExcel PostEdbLib err: %s, params: %s", e.Error(), string(b)))
- return
- }
- resp := new(models.BaseEdbLibResponse)
- if e := json.Unmarshal(result, &resp); e != nil {
- utils.FileLog.Info(fmt.Sprintf("sheet :GetKplerDataByExcel json.Unmarshal err: %s", e))
- return
- }
- if resp.Ret != 200 {
- utils.FileLog.Info(fmt.Sprintf("sheet :GetKplerDataByExcel Msg: %s, ErrMsg: %s", resp.Msg, resp.ErrMsg))
- return
- }
- }
- // 传递list给指标服务
- fmt.Println("GetKplerDataByExcel completed successfully!")
- }
|