From 0f9f860624085e4d90cbb85bac70ec4dc6bb4e04 Mon Sep 17 00:00:00 2001 From: Linh Vo Date: Thu, 6 Jul 2017 12:10:43 -0500 Subject: [PATCH] update timestamp for whole frame --- handler.go | 12 +++++++++++- handler_test.go | 15 ++++++++++++--- input_definition.go | 2 +- input_definition_test.go | 8 +------- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/handler.go b/handler.go index ade37b0c3..c9868a3a9 100644 --- a/handler.go +++ b/handler.go @@ -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) diff --git a/handler_test.go b/handler_test.go index c1e752e32..34eff2424 100644 --- a/handler_test.go +++ b/handler_test.go @@ -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 diff --git a/input_definition.go b/input_definition.go index ae8cc35c9..d8aa7a90a 100644 --- a/input_definition.go +++ b/input_definition.go @@ -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() } diff --git a/input_definition_test.go b/input_definition_test.go index d6bb7ba38..7558f6fac 100644 --- a/input_definition_test.go +++ b/input_definition_test.go @@ -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)