-
Notifications
You must be signed in to change notification settings - Fork 0
/
productedit.go
429 lines (389 loc) · 12.5 KB
/
productedit.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
* (c) 2013, Caoimhe Chaos <caoimhechaos@protonmail.com>,
* Starship Factory. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of the Starship Factory nor the name of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package main
import (
"bytes"
"database/cassandra"
"encoding/binary"
"encoding/json"
"expvar"
"log"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"ancient-solutions.com/ancientauth"
"code.google.com/p/goprotobuf/proto"
)
// Number of errors which occurred viewing products, mapped by type.
var productViewErrors *expvar.Map = expvar.NewMap("num-product-view-errors")
// Number of errors which occurred editing products, mapped by type.
var productEditErrors *expvar.Map = expvar.NewMap("num-product-edit-errors")
type Product struct {
Name string
Price float64
Barcodes []string
VendorId string
Stock uint64
}
type ProductViewAPI struct {
authenticator *ancientauth.Authenticator
client *cassandra.RetryCassandraClient
scope string
}
type ProductEditAPI struct {
authenticator *ancientauth.Authenticator
client *cassandra.RetryCassandraClient
scope string
}
func (self *ProductViewAPI) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var cp *cassandra.ColumnParent
var pred *cassandra.SlicePredicate
var res []*cassandra.ColumnOrSuperColumn
var csc *cassandra.ColumnOrSuperColumn
var ire *cassandra.InvalidRequestException
var ue *cassandra.UnavailableException
var te *cassandra.TimedOutException
var prod Product
var err error
var uuidstr string = req.FormValue("id")
var ts int64 = 0
var uuid UUID
var rawdata []byte
numRequests.Add(1)
numAPIRequests.Add(1)
// Check the user is in the reqeuested scope.
if !self.authenticator.IsAuthenticatedScope(req, self.scope) {
numDisallowedScope.Add(1)
http.Error(w,
"You are not in the right group to access this resource",
http.StatusForbidden)
return
}
if len(uuidstr) <= 0 {
http.Error(w, "Requested UUID empty", http.StatusNotAcceptable)
return
}
uuid, err = ParseUUID(uuidstr)
if err != nil {
http.Error(w, "Requested UUID invalid", http.StatusNotAcceptable)
return
}
cp = cassandra.NewColumnParent()
cp.ColumnFamily = "products"
pred = cassandra.NewSlicePredicate()
pred.ColumnNames = [][]byte{
[]byte("name"), []byte("price"), []byte("vendor"),
[]byte("barcodes"), []byte("stock"),
}
res, ire, ue, te, err = self.client.GetSlice([]byte(uuid), cp, pred,
cassandra.ConsistencyLevel_ONE)
if ire != nil {
log.Print("Invalid request: ", ire.Why)
productViewErrors.Add(ire.Why, 1)
return
}
if ue != nil {
log.Print("Unavailable")
productViewErrors.Add("unavailable", 1)
return
}
if te != nil {
log.Print("Request to database backend timed out")
productViewErrors.Add("timeout", 1)
return
}
if err != nil {
log.Print("Generic error: ", err)
productViewErrors.Add(err.Error(), 1)
return
}
for _, csc = range res {
var col = csc.Column
var cname string
if !csc.IsSetColumn() {
continue
}
cname = string(col.Name)
if col.IsSetTimestamp() && col.Timestamp > ts {
ts = col.Timestamp
}
if cname == "name" {
prod.Name = string(col.Value)
} else if cname == "price" {
var buf *bytes.Buffer = bytes.NewBuffer(col.Value)
err = binary.Read(buf, binary.BigEndian, &prod.Price)
if err != nil {
log.Print("Row ", uuid.String(), " price is invalid")
productViewErrors.Add("corrupted-price", 1)
}
} else if cname == "vendor" {
prod.VendorId = UUID(col.Value).String()
} else if cname == "barcodes" {
var bc Barcodes
err = proto.Unmarshal(col.Value, &bc)
if err != nil {
log.Print("Row ", uuid.String(), " barcode is invalid")
productViewErrors.Add("corrupted-barcode", 1)
return
}
for _, code := range bc.Barcode {
prod.Barcodes = append(prod.Barcodes, code)
}
} else if cname == "stock" {
var buf *bytes.Buffer = bytes.NewBuffer(col.Value)
err = binary.Read(buf, binary.BigEndian, &prod.Stock)
if err != nil {
log.Print("Row ", uuid.String(), " stock is invalid")
productViewErrors.Add("corrupted-stock", 1)
}
}
}
rawdata, err = json.Marshal(prod)
if err != nil {
log.Print("Error marshalling JSON: ", err)
numJSONMarshalErrors.Add(1)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Add("Content-type", "application/json")
w.WriteHeader(http.StatusOK)
_, err = w.Write(rawdata)
if err != nil {
log.Print("Error writing JSON response: ", err)
numHTTPWriteErrors.Add(err.Error(), 1)
}
}
func (self *ProductEditAPI) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var buf *bytes.Buffer = new(bytes.Buffer)
var specid string = req.PostFormValue("id")
var uuid UUID
var codes *Barcodes = new(Barcodes)
var mmap map[string]map[string][]*cassandra.Mutation
var tsprefix []byte
var prod Product
var mutations []*cassandra.Mutation
var mutation *cassandra.Mutation
var col *cassandra.Column
var ire *cassandra.InvalidRequestException
var ue *cassandra.UnavailableException
var te *cassandra.TimedOutException
var barcode string
var now time.Time = time.Now()
var err error
var match bool
numRequests.Add(1)
numAPIRequests.Add(1)
// Check the user is in the reqeuested scope.
if !self.authenticator.IsAuthenticatedScope(req, self.scope) {
numDisallowedScope.Add(1)
http.Error(w,
"You are not in the right group to access this resource",
http.StatusForbidden)
return
}
// Check if the product name has been specified.
prod.Name = req.PostFormValue("prodname")
if len(prod.Name) <= 0 {
log.Print("Parsing product name: ", err)
http.Error(w, "Product name empty", http.StatusNotAcceptable)
return
}
prod.Price, err = strconv.ParseFloat(req.PostFormValue("price"), 64)
if err != nil {
log.Print("Parsing price: ", err)
http.Error(w, "price: "+err.Error(), http.StatusNotAcceptable)
return
}
prod.Stock, err = strconv.ParseUint(req.PostFormValue("stock"), 10, 64)
if err != nil {
log.Print("Parsing stock: ", err)
http.Error(w, "stock: "+err.Error(), http.StatusNotAcceptable)
return
}
// Check if the barcode has been given. If it was, it needs to be
// numeric (EAN-13). If we find different types of barcodes we can
// always revise this.
for _, barcode = range req.PostForm["barcode"] {
barcode = strings.Replace(barcode, " ", "", -1)
if len(barcode) > 0 {
match, err = regexp.MatchString("^[0-9]+$", barcode)
if err != nil {
productEditErrors.Add(err.Error(), 1)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if match {
codes.Barcode = append(codes.Barcode, barcode)
} else {
productEditErrors.Add("barcode-format-error", 1)
http.Error(w, "Barcode should only contain numbers",
http.StatusNotAcceptable)
return
}
}
}
// Create column data for the product row.
col = cassandra.NewColumn()
col.Name = []byte("name")
col.Value = []byte(prod.Name)
col.Timestamp = now.Unix()
mutation = cassandra.NewMutation()
mutation.ColumnOrSupercolumn = cassandra.NewColumnOrSuperColumn()
mutation.ColumnOrSupercolumn.Column = col
mutations = append(mutations, mutation)
err = binary.Write(buf, binary.BigEndian, prod.Price)
if err != nil {
productEditErrors.Add(err.Error(), 1)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
col = cassandra.NewColumn()
col.Name = []byte("price")
col.Value = buf.Bytes()
col.Timestamp = now.Unix()
mutation = cassandra.NewMutation()
mutation.ColumnOrSupercolumn = cassandra.NewColumnOrSuperColumn()
mutation.ColumnOrSupercolumn.Column = col
mutations = append(mutations, mutation)
buf = new(bytes.Buffer)
err = binary.Write(buf, binary.BigEndian, prod.Stock)
if err != nil {
productEditErrors.Add(err.Error(), 1)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
col = cassandra.NewColumn()
col.Name = []byte("stock")
col.Value = buf.Bytes()
col.Timestamp = now.Unix()
mutation = cassandra.NewMutation()
mutation.ColumnOrSupercolumn = cassandra.NewColumnOrSuperColumn()
mutation.ColumnOrSupercolumn.Column = col
mutations = append(mutations, mutation)
col = cassandra.NewColumn()
col.Name = []byte("barcodes")
col.Value, err = proto.Marshal(codes)
if err != nil {
productEditErrors.Add(err.Error(), 1)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
col.Timestamp = now.Unix()
mutation = cassandra.NewMutation()
mutation.ColumnOrSupercolumn = cassandra.NewColumnOrSuperColumn()
mutation.ColumnOrSupercolumn.Column = col
mutations = append(mutations, mutation)
// If we're editing an existing product, re-use that UUID. Otherwise,
// generate one.
if len(specid) > 0 {
uuid, err = ParseUUID(specid)
} else {
uuid, err = GenTimeUUID(&now)
}
if err != nil {
productEditErrors.Add(err.Error(), 1)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
mmap = make(map[string]map[string][]*cassandra.Mutation)
mmap[string(uuid)] = make(map[string][]*cassandra.Mutation)
mmap[string(uuid)]["products"] = mutations
// Create the entry in the products_byname index.
mutations = make([]*cassandra.Mutation, 0)
col = cassandra.NewColumn()
col.Name = []byte("product")
col.Value = uuid
col.Timestamp = now.Unix()
mutation = cassandra.NewMutation()
mutation.ColumnOrSupercolumn = cassandra.NewColumnOrSuperColumn()
mutation.ColumnOrSupercolumn.Column = col
mutations = append(mutations, mutation)
mmap[prod.Name] = make(map[string][]*cassandra.Mutation)
mmap[prod.Name]["products_byname"] = mutations
// If a barcode has been given, specify it in the products_bybarcode
// column.
if len(barcode) > 0 {
mmap[barcode] = make(map[string][]*cassandra.Mutation)
mmap[barcode]["products_bybarcode"] = mutations
}
// Log a timeseries entry stating that the product has been present
// at the time.
mutations = make([]*cassandra.Mutation, 0)
col = cassandra.NewColumn()
col.Name = []byte("product-count")
col.Value = buf.Bytes() // still the product count from above.
col.Timestamp = now.Unix()
mutation = cassandra.NewMutation()
mutation.ColumnOrSupercolumn = cassandra.NewColumnOrSuperColumn()
mutation.ColumnOrSupercolumn.Column = col
mutations = append(mutations, mutation)
// Create the TS prefix: first the UUID, then the timestamp.
buf = new(bytes.Buffer)
err = binary.Write(buf, binary.BigEndian, now.UnixNano())
if err != nil {
productEditErrors.Add(err.Error(), 1)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tsprefix = make([]byte, len(uuid)+buf.Len()+1)
copy(tsprefix, uuid)
tsprefix[len(uuid)] = ':'
for i, v := range buf.Bytes() {
tsprefix[len(uuid)+i+1] = v
}
mmap[string(tsprefix)] = make(map[string][]*cassandra.Mutation)
mmap[string(tsprefix)]["product_tsdata"] = mutations
// Now, write the mutations to the database.
ire, ue, te, err = self.client.AtomicBatchMutate(mmap,
cassandra.ConsistencyLevel_QUORUM)
if ire != nil {
log.Println("Invalid request: ", ire.Why)
productEditErrors.Add(ire.Why, 1)
return
}
if ue != nil {
log.Println("Unavailable")
productEditErrors.Add("unavailable", 1)
return
}
if te != nil {
log.Println("Request to database backend timed out")
productEditErrors.Add("timeout", 1)
return
}
if err != nil {
log.Println("Generic error: ", err)
productEditErrors.Add(err.Error(), 1)
return
}
}