From 6ae19dcc07980f4daaa337edd624d6f68ae3578b Mon Sep 17 00:00:00 2001 From: hanemile Date: Mon, 21 Jan 2019 22:11:25 +0100 Subject: grouped the functions in individual files and created a detailed README.md --- README.md | 98 +++++++++++++++++++++++- export.go | 40 ++++++++++ import.go | 38 ++++++++++ main.go | 252 +------------------------------------------------------------ manage.go | 181 ++++++++++++++++++++++++++++++++++++++++++++ metrics.go | 41 ++++++++++ update.go | 27 +++++++ 7 files changed, 425 insertions(+), 252 deletions(-) create mode 100644 export.go create mode 100644 import.go create mode 100644 manage.go create mode 100644 metrics.go create mode 100644 update.go diff --git a/README.md b/README.md index d61b5c3..f62dd1d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,99 @@ # db-container -This Repo contains the main "database" running an http-api exposing the quadtree. \ No newline at end of file +This Repo contains the main "database" running an http-api exposing the quadtree. + +## API-Endpoints + + +## Files + +Simple description of each individual function + +### export.go + +``` +func export(treeindex int64) error { + // exports the tree with the given tree index by writing it's json + // to /exports/{treeindex}.json +} + +func exportHandler(w http.ResponseWriter, r *http.Request) { + // handles request to /export/{treeindex} and uses the export function + // to export the selected tree +} +``` + +### update.go + +``` +func updateCenterOfMassHandler(w http.ResponseWriter, r *http.Request) { + // updates the center of mass of every node in the tree width the given index +} + +func updateTotalMassHandler(w http.ResponseWriter, r *http.Request) { + // updates the total mass of every node in the tree with the given index +} +``` + +### import.go + +``` +func fastInsertHandler(w http.ResponseWriter, r *http.Request) { + // creates a new tree by reading from /db/{filename}.json and inserting the tree + // from that file into the treeArray + // The function returns the index in which it inserted the tree +} +``` + +### manage.go + +``` +func newTreeHandler(w http.ResponseWriter, r *http.Request) { + // creates a new empty tree with the given width +} + +func newTree(width float64) []byte { + // creates a new tree and returns it json formatted as a bytestring +} + +func printAllHandler(w http.ResponseWriter, r *http.Request) { + // prints all the trees in the treeArray in json format +} + +func insertStarHandler(w http.ResponseWriter, r *http.Request) { + // inserts the given star into the tree +} + +func starlistHandler(w http.ResponseWriter, r *http.Request) { + // lists all the stars in the given tree +} + +func dumptreeHandler(w http.ResponseWriter, r *http.Request) { + // returns the tree with the given index in json format +} +``` + +### metrics.go + +``` +func metricHandler(w http.ResponseWriter, r *http.Request) { + // locally published the databases metrics +} + +func pushMetricsNumOfStars(host string, treeindex int64) { + // pushes the metrics of the tree with the given index to the metric-bundler + // using the given host and +} +``` + +### update.go + +``` +func updateCenterOfMassHandler(w http.ResponseWriter, r *http.Request) { + // updates the center of mass of every node in the tree with the given index +} + +func updateTotalMassHandler(w http.ResponseWriter, r *http.Request) { + // updates the total mass in the tree with the given index +} +``` \ No newline at end of file diff --git a/export.go b/export.go new file mode 100644 index 0000000..9cc67af --- /dev/null +++ b/export.go @@ -0,0 +1,40 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strconv" + + "github.com/gorilla/mux" +) + +// 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 +} + +// export the selected tree to the selected file +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) +} diff --git a/import.go b/import.go new file mode 100644 index 0000000..c52090f --- /dev/null +++ b/import.go @@ -0,0 +1,38 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + + "github.com/gorilla/mux" + + "git.darknebu.la/GalaxySimulator/structs" +) + +// fastInsertHandler gets a tree index and a filename and tries to read +func fastInsertHandler(w http.ResponseWriter, r *http.Request) { + // read the mux variables + vars := mux.Vars(r) + filename, _ := vars["filename"] + + // read the content using the given filename + content, readErr := ioutil.ReadFile(filename) + if readErr != nil { + panic(readErr) + } + + // unmarshal the file content + tree := &structs.Node{} + jsonUnmarshalErr := json.Unmarshal(content, tree) + if jsonUnmarshalErr != nil { + panic(jsonUnmarshalErr) + } + + // append the tree to the treeArray + treeArray = append(treeArray, tree) + + // return the treeArray index the tree was inserted into (== the length of the array) + _, _ = fmt.Fprintln(w, len(treeArray)) +} diff --git a/main.go b/main.go index b7e246c..cdb21c8 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,9 @@ package main import ( - "encoding/json" "fmt" - "io/ioutil" "log" "net/http" - "net/url" - "strconv" "github.com/gorilla/mux" @@ -34,253 +30,6 @@ API: _, _ = 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() @@ -294,6 +43,7 @@ func main() { router.HandleFunc("/updatecenterofmass/{treeindex}", updateCenterOfMassHandler).Methods("GET") router.HandleFunc("/metrics", metricHandler).Methods("GET") router.HandleFunc("/export/{treeindex}", exportHandler).Methods("POST") + router.HandleFunc("/fastinsert/{filename}", fastInsertHandler).Methods("POST") fmt.Println("Database Container up") log.Fatal(http.ListenAndServe(":80", router)) diff --git a/manage.go b/manage.go new file mode 100644 index 0000000..b7a5ce4 --- /dev/null +++ b/manage.go @@ -0,0 +1,181 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "net/http" + "strconv" + + "github.com/gorilla/mux" + + "git.darknebu.la/GalaxySimulator/structs" +) + +// 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]) +} + +// 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) + } +} diff --git a/metrics.go b/metrics.go new file mode 100644 index 0000000..5881c64 --- /dev/null +++ b/metrics.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "net/url" +) + +// 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) +} + +// 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() +} diff --git a/update.go b/update.go new file mode 100644 index 0000000..402a64c --- /dev/null +++ b/update.go @@ -0,0 +1,27 @@ +package main + +import ( + "log" + "net/http" + "strconv" + + "github.com/gorilla/mux" +) + +// 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() +} -- cgit 1.4.1