Created
February 15, 2024 02:08
-
-
Save hfleitas/c2657759fa974fba20c9fa8f2fae297e to your computer and use it in GitHub Desktop.
Plotly.kql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#connect cluster('adxpm10774.eastus.kusto.windows.net').database('IoTAnalytics') | |
.set-or-replace PlotlyTemplate <| cluster('help.kusto.windows.net').database('Samples').PlotlyTemplate | |
PlotlyTemplate | |
.create-or-alter function with (folder = "Packages\\Plotly", docstring = "Render 3D scatter chart using plotly template") | |
plotly_scatter3d_fl(tbl:(*), x_col:string, y_col:string, z_col:string, aggr_col:string='', chart_title:string='3D Scatter chart') | |
{ | |
let scatter3d_chart = toscalar(PlotlyTemplate | where name == "scatter3d" | project plotly); | |
let tbl_ex = tbl | extend _x = column_ifexists(x_col, 0.0), _y = column_ifexists(y_col, 0.0), _z = column_ifexists(z_col, 0.0), _aggr = column_ifexists(aggr_col, 'ALL'); | |
tbl_ex | |
| serialize | |
| summarize _x=pack_array(make_list(_x)), _y=pack_array(make_list(_y)), _z=pack_array(make_list(_z)) by _aggr | |
| summarize _aggr=make_list(_aggr), _x=make_list(_x), _y=make_list(_y), _z=make_list(_z) | |
| extend plotly = scatter3d_chart | |
| extend plotly=replace_string(plotly, '$CLASS1$', tostring(_aggr[0])) | |
| extend plotly=replace_string(plotly, '$CLASS2$', tostring(_aggr[1])) | |
| extend plotly=replace_string(plotly, '$CLASS3$', tostring(_aggr[2])) | |
| extend plotly=replace_string(plotly, '$X_NAME$', x_col) | |
| extend plotly=replace_string(plotly, '$Y_NAME$', y_col) | |
| extend plotly=replace_string(plotly, '$Z_NAME$', z_col) | |
| extend plotly=replace_string(plotly, '$CLASS1_X$', tostring(_x[0])) | |
| extend plotly=replace_string(plotly, '$CLASS1_Y$', tostring(_y[0])) | |
| extend plotly=replace_string(plotly, '$CLASS1_Z$', tostring(_z[0])) | |
| extend plotly=replace_string(plotly, '$CLASS2_X$', tostring(_x[1])) | |
| extend plotly=replace_string(plotly, '$CLASS2_Y$', tostring(_y[1])) | |
| extend plotly=replace_string(plotly, '$CLASS2_Z$', tostring(_z[1])) | |
| extend plotly=replace_string(plotly, '$CLASS3_X$', tostring(_x[2])) | |
| extend plotly=replace_string(plotly, '$CLASS3_Y$', tostring(_y[2])) | |
| extend plotly=replace_string(plotly, '$CLASS3_Z$', tostring(_z[2])) | |
| extend plotly=replace_string(plotly, '$TITLE$', chart_title) | |
| project plotly | |
} | |
.set-or-replace Iris <| cluster('help.kusto.windows.net').database('Samples').Iris | |
Iris | |
| invoke plotly_scatter3d_fl(x_col='SepalLength', y_col='PetalLength', z_col='SepalWidth', aggr_col='Class', chart_title='3D scatter chart using plotly_scatter3d_fl()') | |
Iris | |
| evaluate python(typeof(plotly:string), | |
```if 1: | |
import plotly.express as px | |
fig = px.scatter_3d(df, x='SepalLength', y='SepalWidth', z='PetalLength', color='Class') | |
fig.update_layout(title=dict(text='3D scatter chart using Plotly')) | |
plotly_obj = fig.to_json() | |
result = pd.DataFrame(data = [plotly_obj], columns = ['plotly']) | |
```) | |
.create-or-alter function with (folder = "Packages\\Plotly", docstring = "Render anomaly chart using plotly template") | |
plotly_anomaly_fl(tbl:(*), time_col:string, val_col:string, baseline_col:string, time_high_col:string , val_high_col:string, time_low_col:string , val_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(val_col, 0.0), | |
_high_timestamp = column_ifexists(time_high_col, datetime(null)), _high_values = column_ifexists(val_high_col, 0.0), | |
_low_timestamp = column_ifexists(time_low_col, datetime(null)), _low_values = column_ifexists(val_low_col, 0.0); | |
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, '$TIME_STAMPS_LOW_ANOMALIES$', tostring(_low_timestamp)) | |
| extend plotly=replace_string(plotly, '$LOW_ANOMALIES_VALS$', tostring(_low_values)) | |
| 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 | |
} | |
.set demo_make_series2 <| cluster('help.kusto.windows.net').database('Samples').demo_make_series2 | |
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', 'nTimeStamp', 'nAnomalies', | |
chart_title='Anomaly chart using plotly_anomaly_fl()', series_name=s_name, y_name='# of requests') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment