about summary refs log tree commit diff
diff options
context:
space:
mode:
authorhanemile <hanemile@protonmail.com>2019-01-23 20:25:44 +0100
committerhanemile <hanemile@protonmail.com>2019-01-23 20:25:44 +0100
commit48451fa85af62d28bfb9257ae8c2b05cecea80fe (patch)
tree907650e33b02281f0d926ec1e696a4bbe39c93b8
parente1ec6a26489b311ca5368fd227b527e6591882ee (diff)
push before using everything in docker swarm
-rw-r--r--Dockerfile2
-rw-r--r--import.go62
-rw-r--r--main.go38
-rw-r--r--metrics.go4
4 files changed, 98 insertions, 8 deletions
diff --git a/Dockerfile b/Dockerfile
index f8db51c..8ef2502 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -4,7 +4,7 @@ WORKDIR /home
 
 COPY *.go /home/
 
-RUN ["mkdir", "/exports"]
+RUN ["mkdir", "/home/db"]
 RUN ["go", "get", "github.com/gorilla/mux"]
 RUN ["go", "get", "git.darknebu.la/GalaxySimulator/structs"]
 RUN ["ls", "-l"]
diff --git a/import.go b/import.go
index c52090f..c3c1e96 100644
--- a/import.go
+++ b/import.go
@@ -1,24 +1,29 @@
 package main
 
 import (
+	"encoding/csv"
 	"encoding/json"
 	"fmt"
+	"io"
 	"io/ioutil"
+	"log"
 	"net/http"
+	"strconv"
+	"strings"
 
 	"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) {
+// fastInsertHandler gets a tree index and a filename and tries to read insert all the stars from the file into the tree
+func fastInsertJSONHandler(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)
+	content, readErr := ioutil.ReadFile(fmt.Sprintf("/db/%s", filename))
 	if readErr != nil {
 		panic(readErr)
 	}
@@ -36,3 +41,54 @@ func fastInsertHandler(w http.ResponseWriter, r *http.Request) {
 	// return the treeArray index the tree was inserted into (== the length of the array)
 	_, _ = fmt.Fprintln(w, len(treeArray))
 }
+
+func fastInsertListHandler(w http.ResponseWriter, r *http.Request) {
+	// read the mux variables
+	vars := mux.Vars(r)
+	filename, _ := vars["filename"]
+	treeindex, _ := strconv.ParseInt(vars["filename"], 10, 64)
+
+	// read the content using the given filename
+	content, readErr := ioutil.ReadFile(fmt.Sprintf("/home/db/%s", filename))
+	if readErr != nil {
+		panic(readErr)
+	}
+
+	in := string(content)
+	reader := csv.NewReader(strings.NewReader(in))
+
+	for {
+		record, err := reader.Read()
+		if err == io.EOF {
+			break
+		}
+		if err != nil {
+			log.Println("------------------------")
+			log.Println("error:")
+			log.Println(record)
+			log.Println(err)
+			log.Println("------------------------")
+		}
+		fmt.Println(record)
+
+		x, _ := strconv.ParseFloat(record[0], 64)
+		y, _ := strconv.ParseFloat(record[1], 64)
+
+		fmt.Println("___________________")
+		fmt.Println(record[1])
+		fmt.Println(y)
+		fmt.Println("___________________")
+
+		star := structs.NewStar2D(structs.Vec2{x, y}, structs.Vec2{0, 0}, 42)
+
+		fmt.Printf("Star: %v", star)
+
+		err = treeArray[treeindex].Insert(star)
+		if err != nil {
+			log.Println(err)
+			errorCount[treeindex] += 1
+		}
+		fmt.Printf("Inserted %v\n", star)
+		starCount[treeindex] += 1
+	}
+}
diff --git a/main.go b/main.go
index 680b8ed..572bcfd 100644
--- a/main.go
+++ b/main.go
@@ -2,6 +2,7 @@ package main
 
 import (
 	"fmt"
+	"io/ioutil"
 	"log"
 	"net/http"
 
@@ -11,8 +12,9 @@ import (
 )
 
 var (
-	treeArray []*structs.Node
-	starCount []int
+	treeArray  []*structs.Node
+	starCount  []int
+	errorCount []int
 )
 
 // indexHandler
@@ -26,15 +28,39 @@ API:
 	- /insert/{treeindex} ("POST")
 	- /starlist/{treeindex} ("GET")
 	- /dumptree/{treeindex} ("GET")
+
 	- /updatetotalmass/{treeindex} ("GET")
 	- /updatecenterofmass/{treeindex} ("GET")
+
 	- /metrics ("GET")
 	- /export/{treeindex} ("POST")
-	- /fastinsert/{filename} ("POST")
+
+	- /fastinsertjson/{filename} ("GET")
+	- /fastinsertlist/{filename} ("GET")
+
+	- /readdir ("GET")
 `
 	_, _ = fmt.Fprintf(w, infostring)
 }
 
+func readdirHandler(w http.ResponseWriter, r *http.Request) {
+	vars := mux.Vars(r)
+	dirname := vars["dirname"]
+	log.Printf("Reading from %s", dirname)
+
+	files, err := ioutil.ReadDir(fmt.Sprintf("./%s", dirname))
+	log.Println(files)
+	log.Println(err)
+	if err != nil {
+		fmt.Println(err)
+	}
+
+	for _, f := range files {
+		fmt.Println(f.Name())
+		_, _ = fmt.Fprintf(w, "%v", f.Name())
+	}
+}
+
 func main() {
 	router := mux.NewRouter()
 
@@ -48,7 +74,11 @@ 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")
+
+	router.HandleFunc("/fastinsertjson/{filename}", fastInsertJSONHandler).Methods("GET")
+	router.HandleFunc("/fastinsertlist/{filename}/{treeindex}", fastInsertListHandler).Methods("GET")
+
+	router.HandleFunc("/readdir/{dirname}", readdirHandler).Methods("GET")
 
 	fmt.Println("Database Container up")
 	log.Fatal(http.ListenAndServe(":80", router))
diff --git a/metrics.go b/metrics.go
index 5881c64..5d04517 100644
--- a/metrics.go
+++ b/metrics.go
@@ -16,6 +16,10 @@ func metricHandler(w http.ResponseWriter, r *http.Request) {
 		metricsString += fmt.Sprintf("galaxy_star_count{galaxy_nr=\"%d\"} %d\n", i, starCount[i])
 	}
 
+	for i := 0; i < len(errorCount); i++ {
+		metricsString += fmt.Sprintf("galaxy_error_count{galaxy_nr=\"%d\"} %d\n", i, errorCount[i])
+	}
+
 	log.Println(metricsString)
 	_, _ = fmt.Fprintf(w, metricsString)
 }