Splunk Search

当日分各データと過去データの比較

nishida_tada_ca
Loves-to-Learn Lots

○各ユーザの当日分各データと過去データ5件で比較を行いたいです。
次のようなデータの場合、
①id=8とid=7~id=3、
②id=7とid=6~id=2 で比較したいです。

"id","timestamp","userid","jpy"
1,2019-06-10 8:45:05.012349,156,500005
2,2019-08-08 5:30:05.012349,156,10005
3,2019-09-01 7:20:40.012349,156,20005
4,2019-10-10 22:30:05.012349,156,10005
5,2019-12-01 6:30:05.012349,156,10005
6,2019-12-25 13:24:30.012349,156,20005
7,2020-01-19 10:26:01.012349,156,10005
8,2020-01-19 14:01:01.012349,156,250005

○できている部分
最新のデータ1件(id=8)を取得して過去データ5件と比較する形での記載で①は取れました。
2件目以降の比較もするにはループのような形にすればよいのかと思うのですが、実現方法がよくわかりません。
過去データを全て見たいので、日付範囲指定は「全時間」で実施予定です。

Tags (2)
0 Karma

to4kawa
Ultra Champion
| makeresults 
| eval _raw="id,timestamp,userid,jpy
 1,2019-06-10 8:45:05.012349,156,500005
 2,2019-08-08 5:30:05.012349,156,10005
 3,2019-09-01 7:20:40.012349,156,20005
 4,2019-10-10 22:30:05.012349,156,10005
 5,2019-12-01 6:30:05.012349,156,10005
 6,2019-12-25 13:24:30.012349,156,20005
 7,2020-01-19 10:26:01.012349,156,10005
 8,2020-01-19 14:01:01.012349,156,250005" 
| multikv forceheader=1 
| table id,timestamp,userid,jpy
| autoregress jpy as a_jpy
| trendline sma5(a_jpy) as jpy_last5
| eval jpy_last5=jpy_last5*5 
| eval diff = jpy - jpy_last5

もっと簡単な方法が・・・・

0 Karma

to4kawa
Ultra Champion
| makeresults 
| eval _raw="id,timestamp,userid,jpy
 1,2019-06-10 8:45:05.012349,156,500005
 2,2019-08-08 5:30:05.012349,156,10005
 3,2019-09-01 7:20:40.012349,156,20005
 4,2019-10-10 22:30:05.012349,156,10005
 5,2019-12-01 6:30:05.012349,156,10005
 6,2019-12-25 13:24:30.012349,156,20005
 7,2020-01-19 10:26:01.012349,156,10005
 8,2020-01-19 14:01:01.012349,156,250005" 
| multikv forceheader=1 
| table id,timestamp,userid,jpy 
| streamstats current=f window=5 list(jpy) as list_jpy_last5
| eval list_jpy_last5=if(mvcount(list_jpy_last5)=5,list_jpy_last5,0)
| streamstats window=1 sum(list_jpy_last5) as list_jpy_last5
| eval diff = jpy - list_jpy_last5

@melonman さんの streamstats の使い方をみてつくったらこんなに簡単にできました。

なんかいろいろやったのはなんなんだろうか・・・・

0 Karma

melonman
Motivator

@to4kawa さんの回答に加えて、別アプローチとして。
そもそも質問の解釈が間違えている可能性もありますが、その場合はツッコミ願います。(w

<your search 質問で示されたデータ>
| streamstats current=f window=5 list(id) as list_ids_last5 list(jpy) as list_jpy_last5
| eval count_last5=mvcount(list_jpy_last5)
| eval jpy_last5=mvindex(list_jpy_last5,0), jpy_last4=mvindex(list_jpy_last5,1), jpy_last3=mvindex(list_jpy_last5,2), jpy_last2=mvindex(list_jpy_last5,3), jpy_last1=mvindex(list_jpy_last5,4)
| eval diff_last5=jpy-jpy_last5, diff_last4=jpy-jpy_last4, diff_last3=jpy-jpy_last3, diff_last2=jpy-jpy_last2, diff_last1=jpy-jpy_last1
  • streamstatsで過去5イベントのjpyを取得(イベント順)
  • mvcount(list_jpy_last5) は過去のイベントが幾つ含まれているかカウント、後で4以下のものは削除するとかの処理をるためのフラグ
  • listの配列から一つづつ別フィールドとして値を取得
  • 現在のjpyと上で取得した過去のデータそれぞれを差分

alt text

*この手のものは、pythonで、カスタムコマンドつくっちゃったほうがてっとりばやいかもしれませんね。

to4kawa
Ultra Champion

@melonman さん
streamstats list()がすごいです。
使わせてもらいます

0 Karma

to4kawa
Ultra Champion
| makeresults 
| eval _raw="id,timestamp,userid,jpy
1,2019-06-10 8:45:05.012349,156,500005
2,2019-08-08 5:30:05.012349,156,10005
3,2019-09-01 7:20:40.012349,156,20005
4,2019-10-10 22:30:05.012349,156,10005
5,2019-12-01 6:30:05.012349,156,10005
6,2019-12-25 13:24:30.012349,156,20005
7,2020-01-19 10:26:01.012349,156,10005
8,2020-01-19 14:01:01.012349,156,250005" 
| multikv forceheader=1 
| table id,timestamp,userid,jpy 
`comment("this is your sample")`
`comment("From here, the logic")`
| appendpipe 
    [| reverse 
    | transpose 0 header_field=id 
    | tail 1 
    | fields - column 
    | rename * as col*] 
| reverse 
| filldown 
| where isnotnull(id) 
| eval pre_jpy=0 
| foreach col* 
    [ eval <<FIELD>>=if(id > 5 AND id - <<MATCHSTR>> >=1 AND id - <<MATCHSTR>> <= 5, <<FIELD>>, NULL)
    | eval pre_jpy=if(isnotnull(<<FIELD>>),pre_jpy + <<FIELD>>,pre_jpy)]
    | fields - col*
    | eval diff=jpy - pre_jpy

こんにちわ @nishida_tada_caica さん
一応動的にフィールドを作ってループさせていますので、 id が増えても大丈夫のはず....です。
いかがでしょうか?

最後の差分評価 |eval diff=jpy - pre_jpyは適宜変更してください。

Get Updates on the Splunk Community!

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...