Skip to content

Commit

Permalink
ES stats added to stats
Browse files Browse the repository at this point in the history
  • Loading branch information
gzigzigzeo committed May 27, 2019
1 parent a1fe08d commit 3081191
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions commands/stats.go
@@ -1,10 +1,14 @@
package commands

import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"strconv"

"github.com/astroband/astrologer/config"
"github.com/astroband/astrologer/db"
"github.com/olekukonko/tablewriter"
)
Expand All @@ -22,7 +26,7 @@ func Stats() {
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Min", "Max", "Count"})
table.SetHeader([]string{"Min", "Max", "Count", "ES"})

g = append(g, first.LedgerSeq)

Expand All @@ -35,22 +39,67 @@ func Stats() {
g = append(g, last.LedgerSeq)

total := 0
totalES := 0

for i := 0; i < len(g)/2; i++ {
min := g[i*2]
max := g[i*2+1]
count := max - min + 1
countES := esCount(min, max)

total += count
totalES += countES

table.Append([]string{
strconv.Itoa(min),
strconv.Itoa(max),
strconv.Itoa(count),
strconv.Itoa(countES),
})
}

table.SetFooter([]string{"", "Total", strconv.Itoa(total)})
table.SetFooter([]string{"", "Total", strconv.Itoa(total), strconv.Itoa(totalES)})

table.Render()
}

func esCount(min int, max int) int {
var r map[string]interface{}
var buf bytes.Buffer

query := map[string]interface{}{
"query": map[string]interface{}{
"range": map[string]interface{}{
"seq": map[string]interface{}{
"gte": min,
"lte": max,
},
},
},
}

if err := json.NewEncoder(&buf).Encode(query); err != nil {
log.Fatalf("Error encoding query: %s", err)
}

res, err := config.ES.Count(
config.ES.Count.WithIndex("ledger"),
config.ES.Count.WithBody(&buf),
)

if err != nil {
log.Fatal(err)
}

if res.IsError() {
log.Fatal("Error in response", res.Body)
}

if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
log.Fatalf("Error parsing the response body: %s", err)
}

res.Body.Close()

return int(r["count"].(float64))
}

0 comments on commit 3081191

Please sign in to comment.