{% set ns = namespace() %} {% set query = "project_id is '" + projectid + "' and incident_id is '" + incidentid + "' " %} {% set incidentstream = "oia-incidents-stream" %} {% set _ = logger.info("going to query incidentstream stream with query: " + str(query)) %} {% set incident_res = engine.query_stream_data(stream=incidentstream, cfxql_query=query) %} {% set _ = logger.info("Got incident_res as: " + str(incident_res)) %} {% if incident_res is defined and incident_res|length > 0 %} {% set incident = incident_res[0] %} {% set _ = logger.info("Got incident as: " + str(incident)) %} {% set incident_occurance_time = incident.get("i_cfx_incident_occurred", incident.get("i_created_ts")) %} {% if incident_occurance_time is string %} {% set _ = logger.info("Found incident_occurance_time as string hence converting it to int") %} {% set incident_occurance_time = incident_occurance_time | int %} {% endif %} {% set from_time = incident_occurance_time + ((-12)* 60 * 60 * 1000) %} {% set from_time_str = to_datetime(from_time, unit='ms').isoformat() %} {% set to_time = incident_occurance_time + (4 * 60 * 60 * 1000) %} {% set to_time_str = to_datetime(to_time, unit='ms').isoformat() %} {% set config_query = "roomid is '" + projectid + "' and pagename is 'metrics' " %} {% set config_stream = "oia-incident-page-config" %} {% set config_res = engine.query_stream_data(stream=config_stream, cfxql_query=config_query) %} {% if config_res is defined and config_res|length > 0 %} {% set page_config = config_res[0] %} {% endif %} {% set alert_id_values = [] %} {% set alert_asset_types = [] %} {% set ns.alert_tag_values = {} %} {% if page_config %} {% set _ = logger.debug("page_config is: " + str(page_config)) %} {% set alert_columns = page_config.get("alert_columns") %} {% if alert_columns %} {% set _ = logger.debug("alert_columns from page_config is: " + str(alert_columns)) %} {% if alert_columns.__class__.__name__ != 'list' %} {% set _ = logger.debug("processing alert_columns as str") %} {% if "," in alert_columns %} {% set alert_columns = alert_columns.split(",") %} {% else %} {% set alert_columns = [alert_columns] %} {% endif %} {% endif %} {% else %} {% set alert_columns=[] %} {% endif %} {% set _ = logger.debug("Final alert_columns is: " + str(alert_columns)) %} {% set alert_type_columns = page_config.get("alert_type_columns") %} {% if alert_type_columns %} {% set _ = logger.debug("alert_type_columns from page_config is: " + str(alert_type_columns)) %} {% if alert_type_columns.__class__.__name__ != 'list' %} {% set _ = logger.debug("processing alert_type_columns as str") %} {% if "," in alert_type_columns %} {% set alert_type_columns = alert_type_columns.split(",") %} {% else %} {% set alert_type_columns = [alert_type_columns] %} {% endif %} {% endif %} {% else %} {% set alert_type_columns=[] %} {% endif %} {% set _ = logger.debug("Final alert_type_columns is: " + str(alert_type_columns)) %} {% set alert_tag_columns = page_config.get("alert_tag_columns") %} {% if alert_tag_columns %} {% set _ = logger.debug("alert_tag_columns from page_config is: " + str(alert_tag_columns)) %} {% if alert_tag_columns.__class__.__name__ != 'list' %} {% set _ = logger.debug("processing alert_tag_columns as str") %} {% if "," in alert_tag_columns %} {% set alert_tag_columns = alert_tag_columns.split(",") %} {% else %} {% set alert_tag_columns = [alert_tag_columns] %} {% endif %} {% endif %} {% else %} {% set alert_tag_columns=[] %} {% endif %} {% set _ = logger.debug("Final alert_tag_columns is: " + str(alert_tag_columns)) %} {% set alert_query = "a_incident_id is '" + incidentid + "'" %} {% set alerts_res = engine.query_stream_data(stream="oia-alerts-stream", cfxql_query=alert_query) %} {% set _=logger.debug("alerts_res are: " + str(alerts_res) if alerts_res else "None") %} {% if alerts_res is defined %} {% for item in alerts_res %} {% set ns.cur_alert_ids = [] %} {% for alert_column in alert_columns %} {% set _ = logger.debug("Going to find alert_column '" + alert_column +"' from item: " + str(item)) %} {% set alert_id_val = item.get(alert_column) %} {% set _ = logger.debug("found alert_id_val as: " + str(alert_id_val)) %} {% if alert_id_val %} {% if alert_id_val not in alert_id_values %} {% set x = alert_id_values.append(alert_id_val) %} {% set _ = logger.debug("From alerts added alert_id_val " + alert_id_val +" to list of eligible alert_id_values") %} {% else %} {% set _ = logger.debug("Found existing alert_id_val " + str(alert_id_val) +" hence skipping to add to list of eligible alert_id_values") %} {% endif %} {% if alert_id_val not in ns.cur_alert_ids %} {% set x = ns.cur_alert_ids.append(alert_id_val) %} {% endif %} {% endif %} {% endfor %} {% for alert_type_column in alert_type_columns %} {% set alert_type_val = item.get(alert_type_column) %} {% if alert_type_val %} {% if alert_type_val not in alert_asset_types %} {% set x = alert_asset_types.append(alert_type_val) %} {% set _ = logger.debug("From alerts added alert_type_val " + alert_type_val +" to list of eligible alert_asset_types") %} {% else %} {% set _ = logger.debug("Found existing alert_type_val " + alert_type_val +" hence skipping to add to list of eligible alert_asset_types") %} {% endif %} {% endif %} {% endfor %} {% for alert_tag_column in alert_tag_columns %} {% set alert_tag_val = item.get(alert_tag_column) %} {% if alert_tag_val %} {% for cur_alert_id in ns.cur_alert_ids %} {% if cur_alert_id not in ns.alert_tag_values %} {% set x=ns.alert_tag_values.__setitem__(cur_alert_id, []) %} {% endif %} {% if alert_tag_val not in ns.alert_tag_values[cur_alert_id] %} {% set x = ns.alert_tag_values[cur_alert_id].append(alert_tag_val) %} {% set _ = logger.debug("From alerts added alert_tag_val " + alert_tag_val +" to list of eligible alert_tag_values") %} {% else %} {% set _ = logger.debug("Found existing alert_tag_val " + alert_tag_val +" hence skipping to add to list of eligible alert_tag_values") %} {% endif %} {% endfor %} {% endif %} {% endfor %} {% endfor %} {% else %} {% set _ = logger.warning("No alerts found but page config is defined to use alerts hence no metrics for incident: " + incidentid) %} {% endif %} {% if alert_id_values and alert_asset_types %} {% set _ = logger.info("Final alert_id_values is: " + str(alert_id_values)) %} {% set _ = logger.info("Final alert_asset_types is: " + str(alert_asset_types)) %} {% set _ = logger.info("Final alert_tag_values is: " + str(ns.alert_tag_values)) %} {% set widgets_config_json = page_config.get("widgets_config_json") %} {% set widgets_config_json_type = "string" %} {% if widgets_config_json is sequence and widgets_config_json is not mapping and widgets_config_json is not string %} {% set widgets_config_json_type = "list" %} {% endif %} {% if widgets_config_json and widgets_config_json_type == "string" %} {% set widgets_config_json = json_loads(widgets_config_json) %} {% endif %} {% set _ = logger.info("Final widgets_config_json is: " + ( str(widgets_config_json) if widgets_config_json else "None" )) %} {% if widgets_config_json%} {% for widgets_config in widgets_config_json %} {% set _ = logger.info("going to process widgets_config: " + str(widgets_config)) %} {% set data_format = widgets_config.get("data_format") %} {% if not data_format %} {% set data_format = "row-wise" %} {% endif %} {% set asset_types = widgets_config.get("asset_types") %} {% set matched_asset_types = [] %} {% for asset_type in asset_types %} {% if asset_type in alert_asset_types or asset_type == "*" %} {% set _= matched_asset_types.append(asset_type) %} {% else %} {% set _ = logger.warning("No match for asset_type: '" + asset_type + "' in alert_asset_types: " + str(alert_asset_types)) %} {% endif %} {% endfor %} {% set _ = logger.info("Final value of matched_asset_types: " + str(matched_asset_types)) %} {% if matched_asset_types %} {% set stream_name = widgets_config.get("stream_name") %} {% set widgets = widgets_config.get("widgets") %} {% set asset_types_str = matched_asset_types|join(",") %} {% if widgets and widgets|length > 0 %} {% for widget_config in widgets %} {% set metric_meta_stream = widget_config.get("metric_meta_stream", widget_config.get("metric_stream")) %} {% set metric_data_stream = widget_config.get("metric_data_stream", metric_meta_stream) %} {% set tag_column_names = widget_config.get("meta_tag_columns", widget_config.get("tag_columns")) %} {% set _ = logger.info("Found tag_column_names as: " + str(tag_column_names)) %} {% set meta_asset_columns = widget_config.get("meta_asset_columns", widget_config.get("asset_columns")) %} {% set data_asset_columns = widget_config.get("data_asset_columns", meta_asset_columns) %} {% set meta_metric_id_col = widget_config.get("meta_metric_id_col", widget_config.get("metric_id_col", "metric_id")) %} {% set data_metric_id_col = widget_config.get("data_metric_id_col") %} {% set data_metric_value_col = widget_config.get("data_metric_value_col", widget_config.get("metric_value_col", "value")) %} {% set metric_id_value = widget_config.get("meta_metric_id_value", widget_config.get("metric_id_value")) %} {% set metric_label_col = widget_config.get("meta_metric_label_col", widget_config.get("metric_label_col", "metric_label")) %} {% set unit_col = widget_config.get("meta_unit_col", widget_config.get("unit_col", "unit")) %} {% set meta_timestamp_col = widget_config.get("meta_timestamp_col", widget_config.get("timestamp_col", "timestamp")) %} {% set data_timestamp_col = widget_config.get("data_timestamp_col", meta_timestamp_col) %} {% set meta_query_extra_filter = widget_config.get("meta_query_extra_filter") %} {% set data_query_extra_filter = widget_config.get("data_query_extra_filter") %} {% set use_timestamp_for_meta = widget_config.get("use_timestamp_for_meta", false) %} {% set _ = logger.info("1") %} {% for asset_id in alert_id_values%} {% set _ = logger.info("2") %} {% set ns.meta_timestamp_string = "" %} {% if use_timestamp_for_meta %} {% set meta_timestamp_string = meta_timestamp_col + " is after '" + str(from_time_str) + "' and " + meta_timestamp_col + " is before '" + str(to_time_str) + "' " %} {% endif %} {% set ns.metrics_query = "" %} {% set is_first = "true" %} {% set _ = logger.info("3") %} {% for meta_asset_column in meta_asset_columns %} {% set _ = logger.info("4") %} {% if is_first == "true" %} {% set is_first = "false" %} {% else %} {% set ns.metrics_query = ns.metrics_query + " or " %} {% endif %} {% set ns.metrics_query = ns.metrics_query + meta_asset_column + " is '" + asset_id + "'" %} {% endfor %} {% set ns.metrics_query = " ( " + ns.metrics_query + " ) and " + meta_metric_id_col + " is '" + metric_id_value + "' " %} {% if use_timestamp_for_meta %} {% set ns.metrics_query = ns.metrics_query + " and " + meta_timestamp_string %} {% endif %} {% if meta_query_extra_filter %} {% set ns.metrics_query = ns.metrics_query + " and " + meta_query_extra_filter %} {% endif %} {% set used_asset_ids = [] %} {% set _ = logger.info("Final ns.metrics_query is: " + str(ns.metrics_query)) %} {% set metrics_res = engine.query_stream_data(stream=metric_meta_stream, cfxql_query=ns.metrics_query, max_rows=1, limit=1) %} {% set _ = logger.info("metrics_res are: " + str(metrics_res)) %} {% for item in metrics_res %} {% set metric_label = item.get(metric_label_col, metric_id_value) %} {% set unit = item.get(unit_col, "value") %} {% set ns.tag_columns = [] %} {% if ns.alert_tag_values and asset_id in ns.alert_tag_values%} {% for tag_col in ns.alert_tag_values[asset_id] %} {% set _ = ns.tag_columns.append({"label":tag_col, "value":tag_col}) %} {% endfor %} {% endif %} {% if tag_column_names %} {% for tag_col_name in tag_column_names %} {% set tag_col = item.get(tag_col_name) %} {% if tag_col %} {% set _ = ns.tag_columns.append({"label":tag_col, "value":tag_col}) %} {% endif %} {% endfor %} {% endif %} {% set metric_source = item.get("metric_source", "") %} {% set ns.data_query = "" %} {% set is_first = "true" %} {% set ns.firstAssetId = "" %} {% for data_asset_column in data_asset_columns %} {% set asset_id = item.get(data_asset_column) %} {% if asset_id %} {% if is_first == "true" %} {% set is_first = "false" %} {% set ns.firstAssetId = asset_id %} {% else %} {% set ns.data_query = ns.data_query + " or " %} {% endif %} {% set ns.data_query = ns.data_query + data_asset_column + " is '" + asset_id + "'" %} {% else %} {% set _ = logger.info("Ignoring data_asset_column: " + data_asset_column + " as it is empty for metric_label: " + metric_label) %} {% endif %} {% endfor %} {% set data_timestamp_string = data_timestamp_col + " is after '" + str(from_time_str) + "' and " + data_timestamp_col + " is before '" + str(to_time_str) + "' " %} {% set ns.data_query = " ( " + ns.data_query + " ) and " + data_timestamp_string %} {% if data_metric_id_col %} {% set ns.data_query = ns.data_query + " and " + data_metric_id_col + " is '" + metric_id_value + "'" %} {% endif %} {% if data_query_extra_filter %} {% set ns.data_query = ns.data_query + " and " + data_query_extra_filter %} {% endif %} {% set _ = logger.info("Final ns.data_query is: " + str(ns.data_query)) %} {% set ns.widget = { "title": asset_types_str + " - " + ns.firstAssetId + " - " + metric_label, "widget_type": "timeseries", "stream": metric_data_stream, "extra_filter": ns.data_query, "ts_column": "timestamp", "max_width": 12, "height": 5, "min_width": 12, "fixTimeWindow": True, "xAxisLabel": "Timestamp", "yAxisLabel": unit, "static_markers": [ { "timestamp": incident_occurance_time, "message": "Incident Occurred", "color": "#B71C1C" } ], "chartProperties": { "yAxisLabel": unit, "xAxisLabel": "Timestamp", "legendLocation": "none" }, "interval": "5Min", "series_spec": [ { "column": data_metric_value_col, "agg": "sum", "type": "float" } ], "filterTags": ns.tag_columns, "widget_id": ns.firstAssetId + " - " + metric_label } %} {% set x = dashboard["dashboard_sections"][0]["widgets"].append(ns.widget) %} {% endfor %} {% endfor %} {% endfor %} {% endif %} {% else %} {% set _ = logger.warning("No match found among alert_asset_types: " + str(alert_asset_types) + " and asset_types: " + str(asset_types) + " defined for widget config in page config hence skipping this widget config with matched_asset_types: " + str(matched_asset_types)) %} {% endif %} {% endfor %} {% else %} {% set _ = logger.warning("No widget_config_json defined for page config hence no metrics widgets for incident: " + incidentid) %} {% endif %} {% else %} {% set _ = logger.warning("No alert types or id values found but page config is defined to use alerts hence no metrics for incident: " + incidentid) %} {% endif %} {% else %} {% set timestamp_string = "timestamp is after '" + str(from_time_str) + "' and " + "timestamp is before '" + str(to_time_str) + "' " %} {% set _ = logger.info("Got timestamp_string is: " + str(timestamp_string)) %} {% set x = dashboard["dashboard_filters"]["default_time_range"].__setitem__("from_time", str(from_time_str)) %} {% set x = dashboard["dashboard_filters"]["default_time_range"].__setitem__("to_time", str(to_time_str)) %} {% set config_items_str = incident.get("i_config_items") %} {% set asset_ids = [] %} {% if config_items_str and "metric_asset_id" in config_items_str %} {% set config_items = json_loads(config_items_str) %} {% if "alert_attributes" in config_items %} {% set alert_attributes_str = config_items.get("alert_attributes") %} {% if alert_attributes_str and "metric_asset_id" in alert_attributes_str %} {% set alert_attributes = json_loads(alert_attributes_str) %} {% if "metric_asset_id" in alert_attributes %} {% set x = asset_ids.append(alert_attributes.get("metric_asset_id")) %} {% set _ = logger.info("Added metric_asset_id " + alert_attributes.get("metric_asset_id") +" for incident " + incidentid + " to list of eligible metric asset IDs") %} {% endif %} {% endif %} {% endif %} {% endif %} {% set alert_query = "a_incident_id is '" + incidentid + "'" %} {% set alerts_res = engine.query_stream_aggs(stream="oia-alerts-stream", cfxql_query=alert_query, aggs=[{"field": "count_", "func": "max"}], groupby=["a_en_metric_asset_id"]) %} {% set _ = logger.debug("For incident " + incidentid + " found alerts:" + json_dumps(alerts_res)) %} {% for item in alerts_res %} {% set metric_asset_id = item.get("a_en_metric_asset_id") %} {% if metric_asset_id not in asset_ids %} {% set x = asset_ids.append(metric_asset_id) %} {% set _ = logger.debug("From alerts added metric_asset_id " + metric_asset_id +" for incident " + incidentid + " to list of eligible metric asset IDs") %} {% endif %} {% endfor %} {% if asset_ids %} {% set asset_id_str = asset_ids|join("', '") %} {% set asset_id_str = "['" + asset_id_str + "']" %} {% set metrics_query = "asset_id in " + asset_id_str + " and " + timestamp_string + " and anomaly_score is greater than 0"%} {% set metrics_res = engine.query_stream_aggs(stream="oia-ml-metrics-meta-output", cfxql_query=metrics_query, aggs=[{"field": "anomaly_score", "func": "max"}], groupby=["asset_id", "metric_id", "metric_label", "unit", "metric_source"], sorting=[{"anomaly_score":"desc"}]) %} {% set _ = logger.debug("metrics_res are: " + str(metrics_res)) %} {% for item in metrics_res %} {% set asset_id = item.get("asset_id") %} {% set metric_id = item.get("metric_id") %} {% set metric_label = item.get("metric_label") %} {% set unit = item.get("unit") %} {% set metric_source = item.get("metric_source") %} {% set data_filter = "asset_id is '" + asset_id + "' and metric_id is '" + metric_id + "' and " + timestamp_string %} {% set anomalies_filter = "asset_id is '" + asset_id + "' and metric_id is '" + metric_id + "' and " + timestamp_string + " and anomalies_type is 'upper' and anomalies_severity is 'High' " %} {% set widget = { "title": asset_id + " - " + metric_label, "widget_type": "timeseries_multisource", "max_width": 12, "height": 5, "min_width": 12, "fixTimeWindow": True, "xAxisLabel": "Timestamp", "yAxisLabel": unit, "static_markers": [ { "timestamp": incident_occurance_time, "message": "Incident Occurred", "color": "#B71C1C" } ], "chartProperties": { "xAxisLabel": "Timestamp", "yAxisLabel": unit, "legendLocation": "bottom", "xAxisTickOrientation": "STAGGERED", "mode": "zoom", "options": { "elements": { "line": { "borderWidth": 1 }, "point": { "radius": 2, "hitRadius": 3, "hoverRadius": 3 } } }, "dashboards": [ { "action": { "permission": "rda:user_dashboard:view", "appName": "user-dashboard/oia-war-room", "identifier": "launch.dashboard.oia-war-room", "title": "War Room", "type": "GO_TO_APP_STATE" }, "context": {} } ] }, "sources": [ { "source_label": "upper_anomaly", "stream": "oia-ml-metrics-regression-output", "hide_lines": true, "ts_column": "timestamp", "color": "#ff0000", "extra_filter": anomalies_filter, "series_spec": [ { "label": "Anomaly", "column": "all-baseline", "agg": "max", "type": "float" } ] }, { "source_label": "actual", "stream": "oia-ml-metrics-regression-output", "ts_column": "timestamp", "color": "#42A5F5", "extra_filter": data_filter, "series_spec": [ { "label": "Actual", "column": "all-baseline", "agg": "max", "type": "float" } ] } ], "filterTags": [ { "label": metric_source, "value": metric_source } ], "widget_id": asset_id + "_" + metric_id } %} {% set x = dashboard["dashboard_sections"][0]["widgets"].append(widget) %} {% endfor %} {% else %} {% set _ = logger.warning("No assets hence no metrics for incident: " + incidentid) %} {% endif %} {% endif %} {% else %} {% set _ = logger.warning("Can not find incident details hence can not show metrics for incident: " + incidentid) %} {% endif %} {% set _ = logger.debug("Final metrics dashboard: " + str(dashboard)) %}