Beispiel #1
0
Datei: log.go Projekt: hwhw/mesh
func (ws *Webservice) handler_logdata_delete(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	var counter nodedb.Counter
	switch vars["id"] {
	case "clients":
		counter = &nodedb.CountMeshClients{}
	case "nodes":
		counter = &nodedb.CountMeshNodes{}
	default:
		counter = &nodedb.CountNodeClients{Node: vars["id"]}
	}
	var timestamp time.Time
	err := timestamp.UnmarshalText([]byte(vars["timestamp"]))
	if err != nil {
		http.Error(w, "Bad Request", 400)
		return
	}
	counter.SetTimestamp(timestamp)
	err = ws.db.Logs.Update(func(tx *bolt.Tx) error {
		return ws.db.Logs.Delete(tx, counter)
	})
	if err != nil {
		http.Error(w, "Not Found", 404)
		return
	}
	w.Header().Set("Content-type", "application/json")
	ws.db.GenerateLogJSON(w, counter)
}
Beispiel #2
0
func cmd_log() error {
	switch flag.Arg(1) {
	case "l", "ls", "list":
		list := make([]string, 0, 100)
		err := getjson(fmt.Sprintf("http://%s/log/", *webadmin), &list)
		if err != nil {
			return err
		}
		for _, id := range list {
			fmt.Println(id)
		}
	case "s", "show":
		switch flag.Arg(2) {
		case "":
			failure("must specify which logs you want\n")
		case "nodes":
			list := make([]nodedb.CountMeshNodes, 0, 100)
			err := getjson(fmt.Sprintf("http://%s/log/%s", *webadmin, flag.Arg(2)), &list)
			if err != nil {
				return err
			}
			for _, logitem := range list {
				fmt.Printf("%v; %v\n", logitem.Timestamp.Format(time.RFC3339Nano), logitem.Count.Count)
			}
		case "clients":
			list := make([]nodedb.CountMeshClients, 0, 100)
			err := getjson(fmt.Sprintf("http://%s/log/%s", *webadmin, flag.Arg(2)), &list)
			if err != nil {
				return err
			}
			for _, logitem := range list {
				fmt.Printf("%v; %v\n", logitem.Timestamp.Format(time.RFC3339Nano), logitem.Count.Count)
			}
		default:
			list := make([]nodedb.CountNodeClients, 0, 100)
			err := getjson(fmt.Sprintf("http://%s/log/%s", *webadmin, flag.Arg(2)), &list)
			if err != nil {
				return err
			}
			for _, logitem := range list {
				fmt.Printf("%v; %v\n", logitem.Timestamp.Format(time.RFC3339Nano), logitem.Count.Count)
			}
		}
	case "d", "del", "delete":
		t, err := time.Parse(time.RFC3339Nano, flag.Arg(3))
		if err != nil {
			return err
		}
		switch flag.Arg(2) {
		case "":
			failure("must specify which log type you want to delete\n")
		default:
			req, err := http.NewRequest("DELETE", fmt.Sprintf("http://%s/log/%s/%s", *webadmin, flag.Arg(2), t.Format(time.RFC3339Nano)), nil)
			if err != nil {
				return err
			}
			_, err = http.DefaultClient.Do(req)
			return err
		}
	case "n", "new", "a", "add", "u", "update":
		t, err := time.Parse(time.RFC3339Nano, flag.Arg(3))
		if err != nil {
			return err
		}
		c, err := strconv.Atoi(flag.Arg(4))
		if err != nil {
			return err
		}
		var counter nodedb.Counter
		what := flag.Arg(2)
		switch what {
		case "":
			failure("must specify which log type you want to add/update\n")
		case "clients":
			counter = &nodedb.CountMeshClients{}
		case "nodes":
			counter = &nodedb.CountMeshNodes{}
		default:
			counter = &nodedb.CountNodeClients{Node: what}
			what = "node"
		}
		counter.SetTimestamp(t)
		counter.SetCount(c)
		buf := new(bytes.Buffer)
		enc := json.NewEncoder(buf)
		err = enc.Encode(counter)
		if err != nil {
			return err
		}
		_, err = http.Post(fmt.Sprintf("http://%s/log/%s", *webadmin, what), "application/json", buf)
		if err != nil {
			return err
		}
	case "import":
	default:
		failure("unknown/unspecified log subcommand\n")
	}
	return nil
}