示例#1
0
 def find_by_label(cls, client, org_id, label_name):
   match_label = (lambda d: len([l for l in d.labels if l.name == label_name]) > 0)
   ds = DashboardsService(client.api_client)
   dashboards = ds.get_dashboards(org_id=org_id).dashboards
   matches = [d for d in dashboards if match_label(d)]
   if len(matches) == 0:
     return None
   else:
     return cls(client, org_id, matches[0].id)
示例#2
0
 def __init__(self, client, org_id, dashboard_id):
   self.client = client
   self.org_id = org_id
   self.id = dashboard_id
   self.ds = DashboardsService(self.client.api_client)
   info = self.ds.get_dashboards_id(self.id)
   self.name = info.name
   self.description = info.description
   self.cells_info = info.cells
    def setUp(self) -> None:
        super(DashboardsClientTest, self).setUp()

        self.dashboards_service = DashboardsService(self.client.api_client)
        dashboards = self.dashboards_service.get_dashboards()

        for dashboard in dashboards.dashboards:
            if dashboard.name.endswith("_IT"):
                print("Delete dashboard: ", dashboard.name)
                self.dashboards_service.delete_dashboards_id(dashboard.id)
示例#4
0
 def __init__(self, client, dashboard_id, org_id=None):
   if org_id == None:
     org_id = client.org
   self.client = client
   self.org_id = org_id
   self.id = dashboard_id
   self.ds = DashboardsService(self.client.api_client)
   info = self.ds.get_dashboards_id(self.id)
   self.name = info.name
   self.description = info.description
   self.cells_info = info.cells
   self.init_height()
   self._cells = None
示例#5
0
 def __init__(self, client, dashboard_id, org_id=None):
     if org_id == None:
         org_id = client.org
     self.client = client
     self.org_id = org_id
     self.id = dashboard_id
     self.ds = DashboardsService(self.client.api_client)
     info = self.ds.get_dashboards_id(self.id)
     self.name = info.get('name', '')
     self.description = info.get('description', '')
     self.cells_info = list(
         map(lambda cell: namedtuple('cell', cell.keys())(*cell.values()),
             info['cells']))
     self.init_height()
     self._cells = None
示例#6
0
class InfluxDBDashboardView:
    def __init__(self, client, dashboard_id, org_id=None):
        if org_id == None:
            org_id = client.org
        self.client = client
        self.org_id = org_id
        self.id = dashboard_id
        self.ds = DashboardsService(self.client.api_client)
        info = self.ds.get_dashboards_id(self.id)
        self.name = info.get('name', '')
        self.description = info.get('description', '')
        self.cells_info = list(
            map(lambda cell: namedtuple('cell', cell.keys())(*cell.values()),
                info['cells']))
        self.init_height()
        self._cells = None

    def init_height(self):
        items = [1] + list(map(lambda c: 0 + c.y + c.h, self.cells_info))
        self.height = max(*items)

    def cells(self):
        if self._cells == None:
            self._cells = list(
                map(
                    lambda cell_info: InfluxDBDashboardCell(
                        self.client, self.org_id, self.id, cell_info),
                    self.cells_info))
        return self._cells

    def set_time_range(self,
                       start_offset=-(7 * 24 * 60),
                       end_offset=0,
                       window_period=15,
                       offset_unit='m'):
        for cell in self.cells():
            cell.set_time_range(start_offset=start_offset,
                                end_offset=end_offset,
                                window_period=window_period,
                                offset_unit=offset_unit)

    def set_string_literval_variable(self, name, value):
        for cell in self.cells():
            cell.set_string_literval_variable(name, value)

    # TODO: improve

    @classmethod
    def find_by_label(cls, client, label_name, org_id=None):
        match_label = (
            lambda d: len([l for l in d.labels if l.name == label_name]) > 0)
        ds = DashboardsService(client.api_client)
        if org_id == None:
            org_id = client.org
        dashboards = ds.get_dashboards(org_id=org_id).dashboards
        matches = [d for d in dashboards if match_label(d)]
        if len(matches) == 0:
            return None
        else:
            return cls(client, matches[0].id, org_id=org_id)
class DashboardsClientTest(BaseTest):
    def setUp(self) -> None:
        super(DashboardsClientTest, self).setUp()

        self.dashboards_service = DashboardsService(self.client.api_client)
        dashboards = self.dashboards_service.get_dashboards()

        for dashboard in dashboards.dashboards:
            if dashboard.name.endswith("_IT"):
                print("Delete dashboard: ", dashboard.name)
                self.dashboards_service.delete_dashboards_id(dashboard.id)

    def test_create_dashboard_with_cell(self):
        unique_id = str(datetime.datetime.now().timestamp())

        dashboard = self.dashboards_service.post_dashboards(
            create_dashboard_request=CreateDashboardRequest(
                org_id=self.find_my_org().id,
                name=f"Dashboard_{unique_id}_IT"))
        self.assertEqual(dashboard.name, f"Dashboard_{unique_id}_IT")

        cells_service = CellsService(self.client.api_client)
        cell = cells_service.post_dashboards_id_cells(
            dashboard_id=dashboard.id,
            create_cell=CreateCell(name=f"Cell_{unique_id}_IT", h=3, w=12))
        self.assertIsNotNone(cell.id)
        view = cells_service.get_dashboards_id_cells_id_view(
            dashboard_id=dashboard.id, cell_id=cell.id)
        self.assertEqual(view.name, f"Cell_{unique_id}_IT")

    def test_get_dashboard_with_cell_with_properties(self):
        unique_id = str(datetime.datetime.now().timestamp())

        dashboard = self.dashboards_service.post_dashboards(
            create_dashboard_request=CreateDashboardRequest(
                org_id=self.find_my_org().id,
                name=f"Dashboard_{unique_id}_IT"))

        # create cell
        CellsService(self.client.api_client).post_dashboards_id_cells(
            dashboard_id=dashboard.id,
            create_cell=CreateCell(name=f"Cell_{unique_id}_IT", h=3, w=12))

        # retrieve dashboard
        dashboard = self.dashboards_service.get_dashboards_id(dashboard.id)

        from influxdb_client import DashboardWithViewProperties, CellWithViewProperties
        self.assertEqual(DashboardWithViewProperties, type(dashboard))
        self.assertEqual(1, len(dashboard.cells))
        self.assertEqual(CellWithViewProperties, type(dashboard.cells[0]))
示例#8
0
class InfluxDBDashboardView:
  def __init__(self, client, org_id, dashboard_id):
    self.client = client
    self.org_id = org_id
    self.id = dashboard_id
    self.ds = DashboardsService(self.client.api_client)
    info = self.ds.get_dashboards_id(self.id)
    self.name = info.name
    self.description = info.description
    self.cells_info = info.cells

  def cells(self):
    return map(lambda cell_info: InfluxDBDashboardCell(self.client, self.org_id, self.id, cell_info), self.cells_info)

  @classmethod
  def find_by_label(cls, client, org_id, label_name):
    match_label = (lambda d: len([l for l in d.labels if l.name == label_name]) > 0)
    ds = DashboardsService(client.api_client)
    dashboards = ds.get_dashboards(org_id=org_id).dashboards
    matches = [d for d in dashboards if match_label(d)]
    if len(matches) == 0:
      return None
    else:
      return cls(client, org_id, matches[0].id)