Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
149 additions
and
155 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import elasticsearch | ||
|
||
from kibana_dashboard_api import Visualization, Dashboard | ||
from kibana_dashboard_api import VisualizationsManager, DashboardsManager | ||
|
||
|
||
def generate_dashboard(es_conn, sensor_names, df_scored, index_name): | ||
dashboards = DashboardsManager(es_conn) | ||
dashboard = Dashboard() | ||
dashboard.id = "%s-dashboard" % index_name | ||
dashboard.title = "%s dashboard" % index_name | ||
dashboard.panels = [] | ||
dashboard.options = {"darkTheme": True} | ||
dashboard.time_from = "now-15m" | ||
dashboard.refresh_interval_value = 5000 | ||
dashboard.search_source = { | ||
"filter": [{ | ||
"query": { | ||
"query_string": { | ||
"analyze_wildcard": True, | ||
"query": "*" | ||
} | ||
} | ||
}] | ||
} | ||
visualizations = VisualizationsManager(es_conn) | ||
vis_list = visualizations.get_all() # list all visualizations | ||
panels = [] | ||
i = 0 | ||
for sensor in sensor_names: | ||
viz_id = "%s-%s" % (index_name, sensor) | ||
|
||
# Check if visualization exists | ||
viz = next((v for v in vis_list if v.id == viz_id), None) | ||
if not viz: # If not, create it | ||
viz = Visualization() | ||
viz.id = viz_id | ||
viz.title = "%s-%s" % (index_name, sensor) | ||
viz.search_source = { | ||
"index": index_name, | ||
"query":{ | ||
"query_string":{ | ||
"analyze_wildcard": True, | ||
"query":"*" | ||
} | ||
}, | ||
"filter":[] | ||
} | ||
viz.vis_state = { | ||
"title": "%s-%s" % (index_name, sensor), | ||
"type": "line", | ||
"params": { | ||
"addLegend": True, | ||
"addTimeMarker": True, | ||
"addTooltip": True, | ||
"defaultYExtents": True, | ||
"drawLinesBetweenPoints": True, | ||
"interpolate": "linear", | ||
"radiusRatio": 9, | ||
"scale": "linear", | ||
"setYExtents": False, | ||
"shareYAxis": True, | ||
"showCircles": True, | ||
"smoothLines": True, | ||
"times":[], | ||
"yAxis":{} | ||
}, | ||
"aggs": [ | ||
{ | ||
"id": "2", | ||
"type": "max", | ||
"schema":"radius", | ||
"params": { | ||
"field":"SCORE_%s" % sensor | ||
} | ||
} | ||
], | ||
"listeners": {} | ||
} | ||
try: | ||
res = visualizations.add(viz) | ||
assert res['_id'] == viz_id | ||
except elasticsearch.exceptions.ConflictError: | ||
res = visualizations.update(viz) | ||
|
||
panel = { | ||
"id": viz_id, | ||
"panelIndex": i, | ||
"row": i, | ||
"col": i, | ||
"size_x": 7, | ||
"size_y": 4, | ||
"type": "visualization" | ||
} | ||
panels.append(panel) | ||
ret = dashboard.add_visualization(viz) | ||
i += 1 | ||
|
||
try: | ||
ret = dashboards.add(dashboard) | ||
except elasticsearch.exceptions.ConflictError: | ||
ret = dashboards.update(dashboard) | ||
#pass # Dashboard already exists, let's not overwrite it | ||
|
||
return ret |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
""" Helper functions """ | ||
|
||
import dateparser | ||
|
||
|
||
def detect_time(dataframe, timefield=None, timeunit=None): | ||
""" Attempt to detect the time dimension in a dataframe """ | ||
columns = set(dataframe.columns) | ||
for tfname in ['time', 'datetime', 'date', 'timestamp']: | ||
if tfname in columns: | ||
prev = current = None | ||
for i in dataframe[tfname][:10]: | ||
try: | ||
current = dateparser.parse(str(i)) | ||
# timefield needs to be parsable and always increasing | ||
if not current or (prev and prev > current): | ||
tfname = '' | ||
break | ||
except TypeError: | ||
tfname = '' | ||
break | ||
prev = current | ||
if tfname: | ||
timefield = tfname | ||
if isinstance(i, float) and not timeunit: | ||
timeunit = 's' | ||
break | ||
|
||
return timefield, timeunit |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ elasticsearch | |
numpy | ||
scipy | ||
pandas | ||
-e git://github.com/d-mo/kibana-dashboard-api#egg=kibana_dashboard_api |