Selaa lähdekoodia

Add mysqltojson and api_plot

Sylvrec 1 vuosi sitten
vanhempi
sitoutus
4428edb74a

+ 8 - 0
api_plot/README.txt

@@ -0,0 +1,8 @@
+DEMO JSON > DATA PLOT > OUTPUT SVG OK
+
+1 SEGMENTE LA DEMO CODE
+2 CREATION API POST OUTPUT JSON LINK SVG
+3 MINI SERVER WEB AFFICHE SVG
+
+https://www.it-swarm-fr.com/fr/android/android-charger-le-fichier-svg-partir-du-web-et-lafficher-en-mode-daffichage-dimage/1051408741/
+https://tutorialedge.net/golang/creating-simple-web-server-with-golang/

+ 10 - 0
api_plot/data.csv

@@ -0,0 +1,10 @@
+2021-08-10 15,
+2021-08-11 8,
+2021-08-12 3,
+2021-08-13 15,
+2021-08-14 6,
+2021-08-15 5,
+2021-08-16 4,
+2021-08-17 12,
+2021-08-18 14,
+2021-08-19 27,

+ 19 - 0
api_plot/data.json

@@ -0,0 +1,19 @@
+{
+"title": "Data usage over the last 24 hours",
+"x_size": "1024",
+"y_size": "768",
+"x_label": "Time",
+"y_label": "Traffic",
+"plots": [
+{"x": "2021-08-10", "y": "15"},
+{"x": "2021-08-11", "y": "8"},
+{"x": "2021-08-12", "y": "3"},
+{"x": "2021-08-13", "y": "15"},
+{"x": "2021-08-14", "y": "6"},
+{"x": "2021-08-15", "y": "5"},
+{"x": "2021-08-16", "y": "4"},
+{"x": "2021-08-17", "y": "12"},
+{"x": "2021-08-18", "y": "14"},
+{"x": "2021-08-19", "y": "27"}
+]
+}

BIN
api_plot/gnuplot-5.2.6.tar.gz


+ 89 - 0
api_plot/main.go

@@ -0,0 +1,89 @@
+package main
+
+import (
+	"bytes"
+	"encoding/json"
+	"fmt"
+	"io"
+	"io/ioutil"
+	"math/rand"
+	"os"
+	"os/exec"
+	"strings"
+	"time"
+)
+
+type Plot struct {
+	X string `json:"x"`
+	Y string `json:"y"`
+}
+
+type ChartPlot struct {
+	Title  string `json:"title"`
+	SizeX  string `json:"x_size"`
+	SizeY  string `json:"y_size"`
+	LabelX string `json:"x_label"`
+	LabelY string `json:"y_label"`
+	Plots  []Plot `json:"plots"`
+}
+
+var TITLE string = "{TITLE}"
+var LABEL_X string = "{LABEL_X}"
+var LABEL_Y string = "{LABEL_Y}"
+var OUTPUT string = "{OUTPUT}"
+var INPUT string = "{INPUT}"
+var LINE string = "{LINE}"
+
+func RandString(n int) string {
+	rand.Seed(time.Now().UnixNano())
+	r := []rune("qwertyuiopasdfghjklzxcvbnm1234567890")
+	b := make([]rune, n)
+	for i := range b {
+		b[i] = r[rand.Intn(len(r))]
+	}
+	return string(b)
+}
+
+func main() {
+	filename := "xplot_" + RandString(15)
+	chart := ChartPlot{}
+
+	jsonData, errRead := ioutil.ReadFile("./data.json")
+	if errRead != nil {
+		fmt.Println(errRead.Error())
+	}
+	err := json.Unmarshal([]byte(string(jsonData)), &chart)
+	if err != nil {
+		fmt.Println(err.Error())
+	}
+
+	f, _ := os.Create(filename + ".data")
+	defer f.Close()
+	for _, p := range chart.Plots {
+		f.WriteString(p.X + " " + p.Y + ",\n")
+	}
+	f.Sync()
+
+	buf := bytes.NewBuffer(nil)
+	tp, _ := os.Open("tp_chart.plot")
+	io.Copy(buf, tp)
+	defer tp.Close()
+	str := string(buf.Bytes())
+	str = strings.Replace(str, TITLE, "my Chart", 1)
+	str = strings.Replace(str, LABEL_X, "1024", 1)
+	str = strings.Replace(str, LABEL_Y, "768", 1)
+	str = strings.Replace(str, OUTPUT, filename, 1)
+	str = strings.Replace(str, INPUT, filename, 1)
+	str = strings.Replace(str, LINE, "donut", 1)
+
+	f2, _ := os.Create(filename + ".plot")
+	defer f2.Close()
+	f2.WriteString(str)
+	f2.Sync()
+
+	out, errOut := exec.Command("./gnuplot", "./"+ filename + ".plot").Output()
+	if errOut != nil {
+		fmt.Println(errOut.Error())
+	}
+	fmt.Println(string(out))
+}

+ 11 - 0
api_plot/script.plot

@@ -0,0 +1,11 @@
+set title "Data usage over the last 24 hours"
+unset multiplot
+set xdata time
+set style data lines
+set term svg size 1024, 768
+set timefmt "%Y-%m-%d" 
+set xlabel "Time"
+set ylabel "Traffic"
+set autoscale y
+set output "pic.svg"
+plot './data.csv' using 1:2 t "inbound"

+ 11 - 0
api_plot/tp_chart.plot

@@ -0,0 +1,11 @@
+set title "{TITLE}"
+unset multiplot
+set xdata time
+set style data lines
+set term svg size 1024, 768
+set timefmt "%Y-%m-%d" 
+set xlabel "{LABEL_X}"
+set ylabel "{LABEL_Y}"
+set autoscale y
+set output "{OUTPUT}.svg"
+plot './{INPUT}.data' using 1:2 t "{LINE}"

+ 12 - 0
mysqltojson/api/html/404.html

@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+<body>
+    <h1>404 NOT FOUND</h1>
+</body>
+</html>

+ 12 - 0
mysqltojson/api/html/index.html

@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document test</title>
+</head>
+<body>
+    <h1>Welcome on my own page</h1>
+</body>
+</html>

+ 31 - 0
mysqltojson/api/server.go

@@ -0,0 +1,31 @@
+package api
+
+import (
+	"fmt"
+	"log"
+	"net/http"
+)
+
+type router struct {
+	addr      string
+	hrequests []handleRequest
+}
+
+type handleRequest struct {
+	path    string
+	handler func(http.ResponseWriter, *http.Request)
+}
+
+func Launcher(port int) {
+	router := router{
+		addr: fmt.Sprint(":", port),
+		hrequests: []handleRequest{
+			{"/", homePage},
+			{"/insert", insert},
+		},
+	}
+	for _, hrequest := range router.hrequests {
+		http.HandleFunc(hrequest.path, hrequest.handler)
+	}
+	log.Fatal(http.ListenAndServe(router.addr, nil))
+}

+ 32 - 0
mysqltojson/api/webapi.go

@@ -0,0 +1,32 @@
+package api
+
+import "net/http"
+import "mysql"
+
+func insert(w http.ResponseWriter, r *http.Request) {
+	var bytes : []byte = []
+	query := r.URL.Query();
+	n, err := r.Body.Read(bytes)
+	if (err != nil) {
+		w.Header().Set("content-type", "application/json")
+		w.Write([]byte(`{"error": "` + err.message() + `"}`))
+		return
+	}
+	//serialize bytes to json
+	//get property table, column, values
+	//Insert(table, ,)
+	w.Header().Set("content-type", "application/json")
+	w.Write([]byte(`{"message": "hello world"}`))
+}
+
+func update(){
+
+}
+
+func delete() {
+
+}
+
+func read() {
+	
+}

+ 25 - 0
mysqltojson/api/webpage.go

@@ -0,0 +1,25 @@
+package api
+
+import (
+	"fmt"
+	"net/http"
+	"time"
+)
+
+func homePage(w http.ResponseWriter, r *http.Request) {
+	page := "." + r.URL.Path
+	t := time.Now()
+	formattedTime := fmt.Sprintf("%d/%d/%d %d:%d:%d",
+		t.Month(), t.Day(), t.Year(),
+		t.Hour(), t.Minute(), t.Second())
+	switch page {
+	case "./":
+		page = "./api/html/index.html"
+		break
+	default:
+		fmt.Println("[" + formattedTime + "] " + r.RemoteAddr + " => " + page)
+		page = "./api/html/404.html"
+		break
+	}
+	http.ServeFile(w, r, page)
+}

+ 3 - 1
mysqltojson/main.go

@@ -1,5 +1,7 @@
 package main
 
-func main() {
+import "mysqltojson/api"
 
+func main() {
+	api.Launcher(6565)
 }

BIN
mysqltojson/test.txt