forked from fsprojects/fantomas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typedesign.fs
32 lines (27 loc) · 1.04 KB
/
typedesign.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/// The program is from http://codereview.stackexchange.com/q/20852
module Typedesign
type Metrics =
| Revenue
| Volume
| PriceAsp
member m.FormatGroupings =
match m with
| Revenue -> [ ("revenue", """$sum: { "$Amount" }" """) ]
| Volume -> [ ("volume", """$sum: { "$Quantity" }" """) ]
| PriceAsp -> List.append Revenue.FormatGroupings Volume.FormatGroupings
member m.FormatProjections =
match m with
| Revenue -> [ ("revenue", "$revenue") ]
| Volume -> [ ("volume", "$volume") ]
| PriceAsp -> [ ("priceAsp", """ $divide: ["$revenue", "$volume"] """) ]
let buildQuery groupBy (metrics : Metrics list) =
let concatenate f =
let x =
metrics
|> List.collect f
|> List.map (fun m -> sprintf "{%s: %s}" (fst m) (snd m))
System.String.Join(",", x)
let groupings = concatenate (fun m -> m.FormatGroupings)
let projections = concatenate (fun m -> m.FormatProjections)
sprintf """{$group: {_id: {%s: "$%s"}}, %s}, $project: {%s}}""" groupBy
groupBy groupings projections