sheet-historical-data-conve.../timeSeriesDB/tsdbClient.go

107 lines
1.9 KiB
Go
Raw Permalink Normal View History

2024-11-05 19:59:23 +00:00
package timeSeriesDB
import (
"fmt"
client "github.com/influxdata/influxdb1-client"
"log"
"net/url"
)
const dbName = "mydb"
const username = "shahzad"
const password = "shahzad12"
func getDbAddress() string {
return fmt.Sprintf("http://%s:%d", "116.203.181.138", 8086)
}
func doClientQuery(query string) *client.Response {
host, err := url.Parse(getDbAddress())
if err != nil {
log.Fatal(err)
}
conf := client.Config{
URL: *host,
Username: username,
Password: password,
}
con, err := client.NewClient(conf)
if err != nil {
log.Fatal(err)
}
q := client.Query{
Command: query,
Database: dbName,
}
if response, err := con.Query(q); err == nil && response.Error() == nil {
return response
}else{
//todo handle it later
return nil
}
}
func doClientWrite(pts []client.Point) {
host, err := url.Parse(getDbAddress())
if err != nil {
log.Fatal(err)
}
conf := client.Config{
URL: *host,
Username: username,
Password: password,
}
con, err := client.NewClient(conf)
if err != nil {
log.Fatal(err)
}
bps := client.BatchPoints{
Points: pts,
Database: dbName,
}
_, err = con.Write(bps)
if err != nil {
log.Fatal(err)
}
}
func testNewClient() {
host, err := url.Parse(getDbAddress())
if err != nil {
log.Fatal(err)
}
// NOTE: this assumes you've setup a user and have setup shell env variables,
// namely INFLUX_USER/INFLUX_PWD. If not just omit Username/Password below.
conf := client.Config{
URL: *host,
Username: username,
Password: password,
}
con, err := client.NewClient(conf)
if err != nil {
log.Fatal(err)
}
log.Println("Connection", con)
}
func pingClient() {
host, err := url.Parse(getDbAddress())
if err != nil {
log.Fatal(err)
}
con, err := client.NewClient(client.Config{URL: *host})
if err != nil {
log.Fatal(err)
}
dur, ver, err := con.Ping()
if err != nil {
log.Fatal(err)
}
log.Printf("Happy as a hippo! %v, %s", dur, ver)
}