Skip to content

Commit

Permalink
add flo transaction APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
bitspill committed Feb 26, 2019
1 parent 580e1d1 commit 25a87fc
Showing 1 changed file with 85 additions and 7 deletions.
92 changes: 85 additions & 7 deletions httpapi/flo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ func init() {
rootRouter.HandleFunc("/floData/get/{id:[a-f0-9]+}", handleGetFloData)
rootRouter.HandleFunc("/floData/latest", handleFloDataLatest)
rootRouter.HandleFunc("/floData/search", handleFloDataSearch).Queries("q", "{query}")

rootRouter.HandleFunc("/flo/tx/latest", handleFloTxLatest)
rootRouter.HandleFunc("/flo/tx/get/{id:[a-f0-9]+}", handleGetFloTx)
rootRouter.HandleFunc("/flo/tx/search", handleFloTxSearch).Queries("q", "{query}")
}

var (
txFsc = elastic.NewFetchSourceContext(true).
floDataFsc = elastic.NewFetchSourceContext(true).
Include("tx.floData", "tx.txid", "tx.time", "tx.blockhash", "tx.size", "is_coinbase")
)

Expand All @@ -34,7 +38,7 @@ func handleGetFloData(w http.ResponseWriter, r *http.Request) {
[]string{"transactions"},
q,
[]elastic.SortInfo{{Field: "tx.txid", Ascending: false}},
txFsc,
floDataFsc,
)
RespondSearch(w, searchService)
}
Expand Down Expand Up @@ -64,20 +68,40 @@ func handleFloDataSearch(w http.ResponseWriter, r *http.Request) {
{Ascending: false, Field: "tx.time"},
{Field: "tx.txid", Ascending: true},
},
txFsc,
floDataFsc,
)

RespondSearch(w, searchService)
}

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

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

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

ctx := r.Context()
searchService := BuildCommonSearchService(
ctx,
[]string{"transactions"},
query,
[]elastic.SortInfo{{Ascending: false, Field: "tx.time"}, {Ascending: true, Field: "tx.txid"}},
floDataFsc,
)

RespondSearch(w, searchService)
}

func handleFloTxLatest(w http.ResponseWriter, r *http.Request) {
query := elastic.NewBoolQuery().Must(
elastic.NewExistsQuery("tx.txid"),
)

if c := r.FormValue("coinbase"); c != "" {
coinbase, _ := strconv.ParseBool(c)
if !coinbase {
query.Must(elastic.NewTermQuery("is_coinbase", coinbase))
Expand All @@ -90,7 +114,61 @@ func handleFloDataLatest(w http.ResponseWriter, r *http.Request) {
[]string{"transactions"},
query,
[]elastic.SortInfo{{Ascending: false, Field: "tx.time"}, {Ascending: true, Field: "tx.txid"}},
txFsc,
nil,
)

RespondSearch(w, searchService)
}

func handleGetFloTx(w http.ResponseWriter, r *http.Request) {
var opts = mux.Vars(r)
log.Info("handleGetFloData", logger.Attrs{"opts": opts})

q := elastic.NewBoolQuery().Must(
elastic.NewPrefixQuery("tx.txid", opts["id"]),
)

searchService := BuildCommonSearchService(
r.Context(),
[]string{"transactions"},
q,
[]elastic.SortInfo{{Field: "tx.txid", Ascending: false}},
nil,
)
RespondSearch(w, searchService)
}

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

searchQuery, err := url.PathUnescape(opts["query"])
if err != nil {
RespondJSON(w, 400, map[string]interface{}{
"error": "unable to decode query",
})
return
}

query := elastic.NewBoolQuery().Must(
elastic.NewQueryStringQuery(searchQuery).
DefaultField("tx.floData").
AnalyzeWildcard(false),
)

if c := r.FormValue("coinbase"); c != "" {
coinbase, _ := strconv.ParseBool(c)
query.Must(elastic.NewTermQuery("is_coinbase", coinbase))
}

searchService := BuildCommonSearchService(
r.Context(),
[]string{"transactions"},
query,
[]elastic.SortInfo{
{Ascending: false, Field: "tx.time"},
{Field: "tx.txid", Ascending: true},
},
nil,
)

RespondSearch(w, searchService)
Expand Down

0 comments on commit 25a87fc

Please sign in to comment.