Skip to content

Instantly share code, notes, and snippets.

@greenboxal
Created March 9, 2015 01:45
Show Gist options
  • Save greenboxal/36ac4ff93e45f886fca9 to your computer and use it in GitHub Desktop.
Save greenboxal/36ac4ff93e45f886fca9 to your computer and use it in GitHub Desktop.
require! {
'prelude-ls': {each, map, fold, sum}
}
angular.module \dashboard.metrics <[ dashboard.api dashboard.omnibox ]>
.factory \MetricsProvider ->
return class MetricsProvider
(@provider) ->
prepare-real-conversion-query: ~>
top-agg =
per-amount:
terms:
field: \amount
size: 0
aggs:
paid:
filter:
term:
status: \paid
date_histogram:
field: \date_created
interval: it.transaction-window || \3h
aggs:
per-customer:
filter:
exists:
field: \customer.id
aggs:
customers:
terms:
field: \customer.id
size: 0
aggs: top-agg
per-card:
filter:
and:
* exists:
field: \card.id
* missing:
field: \customer.id
aggs:
cards:
terms:
field: \card.id
size: 0
aggs: top-agg
without-owner:
filter:
and:
* missing:
field: \customer.id
* missing:
field: \card.id
aggs:
paid:
filter:
term:
status: \paid
refused:
filter:
term:
status: if payment-method is \boleto then \waiting_payment else \refused
finish-real-conversion-query: ~>
sum-params = (a, b) -->
paid: a.paid + b.paid
refused: a.refused + b.refused
fold-params = fold sum-params, do
paid: 0
refused: 0
map-unique = ->
it.buckets
|> concat-map (.perAmount.buckets)
|> map ->
paid: it.paid.doc_count
refused: if it.paid.doc_count is 0 then 1 else 0
result =
it.buckets
|> concat-map ->
map-unique it.per-card.cards
map-unique it.per-customer.customers
* paid: it.without-owner.paid.doc_count
refused: it.without-owner.refused.doc_count
|> fold-params
paid: result.paid
refused: result.refused
conversion: result.paid / (result.refused + result.paid) || 0
calculate-real-conversion: (^^query, options = {}, callback) ~>
(err, res) <~ @provider.search \transaction \count do
query:
bool:
must:
query || { match_all: {} }
...
should:
* term:
status: \paid
* term:
status: \refused
aggs:
conversion: @prepare-real-conversion-query(options)
return callback err if err
callback null, @finish-real-conversion-query(res.aggregations.conversion)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment