Skip to content

Fix dashboard create (copy, from file) #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 11, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 14 additions & 70 deletions sdcclient/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1577,87 +1577,33 @@ def filter_fn(panel):
else:
return [False, 'Not found']

def create_dashboard_from_template(self, newdashname, template, scope=[], shared=False, annotations={}):
if scope is None:
scope = []

if type(scope) is str:
checks = scope.strip(' \t\n\r?!.').split(" and ")
scope = []

for c in checks:
elements = c.strip(' \t\n\r?!.').split("=")
if len(elements) != 2:
return [False, "invalid scope format"]
scope.append({elements[0].strip(' \t\n\r?!.'): elements[1].strip(' \t\n\r?!.')})
else:
if not(type(scope) is list):
return [False, "invalid scope format"]

#
# Create the unique ID for this dashboard
#
baseconfid = newdashname
for sentry in scope:
baseconfid = baseconfid + str(list(sentry.keys())[0])
baseconfid = baseconfid + str(list(sentry.values())[0])
def create_dashboard_from_template(self, dashboard_name, template, scope, shared=False, annotations={}):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we need the scope validation logic anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at the old code, it seems that the logic assumes that the scope string is always a list of anded expressions, each with = operator. Also, the logic was then applied to use the obsolete JSON structure for the filter.

I trust the backend validation more than this obsolete logic.

if scope is not None:
if isinstance(scope, basestring) == False:
return [False, 'Invalid scope format: Expected a string']

#
# Clean up the dashboard we retireved so it's ready to be pushed
#
template['id'] = None
template['version'] = None
template['schema'] = 1
template['name'] = newdashname
template['name'] = dashboard_name
template['isShared'] = shared # make sure the dashboard is not shared
template['isPublic'] = False # reset public sharing
template['publicToken'] = None

#
# Assign the filter and the group ID to each view to point to this service
# set dashboard scope to the specific parameter
# NOTE: Individual panels might override the dashboard scope, the override will NOT be reset
#
filters = []
gby = []
for sentry in scope:
filters.append({'metric' : list(sentry.keys())[0], 'op' : '=', 'value' : list(sentry.values())[0],
'filters' : None})
gby.append({'metric': list(sentry.keys())[0]})

filter = {
'filters' :
{
'logic' : 'and',
'filters' : filters
}
}
template['filterExpression'] = scope

#
# create the grouping configurations for each chart
#
j = 0
if 'items' in template:
for chart in template['items']:
if len(scope) != 0:
j = j + 1

confid = baseconfid + str(j)

gconf = {'id': confid,
'groups': [
{
'groupBy': gby
}
]
}

res = requests.post(self.url + '/api/groupConfigurations', headers=self.hdrs,
data=json.dumps(gconf), verify=self.ssl_verify)
if not self._checkResponse(res):
return [False, self.lasterr]

chart['filter'] = filter
chart['groupId'] = confid
else:
chart['filter'] = None
chart['groupId'] = None
if 'overrideFilter' in chart and chart['overrideFilter'] == False:
# patch frontend bug to hide scope override warning even when it's not really overridden
chart['scope'] = scope

if 'annotations' in template:
template['annotations'].update(annotations)
Expand All @@ -1666,12 +1612,10 @@ def create_dashboard_from_template(self, newdashname, template, scope=[], shared

template['annotations']['createdByEngine'] = True

ddboard = {'dashboard': template}

#
# Create the new dashboard
#
res = requests.post(self.url + '/ui/dashboards', headers=self.hdrs, data=json.dumps(ddboard), verify=self.ssl_verify)
res = requests.post(self.url + '/ui/dashboards', headers=self.hdrs, data=json.dumps({'dashboard': template}), verify=self.ssl_verify)
if not self._checkResponse(res):
return [False, self.lasterr]
else:
Expand Down