-
Notifications
You must be signed in to change notification settings - Fork 322
/
fund.go
112 lines (101 loc) · 3.24 KB
/
fund.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Package cron 定时任务
package cron
import (
"context"
"encoding/json"
"io/ioutil"
"time"
"github.com/axiaoxin-com/goutils"
"github.com/axiaoxin-com/investool/core"
"github.com/axiaoxin-com/investool/datacenter"
"github.com/axiaoxin-com/investool/datacenter/eastmoney"
"github.com/axiaoxin-com/investool/models"
"github.com/axiaoxin-com/logging"
)
// SyncFund 同步基金数据
func SyncFund() {
if !goutils.IsTradingDay() {
return
}
ctx := context.Background()
logging.Info(ctx, "SyncFund request start...")
// 获取全量列表
efundlist, err := datacenter.EastMoney.QueryAllFundList(ctx, eastmoney.FundTypeALL)
if err != nil {
logging.Error(ctx, "SyncFund QueryAllFundList error:"+err.Error())
promSyncError.WithLabelValues("SyncFund").Inc()
return
}
fundCodes := []string{}
for _, efund := range efundlist {
fundCodes = append(fundCodes, efund.Fcode)
}
s := core.NewSearcher(ctx)
data, err := s.SearchFunds(ctx, fundCodes)
fundlist := models.FundList{}
typeMap := map[string]struct{}{}
for _, fund := range data {
fundlist = append(fundlist, fund)
typeMap[fund.Type] = struct{}{}
}
// 更新 services 变量
models.FundAllList = fundlist
fundtypes := []string{}
for k := range typeMap {
fundtypes = append(fundtypes, k)
}
models.FundTypeList = fundtypes
// 更新同步时间
models.SyncFundTime = time.Now()
// 更新4433列表
Update4433()
// 更新文件
b, err := json.Marshal(efundlist)
if err != nil {
logging.Errorf(ctx, "SyncFund json marshal efundlist error:%v", err)
promSyncError.WithLabelValues("SyncFund").Inc()
} else if err := ioutil.WriteFile(models.RawFundAllListFilename, b, 0666); err != nil {
logging.Errorf(ctx, "SyncFund WriteFile efundlist error:%v", err)
promSyncError.WithLabelValues("SyncFund").Inc()
}
b, err = json.Marshal(fundlist)
if err != nil {
logging.Errorf(ctx, "SyncFund json marshal fundlist error:%v", err)
promSyncError.WithLabelValues("SyncFund").Inc()
} else if err := ioutil.WriteFile(models.FundAllListFilename, b, 0666); err != nil {
logging.Errorf(ctx, "SyncFund WriteFile fundlist error:%v", err)
promSyncError.WithLabelValues("SyncFund").Inc()
}
b, err = json.Marshal(models.FundTypeList)
if err != nil {
logging.Errorf(ctx, "SyncFund json marshal fundtypelist error:%v", err)
promSyncError.WithLabelValues("SyncFund").Inc()
} else if err := ioutil.WriteFile(models.FundTypeListFilename, b, 0666); err != nil {
logging.Errorf(ctx, "SyncFund WriteFile fundtypelist error:%v", err)
promSyncError.WithLabelValues("SyncFund").Inc()
}
}
// Update4433 更新4433检测结果
func Update4433() {
ctx := context.Background()
fundlist := models.FundList{}
for _, fund := range models.FundAllList {
if fund.Is4433(ctx) {
fundlist = append(fundlist, fund)
}
}
// 更新 models 变量
fundlist.Sort(models.FundSortTypeWeek)
models.Fund4433List = fundlist
// 更新文件
b, err := json.Marshal(fundlist)
if err != nil {
logging.Errorf(ctx, "Update4433 json marshal error:", err)
promSyncError.WithLabelValues("Update4433").Inc()
return
} else if err := ioutil.WriteFile(models.Fund4433ListFilename, b, 0666); err != nil {
logging.Errorf(ctx, "Update4433 WriteFile error:", err)
promSyncError.WithLabelValues("Update4433").Inc()
return
}
}