forked from gobitfly/eth2-beaconchain-explorer
/
gasnow.go
93 lines (77 loc) · 2.45 KB
/
gasnow.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
package handlers
import (
"encoding/json"
"eth2-exporter/db"
"eth2-exporter/price"
"eth2-exporter/services"
"eth2-exporter/templates"
"eth2-exporter/utils"
"fmt"
"net/http"
"sort"
"time"
)
// Will return the gas now page
func GasNow(w http.ResponseWriter, r *http.Request) {
templateFiles := append(layoutTemplateFiles, "gasnow.html")
var gasNowTemplate = templates.GetTemplate(templateFiles...)
w.Header().Set("Content-Type", "text/html")
data := InitPageData(w, r, "gasnow", "/gasnow", fmt.Sprintf("%v Gwei", 34), templateFiles)
now := time.Now().Truncate(time.Minute)
lastWeek := time.Now().Truncate(time.Minute).Add(-time.Hour * 24 * 7)
history, err := db.BigtableClient.GetGasNowHistory(now, lastWeek)
if err != nil {
logger.Errorf("error retrieving gas price histors: %v", err)
return
}
group := make(map[int64]float64, 0)
for i := 0; i < len(history); i++ {
_, ok := group[history[i].Ts.Truncate(time.Hour).Unix()]
if !ok {
group[history[i].Ts.Truncate(time.Hour).Unix()] = float64(history[i].Fast.Int64())
} else {
group[history[i].Ts.Truncate(time.Hour).Unix()] = (group[history[i].Ts.Truncate(time.Hour).Unix()] + float64(history[i].Fast.Int64())) / 2
}
}
resRet := []*struct {
Ts int64 `json:"ts"`
AvgFast float64 `json:"fast"`
}{}
for ts, fast := range group {
resRet = append(resRet, &struct {
Ts int64 `json:"ts"`
AvgFast float64 `json:"fast"`
}{
Ts: ts,
AvgFast: fast,
})
}
sort.SliceStable(resRet, func(i int, j int) bool {
return resRet[i].Ts > resRet[j].Ts
})
data.Data = resRet
if handleTemplateError(w, r, "gasnow.go", "GasNow", "", gasNowTemplate.ExecuteTemplate(w, "layout", data)) != nil {
return // an error has occurred and was processed
}
}
func GasNowData(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
gasnowData := services.LatestGasNowData()
if gasnowData == nil {
utils.LogError(nil, "error obtaining latest gas now data 'nil'", 0)
http.Error(w, "Internal server error", http.StatusServiceUnavailable)
return
}
currency := GetCurrency(r)
if currency == "ETH" {
currency = "USD"
}
gasnowData.Data.Price = price.GetEthPrice(currency)
gasnowData.Data.Currency = currency
err := json.NewEncoder(w).Encode(gasnowData)
if err != nil {
logger.Errorf("error serializing json data for API %v route: %v", r.URL.String(), err)
http.Error(w, "Internal server error", http.StatusServiceUnavailable)
return
}
}