update timestamp for whole frame

This commit is contained in:
Linh Vo 2017-07-06 12:10:43 -05:00
parent ea3e71f78d
commit 0f9f860624
4 changed files with 25 additions and 12 deletions

View file

@ -1670,6 +1670,7 @@ func (h *Handler) InputJSONDataParser(req map[string]interface{}, index *Index,
if field.PrimaryKey {
columnLabel = field.Name
}
// finding frame that need to add timestamp
for _, action := range field.Actions {
if action.ValueDestination == InputSetTimestamp {
timestampFrame[action.Frame] = field.Name
@ -1699,6 +1700,7 @@ func (h *Handler) InputJSONDataParser(req map[string]interface{}, index *Index,
return nil, fmt.Errorf("float64 require, got value:%s, type: %s", value, reflect.TypeOf(value))
}
// Looking into timestampFrame map and set timestamp to the whole frame
var timestamp string
for _, action := range field.Actions {
frame := action.Frame
@ -1706,7 +1708,15 @@ func (h *Handler) InputJSONDataParser(req map[string]interface{}, index *Index,
if !ok {
timestamp = ""
} else {
timestamp = req[timeField].(string)
tmstamp, ok := req[timeField]
if !ok {
timestamp = ""
} else {
timestamp, ok = tmstamp.(string)
if !ok {
return nil, fmt.Errorf("set-timestamp value must be in time format: YYYY-MM-DD, having: %v", req[timeField])
}
}
}
bit, err := HandleAction(action, req[field.Name], uint64(colValue), timestamp)

View file

@ -26,6 +26,7 @@ import (
"strings"
"testing"
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/pilosa/pilosa"
"github.com/pilosa/pilosa/internal"
@ -1279,7 +1280,7 @@ var defaultBody = `
"actions":[
{
"frame":"add-ons",
"valueDestination":"set_timestamp"
"valueDestination":"set-timestamp"
}
]
@ -1306,7 +1307,8 @@ func TestHandler_CreateInput(t *testing.T) {
"id": 1,
"cabType": "yellow",
"distanceMiles": 8,
"withPet": true
"withPet": true,
"time_value": "2017-03-20T19:35"
}]`)
h := test.NewHandler()
h.Holder = hldr.Holder
@ -1328,6 +1330,7 @@ func TestHandler_CreateInput(t *testing.T) {
t.Fatalf("unexpected status code: %d", w.Code)
}
// Test successfully ingest data
w = httptest.NewRecorder()
h.ServeHTTP(w, test.MustNewHTTPRequest("POST", "/index/i0/input/input1", bytes.NewBuffer(inputBody)))
if w.Code != http.StatusOK {
@ -1337,7 +1340,6 @@ func TestHandler_CreateInput(t *testing.T) {
}
// Verify the bits set per frame.
// f := index.Frame("cab-type")
f0 := index.Frame("distance-miles")
v0 := f0.View(pilosa.ViewStandard)
fragment0 := v0.Fragment(0)
@ -1415,6 +1417,13 @@ func TestInput_JSON(t *testing.T) {
"noFrame": 1
}]`,
err: "Frame not found: foo"},
{json: `[{
"id": 1,
"cabType": "yellow",
"distanceMiles": 8,
"time_value": 12345
}]`,
err: "set-timestamp value must be in time format: YYYY-MM-DD, having: 12345"},
}
h := test.NewHandler()
h.Holder = hldr.Holder

View file

@ -348,7 +348,7 @@ func HandleAction(a Action, value interface{}, colID uint64, timestamp string) (
if timestamp != "" {
v, err := time.Parse(TimeFormat, timestamp)
if err != nil {
return nil, fmt.Errorf("set-timestamp value for :%v must in time format: YYYY-MM-DD", timestamp)
return nil, err
}
bit.Timestamp = v.Unix()
}

View file

@ -272,7 +272,7 @@ func TestHandleAction(t *testing.T) {
action.ValueDestination = pilosa.InputSetTimestamp
timestamp = "2017-03-20T19:35"
parsedTime, _ := time.Parse(pilosa.TimeFormat, value.(string))
parsedTime, _ := time.Parse(pilosa.TimeFormat, timestamp)
b, err = pilosa.HandleAction(action, value, colID, timestamp)
if b == nil {
t.Fatalf("Expected return bit")
@ -280,12 +280,6 @@ func TestHandleAction(t *testing.T) {
t.Fatalf("Timestamp is not set correctly")
}
value = "12345677"
b, err = pilosa.HandleAction(action, value, colID, timestamp)
if !strings.Contains(err.Error(), "set-timestamp value for :12345677 must in time format: YYYY-MM-DD") {
t.Fatal("Expect invalid timestamp format")
}
action.ValueDestination = "test"
timestamp = ""
b, err = pilosa.HandleAction(action, value, colID, timestamp)