-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (122 loc) · 3.25 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
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"fmt"
"os"
"time"
"github.com/gin-gonic/gin"
"github.com/mjarkk/machinery-dashboard/plugin"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func resErr(c *gin.Context, err error) bool {
if err == nil {
return false
}
c.JSON(400, gin.H{
"status": false,
"error": err.Error(),
})
return true
}
func res(c *gin.Context, data interface{}) {
c.JSON(200, gin.H{
"status": true,
"data": data,
})
}
func main() {
config, err := getConfig()
if err != nil {
fmt.Println("Error while reading config:", err)
os.Exit(1)
}
client, err := mongo.NewClient(options.Client().ApplyURI(config.Mongodb.ConnectionURI))
if err != nil {
fmt.Println("Error while connecting to database:", err)
os.Exit(1)
}
err = client.Connect(c())
if err != nil {
fmt.Println("Error while connecting to database:", err)
os.Exit(1)
}
err = client.Ping(c(), readpref.Primary())
if err != nil {
fmt.Println("Error while connecting to database:", err)
os.Exit(1)
}
collection := client.Database(config.Mongodb.Database).Collection("machinery-stats")
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.Use(func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, X-Max")
c.Header("Access-Control-Allow-Credentials", "true")
c.Next()
})
r.Static("/static", "./frontend/build/static")
r.StaticFile("/favicon.ico", "./frontend/build/favicon.ico")
r.StaticFile("/", "./frontend/build/index.html")
api := r.Group("/api")
api.GET("", func(g *gin.Context) {
// Get the current database data
cur, err := collection.Find(c(), bson.M{})
if resErr(g, err) {
return
}
defer cur.Close(c())
DBOutput := []plugin.DBEntry{}
for cur.Next(c()) {
var newEntry plugin.DBEntry
err := cur.Decode(&newEntry)
if resErr(g, err) {
return
}
DBOutput = append(DBOutput, newEntry)
}
// Format the data
toReturn := []apiOutput{}
for _, dbEntry := range DBOutput {
dataPoints := []plugin.DataPoint{}
noExtraEmptyPointPrefix := false
for i, point := range dbEntry.Points {
pointTime := time.Unix(point.From, 0)
if !noExtraEmptyPointPrefix {
dataPoints = append(
dataPoints,
plugin.DataPoint{
From: pointTime.Add(-(time.Minute * 10)).Unix(),
Errors: []string{},
},
)
} else {
noExtraEmptyPointPrefix = false
}
dataPoints = append(dataPoints, point)
if len(dbEntry.Points) > i+1 && time.Unix(dbEntry.Points[i+1].From, 0).Before(pointTime.Add(time.Minute*40)) {
noExtraEmptyPointPrefix = true
} else {
dataPoints = append(
dataPoints,
plugin.DataPoint{
From: pointTime.Add(time.Minute * 10).Unix(),
Errors: []string{},
},
)
}
}
toReturn = append(toReturn, apiOutput{
ID: dbEntry.ID,
Queue: dbEntry.Queue,
Timeline: dataPoints,
})
}
res(g, toReturn)
})
port := 9090
fmt.Printf("Running on port: %v\n", port)
r.Run(fmt.Sprintf(":%v", port))
}