-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
131 lines (103 loc) · 3.71 KB
/
main.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"context"
"encoding/json"
"net/http"
"strconv"
"strings"
"time"
"github.com/go-co-op/gocron"
"github.com/kingokeke/go-example-1/binanceUtil"
"github.com/kingokeke/go-example-1/constants"
"github.com/kingokeke/go-example-1/dotenvUtil"
"github.com/kingokeke/go-example-1/mongodbUtil"
"github.com/kingokeke/go-example-1/utils"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
utils.LogToGeneral("App Started...")
ctx := context.Background()
client := mongodbUtil.ConnectToDatabase(&ctx)
defer client.Disconnect(ctx)
db := client.Database(("cs"))
timeInSeconds := [5]int32{constants.FIVE_MINUTES, constants.FIFTEEN_MINUTES, constants.ONE_HOUR, constants.FOUR_HOURS, constants.ONE_DAY}
timeFrames := [5]string{"m5", "m15", "h1", "h4", "d1"}
collectionNames, e := db.ListCollectionNames(ctx, bson.D{})
utils.CheckError(e)
for i, timeframe := range timeFrames {
collectionExists := false
collectionName := "raw-data-" + timeframe
dbCollection := db.Collection(collectionName)
collectionModel := getModel(timeInSeconds[i])
for _, name := range collectionNames {
if name == collectionName {
collectionExists = true
}
}
if collectionExists == false {
createTTLIndexedTablesInDB(&ctx, dbCollection, &collectionModel)
}
}
c := gocron.NewScheduler(time.UTC)
c.Cron(constants.CRON_EVERY_FIVE_MINUTES).Do(func() {
getPriceInfo(&ctx, client, db)
})
c.StartBlocking()
}
func getPriceInfo(ctx *context.Context, client *mongo.Client, database *mongo.Database) {
currentTime := time.Now().UTC()
bodyBytes := binanceUtil.GetPriceInfoAsByteArray()
var priceStatsFromBinance binanceUtil.PriceStats
json.Unmarshal(bodyBytes, &priceStatsFromBinance)
stats := []interface{}{}
for _, stat := range priceStatsFromBinance {
if strings.HasSuffix(stat.Symbol, constants.BNB) || strings.HasSuffix(stat.Symbol, constants.BTC) || strings.HasSuffix(stat.Symbol, constants.ETH) || strings.HasSuffix(stat.Symbol, constants.USDT) {
item := mongodbUtil.RawPriceDataStruct{}
var e error
item.Symbol = stat.Symbol
item.Timestamp = currentTime
item.Price, e = strconv.ParseFloat(stat.Price, 32)
utils.CheckError(e)
item.Volume, e = strconv.ParseFloat(stat.Volume, 32)
utils.CheckError(e)
stats = append(stats, item)
}
}
_, e := database.Collection("raw-data-m5").InsertMany(*ctx, stats)
utils.CheckError(e)
if currentTime.Minute() == 0 || currentTime.Minute() % 15 == 0 {
_, e := database.Collection("raw-data-m15").InsertMany(*ctx, stats)
utils.CheckError(e)
}
if currentTime.Minute() == 0 {
_, e := database.Collection("raw-data-h1").InsertMany(*ctx, stats)
utils.CheckError(e)
}
if (currentTime.Hour() == 0 || currentTime.Hour() % 4 == 0) && currentTime.Minute() == 0 {
_, e := database.Collection("raw-data-h4").InsertMany(*ctx, stats)
utils.CheckError(e)
}
if currentTime.Hour() == 0 && currentTime.Minute() == 0 {
_, e := database.Collection("raw-data-d1").InsertMany(*ctx, stats)
utils.CheckError(e)
}
_, e = http.Get(dotenvUtil.GetValue("MAIN_SERVICE_URL"))
if e != nil {}
utils.LogToGeneral("Successfully persisted raw price data...")
}
func getModel(numberOfSeconds int32) mongo.IndexModel {
return mongo.IndexModel{
Keys: bson.M{
"timestamp": 1,
},
Options: options.Index().SetExpireAfterSeconds(numberOfSeconds * 300),
}
}
func createTTLIndexedTablesInDB(appContext *context.Context, collectionName *mongo.Collection, collectionModel *mongo.IndexModel) {
_, e := collectionName.InsertOne(*appContext, mongodbUtil.RawPriceDataStruct{})
utils.CheckError(e)
_, e = collectionName.Indexes().CreateOne(*appContext, *collectionModel)
utils.CheckError(e)
}