# SPLコード一行ずつコメント完全版
## パターン1: 基本的なデータ取得
### 1-1. データ件数を確認
```spl
index=web
```
// インデックス「web」からすべてのデータを取得
```spl
| stats count
```
// パイプで前の結果を受け取り、件数を集計
### 1-2. グループ別に集計
```spl
index=web
```
// インデックス「web」からデータ取得
```spl
| stats count by host
```
// hostフィールドごとにグループ化して件数を集計
---
## パターン2: フィルタリング
### 2-1. 特定のステータスコード
```spl
index=web
```
// Webログを取得
```spl
status=500
```
// statusフィールドが500のイベントのみ抽出
```spl
| stats count
```
// 500エラーの総数をカウント
### 2-2. 複数条件でフィルタ
```spl
index=web
```
// Webログを取得
```spl
status=500
```
// statusが500のイベント
```spl
| search uri="/api/*"
```
// URIが/apiで始まるものだけさらに絞り込み
```spl
| stats count by uri
```
// URI別にエラー数を集計
---
## パターン3: データ変換(eval)
### 3-1. 新しいフィールド作成
```spl
index=web
```
// Webログを取得
```spl
| eval response_sec = response_time / 1000
```
// response_timeを1000で割って秒単位に変換、新フィールドresponse_secに格納
```spl
| table _time, host, response_time, response_sec
```
// 指定した4つのフィールドを表形式で表示
```spl
| head 10
```
// 上位10件のみ表示
### 3-2. 条件分岐(if)
```spl
index=web
```
// Webログを取得
```spl
| eval status_text = if(status < 400, "正常", "エラー")
```
// statusが400未満なら「正常」、それ以外は「エラー」をstatus_textに格納
```spl
| stats count by status_text
```
// 正常/エラー別に件数を集計
### 3-3. 複数条件分岐(case)
```spl
index=web
```
// Webログを取得
```spl
| eval severity = case(
status >= 500, "重大",
status >= 400, "警告",
status >= 300, "情報",
1=1, "正常"
)
```
// 上から順に条件を評価、最初にマッチした値をseverityに格納
// status >= 500なら「重大」
// status >= 400なら「警告」
// status >= 300なら「情報」
// それ以外(1=1は常に真)なら「正常」
```spl
| stats count by severity
```
// severity別に件数を集計
```spl
| sort -count
```
// countの降順で並び替え(-は降順の意味)
---
## パターン4: 複数の統計を同時に計算
### 4-1. 基本的な複数統計
```spl
index=web
```
// Webログを取得
```spl
| stats count as total,
avg(response_time) as avg_time,
max(response_time) as max_time,
min(response_time) as min_time
by host
```
// hostごとに以下を計算
// count as total: 件数を「total」という名前で
// avg(response_time) as avg_time: 平均応答時間を「avg_time」という名前で
// max(response_time) as max_time: 最大応答時間を「max_time」という名前で
// min(response_time) as min_time: 最小応答時間を「min_time」という名前で
```spl
| eval avg_time = round(avg_time, 2)
```
// avg_timeを小数点以下2桁に四捨五入
```spl
| sort -avg_time
```
// avg_timeの降順で並び替え
### 4-2. 条件付きカウント
```spl
index=web
```
// Webログを取得
```spl
| stats count as total,
count(eval(status >= 200 AND status < 300)) as success,
count(eval(status >= 400)) as error
by host
```
// hostごとに以下を計算
// count as total: 全件数
// count(eval(...)): 条件を満たすものだけカウント
// status >= 200 AND status < 300: 200番台(成功)の件数をsuccessに
// status >= 400: 400以上(エラー)の件数をerrorに
```spl
| eval success_rate = round(success / total * 100, 2)
```
// 成功率を計算: (成功件数 / 総件数 * 100) を小数点2桁に
```spl
| eval error_rate = round(error / total * 100, 2)
```
// エラー率を計算: (エラー件数 / 総件数 * 100) を小数点2桁に
```spl
| sort -error_rate
```
// エラー率の降順で並び替え
---
## パターン5: 時系列分析(timechart)
### 5-1. 基本的な時系列
```spl
index=web
```
// Webログを取得
```spl
| timechart count
```
// 時系列で件数を集計(時間間隔は自動)
### 5-2. 時間間隔を指定
```spl
index=web
```
// Webログを取得
```spl
earliest=-24h
```
// 過去24時間のデータに限定
```spl
| timechart span=1h count
```
// 1時間(hour)ごとに件数を集計
### 5-3. グループ別の時系列
```spl
index=web
```
// Webログを取得
```spl
earliest=-24h
```
// 過去24時間のデータ
```spl
| timechart span=1h count by host
```
// 1時間ごとに、hostごとの件数を集計
// 結果: 各ホストが別々の線として表示される
---
## パターン6: 集計後のフィルタ(where)
```spl
index=web
```
// Webログを取得
```spl
| stats count as total,
avg(response_time) as avg_time
by host
```
// hostごとに件数(total)と平均応答時間(avg_time)を集計
```spl
| where total > 10000 AND avg_time > 500
```
// 集計結果に対してフィルタ
// total > 10000: 件数が10000より大きい
// AND avg_time > 500: かつ平均応答時間が500msより大きい
// 両方の条件を満たす行のみ残す
```spl
| sort -avg_time
```
// 平均応答時間の降順で並び替え
---
## パターン7: シンプルなカード表示
```spl
index=web
```
// Webログを取得
```spl
earliest=-1h
```
// 過去1時間のデータに限定
```spl
| stats count as total
```
// 総件数を「total」として取得
```spl
| eval html = "<div style='background:#3b82f6; padding:30px; border-radius:10px; text-align:center; color:white;'>"
```
// HTML文字列の作成開始
// divタグ: コンテナ要素
// background:#3b82f6: 背景色(青)
// padding:30px: 内側の余白30ピクセル
// border-radius:10px: 角を丸く10ピクセル
// text-align:center: テキストを中央揃え
// color:white: 文字色を白に
```spl
| eval html = html + "<h1 style='margin:0; font-size:64px;'>" + total + "</h1>"
```
// 既存のhtmlに追加
// h1タグ: 大見出し
// margin:0: 余白なし
// font-size:64px: 文字サイズ64ピクセル
// + total +: totalの値(数値)を文字列として埋め込む
```spl
| eval html = html + "<p style='margin:10px 0 0 0; font-size:18px;'>過去1時間のアクセス数</p>"
```
// pタグ: 段落
// margin:10px 0 0 0: 上だけ10ピクセルの余白
// ラベルテキストを追加
```spl
| eval html = html + "</div>"
```
// divタグを閉じる
```spl
| table html
```
// html列のみを表として出力
---
## パターン8: データに応じて色を変える
```spl
index=web
```
// Webログを取得
```spl
earliest=-1h
```
// 過去1時間のデータ
```spl
| stats count as total,
count(eval(status >= 400)) as errors
```
// 総件数(total)とエラー件数(errors)を集計
// status >= 400: 400以上のステータス(エラー)
```spl
| eval error_rate = round(errors / total * 100, 2)
```
// エラー率を計算: (エラー数 / 総数 * 100) を小数点2桁に
```spl
| eval color = case(
error_rate >= 10, "#ef4444",
error_rate >= 5, "#f59e0b",
1=1, "#10b981"
)
```
// エラー率に応じて背景色を決定
// error_rate >= 10: 10%以上なら赤色(#ef4444)
// error_rate >= 5: 5%以上ならオレンジ色(#f59e0b)
// 1=1: それ以外は緑色(#10b981)
```spl
| eval html = "<div style='background:" + color + "; padding:30px; border-radius:10px; text-align:center; color:white;'>"
```
// divタグ開始
// background:" + color + ": 動的に決定した色を背景に設定
// colorの値(例: #ef4444)が埋め込まれる
```spl
| eval html = html + "<h1 style='margin:0; font-size:48px;'>" + error_rate + "%</h1>"
```
// エラー率を大きく表示(48ピクセル)
// パーセント記号を付けて表示
```spl
| eval html = html + "<p style='margin:10px 0 0 0;'>エラー率</p>"
```
// ラベル「エラー率」を表示
```spl
| eval html = html + "<p style='margin:10px 0 0 0; font-size:14px; opacity:0.8;'>総数: " + total + " | エラー: " + errors + "</p>"
```
// 詳細情報を小さく表示(14ピクセル)
// opacity:0.8: 透明度80%(少し薄く)
// 総数とエラー数を表示
```spl
| eval html = html + "</div>"
```
// divタグを閉じる
```spl
| table html
```
// 最終的なHTMLを出力
---
## パターン9: 複数サーバーの比較表示
```spl
index=web
```
// Webログを取得
```spl
earliest=-1h
```
// 過去1時間のデータ
```spl
| stats count as total,
count(eval(status >= 400)) as errors,
avg(response_time) as avg_time
by host
```
// hostごとに3つの統計を計算
// total: 総件数
// errors: エラー件数(status >= 400)
// avg_time: 平均応答時間
```spl
| eval error_rate = round(errors / total * 100, 2)
```
// エラー率を計算(パーセント、小数点2桁)
```spl
| eval avg_time = round(avg_time, 2)
```
// 平均応答時間を小数点2桁に丸める
```spl
| eval color = case(
error_rate >= 5 OR avg_time >= 500, "#ef4444",
error_rate >= 2 OR avg_time >= 300, "#f59e0b",
1=1, "#10b981"
)
```
// 状態に応じて色を決定
// エラー率5%以上 または 応答時間500ms以上: 赤
// エラー率2%以上 または 応答時間300ms以上: オレンジ
// それ以外: 緑
```spl
| eval html = "<div style='display:inline-block; margin:10px; padding:20px; background:" + color + "; border-radius:10px; min-width:200px; color:white;'>"
```
// 各サーバー用のカードを作成
// display:inline-block: 横並びに配置
// margin:10px: 外側の余白
// padding:20px: 内側の余白
// background:" + color + ": 動的な背景色
// min-width:200px: 最小幅200ピクセル
```spl
| eval html = html + "<h3 style='margin:0 0 15px 0; font-size:20px;'>" + host + "</h3>"
```
// サーバー名を見出しで表示(20ピクセル)
// margin:0 0 15px 0: 下だけ15ピクセルの余白
```spl
| eval html = html + "<div style='font-size:14px;'>"
```
// メトリクス表示エリア開始(文字サイズ14ピクセル)
```spl
| eval html = html + "<div>総数: <strong>" + total + "</strong></div>"
```
// 総リクエスト数を表示
// strongタグ: 太字
```spl
| eval html = html + "<div>エラー: <strong>" + errors + "</strong></div>"
```
// エラー数を表示
```spl
| eval html = html + "<div>エラー率: <strong>" + error_rate + "%</strong></div>"
```
// エラー率を表示(パーセント付き)
```spl
| eval html = html + "<div>平均応答: <strong>" + avg_time + "ms</strong></div>"
```
// 平均応答時間を表示(ミリ秒付き)
```spl
| eval html = html + "</div></div>"
```
// メトリクスエリアとカードを閉じる
```spl
| stats values(html) as cards
```
// すべてのサーバーのカードHTMLを配列として集める
// values(): 重複を除いた値の配列を作成
// as cards: 配列を「cards」という名前で保存
```spl
| eval html = "<div style='background:#0f172a; padding:20px;'>"
```
// 全体の背景コンテナ開始
// background:#0f172a: ダークブルーの背景
// padding:20px: 内側の余白
```spl
| eval html = html + "<h2 style='color:white; margin:0 0 20px 0; font-size:24px;'>サーバー監視ダッシュボード</h2>"
```
// ダッシュボードのタイトルを追加
// color:white: 白色の文字
// font-size:24px: 文字サイズ24ピクセル
```spl
| eval html = html + mvjoin(cards, "")
```
// 配列の全要素を結合して追加
// mvjoin(配列, 区切り文字): 配列を文字列に結合する関数
// cards: 各サーバーのカードHTML配列
// "": 区切り文字なし(そのまま連結)
// 結果: すべてのカードが横並びになる
```spl
| eval html = html + "</div>"
```
// 全体のコンテナを閉じる
```spl
| table html
```
// 最終的なHTML文字列を表として出力
---
## パターン10: プロレベル完全版
```spl
index=web
```
// Webログを取得
```spl
earliest=-1h
```
// 過去1時間のデータに限定
```spl
| stats count as total,
count(eval(status >= 200 AND status < 300)) as success,
count(eval(status >= 400 AND status < 500)) as client_error,
count(eval(status >= 500)) as server_error,
avg(response_time) as avg_response,
max(response_time) as max_response,
min(response_time) as min_response
by host
```
// hostごとに7つの統計値を一度に計算
// total: 総件数
// success: 2xx(成功)の件数
// client_error: 4xx(クライアントエラー)の件数
// server_error: 5xx(サーバーエラー)の件数
// avg_response: 平均応答時間
// max_response: 最大応答時間
// min_response: 最小応答時間
```spl
| eval success_rate = round(success / total * 100, 2)
```
// 成功率を計算: (成功件数 / 総件数 * 100) を小数点2桁に
```spl
| eval error_rate = round((client_error + server_error) / total * 100, 2)
```
// エラー率を計算: ((4xx + 5xx) / 総件数 * 100) を小数点2桁に
```spl
| eval avg_response = round(avg_response, 2)
```
// 平均応答時間を小数点2桁に丸める
```spl
| eval max_response = round(max_response, 2)
```
// 最大応答時間を小数点2桁に丸める
```spl
| eval min_response = round(min_response, 2)
```
// 最小応答時間を小数点2桁に丸める
```spl
| eval status_level = case(
error_rate >= 10 OR avg_response >= 1000, "critical",
error_rate >= 5 OR avg_response >= 500, "warning",
1=1, "ok"
)
```
// サーバーの状態レベルを3段階で判定
// critical: エラー率10%以上 または 平均応答1000ms以上
// warning: エラー率5%以上 または 平均応答500ms以上
// ok: それ以外
```spl
| eval color = case(
status_level="critical", "#ef4444",
status_level="warning", "#f59e0b",
1=1, "#10b981"
)
```
// 状態レベルに応じた背景色を設定
// critical: 赤色
// warning: オレンジ色
// ok: 緑色
```spl
| eval html = "<div style='display:inline-block; margin:15px; padding:25px; background:" + color + "; border-radius:12px; min-width:280px; box-shadow:0 4px 6px rgba(0,0,0,0.3);'>"
```
// カードコンテナ開始
// display:inline-block: 横並び配置
// margin:15px: 外側の余白
// padding:25px: 内側の余白
// background:" + color + ": 動的な背景色
// border-radius:12px: 角を丸く12ピクセル
// min-width:280px: 最小幅280ピクセル
// box-shadow:0 4px 6px rgba(0,0,0,0.3): 影を追加(X=0, Y=4px, ぼかし=6px, 黒30%透明)
```spl
| eval html = html + "<h3 style='margin:0 0 20px 0; color:white; font-size:22px; border-bottom:2px solid rgba(255,255,255,0.3); padding-bottom:10px;'>" + host + "</h3>"
```
// サーバー名の見出し
// border-bottom:2px solid rgba(...): 下に2ピクセルの線(白30%透明)
// padding-bottom:10px: 下に10ピクセルの余白
```spl
| eval html = html + "<div style='color:white; font-size:14px; line-height:1.8;'>"
```
// メトリクスエリア開始
// line-height:1.8: 行間を1.8倍に(読みやすく)
```spl
| eval html = html + "<div style='display:flex; justify-content:space-between; margin-bottom:8px;'><span>総リクエスト:</span><strong style='font-size:16px;'>" + total + "</strong></div>"
```
// 総リクエスト数の行
// display:flex: フレックスボックスレイアウト
// justify-content:space-between: 左右に配置
// margin-bottom:8px: 下に8ピクセルの余白
// span: ラベル
// strong: 値(太字、16ピクセル)
```spl
| eval html = html + "<div style='display:flex; justify-content:space-between; margin-bottom:8px;'><span>成功率:</span><strong style='font-size:16px; color:#4ade80;'>" + success_rate + "%</strong></div>"
```
// 成功率の行
// color:#4ade80: 明るい緑色で強調
```spl
| eval html = html + "<div style='display:flex; justify-content:space-between; margin-bottom:8px;'><span>エラー率:</span><strong style='font-size:16px; color:#fca5a5;'>" + error_rate + "%</strong></div>"
```
// エラー率の行
// color:#fca5a5: 明るい赤色で強調
```spl
| eval html = html + "<div style='display:flex; justify-content:space-between; margin-bottom:8px;'><span>平均応答:</span><strong style='font-size:16px;'>" + avg_response + "ms</strong></div>"
```
// 平均応答時間の行
```spl
| eval html = html + "<div style='display:flex; justify-content:space-between; margin-bottom:8px;'><span>最大応答:</span><strong style='font-size:16px;'>" + max_response + "ms</strong></div>"
```
// 最大応答時間の行
```spl
| eval html = html + "<div style='display:flex; justify-content:space-between;'><span>最小応答:</span><strong style='font-size:16px;'>" + min_response + "ms</strong></div>"
```
// 最小応答時間の行(最後なのでmargin-bottomなし)
```spl
| eval html = html + "<div style='margin-top:15px; padding-top:15px; border-top:1px solid rgba(255,255,255,0.2); text-align:center;'><span style='font-size:11px; opacity:0.8;'>ステータス: " + upper(status_level) + "</span></div>"
```
// フッターエリア
// margin-top:15px: 上に15ピクセルの余白
// padding-top:15px: 上に15ピクセルの内側余白
// border-top:1px solid rgba(...): 上に1ピクセルの線(白20%透明)
// text-align:center: 中央揃え
// font-size:11px: 小さい文字
// opacity:0.8: 透明度80%
// upper(status_level): status_levelを大文字に変換
```spl
| eval html = html + "</div></div>"
```
// メトリクスエリアとカードコンテナを閉じる
```spl
| stats values(html) as cards
```
// すべてのサーバーのカードHTMLを配列として集める
```spl
| eval html = "<div style='background:linear-gradient(to bottom, #0a0e1a, #1a1f2e); padding:30px; min-height:100vh;'>"
```
// 全体の外側コンテナ開始
// background:linear-gradient(...): グラデーション背景(上から下へ)
// #0a0e1a: 上部の色(濃いダークブルー)
// #1a1f2e: 下部の色(少し明るいダークブルー)
// padding:30px: 内側の余白
// min-height:100vh: 最小高さを画面全体(viewport height 100%)に
```spl
| eval html = html + "<div style='max-width:1400px; margin:0 auto;'>"
```
// 内側コンテナ開始
// max-width:1400px: 最大幅を1400ピクセルに制限
// margin:0 auto: 上下0、左右自動(中央寄せ)
```spl
| eval html = html + "<h1 style='color:white; margin:0 0 30px 0; font-size:32px; font-weight:bold;'>🖥️ リアルタイムサーバー監視ダッシュボード</h1>"
```
// メインタイトル
// color:white: 白色
// margin:0 0 30px 0: 下だけ30ピクセルの余白
// font-size:32px: 大きい文字サイズ
// font-weight:bold: 太字
// 🖥️: 絵文字を追加
```spl
| eval html = html + "<p style='color:#94a3b8; margin:0 0 30px 0; font-size:14px;'>過去1時間のサーバーパフォーマンス | 自動更新: 30秒</p>"
```
// サブタイトル(説明文)
// color:#94a3b8: グレー色
// font-size:14px: 小さめの文字
// |: 区切り文字
```spl
| eval html = html + "<div style='display:flex; flex-wrap:wrap; gap:10px;'>"
```
// カード配置コンテナ開始
// display:flex: フレックスボックスレイアウト
// flex-wrap:wrap: 幅が足りない場合は次の行に折り返す
// gap:10px: カード間の余白(10ピクセル)
```spl
| eval html = html + mvjoin(cards, "")
```
// すべてのサーバーカードを挿入
// mvjoin(): 配列を文字列に結合
// cards: 各サーバーのカードHTML配列
// "": 区切り文字なし
```spl
| eval html = html + "</div>"
```
// カード配置コンテナを閉じる
```spl
| eval html = html + "<div style='margin-top:40px; padding-top:20px; border-top:1px solid #334155; color:#64748b; font-size:12px;'><p style='margin:0;'>最終更新: " + strftime(now(), "%Y-%m-%d %H:%M:%S") + "</p></div>"
```
// フッター情報エリア
// margin-top:40px: 上に40ピクセルの余白
// padding-top:20px: 上に20ピクセルの内側余白
// border-top:1px solid #334155: 上に1ピクセルの線(グレー)
// color:#64748b: グレー色
// font-size:12px: 小さい文字
// now(): 現在のUnix時刻
// strftime(..., "%Y-%m-%d %H:%M:%S"): 時刻を「年-月-日 時:分:秒」形式に変換
// 例: 2025-01-24 10:30:45
```spl
| eval html = html + "</div></div>"
```
// 内側コンテナと全体コンテナを閉じる
```spl
| table html
```
// 最終的なHTML文字列を表として出力
---