From effc498b30e32625354ca6ee7cacb32cc0a5556d Mon Sep 17 00:00:00 2001 From: Todd Gruben Date: Thu, 3 Apr 2014 14:44:45 -0500 Subject: [PATCH] initial support time frame set_bit --- core/http.go | 46 +++++++++++++++++++++++++++++++++++++++------- core/service.go | 2 +- index/timeframe.go | 6 +++--- 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/core/http.go b/core/http.go index f549b7184..4dba2179d 100644 --- a/core/http.go +++ b/core/http.go @@ -7,10 +7,13 @@ import ( "net/http" "pilosa/config" "pilosa/db" + "pilosa/index" "reflect" "runtime" "strconv" + "strings" + "time" notify "github.com/bitly/go-notify" "github.com/davecgh/go-spew/spew" "github.com/gorilla/websocket" @@ -171,15 +174,40 @@ func (self *WebService) HandleQuery(w http.ResponseWriter, r *http.Request) { type JsonObject map[string]interface{} // post this json to the body -//[{ "db": "3", "frame":"brand.","profile_id": 122,"filter":0, "bitmap_id":123}, -// { "db": "3", "frame":"brand.","profile_id": 122,"filter":2, "bitmap_id":124}]' +//[{ "db": "3", "frame":"brand.n","profile_id": 122,"filter":0, "bitmap_id":123}, +// { "db": "3", "frame":"brand.n","profile_id": 122,"filter":2, "bitmap_id":124}]' // +func bitmaps(frame string, obj JsonObject) chan uint64 { + c := make(chan uint64) + const shortForm = "2006-01-02 15:04:01" + + go func() { + t := float64(obj["bitmap_id"].(float64)) + base_id := uint64(t) + + if strings.HasSuffix(frame, ".t") { + timestamp := obj["timestamp"].(string) + atime, _ := time.Parse(shortForm, timestamp) + + for _, id := range index.GetTimeIds(base_id, atime, index.YMDH) { + c <- id + } + } else { + c <- base_id + } + + close(c) + }() + + return c +} func (self *WebService) HandleSetBit(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { http.Error(w, "Only POST allowed", http.StatusMethodNotAllowed) return } + decoder := json.NewDecoder(r.Body) var args []JsonObject @@ -191,13 +219,17 @@ func (self *WebService) HandleSetBit(w http.ResponseWriter, r *http.Request) { //[{ "db": "3", "frame":"brand.","profile_id": 122,"filter":0, "bitmap_id":123}] var results []interface{} for _, obj := range args { + t := float64(obj["profile_id"].(float64)) + profile_id := uint64(t) db := obj["db"].(string) frame := obj["frame"].(string) - profile_id := obj["profile_id"].(int) - bitmap_id := obj["bitmap_id"].(int64) - filter := obj["filter"].(int) - pql := fmt.Sprintf("set(%d, %s, %d, %d)", bitmap_id, frame, filter, profile_id) - results = append(results, self.service.Executor.RunPQL(db, pql)) + t = float64(obj["filter"].(float64)) + filter := int(t) + for bitmap_id := range bitmaps(frame, obj) { + pql := fmt.Sprintf("set(%d, %s, %d, %d)", bitmap_id, frame, filter, profile_id) + results = append(results, self.service.Executor.RunPQL(db, pql)) + // results = append(results, db+" "+pql) + } } diff --git a/core/service.go b/core/service.go index 8f0e3197d..70ead9140 100644 --- a/core/service.go +++ b/core/service.go @@ -45,7 +45,7 @@ func NewService() *Service { service.WebService = NewWebService(service) service.Index = index.NewFragmentContainer() service.Hold = hold.NewHolder() - service.version = "0.0.9" + service.version = "0.0.10" service.name = "Cruncher" service.PrepareLogging() fmt.Printf("Pilosa %s\n", service.version) diff --git a/index/timeframe.go b/index/timeframe.go index a6d9e5f6d..313d5677f 100644 --- a/index/timeframe.go +++ b/index/timeframe.go @@ -12,13 +12,13 @@ const ( ) func GetTimeID(t TimeQuantum, year uint, month uint, day uint, hour uint, tile_id uint64) uint64 { - y := year - 2014 //config.startyear - time_stamp := uint64((uint(t) << 30) | (y << 24) | (month << 20) | (day << 15) | (hour << 10)) + y := year - 1970 //config.startyear + time_stamp := uint64((uint(t) << 30) | (y << 23) | (month << 19) | (day << 14) | (hour << 9)) time_stamp = time_stamp << 32 return time_stamp | tile_id } -func getTimeIds(tile_id uint64, atime time.Time, min_quantum TimeQuantum) []uint64 { +func GetTimeIds(tile_id uint64, atime time.Time, min_quantum TimeQuantum) []uint64 { results := make([]uint64, 0, 4) year := uint(atime.Year()) month := atime.Month()