Skip to content

Commit

Permalink
add get latest floData api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bitspill committed Dec 1, 2018
1 parent 4ea7a86 commit 6530e34
Showing 1 changed file with 57 additions and 4 deletions.
61 changes: 57 additions & 4 deletions httpapi/flo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ func init() {
router.HandleFunc("/floData/search", handleFloDataSearch).Queries("q", "{query}", "limit", "{limit:[0-9]+}")
router.HandleFunc("/floData/search", handleFloDataSearch).Queries("q", "{query}")
router.HandleFunc("/floData/get/{id:[a-f0-9]+}", handleGetFloData)

router.HandleFunc("/floData/latest/{limit:[0-9]+}", handleFloDataLatest).Queries("coinbase", "{coinbase}")
router.HandleFunc("/floData/latest/{limit:[0-9]+}", handleFloDataLatest)
}

func handleGetFloData(w http.ResponseWriter, r *http.Request) {
Expand All @@ -41,7 +42,7 @@ func handleGetFloData(w http.ResponseWriter, r *http.Request) {

if err != nil {
log.Error("elastic search failed", logger.Attrs{"err": err})
RespondJSON(w, 500, map[string]interface{} {
RespondJSON(w, 500, map[string]interface{}{
"error": "database error",
})
}
Expand All @@ -51,8 +52,8 @@ func handleGetFloData(w http.ResponseWriter, r *http.Request) {
sources[k] = v.Source
}

RespondJSON(w, http.StatusOK, map[string]interface{} {
"total": results.Hits.TotalHits,
RespondJSON(w, http.StatusOK, map[string]interface{}{
"total": results.Hits.TotalHits,
"results": sources,
})
}
Expand Down Expand Up @@ -112,3 +113,55 @@ func handleFloDataSearch(w http.ResponseWriter, r *http.Request) {
"results": sources,
})
}

func handleFloDataLatest(w http.ResponseWriter, r *http.Request) {
var opts = mux.Vars(r)

lim, _ := opts["limit"]
size, _ := strconv.ParseInt(lim, 10, 0)
if size <= 0 || size > 1000 {
size = -1
}

q := elastic.NewBoolQuery().Must(
elastic.NewExistsQuery("tx.floData"),
)

if c, ok := opts["coinbase"]; ok {
coinbase, _ := strconv.ParseBool(c)
if coinbase == false {
q.Must(elastic.NewTermQuery("is_coinbase", coinbase))
}
}

fsc := elastic.NewFetchSourceContext(true).
Include("tx.floData", "tx.txid", "tx.time", "tx.blockhash", "tx.size", "is_coinbase")

results, err := datastore.Client().
Search(datastore.Index("transactions")).
Type("_doc").
Query(q).
Size(int(size)).
Sort("tx.time", false).
FetchSourceContext(fsc).
Do(context.TODO())

if err != nil {
log.Error("elastic search failed", logger.Attrs{"err": err})
RespondJSON(w, 500, map[string]interface{}{
"error": "database error",
})
return
}

sources := make([]interface{}, len(results.Hits.Hits))
for k, v := range results.Hits.Hits {
sources[k] = v.Source
}

RespondJSON(w, http.StatusOK, map[string]interface{}{
"count": len(results.Hits.Hits),
"total": results.Hits.TotalHits,
"results": sources,
})
}

0 comments on commit 6530e34

Please sign in to comment.