Esempio n. 1
0
 def test_removing_tiles(self):
     rd, master_tile = self.test_handle_tpcreator()
     tiles = Tile.select_multi(rd.dashboard_id,
                               _select_tile_ids(rd.dashboard_id)).values()
     tpcreated_tile = util.first(t for t in tiles if t.get_master_tile_id())
     layouts.detach_tile(tpcreated_tile)
     self.assertEqual(1,
                      len(tpcreator.select_tpcreated_tile_ids(master_tile)))
Esempio n. 2
0
    def test_promote_other_master_tile(self):
        rd, master_tile = self.test_handle_tpcreator()
        tpcreated_ids = tpcreator.select_tpcreated_tile_ids(master_tile)
        tpcreated_tiles = Tile.select_multi(rd.dashboard_id,
                                            tpcreated_ids).values()
        #print 'tpcreated', [t.tags for t in tpcreated_tiles]

        new_master_repl = util.first(t for t in tpcreated_tiles
                                     if t.tile_options['tile_title'] == 'tpc0')
        new_master_tile = tpcreator.make_master_from_tpcreated(
            master_tile, new_master_repl)
        self.assertEqual('tpc0', new_master_tile.tile_options['tile_title'])

        self.assertTrue(
            layouts.replace_tiles({master_tile: new_master_tile}, None))
        self.assertFalse(tpcreator.select_tpcreated_tile_ids(master_tile))

        new_tpcreated_ids = tpcreator.select_tpcreated_tile_ids(
            new_master_tile)
        new_tpcreated_tiles = Tile.select_multi(rd.dashboard_id,
                                                new_tpcreated_ids).values()
        self.assertTrue(new_tpcreated_tiles)

        self.assertEqual([['p1:20'], ['p1:30']],
                         sorted([t.tags for t in new_tpcreated_tiles]))

        d = [
            OrderedDict([('user_name', 'robert10'), ('is_active', True),
                         ('points', 128)])
        ]
        rd.report.process_input(json.dumps(d), tags=['p1:15', 'p2:34'])

        self.assertEqual(
            len(new_tpcreated_ids) + 1,
            len(tpcreator.select_tpcreated_tile_ids(new_master_tile)))
        latest_tile = Tile.select(rd.dashboard_id,
                                  _select_tile_ids(rd.dashboard_id)[-1])
        self.assertEqual(['p1:15'], latest_tile.tags)
        self.assertEqual(['monique', 'robert3', 'robert10'], [
            ss.params['filtering_expr']['args'][0]
            for ss in latest_tile.series_specs()
        ])
Esempio n. 3
0
    def test_synchronize_options_of_tpcreated(self):
        rd, master_tile = self.test_handle_tpcreator()

        self.assertEqual(2,
                         len(tpcreator.select_tpcreated_tile_ids(master_tile)))

        tile_config = master_tile.get_tile_config()
        tile_config['series_spec_list'].append(
            dataseries.SeriesSpec(2, 0, dict(op='eq', args=['chris'])))
        tile_config['tile_options']['seconds_back'] = 125
        tile_config['tile_options']['tile_title'] = 'A B C'
        master_tile = master_tile.insert_similar(tile_config)

        tiles = Tile.select_multi(
            rd.dashboard_id,
            tpcreator.select_tpcreated_tile_ids(master_tile)).values()
        for tile in tiles:
            self.assertIn(
                dataseries.SeriesSpec(2, 0, dict(op='eq', args=['chris'])),
                tile.series_specs())
            self.assertEqual(125, tile.tile_options['seconds_back'])
            self.assertEqual(
                'A B C',
                tile.tile_options['tpcreator_data']['tile_title_base'])
Esempio n. 4
0
    def test_synchronize_sizes_of_tpcreated(self):
        rd, master_tile = self.test_handle_tpcreator()

        layout = layouts.Layout.select(rd.owner_id, rd.dashboard_id)
        ld = layout.layout_dict
        ld[master_tile.tile_id]['width'] = 8
        ld[master_tile.tile_id]['height'] = 3
        ld = layouts.apply_mods_for_noninserted_layout(
            [layouts.repack_mod()], Layout(ld)).new_layout.layout_dict
        layout_id = layout.set(rd.owner_id, rd.dashboard_id)
        self.assertTrue(layout_id)

        self.assertTrue(
            tpcreator.synchronize_sizes_of_tpcreated(master_tile, layout_id))
        layout = Layout.select(rd.owner_id, rd.dashboard_id)
        ld = layout.layout_dict
        for tile_id in tpcreator.select_tpcreated_tile_ids(master_tile):
            self.assertEqual(8, ld[tile_id]['width'])
            self.assertEqual(3, ld[tile_id]['height'])
Esempio n. 5
0
    def test_removing_tiles_by_expiring(self):
        rd, master_tile = self.test_handle_tpcreator()
        tiles = Tile.select_multi(rd.dashboard_id,
                                  _select_tile_ids(rd.dashboard_id)).values()

        #print tpcreator.select_tpcreated_tile_ids(master_tile)
        #print select_tile_ids(rd.dashboard_id)
        #print rd.layout_data().layout_dict.keys()

        #tile = Tile.select(rd.dashboard_id, util.first(tile_id for tile_id in select_tile_ids(rd.dashboard_id) if tile_id not in rd.layout_data().layout_dict.keys()))
        #print tile

        self.assertTrue(
            expire_tiles_without_data(tiles, 0,
                                      rd.layout().layout_id))

        self.assertEqual(0,
                         len(tpcreator.select_tpcreated_tile_ids(master_tile)))
        self.assertEqual(1, len(_select_tile_ids(rd.dashboard_id)))
Esempio n. 6
0
    def test_handle_tpcreator(self):
        rd = new_report_data('points')

        tile_config = {
            'tw_type':
            'Range',
            'tags': ['p1:10'],
            'series_spec_list': [
                dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])),
            ],
            'tile_options': {
                'seconds_back':
                600,
                'tile_title':
                'm0',
                'sscs':
                dataseries.SeriesSpec(2, 0, dict(op='eq', args=['monique'])),
            }
        }
        tile_config['tile_options'][
            'tpcreator_uispec'] = tpcreator.suggested_tpcreator_uispec(
                tile_config['tags'])

        master_tile = Tile.insert(rd.owner_id, rd.report.report_id,
                                  rd.dashboard_id, tile_config)
        layouts.place_tile(master_tile)

        d = [
            OrderedDict([('user_name', 'robert3'), ('is_active', True),
                         ('points', 128)])
        ]
        rd.report.process_input(json.dumps(d), tags=['p1:10'])
        master_tile = rd.only_tile_from_layout()
        self.assertEqual([], tpcreator.select_tpcreated_tile_ids(master_tile))
        self.assertEqual([master_tile.tile_id],
                         _select_tile_ids(rd.dashboard_id))

        d = [
            OrderedDict([('user_name', 'robert3'), ('is_active', True),
                         ('points', 128)])
        ]
        rd.report.process_input(json.dumps(d), tags=['p1:20'])
        self.assertEqual(2, len(_select_tile_ids(rd.dashboard_id)))
        tiles = Tile.select_multi(rd.dashboard_id,
                                  _select_tile_ids(rd.dashboard_id)).values()
        created_tile = util.first(tiles, key=lambda t: not t.is_master_tile())
        self.assertEqual(['p1:20'], created_tile.tile_options['tags'])
        self.assertEqual(600, created_tile.tile_options['seconds_back'])
        self.assertEqual(tile_config['tile_options']['sscs'],
                         created_tile.tile_options['sscs'])
        td = created_tile.get_tile_data()
        self.assertEqual('points (monique, robert3)',
                         td['generated_tile_title'])
        self.assertEqual('[p1:20]', td['generated_tile_title_postfix'])

        d = [
            OrderedDict([('user_name', 'robert3'), ('is_active', True),
                         ('points', 128)])
        ]
        rd.report.process_input(json.dumps(d), tags=['p1:30', 'p2:30'])
        self.assertEqual(3, len(_select_tile_ids(rd.dashboard_id)))

        del tile_config['tile_options']['tpcreator_uispec']
        tile_config['tile_options']['tile_title'] = 'ot0'
        other_tile = Tile.insert(rd.owner_id, rd.report.report_id,
                                 rd.dashboard_id, tile_config)
        layouts.place_tile(other_tile)
        self.assertEqual(4, len(_select_tile_ids(rd.dashboard_id)))

        self.assertEqual(2,
                         len(tpcreator.select_tpcreated_tile_ids(master_tile)))

        for i, tile_id in enumerate(
                tpcreator.select_tpcreated_tile_ids(master_tile)):
            tile = Tile.select(rd.dashboard_id, tile_id)
            tile_config = tile.get_tile_config()
            tile_config['tile_options']['tile_title'] = 'tpc%d' % i
            new_tile = tile.insert_similar(tile_config)
            layouts.replace_tiles({tile: new_tile}, None)

        return rd, Tile.select(master_tile.dashboard_id, master_tile.tile_id)
Esempio n. 7
0
def expire_tiles_without_data(tile_list, max_seconds_without_data,
                              for_layout_id):
    """Delete and detach tiles from a dashboard which don't have data for
    at least the specified time period.

    :param tile_list: a list of :class:`Tile` objects to expire, belonging to the same dashboard
    :param int max_seconds_without_data: the maximal age (specified in seconds) of the tile's
        data to avoid the expiration
    :param ~uuid.UUID for_layout_id: the version of the layout to perform the expiration
    :return: ``layout_id`` of the new layout if the operation was successful, ``None``
        otherwise
    """
    from mqe import layouts
    from mqe import tpcreator

    regular_tiles = [t for t in tile_list if not t.is_master_tile() \
                     and _should_expire_tile(t, max_seconds_without_data)]
    master_tiles = [t for t in tile_list if t.is_master_tile() \
                    and _should_expire_tile(t, max_seconds_without_data)]

    log.info(
        'Will try to expire %s regular and %s master tiles out of %s passed',
        len(regular_tiles), len(master_tiles), len(tile_list))

    layout_id = for_layout_id

    if regular_tiles:
        repl_res = layouts.replace_tiles(
            {tile: None
             for tile in regular_tiles}, layout_id)
        if repl_res:
            log.info('Successfully expired regular tiles')
            layout_id = repl_res.new_layout.layout_id
        else:
            log.warn('Failed to expire regular tiles')

    master_repl = {}
    for master_tile in master_tiles:
        tpcreated_tile_ids = tpcreator.select_tpcreated_tile_ids(master_tile,
                                                                 layout_id,
                                                                 sort=True)
        if not tpcreated_tile_ids:
            continue
        new_master_base = Tile.select(master_tile.dashboard_id,
                                      tpcreated_tile_ids[0])
        if not new_master_base:
            log.warn('Could not select master tile replacement')
            continue
        new_master = tpcreator.make_master_from_tpcreated(
            master_tile, new_master_base)
        master_repl[master_tile] = new_master
        master_repl[new_master_base] = None

    if master_repl:
        repl_res = layouts.replace_tiles(master_repl, layout_id)
        if repl_res:
            log.info('Successfully expired master tiles')
            layout_id = repl_res.new_layout.layout_id
        else:
            log.warn('Failed to expire master tiles')

    if layout_id == for_layout_id:
        return None
    return layout_id