🔥

ターミナルでもsemantic layerの恩恵を受ける

に公開

背景

semantic layer でゴリゴリ開発しててなんとなくターミナルで可視化できればなーと思った

目的

  • terminal上でもグラフで可視化する

前提

  • metricflow を使います
  • 詳細な環境セットアップは述べないのでスニペットです

内容

結論、以下を使ってパイプで流すだけ

https://github.com/medialab/xan

具体例

mf query --metrics total_orders_revenue \
  --group-by metric_time__week \
  --where "metric_time__week >= '2024-10-01' AND metric_time__week <= '2024-12-31'" \
  --order metric_time \
  --quiet --csv /dev/stdout 2>/dev/null | \
xan fill -v 0 - | \
xan plot -L -T metric_time__week total_orders_revenue - \
  --cols 60 --rows 12

  400,000┼total_orders_revenue⣀                             
         │         ⡄       ⡜   ⠑⢢                           
         │        ⡸⢸      ⢰⠁     ⡇                          
         │       ⢀⠇ ⡇     ⡎      ⢸                          
  200,000┼       ⡜  ⠸⡀   ⢸        ⢇                         
         │      ⢰⠁   ⢇  ⢀⠇        ⠸⡀                        
         │     ⢀⠇    ⠘⡄ ⡸          ⢣                        
         │     ⡸      ⢣⢠⠃          ⠘⡄                       
        0┼⣀⣀⣀⣀⣠⠃      ⠘⡜            ⢣⣀⣀⣀⣀⣀⣀metric_time__week
         └───────────────┼────────────────┼────────────────┼
2024-10-07          2024-11-01       2024-11-27   2024-12-23
 mf query --metrics total_orders_revenue,gross_profit \
  --group-by metric_time__day \
  --where "metric_time__day >= '2024-06-01' AND metric_time__day <= '2024-06-15'" \
  --order metric_time \
  --quiet --csv /dev/stdout 2>/dev/null | \
xan fill -v 0 - | \
xan plot -L -T -Y gross_profit metric_time__day total_orders_revenue - \
  --cols 60 --rows 12


   60,000┼                    ⢀⠗⢄⡀    ┌────────────────────┐
         │                    ⢸  ⠈⢢   │total_orders_revenue│
         │                    ⡇   ⠈⡆  │gross_profit        │
   40,000┼                   ⢰⠁    ⢱  └────────────────────┘
         │                   ⡜     ⠈⡆   ⢀⠇⢇           ⡇  ⢣  
         │                  ⢀⠇      ⢱   ⡜ ⠘⡄         ⢸   ⠘⡄ 
   20,000┼                  ⢸       ⠈⡆ ⢰⠁  ⢱         ⡇    ⢣ 
         │                  ⡇        ⢱⢀⠇    ⢇       ⢰⠁    ⠘⡄
        0┼⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀
         └───────────────┼────────────────┼────────────────┼
2024-06-01          2024-06-05       2024-06-10   2024-06-15
mf query --metrics total_orders_revenue,gross_profit \
  --group-by metric_time__day \
  --where "metric_time__day >= '2024-06-01' AND metric_time__day <= '2024-06-15'" \
  --order metric_time \
  --quiet --csv /dev/stdout 2>/dev/null | \
xan fill -v 0 - | \
xan plot -L -T -S 2 -Y gross_profit metric_time__day total_orders_revenue - \
  --cols 70 --rows 12

   60,000┼total_orders_revenue              1┼gross_profit            
         │         ⢰⠁⢣         ⢸             │                        
         │         ⢸ ⠸⡀        ⡜⡇         0.5┼                        
   40,000┼         ⡜  ⡇  ⡄     ⡇⡇            │                        
         │         ⡇  ⡇ ⢰⢣     ⡇⢸           0┼⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒
         │        ⢠⠃  ⢱ ⡜⢸    ⢸ ⢸            │                        
   20,000┼        ⢸   ⢸ ⡇⠈⡆   ⢸  ⡇       -0.5┼                        
         │        ⡸   ⠸⣸  ⡇   ⡜  ⡇           │                        
        0┼⣀⣀⣀⣀⣀⣀⣀⣀⡇    ⡏  ⢸⣀⣀⣀⡇  ⢸         -1┼                        
         └───────────────────────┼           └───────────────────────┼
2024-06-01              2024-06-15  2024-06-01              2024-06-15
mf query --metrics total_orders_revenue \
  --group-by metric_time__day \
  --where "metric_time__day >= '2024-06-01' AND metric_time__day <= '2024-06-30'" \
  --quiet --csv /dev/stdout 2>/dev/null | \
xan fill -v 0 - | \
xan bins -s total_orders_revenue -b 5 - | \
xan hist -

Histogram for total_orders_revenue (bars: 5, sum: 30, max: 22):

>= 0      <  14… |22  73.33%|■■■■■■■■■■■■■■■■■■■■|
>= 14,021 <  28… | 0   0.00%|                    |
>= 28,042 <  42… | 1   3.33%|■                   |
>= 42,064 <  56… | 2   6.67%|■■                  |
>= 56,085 <= 70… | 5  16.67%|■■■■■               |

所感

まあ正直使わないんだけど

Discussion