示例#1
0
func ListVariables(ctx context.Context, ds *datastore.Datastore, w http.ResponseWriter, req *http.Request) {
	prefix := req.FormValue("p")
	if prefix == "" {
		prefix = "/*"
	}
	if prefix[len(prefix)-1] != '*' {
		prefix = prefix + "*"
	}
	v := variable.NewFromString(prefix)
	vars := make(map[string]*oproto.StreamVariable)
	for stream := range ds.Reader(ctx, v) {
		vars[stream.Variable.Name] = stream.Variable
	}
	if len(vars) == 0 {
		w.WriteHeader(404)
		return
	}
	w.WriteHeader(200)
	keys := make([]string, len(vars))
	i := 0
	for k := range vars {
		keys[i] = k
		i++
	}
	sort.Strings(keys)
	out, _ := json.Marshal(keys)
	w.Header().Set("Content-Type", "text/json")
	w.Write(out)
}
示例#2
0
func GetBlocks(ctx context.Context, ds *datastore.Datastore, w http.ResponseWriter, req *http.Request) {
	b := []*oproto.Block{}
	for _, block := range ds.Blocks() {
		b = append(b, block.ToProto())
	}
	datastore.ProtoBlockBy(func(a, b *oproto.Block) bool { return a.EndKey < b.EndKey }).Sort(b)
	out, _ := json.Marshal(b)
	w.Header().Set("Content-Type", "text/json")
	w.Write(out)
}
示例#3
0
func Add(ctx context.Context, ds *datastore.Datastore, w http.ResponseWriter, req *http.Request) {
	var request oproto.AddRequest
	var response oproto.AddResponse
	if parseRequest(w, req, &request) != nil {
		return
	}

	in := ds.Writer(ctx)
	for _, stream := range request.Stream {
		in <- stream
	}
	close(in)

	response.Success = true
	returnResponse(w, req, &response)
}
示例#4
0
func InspectVariable(ctx context.Context, ds *datastore.Datastore, w http.ResponseWriter, req *http.Request) {
	t, err := template.ParseFiles(fmt.Sprintf("%s/inspect_variable.html", *templatePath))
	if err != nil {
		openinstrument.Logf(ctx, "Couldn't find template file: %s", err)
		return
	}
	type varInfo struct {
		Name           string
		FirstTimestamp time.Time
		LastTimestamp  time.Time
	}
	p := struct {
		Title     string
		Query     string
		Variables []varInfo
	}{
		Title:     "Inspect Variable",
		Query:     req.FormValue("q"),
		Variables: make([]varInfo, 0),
	}

	if p.Query == "" {
		w.WriteHeader(404)
		fmt.Fprintf(w, "Specify q=")
		return
	}

	v := variable.NewFromString(p.Query)
	c := ds.Reader(ctx, v)
	for stream := range c {
		lt := stream.Value[len(stream.Value)-1].EndTimestamp
		if lt == 0 {
			lt = stream.Value[len(stream.Value)-1].Timestamp
		}
		p.Variables = append(p.Variables, varInfo{
			Name:           variable.ProtoToString(stream.Variable),
			FirstTimestamp: time.Unix(int64(stream.Value[0].Timestamp/1000), 0),
			LastTimestamp:  time.Unix(int64(lt/1000), 0),
		})
	}

	err = t.Execute(w, p)
	if err != nil {
		log.Println(err)
	}
}
示例#5
0
func Query(ctx context.Context, ds *datastore.Datastore, w http.ResponseWriter, req *http.Request) {
	query := req.FormValue("q")
	showValues := req.FormValue("v") == "1"
	type Result struct {
		Variable string          `json:"name"`
		Values   [][]interface{} `json:"values"`
	}

	var duration *time.Duration
	requestVariable := variable.NewFromString(query)
	if req.FormValue("d") != "" {
		d, err := time.ParseDuration(req.FormValue("d"))
		if err != nil {
			w.WriteHeader(401)
			fmt.Fprintf(w, "Invalid duration")
			return
		}
		duration = &d
		requestVariable.MinTimestamp = int64(time.Now().UnixNano()-d.Nanoseconds()) / 1000000
	}

	if query == "" {
		w.WriteHeader(401)
		fmt.Fprintf(w, "Specify q=")
		return
	}

	results := make([]Result, 0)

	for stream := range ds.Reader(ctx, requestVariable) {
		r := Result{
			Variable: variable.ProtoToString(stream.Variable),
		}
		if !showValues {
			results = append(results, r)
			continue
		}
		r.Values = make([][]interface{}, 0)

		if duration == nil {
			// Latest value only
			if len(stream.Value) > 0 {
				v := stream.Value[len(stream.Value)-1]
				r.Values = append(r.Values, []interface{}{v.Timestamp, v.DoubleValue})
			}
		} else {
			// All values over a specific time period
			for _, v := range stream.Value {
				if requestVariable.MinTimestamp == 0 || requestVariable.MinTimestamp > int64(v.Timestamp) {
					r.Values = append(r.Values, []interface{}{v.Timestamp, v.DoubleValue})
				}
			}
		}
		results = append(results, r)
	}

	b, err := json.Marshal(results)
	if err != nil {
		w.WriteHeader(500)
		fmt.Fprintf(w, "Couldn't marshal: %s", err)
		return
	}
	w.WriteHeader(200)
	w.Write(b)
}