Exemple #1
0
func ReadDB(c *client.Client, sdb, ddb, cmd string) client.BatchPoints {

	q := client.Query{
		Command:  cmd,
		Database: sdb,
	}

	//get type client.BatchPoints
	var batchpoints client.BatchPoints

	response, err := c.Query(q)
	if err != nil {
		fmt.Printf("Fail to get response from database, read database error: %s\n", err.Error())
	}

	res := response.Results
	if len(res) == 0 {
		fmt.Printf("The response of database is null, read database error!\n")
	} else {

		res_length := len(res)
		for k := 0; k < res_length; k++ {

			//show progress of reading series
			count := len(res[k].Series)
			bar := pb.StartNew(count)
			for _, ser := range res[k].Series {

				//get type client.Point
				var point client.Point

				point.Measurement = ser.Name
				point.Tags = ser.Tags
				for _, v := range ser.Values {
					point.Time, _ = time.Parse(time.RFC3339, v[0].(string))

					field := make(map[string]interface{})
					l := len(v)
					for i := 1; i < l; i++ {
						if v[i] != nil {
							field[ser.Columns[i]] = v[i]
						}
					}
					point.Fields = field
					point.Precision = "s"
					batchpoints.Points = append(batchpoints.Points, point)
				}
				bar.Increment()
				time.Sleep(3 * time.Millisecond)
			}
			bar.FinishPrint("Read series has finished!\n")
		}
		batchpoints.Database = ddb
		batchpoints.RetentionPolicy = "default"
	}
	return batchpoints
}
Exemple #2
0
func resetDB(c *client.Client, database string) error {
	_, err := c.Query(client.Query{
		Command: fmt.Sprintf("DROP DATABASE %s", database),
	})

	if err != nil && !strings.Contains(err.Error(), "database not found") {
		return err
	}

	return nil
}
/*
	Send a query to InfluxDB server
*/
func queryDB(con *influxdb.Client, cmd string) (res []influxdb.Result, err error) {
	q := influxdb.Query{
		Command:  cmd,
		Database: DGConfig.DockerGuard.InfluxDB.DB,
	}
	if response, err := con.Query(q); err == nil {
		if response.Error() != nil {
			return res, response.Error()
		}
		res = response.Results
	}
	return
}
Exemple #4
0
// queryDB convenience function to query the database
func queryDB(con *client.Client, cmd string) (res []client.Result, err error) {
	q := client.Query{
		Command:  cmd,
		Database: MyDB,
	}
	if response, err := con.Query(q); err == nil {
		if response.Error() != nil {
			return res, response.Error()
		}
		res = response.Results
	}
	return res, nil
}
func queryDB(con *influx.Client, cmd string, database string) (res []influx.Result, err error) {
	q := influx.Query{
		Command:  cmd,
		Database: database,
	}
	if response, err := con.Query(q); err == nil {
		if response.Error() != nil {
			return res, response.Error()
		}
		res = response.Results
	}
	return
}
Exemple #6
0
func (s *Service) execQuery(cli *client.Client, q string) (*client.Response, error) {
	query := client.Query{
		Command: q,
	}
	resp, err := cli.Query(query)
	if err != nil {
		return nil, err
	}
	if resp.Err != nil {
		return nil, resp.Err
	}
	return resp, nil
}
Exemple #7
0
func createDatabase(con *client.Client, l *LoadTest) {
	database := *l.db
	l.Logger.Printf("creating database %s, if doesn't already exist", database)

	q := client.Query{
		Command:  fmt.Sprintf("create database %s", database),
		Database: database,
	}

	if _, err := con.Query(q); err != nil {
		panic(err)
	}
}
Exemple #8
0
func QueryDB(conn *client.Client, cmd string) (res []client.Result, err error) {
	q := client.Query{
		Command:  cmd,
		Database: DBName,
	}
	response, err := conn.Query(q)

	if err != nil {
		if response.Error() != nil {
			return res, response.Error()
		} else {
			return res, err
		}
	}
	res = response.Results
	return
}
Exemple #9
0
func queryInfluxDB(t *testing.T, client *influxdb.Client) {
	var series []*influxdb.Series
	var err error
	success := false
	for i := 0; i < maxInfluxdbRetries; i++ {
		if series, err = client.Query("list series", influxdb.Second); err == nil {
			glog.V(1).Infof("query:' list series' - output %+v from influxdb", series[0].Points)
			if len(series[0].Points) >= (len(sink_api.SupportedStatMetrics()) - 1) {
				success = true
				break
			}
		}
		glog.V(2).Infof("influxdb test case failed. Retrying")
		time.Sleep(30 * time.Second)
	}
	require.NoError(t, err, "failed to list series in Influxdb")
	require.True(t, success, "list series test case failed.")
}
func ensureSeriesExists(conn *influxdb.Client, existingQueries *influxdb.Series, seriesName, contQuery string) error {
	queryExists := false
	for _, p := range existingQueries.GetPoints() {
		id := p[1].(float64)
		query := p[2].(string)
		if strings.Contains(query, "into "+seriesName) {
			if query != contQuery {
				if _, err := conn.Query(fmt.Sprintf("drop continuous query %v", id), influxdb.Second); err != nil {
					return err
				}
			} else {
				queryExists = true
			}
		}
	}
	if !queryExists {
		if _, err := conn.Query("drop series "+seriesName, influxdb.Second); err != nil {
			return err
		}
		if _, err := conn.Query(contQuery, influxdb.Second); err != nil {
			return err
		}
	}
	return nil
}
func getInfluxData(flightName string, influxCl *client.Client) InfluxResults {
	query := fmt.Sprintf("select lat, lon, altitude, track from /^flight." + flightName + "/ limit 1")
	var infRes InfluxResults
	var err error

	infRes, err = influxCl.Query(query)
	if err != nil {
		log.Fatalf("Query %s failed!", query)
		os.Exit(1)
	}

	if len(infRes) != 1 {
		log.Printf("No series present?!")
	}

	pointsCount := len(infRes[0].GetPoints())
	var _ = pointsCount
	//log.Printf("Series count: %d\n", pointsCount)
	//log.Printf("DATA: %v", infRes[0].GetPoints());

	return infRes
}
Exemple #12
0
func Getmeasurements(c *client.Client, sdb, cmd string) []string {

	//get measurements from database
	q := client.Query{
		Command:  cmd,
		Database: sdb,
	}

	var measurements []string

	response, err := c.Query(q)
	if err != nil {
		fmt.Printf("Fail to get response from database, get measurements error: %s\n", err.Error())
	}

	res := response.Results

	if len(res[0].Series) == 0 {
		fmt.Printf("The response of database is null, get measurements error!\n")
	} else {

		values := res[0].Series[0].Values

		//show progress of getting measurements
		count := len(values)
		bar := pb.StartNew(count)

		for _, row := range values {
			measurement := fmt.Sprintf("%v", row[0])
			measurements = append(measurements, measurement)
			bar.Increment()
			time.Sleep(3 * time.Millisecond)
		}
		bar.FinishPrint("Get measurements has finished!\n")
	}
	return measurements

}
func getInfluxdbData(c *influxdb.Client, query string) (map[string]bool, error) {
	series, err := c.Query(query, influxdb.Second)
	if err != nil {
		return nil, err
	}
	if len(series) != 1 {
		return nil, fmt.Errorf("expected only one series from Influxdb for query %q. Got %+v", query, series)
	}
	if len(series[0].GetColumns()) != 2 {
		Failf("Expected two columns for query %q. Found %v", query, series[0].GetColumns())
	}
	result := map[string]bool{}
	for _, point := range series[0].GetPoints() {
		if len(point) != 2 {
			Failf("Expected only two entries in a point for query %q. Got %v", query, point)
		}
		name, ok := point[1].(string)
		if !ok {
			Failf("expected %v to be a string, but it is %T", point[1], point[1])
		}
		result[name] = false
	}
	return result, nil
}
Exemple #14
0
func prepareDatabase(client *influxdb.Client, database string) error {
	dropDbQuery := influxdb.Query{
		Command: fmt.Sprintf("drop database \"%v\"", database),
	}
	createDbQuery := influxdb.Query{
		Command: fmt.Sprintf("create database \"%v\"", database),
	}
	// A default retention policy must always be present.
	// Depending on the InfluxDB configuration it may be created automatically with the database or not.
	// TODO create ret. policy only if not present
	createPolicyQuery := influxdb.Query{
		Command: fmt.Sprintf("create retention policy \"default\" on \"%v\" duration 1h replication 1 default", database),
	}
	_, err := client.Query(dropDbQuery)
	if err != nil {
		return err
	}
	_, err = client.Query(createDbQuery)
	if err != nil {
		return err
	}
	_, err = client.Query(createPolicyQuery)
	return err
}