-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
136 lines (117 loc) · 3.81 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
132
133
134
135
136
package main
import (
// "bufio"
"context"
// "fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
// "os"
// "strings"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/go-gota/gota/dataframe"
"github.com/go-gota/gota/series"
"rh_metrics/m/src/rhwrapper"
)
func isAuthenticated(c *gin.Context) {
session := sessions.Default(c)
// Check if "authenticated" is set to true in the session
isAuthenticated := session.Get("authenticated") == true
if !isAuthenticated {
// If the user is not authenticated, redirect them to the login page
c.Redirect(http.StatusSeeOther, "/metrics")
c.Abort() // Prevent the handler from running
return
}
c.Next() // If the user is authenticated, proceed to the handler
}
func main() {
rhClient := rhwrapper.Hood{}
router := gin.Default()
store := cookie.NewStore([]byte("secret"))
router.Use(sessions.Sessions("stateStorage", store))
router.LoadHTMLGlob("templates/*.tmpl")
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "login.tmpl", nil)
})
router.GET("/error", func(c *gin.Context) {
// Get the last error message
errMsg := c.Errors.ByType(gin.ErrorTypePrivate).Last().Error()
c.HTML(http.StatusOK, "error.tmpl", gin.H{
"ErrorMessage": errMsg,
})
})
router.POST("/", func(c *gin.Context) {
email := c.PostForm("email")
password := c.PostForm("password")
mfa := c.PostForm("mfa")
cli, err := rhClient.Auth(email, password, mfa)
if err != nil {
c.Error(err) //nolint:errcheck
c.Redirect(http.StatusSeeOther, "/error")
return
}
session := sessions.Default(c)
if err := session.Save(); err != nil {
c.Error(err) //nolint:errcheck
c.Redirect(http.StatusSeeOther, "/error")
return
}
session.Set("authenticated", true)
c.Redirect(http.StatusMovedPermanently, "/metrics")
rhClient.Cli = cli
})
router.GET("/metrics", isAuthenticated, func(c *gin.Context) {
session := sessions.Default(c)
session.Set("authenticated", false)
ctx := context.Background()
profitDf, unrealizedProfitDf, err := rhClient.ProcessRealizedEarnings(ctx)
if err != nil {
log.Fatalf("failing %v", err)
}
// see rhwrapper.go
aggregatedDf := profitDf.
GroupBy("Year").
Aggregation([]dataframe.AggregationType{dataframe.Aggregation_SUM}, []string{"Amount"})
aggregatedDf = aggregatedDf.Arrange(
dataframe.Sort("Year"),
)
years := aggregatedDf.Col("Year").Records()
ytdRealizedGains := aggregatedDf.Col("Amount_SUM").Records()
tagDf := profitDf.
GroupBy("Year", "Tag").
Aggregation([]dataframe.AggregationType{dataframe.Aggregation_SUM}, []string{"Amount"})
tagDf = tagDf.Arrange(
dataframe.Sort("Year"),
)
amount := tagDf.Col("Amount_SUM").Records()
yearsTag := tagDf.Col("Year").Records()
tag := tagDf.Col("Tag").Records()
earningsDfByTicker := profitDf.
Filter(dataframe.F{
Colname: "Ticker",
Comparator: series.Neq,
Comparando: "",
}).
GroupBy("Year", "Ticker").
Aggregation([]dataframe.AggregationType{dataframe.Aggregation_SUM}, []string{"Amount"})
earningsByTickerAmount := earningsDfByTicker.Col("Amount_SUM").Records()
earningsByTickerLabels := earningsDfByTicker.Col("Ticker").Records()
earningsByTickerYear := earningsDfByTicker.Col("Year").Records()
labels := years
data := ytdRealizedGains
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"LabelsTimeSeries": labels,
"DataTimeSeries": data,
"LabelsYearsTag": yearsTag,
"LabelsTags": tag,
"DataAmount": amount,
"DataLabelsByTicker": earningsByTickerLabels,
"DataValByTicker": earningsByTickerAmount,
"DataValByTickerYear": earningsByTickerYear,
"UnrealizedProfitTransactions": unrealizedProfitDf.Records(),
})
})
router.Run(":8080") //nolint:errcheck
}