forked from pokt-network/relay_counter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
105 lines (97 loc) · 2.25 KB
/
config.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math"
"net/http"
"os"
"strings"
"time"
)
type Config struct {
Timeline Timeline `json:"timeline"`
Endpoint string `json:"endpoint"`
HTTPRetry int `json:"http_retry"`
Params Params `json:"params"`
}
type TimelineJSON Timeline
type Params struct {
AppxBlockTimeInMinutes int64 `json:"approx_block_time_in_min"`
BlocksPerSession int64 `json:"blocks_per_session"`
}
func (t *Timeline) UnmarshalJSON(data []byte) error {
tlj := TimelineJSON{}
err := json.Unmarshal(data, &tlj)
if err != nil {
return err
}
t.Unit = tlj.Unit
t.End = int64(math.Abs(float64(tlj.End)) * -1)
t.Start = int64(math.Abs(float64(tlj.Start)) * -1)
if t.Start > t.End {
return NewInvalidStartEndError(t.Start*-1, t.End*-1, t.Unit)
}
switch strings.ToLower(t.Unit) {
case UnitMinutes, UnitMinute, UnitMin, UnitM:
// all good
case UnitHours, UnitHour, UnitHr, UnitH:
// all good
case UnitDays, UnitDay, UnitD:
// all good
case UnitWeeks, UnitWeek, UnitW:
// all good
case UnitBlocks, UnitBlock, UnitB:
// all good
case UnitSessions, UnitSession, UnitS:
// all good
default:
return NewInvalidUnitError(t.Unit)
}
return nil
}
// Gets the conf in the config file
func getConfig() Config {
file := "config/config.json"
fBz, err := ioutil.ReadFile(file)
if err != nil {
log.Fatalf(err.Error())
}
c := Config{}
err = json.Unmarshal(fBz, &c)
if err != nil {
log.Fatalf(err.Error())
}
return c
}
func testEndpoint(endpoint string) error {
r, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return err
}
c := http.Client{}
res, err := c.Do(r)
if err != nil {
return err
}
if endpoint[len(endpoint)-2:] != "v1" {
return fmt.Errorf("endpoint must be pocket-core version endpoint")
}
if res.StatusCode != 200 {
return NewHTTPStatusCode(res.StatusCode, "")
}
return nil
}
func writeResultFile(result Report) {
j, err := json.MarshalIndent(result, "", " ")
if err != nil {
log.Fatal(err)
}
now := time.Now().AddDate(0, 0, -1).Format("01-02-06T15:04:05")
err = ioutil.WriteFile("result/"+now+".json", j, os.ModePerm)
if err != nil {
log.Println("ERROR : COULD NOT WRITE REPORT FILE: ", err.Error(), "\nPRINTING TO SCREEN")
fmt.Println(string(j))
}
}