-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
65 lines (54 loc) · 1.32 KB
/
store.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
package vwap
import (
"fmt"
"math/rand"
"time"
"github.com/bxcodec/faker/v3"
_ "github.com/go-sql-driver/mysql"
"gorm.io/gorm"
)
/* Schema:
* CREATE TABLE vwap_data (
* id SERIAL PRIMARY KEY,
* token_name VARCHAR(50) NOT NULL,
* calculated_at TIMESTAMP NOT NULL DEFAULT NOW(),
* vwap DECIMAL(20, 10) NOT NULL,
* total_volume DECIMAL(20, 10) NOT NULL
* );
*/
type VWAPData struct {
gorm.Model
TokenName string
VWAP float64
TotalVolume float64
CalculatedAt time.Time
}
func store(db *gorm.DB, tokenName string, vwap, totalVolume float64, calculatedAt time.Time) error {
vwapData := VWAPData{
TokenName: tokenName,
VWAP: vwap,
TotalVolume: totalVolume,
CalculatedAt: calculatedAt,
}
result := db.Create(&vwapData)
if result.Error != nil {
return fmt.Errorf("failed to insert data: %v", result.Error)
}
return nil
}
// testing purpose
func PopulateVWAPData(db *gorm.DB, count int) error {
for i := 0; i < count; i++ {
vwapData := VWAPData{
TokenName: faker.Currency(),
VWAP: rand.Float64(),
TotalVolume: rand.Float64(),
CalculatedAt: time.Now().Add(time.Duration(rand.Intn(1000)) * time.Minute),
}
result := db.Create(&vwapData)
if result.Error != nil {
return fmt.Errorf("failed to insert data: %v", result.Error)
}
}
return nil
}