小ネタ
はじめに
timechart
で時系列の表を作って、30分前の値と比較したいとかの場合とか、いろいろな場合での前の値との比較方法をいろいろと
SPL
| makeresults count=2
| streamstats count
| eval _time = if (count==2,relative_time(_time,"-1h@m"), relative_time(_time,"@m"))
| makecontinuous span=1m _time
| eval count=random() % 100
データはこちらを使用
delta
| delta count as defference
_time | count | defference |
---|---|---|
2019/12/22 08:24:00 | 1 | |
2019/12/22 08:25:00 | 55 | 54 |
2019/12/22 08:26:00 | 75 | 20 |
2019/12/22 08:27:00 | 29 | -46 |
前の値との差分をだしてくれる。
p= を指定することでどれだけ前の値と比較するか指定できる。
autoregress
| autoregress count
| eval perc=round(count/(count+count_p1)*100)."%"
| eval perc=case(count = count_p1,"±", count > count_p1,"+",count < count_p1,"-").perc
_time | count | count_p1 | perc |
---|---|---|---|
2019/12/22 08:35:00 | 69 | ||
2019/12/22 08:36:00 | 32 | 69 | -32% |
2019/12/22 08:37:00 | 53 | 32 | +62% |
2019/12/22 08:38:00 | 35 | 53 | -40% |
delta
と違い、前の値をそのまま出すautoregress
こんな感じで前の値との比較を少し凝った形にしたい時に使う。
こちらも p= が指定できる。
ダッシュボードSingle valueで表示したら、自動でやってくれてるね。これ
streamstats
| streamstats current=f last(count) as count_p1
_time | count | count_p1 |
---|---|---|
2019/12/22 08:40:00 | 17 | |
2019/12/22 08:41:00 | 88 | 17 |
2019/12/22 08:42:00 | 81 | 88 |
2019/12/22 08:43:00 | 89 | 81 |
autoregress
の出力と同じにしてみた。
_current=f_がポイント。
autoregress
との違いは、条件をeval
にて指定できること。
あとは _window=_が指定できるので、いろいろ応用が効く。
| streamstats current=f last(eval(if(searchmatch("hoge"),count,NULL))) as count_p1
文字列_hoge_があるログの前の値とか指定できる。
まとめ
表(じゃなくてもいい)の順番をreverse
とかsort 0
で揃えて使うのが基本。
この3つのコマンドを使えば、前の値との比較は大丈夫 かな