Skip to content

Commit

Permalink
feat: support points write method
Browse files Browse the repository at this point in the history
Signed-off-by: PennyYoon <[email protected]>
  • Loading branch information
Chenxulin97 committed Dec 11, 2023
1 parent ab1de2e commit d5b3235
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
5 changes: 4 additions & 1 deletion opengemini/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package opengemini

import (
"crypto/tls"
"net/http"
"time"
)

Expand All @@ -18,7 +19,9 @@ type Client interface {
Ping(idx int) error
Query(query Query) (*QueryResult, error)

// WriteBatchPoints batch points to assigned database
// WritePoint write single point to assigned database
WritePoint(database string, point *Point, callbackFunc func(*http.Response, error))
// WriteBatchPoints write batch points to assigned database
WriteBatchPoints(database string, bp *BatchPoints) error
// CreateDatabase Create database
CreateDatabase(database string) error
Expand Down
30 changes: 30 additions & 0 deletions opengemini/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,33 @@ func (c *client) WriteBatchPoints(database string, bp *BatchPoints) error {
}
return nil
}

func (c *client) WritePoint(database string, point *Point, callbackFunc func(*http.Response, error)) {
var buffer bytes.Buffer

var writer io.Writer

if c.config.GzipEnabled {
writer = gzip.NewWriter(&buffer)
} else {
writer = &buffer
}

if _, err := io.WriteString(writer, point.String()); err != nil {
callbackFunc(nil, err)
}

if closer, ok := writer.(io.Closer); ok {
if err := closer.Close(); err != nil {
callbackFunc(nil, err)
}
}

req := requestDetails{
queryValues: make(url.Values),
body: &buffer,
}
req.queryValues.Add("db", database)
resp, err := c.executeHttpPost(UrlWrite, req)
callbackFunc(resp, err)
}
29 changes: 28 additions & 1 deletion opengemini/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package opengemini

import (
"github.com/stretchr/testify/assert"
"net/http"
"testing"
"time"
)

func TestClient_Write(t *testing.T) {
func TestClient_WriteBatchPoints(t *testing.T) {
c := testDefaultClient(t)

// create a test database with rand suffix
Expand Down Expand Up @@ -60,3 +61,29 @@ func TestClient_Write(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, 2, len(result.Results[0].Series[0].Values))
}

func TestClient_WritePoint(t *testing.T) {
c := testDefaultClient(t)

// create a test database with rand suffix
database := randomDatabaseName()
err := c.CreateDatabase(database)
assert.Nil(t, err)

// delete test database before exit test case
defer func() {
err := c.DropDatabase(database)
assert.Nil(t, err)
}()

callback := func(resp *http.Response, err error) {
assert.Equal(t, resp.StatusCode, http.StatusNoContent)
assert.Nil(t, err)
}
point := &Point{}
point.Measurement = randomMeasurement()
point.AddTag("tag", "test")
point.AddField("field", "test")
c.WritePoint(database, point, callback)

}

0 comments on commit d5b3235

Please sign in to comment.