/
main.go
176 lines (145 loc) · 4.1 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"bufio"
"context"
"encoding/csv"
"errors"
"fmt"
"io"
"os"
"path"
"strconv"
"time"
"cloud.google.com/go/pubsub"
mpb "github.com/mtraver/environmental-sensor/measurementpb"
"google.golang.org/protobuf/proto"
tspb "google.golang.org/protobuf/types/known/timestamppb"
wpb "google.golang.org/protobuf/types/known/wrapperspb"
)
const usageStr = `usage: %v csv_file project_id topic_id device_id
Reads lines from a CSV file, converts them into Measurement protobufs as defined
in measurement.proto, and publishes them to Google Cloud Pub/Sub. It's expected
that an instance of the web app is running on GCP to process the Pub/Sub
messages.
This program was written to migrate data stored in a Google Sheet to the
storage back ends supported by the web app.
The first line of the CSV file is expected to be column headers, followed by
lines of this format:
timestamp,temp1,temp2,...
The timestamp field must be formatted like this: %v
There must be at least one temperature value. If there is more than one
they are averaged before packing into the Measurement protobuf.
Arguments:
csv_file: Path to the CSV file.
project_id: Google Cloud Platform project ID.
topic_id: Google Cloud Pub/Sub topic ID. This topic must be configured to
push to the web app's Pub/Sub endpoint URL.
device_id: The device ID to include in the Pub/Sub message. Data from multiple
devices can be saved and displayed by the web app, so this
must be a unique identifier for the device from which the data in
the CSV file came.
`
const timeFormat = "2006-01-02T15:04:05.999999"
func strsToFloats(x []string) ([]float32, error) {
var numbers []float32
for _, v := range x {
f, err := strconv.ParseFloat(v, 32)
if err != nil {
return numbers, err
}
numbers = append(numbers, float32(f))
}
return numbers, nil
}
func mean(x []float32) float32 {
var total float32
for _, v := range x {
total += v
}
return total / float32(len(x))
}
func lineToProto(line []string, deviceID string) (*mpb.Measurement, error) {
if len(line) < 2 {
return nil, errors.New("Line length must be at least 2 (timestamp and one measurement)")
}
// Convert the timestamp string to a timestamp for packing into a protobuf
timestamp, err := time.Parse(timeFormat, line[0])
if err != nil {
return nil, err
}
timestampProto := tspb.New(timestamp)
if err := timestampProto.CheckValid(); err != nil {
return nil, err
}
temps, err := strsToFloats(line[1:])
if err != nil {
return nil, err
}
m := &mpb.Measurement{
DeviceId: deviceID,
Timestamp: timestampProto,
Temp: wpb.Float(mean(temps)),
}
return m, nil
}
func publishFromCSV(csvFile string, projectID string, pubsubTopic string, deviceID string) error {
f, err := os.Open(csvFile)
defer f.Close()
if err != nil {
return err
}
reader := csv.NewReader(bufio.NewReader(f))
// Set up the Pub/Sub client
ctx := context.Background()
client, err := pubsub.NewClient(ctx, projectID)
if err != nil {
return err
}
if exists, err := client.Topic(pubsubTopic).Exists(ctx); err != nil {
return err
} else if !exists {
return fmt.Errorf("Topic does not exist. Create it first: %v", pubsubTopic)
}
topic := client.Topic(pubsubTopic)
defer topic.Stop()
topic.PublishSettings = pubsub.PublishSettings{
DelayThreshold: 10 * time.Millisecond,
CountThreshold: 50,
}
// Assume that the CSV file has column headers, and strip them off first
if _, err := reader.Read(); err == io.EOF {
return err
}
for {
line, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
return err
}
// Make and marshal the protobuf
m, err := lineToProto(line, deviceID)
if err != nil {
return err
}
data, err := proto.Marshal(m)
if err != nil {
return err
}
topic.Publish(ctx, &pubsub.Message{Data: data})
}
return nil
}
func usage() {
fmt.Printf(usageStr, path.Base(os.Args[0]), timeFormat)
}
func main() {
if len(os.Args) != 5 {
usage()
os.Exit(2)
}
if err := publishFromCSV(os.Args[1], os.Args[2], os.Args[3], os.Args[4]); err != nil {
fmt.Println(err)
os.Exit(1)
}
}