Skip to content

Commit 5cb5327

Browse files
author
Davide Schiera
authored
Fix dashboard create (copy, from file) (#68)
1 parent aba1cb0 commit 5cb5327

File tree

1 file changed

+14
-70
lines changed

1 file changed

+14
-70
lines changed

sdcclient/_client.py

Lines changed: 14 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,87 +1577,33 @@ def filter_fn(panel):
15771577
else:
15781578
return [False, 'Not found']
15791579

1580-
def create_dashboard_from_template(self, newdashname, template, scope=[], shared=False, annotations={}):
1581-
if scope is None:
1582-
scope = []
1583-
1584-
if type(scope) is str:
1585-
checks = scope.strip(' \t\n\r?!.').split(" and ")
1586-
scope = []
1587-
1588-
for c in checks:
1589-
elements = c.strip(' \t\n\r?!.').split("=")
1590-
if len(elements) != 2:
1591-
return [False, "invalid scope format"]
1592-
scope.append({elements[0].strip(' \t\n\r?!.'): elements[1].strip(' \t\n\r?!.')})
1593-
else:
1594-
if not(type(scope) is list):
1595-
return [False, "invalid scope format"]
1596-
1597-
#
1598-
# Create the unique ID for this dashboard
1599-
#
1600-
baseconfid = newdashname
1601-
for sentry in scope:
1602-
baseconfid = baseconfid + str(list(sentry.keys())[0])
1603-
baseconfid = baseconfid + str(list(sentry.values())[0])
1580+
def create_dashboard_from_template(self, dashboard_name, template, scope, shared=False, annotations={}):
1581+
if scope is not None:
1582+
if isinstance(scope, basestring) == False:
1583+
return [False, 'Invalid scope format: Expected a string']
16041584

16051585
#
16061586
# Clean up the dashboard we retireved so it's ready to be pushed
16071587
#
16081588
template['id'] = None
16091589
template['version'] = None
16101590
template['schema'] = 1
1611-
template['name'] = newdashname
1591+
template['name'] = dashboard_name
16121592
template['isShared'] = shared # make sure the dashboard is not shared
1593+
template['isPublic'] = False # reset public sharing
1594+
template['publicToken'] = None
16131595

16141596
#
1615-
# Assign the filter and the group ID to each view to point to this service
1597+
# set dashboard scope to the specific parameter
1598+
# NOTE: Individual panels might override the dashboard scope, the override will NOT be reset
16161599
#
1617-
filters = []
1618-
gby = []
1619-
for sentry in scope:
1620-
filters.append({'metric' : list(sentry.keys())[0], 'op' : '=', 'value' : list(sentry.values())[0],
1621-
'filters' : None})
1622-
gby.append({'metric': list(sentry.keys())[0]})
1623-
1624-
filter = {
1625-
'filters' :
1626-
{
1627-
'logic' : 'and',
1628-
'filters' : filters
1629-
}
1630-
}
1600+
template['filterExpression'] = scope
16311601

1632-
#
1633-
# create the grouping configurations for each chart
1634-
#
1635-
j = 0
16361602
if 'items' in template:
16371603
for chart in template['items']:
1638-
if len(scope) != 0:
1639-
j = j + 1
1640-
1641-
confid = baseconfid + str(j)
1642-
1643-
gconf = {'id': confid,
1644-
'groups': [
1645-
{
1646-
'groupBy': gby
1647-
}
1648-
]
1649-
}
1650-
1651-
res = requests.post(self.url + '/api/groupConfigurations', headers=self.hdrs,
1652-
data=json.dumps(gconf), verify=self.ssl_verify)
1653-
if not self._checkResponse(res):
1654-
return [False, self.lasterr]
1655-
1656-
chart['filter'] = filter
1657-
chart['groupId'] = confid
1658-
else:
1659-
chart['filter'] = None
1660-
chart['groupId'] = None
1604+
if 'overrideFilter' in chart and chart['overrideFilter'] == False:
1605+
# patch frontend bug to hide scope override warning even when it's not really overridden
1606+
chart['scope'] = scope
16611607

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

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

1669-
ddboard = {'dashboard': template}
1670-
16711615
#
16721616
# Create the new dashboard
16731617
#
1674-
res = requests.post(self.url + '/ui/dashboards', headers=self.hdrs, data=json.dumps(ddboard), verify=self.ssl_verify)
1618+
res = requests.post(self.url + '/ui/dashboards', headers=self.hdrs, data=json.dumps({'dashboard': template}), verify=self.ssl_verify)
16751619
if not self._checkResponse(res):
16761620
return [False, self.lasterr]
16771621
else:

0 commit comments

Comments
 (0)