-
Notifications
You must be signed in to change notification settings - Fork 1
/
cassandra.go
267 lines (212 loc) · 7.07 KB
/
cassandra.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package db
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/spf13/viper"
)
type astra_config struct {
dbPath string
clusterId string
clusterRegion string
keyspace string
table string
token string
}
type gomodoro struct {
Date string
End_timestamp string
Start_timestamp string
Sub_category string
Minutes int
Category string
}
var timeout = time.Duration(5 * time.Second)
var client = http.Client{Timeout: timeout}
var config astra_config
type gomodoros struct {
Rows []gomodoro `json:"rows"`
}
func Sync() {
token := config.token
createCassandraTable(token)
pushToRemote(token)
readRemote(token)
}
func ValidateSync() {
if isStringEmpty(config.dbPath, config.token, config.keyspace, config.clusterId, config.clusterRegion) == true {
log.Println("[ERROR] configure cassandra details at config [$HOME/gomodoro/config.yaml] to use the sync functionality")
os.Exit(0)
}
}
func LoadConfig() {
config = astra_config{
dbPath: strings.Replace(viper.GetString("gomodoro.databasePath"), "$HOME", os.Getenv("HOME"), -1),
keyspace: viper.GetString("gomodoro.cassandra.keyspace"),
clusterId: viper.GetString("gomodoro.cassandra.cluster_id"),
clusterRegion: viper.GetString("gomodoro.cassandra.cluster_region"),
token: viper.GetString("gomodoro.cassandra.token"),
table: "gomodoros",
}
}
func GetDbPath() string {
return config.dbPath
}
func isStringEmpty(values ...string) bool {
for _, v := range values {
if v == "" {
return true
}
}
return false
}
func createCassandraTable(token string) {
postURL := fmt.Sprintf("https://%v-%v.apps.astra.datastax.com/api/rest/v1/keyspaces/%v/tables", config.clusterId, config.clusterRegion, config.keyspace)
type gomodoroTable struct {
Name string `json:"name"`
IfNotExists bool `json:"ifNotExists"`
TableOptions map[string]int `json:"tableOptions"`
ColumnDefinitions []map[string]string `json:"columnDefinitions"`
PrimaryKey map[string][]string `json:"primaryKey"`
}
columnDefinitions := []map[string]string{
{"name": "year", "typeDefinition": "int", "static": "false"},
{"name": "date", "typeDefinition": "text", "static": "false"},
{"name": "start_timestamp", "typeDefinition": "text", "static": "false"},
{"name": "end_timestamp", "typeDefinition": "text", "static": "false"},
{"name": "minutes", "typeDefinition": "int", "static": "false"},
{"name": "category", "typeDefinition": "text", "static": "false"},
{"name": "sub_category", "typeDefinition": "text", "static": "false"},
}
partitionKey := []string{"year"}
clusteringKey := []string{"date", "start_timestamp"}
primaryKey := map[string][]string{
"partitionKey": partitionKey,
"clusteringKey": clusteringKey,
}
table := gomodoroTable{
Name: "gomodoros",
IfNotExists: true,
TableOptions: map[string]int{"defaultTimeToLive": 0},
ColumnDefinitions: columnDefinitions,
PrimaryKey: primaryKey,
}
var body []byte
body, err := json.Marshal(table)
if err != nil {
fmt.Println(err)
}
request, err := http.NewRequest("POST", postURL, bytes.NewBuffer(body))
request.Header.Set("Content-type", "application/json")
request.Header.Set("x-cassandra-token", token)
if err != nil {
log.Fatalf("[ERROR] error building createTable httpRequest: %v", err)
}
resp, err := client.Do(request)
if err != nil {
log.Fatalf("[ERROR] error creation Cassandra Gomodoro table: %v", err)
}
defer resp.Body.Close()
bodyResp, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("[ERROR] error reading Cassandra table creation response: %v", err)
}
log.Printf("[INFO] cassandra table synced successfully. DSE response: %v", string(bodyResp))
}
func pushToRemote(token string) {
localGomodoros := GetGomodorosCassandra()
postURL := fmt.Sprintf("https://%v-%v.apps.astra.datastax.com/api/rest/v1/keyspaces/%v/tables/%v/rows", config.clusterId, config.clusterRegion, config.keyspace, config.table)
type gomodoroPush struct {
Columns []map[string]string `json:"columns"`
}
for _, gomodoro := range localGomodoros {
columns := []map[string]string{
{"name": "year", "value": gomodoro.Year},
{"name": "date", "value": gomodoro.Date[0:10]},
{"name": "start_timestamp", "value": gomodoro.StartTimestamp},
{"name": "end_timestamp", "value": gomodoro.EndTimestamp},
{"name": "minutes", "value": strconv.Itoa(gomodoro.Minutes)},
{"name": "category", "value": gomodoro.Category},
{"name": "sub_category", "value": gomodoro.SubCategory},
}
pushEntry := gomodoroPush{
Columns: columns,
}
body, err := json.Marshal(pushEntry)
if err != nil {
log.Fatalf("[ERROR] error bulding json entry for remote push, %v", err.Error())
}
request, err := http.NewRequest("POST", postURL, bytes.NewBuffer(body))
request.Header.Set("Content-type", "application/json")
request.Header.Set("x-cassandra-token", token)
resp, err := client.Do(request)
if err != nil {
log.Fatalf("[ERROR] error pushing entry to remote, %v", err.Error())
}
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("[ERROR] error reading entry push response: %v", err.Error())
}
}
log.Printf("[INFO] local [Go]modoros pushed to remote successfully")
}
func readRemote(token string) {
years := GetValidYears()
gToPull := []gomodoro{}
for _, v := range years {
remoteGomodoros := getRemoteGomodorosByYear(v, token)
localGomodoros := GetLocalGomodorosByYear(v)
for _, v := range remoteGomodoros.Rows {
remoteEntry := fmt.Sprintf("%v %v", v.Date, v.Start_timestamp)
found := false
// TODO: implement better approach
for _, localEntry := range localGomodoros {
if localEntry == remoteEntry {
found = true
break
}
}
if !found {
gToPull = append(gToPull, v)
}
}
}
if len(gToPull) > 0 {
for _, v := range gToPull {
log.Printf("[INFO] pulling remote [Go]modoro: date: %v, startTimestamp: %v, category: %v", v.Date, v.Start_timestamp, v.Category)
InsertRecord(v.Date, v.Start_timestamp, v.End_timestamp, v.Minutes, v.Category, v.Sub_category)
}
log.Println("[INFO] [Go]modoro app synced successfully!")
} else {
log.Printf("[INFO] [Go]modoro already synchronized, no remote changes were pulled.")
}
}
func getRemoteGomodorosByYear(year int, token string) gomodoros {
getURL := fmt.Sprintf("https://%v-%v.apps.astra.datastax.com/api/rest/v1/keyspaces/%v/tables/%v/rows/%v", config.clusterId, config.clusterRegion, config.keyspace, config.table, year)
request, err := http.NewRequest("GET", getURL, nil)
request.Header.Set("Content-type", "application/json")
request.Header.Set("x-cassandra-token", token)
resp, err := client.Do(request)
if err != nil {
print(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
print(err)
}
remoteGomodoros := gomodoros{}
jsonErr := json.Unmarshal(body, &remoteGomodoros)
if jsonErr != nil {
log.Fatal(jsonErr)
}
return remoteGomodoros
}