-
Notifications
You must be signed in to change notification settings - Fork 1
/
insert_row.go
81 lines (66 loc) · 2 KB
/
insert_row.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package cmd
import (
"context"
"fmt"
"log"
"time"
"github.com/spf13/cobra"
"google.golang.org/grpc"
// "google.golang.org/grpc/credentials"
tspb "github.com/golang/protobuf/ptypes/timestamp"
pb "github.com/bartmika/tstorage-server/proto"
)
var (
metric string
value float64
tsv int64
)
func init() {
// The following are required.
insertRowCmd.Flags().StringVarP(&metric, "metric", "m", "", "The metric to attach to the TSD.")
insertRowCmd.MarkFlagRequired("metric")
insertRowCmd.Flags().Float64VarP(&value, "value", "v", 0.00, "The value to attach to the TSD.")
insertRowCmd.MarkFlagRequired("value")
insertRowCmd.Flags().Int64VarP(&tsv, "timestamp", "t", 0, "The timestamp to attach to the TSD.")
insertRowCmd.MarkFlagRequired("timestamp")
// The following are optional and will have defaults placed when missing.
insertRowCmd.Flags().IntVarP(&port, "port", "p", 50051, "The port of our server.")
rootCmd.AddCommand(insertRowCmd)
}
func doInsertRow() {
// Set up a direct connection to the gRPC server.
conn, err := grpc.Dial(
fmt.Sprintf(":%v", port),
grpc.WithInsecure(),
grpc.WithBlock(),
)
if err != nil {
log.Fatalf("did not connect: %v", err)
}
// Set up our protocol buffer interface.
client := pb.NewTStorageClient(conn)
defer conn.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
ts := &tspb.Timestamp{
Seconds: tsv,
Nanos: 0,
}
// Generate our labels.
labels := []*pb.Label{}
labels = append(labels, &pb.Label{Name: "Source", Value: "Command"})
// Perform our gRPC request.
_, err = client.InsertRow(ctx, &pb.TimeSeriesDatum{Labels: labels, Metric: metric, Value: value, Timestamp: ts})
if err != nil {
log.Fatalf("could not add: %v", err)
}
log.Printf("Successfully inserted")
}
var insertRowCmd = &cobra.Command{
Use: "insert_row",
Short: "Insert single datum",
Long: `Connect to the gRPC server and sends a single time-series datum.`,
Run: func(cmd *cobra.Command, args []string) {
doInsertRow()
},
}