about summary refs log tree commit diff
path: root/main.go
blob: b7e246c3bb66095a96de64fbda64a01570996a47 (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"strconv"

	"github.com/gorilla/mux"

	"git.darknebu.la/GalaxySimulator/structs"
)

var (
	treeArray []*structs.Node
	starCount []int
)

// indexHandler
func indexHandler(w http.ResponseWriter, r *http.Request) {
	infostring := `Galaxy Simulator Database

API:
	/ GET
	/new POST w float64
	/insert/{treeindex} POST x float64, y float64, vx float64, vy float64, m float64
	/starlist/{treeindex}
	/printall GET
	/metrics GET
`
	_, _ = fmt.Fprintf(w, infostring)
}

// newTreeHandler creates a new tree
func newTreeHandler(w http.ResponseWriter, r *http.Request) {
	// set the content type to json (looks fancy in firefox :D)
	w.Header().Set("Content-Type", "application/json")

	fmt.Println("Creating a new tree")

	// get the star by parsing http-post parameters
	errParseForm := r.ParseForm() // parse the POST form
	if errParseForm != nil {      // handle errors
		panic(errParseForm)
	}

	// default values
	width := 100.0

	// value from the user
	userWidth, _ := strconv.ParseFloat(r.Form.Get("w"), 64) // bounding box width
	log.Printf("width: %f", userWidth)

	// overwrite the default width
	if userWidth != width {
		width = userWidth
	}

	jsonData := newTree(width)

	// return the new tree as json
	_, _ = fmt.Fprintf(w, "%v", string(jsonData))
}

// newTree generates a new tree using the width it is given and returns it as json in an array of bytes
func newTree(width float64) []byte {

	// generate a new tree and add it to the treeArray
	newTree := structs.NewRoot(width)
	treeArray = append(treeArray, newTree)
	starCount = append(starCount, 0)

	// convert the tree to json format
	jsonData, jsonMarshalErr := json.Marshal(newTree)
	if jsonMarshalErr != nil {
		panic(jsonMarshalErr)
	}

	return jsonData

}

// printAllHandler prints all the trees in the treeArray
func printAllHandler(w http.ResponseWriter, r *http.Request) { // set the content type to json (looks fancy in firefox :D)
	w.Header().Set("Content-Type", "application/json")

	// Convert the data to json
	jsonData, jsonMarshalerError := json.Marshal(treeArray)
	if jsonMarshalerError != nil {
		panic(jsonMarshalerError)
	}

	// print the jsonData to the ResponseWriter
	_, printTreeErr := fmt.Fprintf(w, "%v\n", string(jsonData))
	if printTreeErr != nil {
		panic(printTreeErr)
	}

	log.Printf("The printAll endpoint was accessed.\n")
}

// insertStarHandler inserts a star into the given tree
func insertStarHandler(w http.ResponseWriter, r *http.Request) {
	log.Println("The insert handler was accessed")

	// get the treeindex in which the star should be inserted into
	vars := mux.Vars(r)
	treeindex, _ := strconv.ParseInt(vars["treeindex"], 10, 0)

	// get the star by parsing http-post parameters
	errParseForm := r.ParseForm() // parse the POST form
	if errParseForm != nil {      // handle errors
		panic(errParseForm)
	}

	// get the star coordinates
	x, _ := strconv.ParseFloat(r.Form.Get("x"), 64)
	y, _ := strconv.ParseFloat(r.Form.Get("y"), 64)
	vx, _ := strconv.ParseFloat(r.Form.Get("vx"), 64)
	vy, _ := strconv.ParseFloat(r.Form.Get("vy"), 64)
	m, _ := strconv.ParseFloat(r.Form.Get("m"), 64)

	log.Printf("treeindex: %d", treeindex)
	log.Printf("x: %f", x)
	log.Printf("y: %f", y)
	log.Printf("vx: %f", vx)
	log.Printf("vy: %f", vy)
	log.Printf("m: %f", m)

	s1 := structs.Star2D{
		C: structs.Vec2{x, y},
		V: structs.Vec2{vx, vy},
		M: m,
	}

	log.Printf("s1: %v", s1)

	treeInsertError := treeArray[treeindex].Insert(s1)
	if treeInsertError != nil {
		panic(fmt.Sprintf("Error inserting %v into tree %d: %v", s1, treeindex, treeInsertError))
	}

	fmt.Println("-------------")
	fmt.Println(treeArray)
	fmt.Println("-------------")

	log.Println("Done inserting the star")
	starCount[treeindex] += 1

	pushMetricsNumOfStars("http://db:80/metrics", treeindex)

	_, _ = fmt.Fprintf(w, "%d", starCount[treeindex])
}

// pushMetricsNumOfStars pushes the amount of stars in the given galaxy with the given index to the given host
// the host is (normally) the service bundling the metrics
func pushMetricsNumOfStars(host string, treeindex int64) {

	// define a post-request and send it to the given host
	requestURL := fmt.Sprintf("%s", host)
	resp, err := http.PostForm(requestURL,
		url.Values{
			"key":   {fmt.Sprintf("db_%s{nr=\"%s\"}", "stars_num", treeindex)},
			"value": {fmt.Sprintf("%d", starCount[treeindex])},
		},
	)
	if err != nil {
		fmt.Printf("Cound not make a POST request to %s", requestURL)
	}

	// close the response body
	defer resp.Body.Close()
}

// starlistHandler lists all the stars in the given tree
func starlistHandler(w http.ResponseWriter, r *http.Request) {
	log.Println("The starlist handler was accessed")

	w.Header().Set("Content-Type", "application/json")

	vars := mux.Vars(r)
	treeindex, _ := strconv.ParseInt(vars["treeindex"], 10, 0)

	listofallstars := treeArray[treeindex].GetAllStars()
	log.Printf("listofallstars: %v", listofallstars)
	// listofallstars: [{{-42 10} {0 0} 100} {{10 10} {0 0} 100}]

	// convert the list of all stars to json
	jsonlistofallstars, jsonMarshalErr := json.Marshal(listofallstars)
	if jsonMarshalErr != nil {
		panic(jsonMarshalErr)
	}

	log.Printf("jsonlistofallstars: %v", string(jsonlistofallstars))

	_, _ = fmt.Fprintln(w, string(jsonlistofallstars))
	log.Println("Done")
}

// dumptreeHandler dumps the requested tree
func dumptreeHandler(w http.ResponseWriter, r *http.Request) {
	log.Printf("The dumptree endpoint was accessed.\n")

	w.Header().Set("Content-Type", "application/json")

	vars := mux.Vars(r)
	treeindex, _ := strconv.ParseInt(vars["treeindex"], 10, 0)

	// Convert the data to json
	jsonData, jsonMarshalerError := json.Marshal(treeArray[treeindex])
	if jsonMarshalerError != nil {
		panic(jsonMarshalerError)
	}

	// print the jsonData to the ResponseWriter
	_, printTreeErr := fmt.Fprintf(w, "%v\n", string(jsonData))
	if printTreeErr != nil {
		panic(printTreeErr)
	}
}

// updateCenterOfMassHandler updates the center of mass in each node in tree with the given index
func updateCenterOfMassHandler(w http.ResponseWriter, r *http.Request) {
	log.Println("Updating the center of mass")
	vars := mux.Vars(r)
	treeindex, _ := strconv.ParseInt(vars["treeindex"], 10, 0)

	treeArray[treeindex].CalcCenterOfMass()
}

// updateTotalMassHandler updates the total mass in each node in the tree with the given index
func updateTotalMassHandler(w http.ResponseWriter, r *http.Request) {
	log.Println("Updating the total mass")
	vars := mux.Vars(r)
	treeindex, _ := strconv.ParseInt(vars["treeindex"], 10, 0)

	treeArray[treeindex].CalcTotalMass()
}

// metricHandler prints all the metrics to the ResponseWriter
func metricHandler(w http.ResponseWriter, r *http.Request) {
	var metricsString string
	metricsString += fmt.Sprintf("nr_galaxies %d\n", len(treeArray))

	for i := 0; i < len(starCount); i++ {
		metricsString += fmt.Sprintf("galaxy_star_count{galaxy_nr=\"%d\"} %d\n", i, starCount[i])
	}

	log.Println(metricsString)
	_, _ = fmt.Fprintf(w, metricsString)
}

// export exports all the trees
func export(treeindex int64) error {
	// Convert the data to json
	jsonData, jsonMarshalerError := json.Marshal(treeArray[treeindex])
	if jsonMarshalerError != nil {
		panic(jsonMarshalerError)
	}

	// write the json formatted byte data to a file
	err := ioutil.WriteFile(fmt.Sprintf("/exports/tree_%d.json", treeindex), jsonData, 0644)
	if err != nil {
		return err
	}
	return nil
}

func exportHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	treeindex, _ := strconv.ParseInt(vars["treeindex"], 10, 0)

	err := export(treeindex)
	if err != nil {
		panic(err)
	}

	_, _ = fmt.Fprintf(w, "Exportet Tree %d", treeindex)
}

func main() {
	router := mux.NewRouter()

	router.HandleFunc("/", indexHandler).Methods("GET")
	router.HandleFunc("/new", newTreeHandler).Methods("POST")
	router.HandleFunc("/printall", printAllHandler).Methods("GET")
	router.HandleFunc("/insert/{treeindex}", insertStarHandler).Methods("POST")
	router.HandleFunc("/starlist/{treeindex}", starlistHandler).Methods("GET")
	router.HandleFunc("/dumptree/{treeindex}", dumptreeHandler).Methods("GET")
	router.HandleFunc("/updatetotalmass/{treeindex}", updateTotalMassHandler).Methods("GET")
	router.HandleFunc("/updatecenterofmass/{treeindex}", updateCenterOfMassHandler).Methods("GET")
	router.HandleFunc("/metrics", metricHandler).Methods("GET")
	router.HandleFunc("/export/{treeindex}", exportHandler).Methods("POST")

	fmt.Println("Database Container up")
	log.Fatal(http.ListenAndServe(":80", router))
}