Example #1
0
func Diag(n *core.IpfsNode, cmdparts []string) (string, error) {
	diag, err := n.Diagnostics.GetDiagnostic(time.Second * 5)
	if err != nil {
		return "", err
	}
	out := new(bytes.Buffer)
	var format string
	if len(cmdparts) > 2 {
		format = cmdparts[2]
	}
	switch {
	case format == "d3":
		b := diagnostics.GetGraphJson(diag)
		if _, err := out.Write(b); err != nil {
			return "", err
		}
		out.Write([]byte("\n"))
	case format == "json":
		enc := json.NewEncoder(out)
		if err := enc.Encode(diag); err != nil {
			return "", err
		}
		out.Write([]byte("\n"))
	default:
		fmt.Fprintln(out, diag)
	}
	return out.String(), nil
}
Example #2
0
// Runs the visualization server to view d3 graph of the network
func RunServer(s string) {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		http.ServeFile(w, r, "index.html")
	})
	http.HandleFunc("/data", func(w http.ResponseWriter, r *http.Request) {
		nd, ok := controllers[0].(*localNode)
		if !ok {
			fmt.Println("Invalid controller type!")
			return
		}
		diag, err := nd.n.Diagnostics.GetDiagnostic(time.Second * 5)
		if err != nil {
			fmt.Println(err)
		}
		w.Write(diagnostics.GetGraphJson(diag))
	})
	err := http.ListenAndServe(s, nil)
	if err != nil {
		fmt.Println(err)
	}
}