-
Notifications
You must be signed in to change notification settings - Fork 1
/
firebase.go
135 lines (104 loc) · 3.56 KB
/
firebase.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
132
133
134
135
package tokens
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"time"
firebase "firebase.google.com/go"
"google.golang.org/api/option"
)
func GetFirebaseApp(ctx context.Context) (*firebase.App, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("error UserHomeDir: %v", err)
}
opt := option.WithCredentialsFile(home + "/atw/fair-theater-238820-firebase-adminsdk-uyr4z-63b4da8ff3.json")
//ctx := context.Background()
config := &firebase.Config{
DatabaseURL: "https://fair-theater-238820-default-rtdb.firebaseio.com/",
}
app, err := firebase.NewApp(ctx, config, opt)
if err != nil {
return nil, fmt.Errorf("error initializing app: %v", err)
}
return app, nil
}
// CalcTokenPrice figures out how much we would need to pay to get this token.
// TODO: move out of firebase
func CalcTokenPrice(token *KnotFreeTokenPayload, unixIssueTime uint32) float32 {
price := float32(0.0)
// at DigitalOcean 4/2021:
// $5
// 0.5 cpu
// 1 Gb so 500k subs
// 1 tb io
// say 10k connections
// $0.01/GB. when over
// so, divide by 5000
// tinyCost := float32(5) / float32(5000) // 100 m$
// tinyConnectAmt := float32(10*1000) / float32(5000)
// tinyIO := float32(1000 * 1000 * 1000 * 1000 / 5000) // per month
// fmt.Println("tinyCost ", tinyCost) // 0.001 or 1 m$ or 1000 u$
// fmt.Println("tinyConnectAmt ", tinyConnectAmt) // 2
// fmt.Println("tinyIO/sec ", tinyIO/secsInMonth) // 77 bytes/sec
greaterPrice := float32(-1.0)
secsInMonth := float32(60 * 60 * 24 * 30)
connectionPrice := token.Connections * float32(5) / float32(10*1000)
subscriptionPrice := token.Subscriptions * float32(5) / float32(500*1000)
subscriptionPrice *= 2 // because it's on two layers
ioPrice1 := token.Input * secsInMonth * float32(5) / float32(1000*1000*1000*1000) // per month
ioPrice2 := token.Output * secsInMonth * float32(5) / float32(1000*1000*1000*1000) // per month
greaterPrice = connectionPrice
if subscriptionPrice > greaterPrice {
greaterPrice = subscriptionPrice
}
if ioPrice1 > greaterPrice {
greaterPrice = ioPrice1
}
if ioPrice2 > greaterPrice {
greaterPrice = ioPrice2
}
nowSeconds := unixIssueTime //uint32(time.Now().Unix())
deltaTime := float32(uint32(token.ExpirationTime - nowSeconds))
deltaTime = deltaTime / secsInMonth // now in months
price = greaterPrice * deltaTime
return price
}
type TokenLogStruct struct {
RemoteAddr string
When uint32 // unix time
Token *KnotFreeTokenPayload
}
//LogNewToken to make a record that this token was delivered to customer.
// Let's not include the whole jwt.
func LogNewToken(ctx context.Context, token *KnotFreeTokenPayload, remoteAddr string) error {
//ctx := context.Background()
app, err := GetFirebaseApp(ctx)
if err != nil {
log.Fatalf("app.Firestore: %v", err)
}
client, err := app.Database(ctx)
if err != nil {
log.Fatalf("app.Firestore: %v", err)
}
tokenLogStruct := &TokenLogStruct{}
tokenLogStruct.RemoteAddr = remoteAddr
tokenLogStruct.Token = token
tokenLogStruct.When = uint32(time.Now().Unix())
jsonbytes, err := json.Marshal(tokenLogStruct)
_ = jsonbytes
currentTime := time.Now()
str := currentTime.Format("2006-01-02")
dbpath := "tokens/requests/" + str + "/"
dbref := client.NewRef(dbpath)
log.Println(" starting fire push : ")
_, seterr := dbref.Push(ctx, tokenLogStruct)
if seterr != nil {
fmt.Println("about to die from app.Firestore error ", seterr)
//log.Fatalf("app.Firestore: set %v", seterr)
log.Println(" ERROR app.Firestore: ", seterr)
}
return nil
}