Skip to content

Commit

Permalink
feat: Helper get function for series (#66)
Browse files Browse the repository at this point in the history
* chore: upgrade dependencies

* doc: example

* feat: helper GetXxx method

* doc: readme and Example output
  • Loading branch information
yuanbohan authored Apr 19, 2023
1 parent 0b1706b commit bec100f
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 246 deletions.
62 changes: 30 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,72 +1,70 @@
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/GreptimeTeam/greptimedb-client-go/blob/main/LICENSE)
[![Build Status](https://github.com/greptimeteam/greptimedb-client-go/actions/workflows/ci.yml/badge.svg)](https://github.com/GreptimeTeam/greptimedb-client-go/blob/main/.github/workflows/ci.yml)
[![codecov](https://codecov.io/gh/GreptimeTeam/greptimedb-client-go/branch/main/graph/badge.svg?token=76KIKITADQ)](https://codecov.io/gh/GreptimeTeam/greptimedb-client-go)
[![Go Reference](https://pkg.go.dev/badge/github.com/GreptimeTeam/greptimedb-client-go.svg)](https://pkg.go.dev/github.com/GreptimeTeam/greptimedb-client-go)
# GreptimeDB Go Client

Provide API for using GreptimeDB client in Go.

## Installation

```sh
go get github.com/GreptimeTeam/greptimedb-client-go
```

## Usage

### Example

you can visit [example](examples/request.go) for usage details.

```shell
go run examples/request.go
```
## Example

this will output the following:
you can visit [Example][example] for usage details.

```text
Success! AffectedRows: value:1
Query monitors from db: [{host:localhost memory:1024 cpu:0.9 ts:{wall:4000000 ext:63817385045 loc:0xd6b740}}]
```
## Usage

### Datatype Supported
#### Datatype Supported
```go
int32, int64, int (as int64),
uint32, uint64, uint (as uint64),
float64,
float32, float64,
bool,
[]byte,
string,
time.Time (as int64),
time.Time,
```

```go
// Attention! The following data types may cause conversion and are not recommended.
int8, int16, // they will be stored as int32
uint8, uint16, // they will be stored as uint32
float32, // it will be stored as float64
[]byte, // it will be stored as string
// Attention! The following data types may cause conversion, not recommended.
int8, int16 // stored as int32
uint8, uint16 // stored as uint32
```

### More Options

#### Precision for Timestamp

The default precision is `Millisecond`, you can set a different precision before inserting into greptimedb.
We support `Second`, `Millisecond`, `Microsecond` and `Nanosecond`. Once the precision is setted, you can not
change it any more.
The default precision is `Millisecond`, you can set a different precision,
once the precision is setted, you can not change it any more.

- `Second`
- `Millisecond`
- `Microsecond`
- `Nanosecond`

```go
metric.SetTimePrecision(time.Microsecond)
metric.SetTimePrecision(time.Microsecond)
```

#### Stream Insert

We support stream insert. You can send several insert request by `Send()` and notify DB the stream is at end by `CloseAndRecv()`
You can send several insert request by `Send()` and notify DB no more messages by `CloseAndRecv()`

you can visit `stream_client_test.go` for details
you can visit [stream_client_test.go](stream_client_test.go) for details

#### Prometheus

We also support querying with RangePromql and Promql(TODO).

you can visit `promql_test.go` for details
you can visit [promql_test.go](promql_test.go) for details

## License

This greptimedb-client-go uses the __Apache 2.0 license__ to strike a balance between open contributions and allowing you to use the software however you want.
This greptimedb-client-go uses the __Apache 2.0 license__ to strike a balance
between open contributions and allowing you to use the software however you want.

<!-- links -->
[example]: https://pkg.go.dev/github.com/GreptimeTeam/greptimedb-client-go#example-package
104 changes: 54 additions & 50 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ import (
"google.golang.org/grpc/credentials/insecure"
)

type monitor struct {
host string
memory uint64
cpu float64
temperature int64
ts time.Time
isAuthed bool
}

var (
database = "public"
addr = "127.0.0.1"
Expand Down Expand Up @@ -77,15 +86,6 @@ func init() {
}
}

type monitor struct {
host string
memory uint64
cpu float64
temperature int64
ts time.Time
isAuthed bool
}

func TestInsertAndQueryWithSql(t *testing.T) {
table := "test_insert_and_query_with_sql"
insertMonitors := []monitor{
Expand Down Expand Up @@ -146,24 +146,25 @@ func TestInsertAndQueryWithSql(t *testing.T) {

queryMonitors := []monitor{}
for _, series := range resMetric.GetSeries() {
host, ok := series.Get("host")
host, ok := series.GetString("host")
assert.True(t, ok)
ts := series.GetTimestamp()
temperature, ok := series.Get("temperature")
temperature, ok := series.GetInt("temperature")
assert.True(t, ok)
memory, ok := series.Get("memory")
memory, ok := series.GetUint("memory")
assert.True(t, ok)
cpu, ok := series.Get("cpu")
cpu, ok := series.GetFloat("cpu")
assert.True(t, ok)
isAuthed, ok := series.Get("is_authed")
isAuthed, ok := series.GetBool("is_authed")
assert.True(t, ok)

ts := series.GetTimestamp()
queryMonitors = append(queryMonitors, monitor{
host: host.(string),
host: host,
ts: ts,
memory: memory.(uint64),
cpu: cpu.(float64),
temperature: temperature.(int64),
isAuthed: isAuthed.(bool),
memory: memory,
cpu: cpu,
temperature: temperature,
isAuthed: isAuthed,
})
}
assert.Equal(t, insertMonitors, queryMonitors)
Expand Down Expand Up @@ -382,13 +383,16 @@ func TestDataTypes(t *testing.T) {
series.AddTag("int16_v", data.int16V)
series.AddTag("int8_v", data.int8V)
series.AddTag("int_v", data.intV)

series.AddTag("uint64_v", data.uint64V)
series.AddField("uint32_v", data.uint32V)
series.AddField("uint16_v", data.uint16V)
series.AddField("uint8_v", data.uint8V)
series.AddField("uint_v", data.uintV)

series.AddField("float64_v", data.float64V)
series.AddField("float32_v", data.float32V)

series.AddField("string_v", data.stringV)
series.AddField("byte_v", data.byteV)
series.AddField("bool_v", data.boolV)
Expand All @@ -411,54 +415,54 @@ func TestDataTypes(t *testing.T) {
assert.Equal(t, 1, len(resMetric.GetSeries()))

series = resMetric.GetSeries()[0]
int64V, ok := series.Get("int64_v")
int64V, ok := series.GetInt("int64_v")
assert.True(t, ok)
int32V, ok := series.Get("int32_v")
int32V, ok := series.GetInt("int32_v")
assert.True(t, ok)
int16V, ok := series.Get("int16_v")
int16V, ok := series.GetInt("int16_v")
assert.True(t, ok)
int8V, ok := series.Get("int8_v")
int8V, ok := series.GetInt("int8_v")
assert.True(t, ok)
intV, ok := series.Get("int_v")
intV, ok := series.GetInt("int_v")
assert.True(t, ok)
uint64V, ok := series.Get("uint64_v")
uint64V, ok := series.GetUint("uint64_v")
assert.True(t, ok)
uint32V, ok := series.Get("uint32_v")
uint32V, ok := series.GetUint("uint32_v")
assert.True(t, ok)
uint16V, ok := series.Get("uint16_v")
uint16V, ok := series.GetUint("uint16_v")
assert.True(t, ok)
uint8V, ok := series.Get("uint8_v")
uint8V, ok := series.GetUint("uint8_v")
assert.True(t, ok)
uintV, ok := series.Get("uint_v")
uintV, ok := series.GetUint("uint_v")
assert.True(t, ok)
float64V, ok := series.Get("float64_v")
float64V, ok := series.GetFloat("float64_v")
assert.True(t, ok)
float32V, ok := series.Get("float32_v")
float32V, ok := series.GetFloat("float32_v")
assert.True(t, ok)
stringV, ok := series.Get("string_v")
stringV, ok := series.GetString("string_v")
assert.True(t, ok)
byteV, ok := series.Get("byte_v")
byteV, ok := series.GetBytes("byte_v")
assert.True(t, ok)
boolV, ok := series.Get("bool_v")
boolV, ok := series.GetBool("bool_v")
assert.True(t, ok)
timeV := series.GetTimestamp()

querydata := datatype{
int64V: int64V.(int64),
int32V: int32V.(int32),
int16V: int16(int16V.(int32)),
int8V: int8(int8V.(int32)),
intV: int(intV.(int64)),
uint64V: uint64V.(uint64),
uint32V: uint32V.(uint32),
uint16V: uint16(uint16V.(uint32)),
uint8V: uint8(uint8V.(uint32)),
uintV: uint(uintV.(uint64)),
float64V: float64V.(float64),
float32V: float32(float32V.(float64)),
stringV: stringV.(string),
byteV: []byte(byteV.(string)),
boolV: boolV.(bool),
int64V: int64V,
int32V: int32(int32V),
int16V: int16(int16V),
int8V: int8(int8V),
intV: int(intV),
uint64V: uint64V,
uint32V: uint32(uint32V),
uint16V: uint16(uint16V),
uint8V: uint8(uint8V),
uintV: uint(uintV),
float64V: float64V,
float32V: float32(float32V),
stringV: stringV,
byteV: byteV,
boolV: boolV,
timeV: timeV,
}
assert.Equal(t, data, querydata)
Expand Down
Loading

0 comments on commit bec100f

Please sign in to comment.