Example #1
0
func QDataGet(c *gin.Context) []*cmodel.GraphQueryResponse {
	log := logger.Logger()
	startTmp := c.DefaultQuery("startTs", string(time.Now().Unix()-(86400)))
	startTmp2, _ := strconv.Atoi(startTmp)
	startTs := int64(startTmp2)
	endTmp := c.DefaultQuery("endTs", string(time.Now().Unix()))
	endTmp2, _ := strconv.Atoi(endTmp)
	endTs := int64(endTmp2)
	consolFun := c.DefaultQuery("consolFun", "AVERAGE")
	stepTmp := c.DefaultQuery("step", "60")
	step, _ := strconv.Atoi(stepTmp)
	counter := c.DefaultQuery("counter", "cpu.idle")
	endpoints := model.EndpointQuery()
	var result []*cmodel.GraphQueryResponse
	for _, enp := range endpoints {
		q := cmodel.GraphQueryParam{
			Start:     startTs,
			End:       endTs,
			ConsolFun: consolFun,
			Step:      step,
			Endpoint:  enp,
			Counter:   counter,
		}
		res, _ := graph.QueryOne(q)
		log.Debug(fmt.Sprintf("%v, %v, %v", res.Counter, res.Endpoint, len(res.Values)))
		result = append(result, res)
	}
	log.Debug(fmt.Sprintf("%s: %d", "openfaclon query got", len(result)))
	return result
}
Example #2
0
File: api.go Project: donh/query
func getGraphQueryResponse(metrics []string, duration string, hostnames []string, result map[string]interface{}) []*cmodel.GraphQueryResponse {
	data := []*cmodel.GraphQueryResponse{}
	start, end := convertDurationToPoint(duration, result)

	proc.HistoryRequestCnt.Incr()
	for _, hostname := range hostnames {
		for _, metric := range metrics {
			request := cmodel.GraphQueryParam{
				Start:     start,
				End:       end,
				ConsolFun: "AVERAGE",
				Endpoint:  hostname,
				Counter:   metric,
			}
			response, err := graph.QueryOne(request)
			if err != nil {
				setError("graph.queryOne fail, "+err.Error(), result)
			}
			if result == nil {
				continue
			}
			data = append(data, response)
		}
	}

	proc.HistoryResponseCounterCnt.IncrBy(int64(len(data)))
	for _, item := range data {
		proc.HistoryResponseItemCnt.IncrBy(int64(len(item.Values)))
	}
	return data
}
Example #3
0
func rrdQuery(in *pb.QueryInput) (resp []*cmodel.GraphQueryResponse) {
	queryParams := getq(in)
	endpointList, _ := getEndPoints(queryParams.Endpoint)
	counterList, _ := getCounter(queryParams.Counter)
	for _, enp := range endpointList {
		queryParams.Endpoint = enp
		for _, con := range counterList {
			queryParams.Counter = con
			res, err := graph.QueryOne(queryParams)
			if err != nil {
				log.Printf("%v", err.Error())
			}
			resp = append(resp, res)
		}
	}
	return
}
Example #4
0
func graphQueryOne(ec cmodel.GraphInfoParam, body GraphHistoryParam, endpoint string, counter string) *cmodel.GraphQueryResponse {
	if endpoint == "" {
		endpoint = ec.Endpoint
	}
	if counter == "" {
		counter = ec.Counter
	}
	request := cmodel.GraphQueryParam{
		Start:     int64(body.Start),
		End:       int64(body.End),
		ConsolFun: body.CF,
		Endpoint:  endpoint,
		Counter:   counter,
	}
	result, err := graph.QueryOne(request)
	if err != nil {
		log.Printf("graph.queryOne fail, %v, (endpoint, counter) = (%s, %s)", err, endpoint, counter)
	}
	return result
}
Example #5
0
func configGraphRoutes() {

	// method:post
	http.HandleFunc("/graph/history", func(w http.ResponseWriter, r *http.Request) {
		// statistics
		proc.HistoryRequestCnt.Incr()

		var body GraphHistoryParam
		decoder := json.NewDecoder(r.Body)
		err := decoder.Decode(&body)
		if err != nil {
			StdRender(w, "", err)
			return
		}

		if len(body.EndpointCounters) == 0 {
			StdRender(w, "", errors.New("empty_payload"))
			return
		}

		data := []*cmodel.GraphQueryResponse{}
		for _, ec := range body.EndpointCounters {
			request := cmodel.GraphQueryParam{
				Start:     int64(body.Start),
				End:       int64(body.End),
				ConsolFun: body.CF,
				Endpoint:  ec.Endpoint,
				Counter:   ec.Counter,
			}
			result, err := graph.QueryOne(request)
			if err != nil {
				log.Printf("graph.queryOne fail, %v", err)
			}
			if result == nil {
				continue
			}
			data = append(data, result)
		}

		// statistics
		proc.HistoryResponseCounterCnt.IncrBy(int64(len(data)))
		for _, item := range data {
			proc.HistoryResponseItemCnt.IncrBy(int64(len(item.Values)))
		}

		StdRender(w, data, nil)
	})

	// post, info
	http.HandleFunc("/graph/info", func(w http.ResponseWriter, r *http.Request) {
		// statistics
		proc.InfoRequestCnt.Incr()

		var body []*cmodel.GraphInfoParam
		decoder := json.NewDecoder(r.Body)
		err := decoder.Decode(&body)
		if err != nil {
			StdRender(w, "", err)
			return
		}

		if len(body) == 0 {
			StdRender(w, "", errors.New("empty"))
			return
		}

		data := []*cmodel.GraphFullyInfo{}
		for _, param := range body {
			if param == nil {
				continue
			}
			info, err := graph.Info(*param)
			if err != nil {
				log.Printf("graph.info fail, resp: %v, err: %v", info, err)
			}
			if info == nil {
				continue
			}
			data = append(data, info)
		}

		StdRender(w, data, nil)
	})

	// post, last
	http.HandleFunc("/graph/last", func(w http.ResponseWriter, r *http.Request) {
		// statistics
		proc.LastRequestCnt.Incr()

		var body []*cmodel.GraphLastParam
		decoder := json.NewDecoder(r.Body)
		err := decoder.Decode(&body)
		if err != nil {
			StdRender(w, "", err)
			return
		}

		if len(body) == 0 {
			StdRender(w, "", errors.New("empty"))
			return
		}

		data := []*cmodel.GraphLastResp{}
		for _, param := range body {
			if param == nil {
				continue
			}
			last, err := graph.Last(*param)
			if err != nil {
				log.Printf("graph.last fail, resp: %v, err: %v", last, err)
			}
			if last == nil {
				continue
			}
			data = append(data, last)
		}

		// statistics
		proc.LastRequestItemCnt.IncrBy(int64(len(data)))

		StdRender(w, data, nil)
	})

	// post, last/raw
	http.HandleFunc("/graph/last/raw", func(w http.ResponseWriter, r *http.Request) {
		// statistics
		proc.LastRawRequestCnt.Incr()

		var body []*cmodel.GraphLastParam
		decoder := json.NewDecoder(r.Body)
		err := decoder.Decode(&body)
		if err != nil {
			StdRender(w, "", err)
			return
		}

		if len(body) == 0 {
			StdRender(w, "", errors.New("empty"))
			return
		}

		data := []*cmodel.GraphLastResp{}
		for _, param := range body {
			if param == nil {
				continue
			}
			last, err := graph.LastRaw(*param)
			if err != nil {
				log.Printf("graph.last.raw fail, resp: %v, err: %v", last, err)
			}
			if last == nil {
				continue
			}
			data = append(data, last)
		}
		// statistics
		proc.LastRawRequestItemCnt.IncrBy(int64(len(data)))
		StdRender(w, data, nil)
	})

}