plotly_anomaly_fl()

関数 plotly_anomaly_fl()ユーザー定義関数 (UDF) です。これにより、 プロット形式 のテンプレートをカスタマイズして対話型の異常グラフを作成できます。

関数は、ソースとベースラインの時系列、それぞれのサイズを持つ正と負の異常の一覧、グラフのラベル付け文字列を含むテーブルを受け入れます。 この関数は、 プロットされた JSON を含む単一のセル テーブルを返します。 必要に応じて、Azure Data Explorer ダッシュボード タイルでデータをレンダリングできます。 詳細については、「 Plotly (プレビュー)」を参照してください。

注意

非対話型の異常グラフをレンダリングするために、Azure Data Explorerネイティブ" | render anomalychart"メソッドを使用することを検討してください。

前提条件

一般公開 PlotlyTemplate されているテーブルから必要な "異常" テンプレートを抽出します。 ターゲット データベースから次の KQL コマンドを実行して、サンプル データベースからデータベースにこのテーブルをコピーします。

.set PlotlyTemplate <| cluster('help.kusto.windows.net').database('Samples').PlotlyTemplate

構文

T | invoke plotly_anomaly_fl(, time_col, val_col, baseline_col, time_high_col, val_high_col, size_high_col, time_low_col, val_low__col, size_low_col, chart_title, series_nameval_name)

構文規則について詳しく知る。

パラメーター

名前 必須 説明
time_col string ✔️ 元の時系列のタイム ポイントの動的配列を含む列の名前
val_col string ✔️ 元の時系列の値を含む列の名前
baseline_col string ✔️ 基準時系列の値を含む列の名前。 通常、異常は、予想されるベースライン値からの大きな値オフセットによって検出されます。
time_high_col string ✔️ 高値 (ベースラインより上) の異常のタイム ポイントを含む列の名前
val_high_col string ✔️ 異常が高い値を含む列の名前
size_high_col string ✔️ 異常の高いマーカー サイズを含む列の名前
time_low_col string ✔️ 低異常のタイム ポイントを含む列の名前
val_low_col string ✔️ 低異常の値を含む列の名前
size_low_col string ✔️ 低異常のマーカー サイズを含む列の名前
chart_title string グラフタイトル、既定値は 'Anomaly Chart' です
series_name string 時系列名、既定値は 'Metric' です
val_name string 数値軸名、既定値は 'Value' です

関数の定義

関数を定義するには、次のように、コードをクエリ定義関数として埋め込むか、データベースに格納されている関数として作成します。

次の let ステートメントを使用して関数を定義します。 権限は必要ありません。

重要

let ステートメントは単独では実行できません。 その後に 表形式の式ステートメントを記述する必要があります。 の動作例を実行するには、「plotly_anomaly_fl()」を参照してください。

let plotly_anomaly_fl=(tbl:(*), time_col:string, val_col:string, baseline_col:string, time_high_col:string , val_high_col:string, size_high_col:string,
                                time_low_col:string, val_low_col:string, size_low_col:string,
                                chart_title:string='Anomaly chart', series_name:string='Metric', val_name:string='Value')
{
    let anomaly_chart = toscalar(PlotlyTemplate | where name == "anomaly" | project plotly);
    let tbl_ex = tbl | extend _timestamp = column_ifexists(time_col, datetime(null)), _values = column_ifexists(val_col, 0.0), _baseline = column_ifexists(baseline_col, 0.0),
                              _high_timestamp = column_ifexists(time_high_col, datetime(null)), _high_values = column_ifexists(val_high_col, 0.0), _high_size = column_ifexists(size_high_col, 1),
                              _low_timestamp = column_ifexists(time_low_col, datetime(null)), _low_values = column_ifexists(val_low_col, 0.0), _low_size = column_ifexists(size_low_col, 1);
    tbl_ex
    | extend plotly = anomaly_chart
    | extend plotly=replace_string(plotly, '$TIME_STAMPS$', tostring(_timestamp))
    | extend plotly=replace_string(plotly, '$SERIES_VALS$', tostring(_values))
    | extend plotly=replace_string(plotly, '$BASELINE_VALS$', tostring(_baseline))
    | extend plotly=replace_string(plotly, '$TIME_STAMPS_HIGH_ANOMALIES$', tostring(_high_timestamp))
    | extend plotly=replace_string(plotly, '$HIGH_ANOMALIES_VALS$', tostring(_high_values))
    | extend plotly=replace_string(plotly, '$HIGH_ANOMALIES_MARKER_SIZE$', tostring(_high_size))
    | extend plotly=replace_string(plotly, '$TIME_STAMPS_LOW_ANOMALIES$', tostring(_low_timestamp))
    | extend plotly=replace_string(plotly, '$LOW_ANOMALIES_VALS$', tostring(_low_values))
    | extend plotly=replace_string(plotly, '$LOW_ANOMALIES_MARKER_SIZE$', tostring(_low_size))
    | extend plotly=replace_string(plotly, '$TITLE$', chart_title)
    | extend plotly=replace_string(plotly, '$SERIES_NAME$', series_name)
    | extend plotly=replace_string(plotly, '$Y_NAME$', val_name)
    | project plotly
};
// Write your query to use the function here.

次の例では、 invoke 演算子 を使用して関数を実行します。

クエリ定義関数を使用するには、埋め込み関数定義の後にそれを呼び出します。

let plotly_anomaly_fl=(tbl:(*), time_col:string, val_col:string, baseline_col:string, time_high_col:string , val_high_col:string, size_high_col:string,
                                time_low_col:string, val_low_col:string, size_low_col:string,
                                chart_title:string='Anomaly chart', series_name:string='Metric', val_name:string='Value')
{
    let anomaly_chart = toscalar(PlotlyTemplate | where name == "anomaly" | project plotly);
    let tbl_ex = tbl | extend _timestamp = column_ifexists(time_col, datetime(null)), _values = column_ifexists(val_col, 0.0), _baseline = column_ifexists(baseline_col, 0.0),
                              _high_timestamp = column_ifexists(time_high_col, datetime(null)), _high_values = column_ifexists(val_high_col, 0.0), _high_size = column_ifexists(size_high_col, 1),
                              _low_timestamp = column_ifexists(time_low_col, datetime(null)), _low_values = column_ifexists(val_low_col, 0.0), _low_size = column_ifexists(size_low_col, 1);
    tbl_ex
    | extend plotly = anomaly_chart
    | extend plotly=replace_string(plotly, '$TIME_STAMPS$', tostring(_timestamp))
    | extend plotly=replace_string(plotly, '$SERIES_VALS$', tostring(_values))
    | extend plotly=replace_string(plotly, '$BASELINE_VALS$', tostring(_baseline))
    | extend plotly=replace_string(plotly, '$TIME_STAMPS_HIGH_ANOMALIES$', tostring(_high_timestamp))
    | extend plotly=replace_string(plotly, '$HIGH_ANOMALIES_VALS$', tostring(_high_values))
    | extend plotly=replace_string(plotly, '$HIGH_ANOMALIES_MARKER_SIZE$', tostring(_high_size))
    | extend plotly=replace_string(plotly, '$TIME_STAMPS_LOW_ANOMALIES$', tostring(_low_timestamp))
    | extend plotly=replace_string(plotly, '$LOW_ANOMALIES_VALS$', tostring(_low_values))
    | extend plotly=replace_string(plotly, '$LOW_ANOMALIES_MARKER_SIZE$', tostring(_low_size))
    | extend plotly=replace_string(plotly, '$TITLE$', chart_title)
    | extend plotly=replace_string(plotly, '$SERIES_NAME$', series_name)
    | extend plotly=replace_string(plotly, '$Y_NAME$', val_name)
    | project plotly
};
let min_t = datetime(2017-01-05);
let max_t = datetime(2017-02-03 22:00);
let dt = 2h;
let marker_scale = 8;
let s_name = 'TS1';
demo_make_series2
| make-series num=avg(num) on TimeStamp from min_t to max_t step dt by sid
| where sid == s_name
| extend (anomalies, score, baseline) = series_decompose_anomalies(num, 1.5, -1, 'linefit')
| mv-apply num1=num to typeof(double), anomalies1=anomalies to typeof(double), score1=score to typeof(double), TimeStamp1=TimeStamp to typeof(datetime)  on (
    summarize pAnomalies=make_list_if(num1, anomalies1 > 0), pTimeStamp=make_list_if(TimeStamp1, anomalies1 > 0), pSize=make_list_if(toint(score1*marker_scale), anomalies1 > 0),
              nAnomalies=make_list_if(num1, anomalies1 < 0), nTimeStamp=make_list_if(TimeStamp1, anomalies1 < 0), nSize=make_list_if(toint(-score1*marker_scale), anomalies1 < 0)
)
| invoke plotly_anomaly_fl('TimeStamp', 'num', 'baseline', 'pTimeStamp', 'pAnomalies', 'pSize', 'nTimeStamp', 'nAnomalies', 'nSize',
                           chart_title='Anomaly chart using plotly_anomaly_fl()', series_name=s_name, val_name='# of requests')
| render plotly

出力

出力は、'| を使用してレンダリングできる Plotly JSON 文字列ですをプロットしてレンダリングするか、Azure Data Explorer ダッシュボード タイルに表示します。 ダッシュボード タイルの作成の詳細については、「Azure Data Explorer ダッシュボードを使用してデータを視覚化する」を参照してください。

次の図は、上記の関数を使用した異常グラフの例を示しています。

サンプル データセットの異常グラフのスクリーンショット。

ズームインして異常をポイントできます。

異常なリージョンのズームのスクリーンショット。異常にマウス ポインターを合わせたスクリーンショット。