def view(): link_name = request.args.get('link_name', '') if link_name: form = ViewFiltersForm() else: form = ViewForm() if request.method=='GET': data_rows_manager = DataRowsManager() view_manager = ViewManager() if link_name: view_manager.set_view(link_name) if data_rows_manager.set_view(link_name): extra_filter = False # Get filters parameters if any filters_name = data_rows_manager.get_filters_name() filters_url_values = {} url = urlparse.urlparse(request.url) query_param = urlparse.parse_qs(url.query) query_param = dict([(k, v[0]) for k, v in query_param.items()]) for name in filters_name: arg = query_param.get(name, '') if arg: # TODO: find a better way to check if it is a dictionnary if '{' in arg: filters_url_values[name] = dict(ast.literal_eval(arg)) else: filters_url_values[name] = arg if filters_url_values: data_rows_manager.set_extra_filters(filters_url_values) arg = query_param.get('sort_by', '') if arg: sort_by = dict(ast.literal_eval(arg)) data_rows_manager.set_extra_sorters(sort_by) view = data_rows_manager.get_view() viewFilters = view_manager.get_filters() form = ViewFiltersForm(obj=viewFilters) form.populate_obj(viewFilters) filter_display = view_manager.get_filter_display(form) return snapins.render_sidebar_template('views/view.html', controller=globals(), form=form, data_rows_manager=data_rows_manager, extra_filter = extra_filter, filter_display = filter_display) else: flash(_(u'View') + ' \'' + link_name + '\' ' + _(u'doesn\'t exist'), 'error') return redirect('/view') else: views = View.query.all() return snapins.render_sidebar_template('views/view.html', views=views, form=form) return snapins.render_sidebar_template('views/view.html', link_name=link_name) elif request.method=='POST': if link_name: viewFilters = form.get_filters() return redirect(add_to_cur_url(request, viewFilters.get_filters())) else: datasource = form.datasource.data return redirect('/edit_view?datasource='+ datasource)
def set_view(self, link_name): self.__view_manager = ViewManager() if self.__view_manager.set_view(link_name): self.__filters_manager = FiltersManager() self.update_filters() return True else: return False
def get_link_data(self): link_data = {} columns = self.__view_manager.get_columns() for col in columns: if col.link: linked_view_manager = ViewManager() linked_view_manager.set_view(col.link) link_details = {} hidden_filters = linked_view_manager.get_hidden_filters() link_details['link_name'] = col.link link_details['filters'] = hidden_filters link_data[col.column] = link_details return link_data
def get_link_data(self): link_data = {} columns = self.__view_manager.get_columns() for col in columns: if col.link: linked_view_manager = ViewManager() linked_view_manager.set_view(col.link) link_details = {} hidden_filters = linked_view_manager.get_hidden_filters() link_details['link_name'] = col.link link_details['filters'] = hidden_filters link_data[col.column] = link_details return link_data
def set_view(self, link_name): self.__view_manager = ViewManager() if self.__view_manager.set_view(link_name): self.__filters_manager = FiltersManager() self.update_filters() return True else: return False
class DataRowsManager(): """ A sort of proxy that facilitate gathering data from LiveStatus without knowing implementation details. """ def __init__(self): self.__view_manager = None self.__filters_manager = None ''' Set the view with the link_name. If a view is found, return true, else return false. ''' def set_view(self, link_name): self.__view_manager = ViewManager() if self.__view_manager.set_view(link_name): self.__filters_manager = FiltersManager() self.update_filters() return True else: return False def update_filters(self): self.__filters_manager.set_filters(self.__view_manager.get_filters()) def get_group_columns_list(self): groupers = self.__view_manager.get_groupers() group_list = [] if groupers: for group in groupers: group_list.append(group.column) return group_list def get_link_data(self): link_data = {} columns = self.__view_manager.get_columns() for col in columns: if col.link: linked_view_manager = ViewManager() linked_view_manager.set_view(col.link) link_details = {} hidden_filters = linked_view_manager.get_hidden_filters() link_details['link_name'] = col.link link_details['filters'] = hidden_filters link_data[col.column] = link_details return link_data def get_rows(self): """ get rows corresponding to the query builded with the view and its options """ # prepare basic parameter datasource and columns_name datasource = datasources[self.__view_manager.get_view().datasource] columns_name = self.get_asked_columns_name() filters_string = self.__filters_manager.get_filter_query() print '\nfilters: ' + filters_string columns_name += self.__view_manager.get_sorters_columns() rows = livestatus_query.get_rows(datasource, columns_name, filters_string) if not rows == []: groupers = self.__view_manager.get_groupers() sort_list = [] for grouper in groupers: sort_list.append((grouper.column, 0)) rows = self.group(rows, self.get_group_columns_list()) rows = sorted(list((k, v) for k, v in rows.items())) sorters = self.__view_manager.get_sorters() if sorters: sort_list = [] for sorter in sorters: sort_list.append((sorter.column, sorter.sorter_option)) rows = self.__sorted_rows(rows, sort_list) rows = self.__readable_rows(rows) return rows def get_sorters_dic(self): sorters = {} for sorter in self.__view_manager.get_sorters(): sorters[sorter.column] = sorter.sorter_option return sorters def __readable_rows(self, rows): rows_readable = rows for group in rows_readable: for row in group[1]: for name, value in row.iteritems(): if name in painters.keys(): row[name] = painters[name].get_readable(row) return rows_readable def __sorted_rows(self, rows, sort_list): arguments = [] callers = {} for sorter in sort_list: prefix = '' sort_order = sorter[1] column = sorter[0] if sort_order == '1': prefix = '-' arguments.append(prefix + column) if isinstance(rows[0][1][0][column], basestring): get_lower = compose(itemgetter(column), methodcaller('lower')) callers[column] = get_lower for group in rows: multikeysort(group[1], arguments, callers) return rows def get_asked_columns_name(self): columns_names = [] for column in self.__view_manager.get_columns(): columns_names.append(column.column) return columns_names def get_rows_count(self, rows): count = 0 for group in rows: count = count + len(group[1]) return count def get_asked_columns_short_title(self): columns_names = [] for column in self.__view_manager.get_columns(): columns_names.append(painters[column.column].short_title) return columns_names def get_group_header(self, groupEnum): header = "" groups_list = self.get_group_columns_list() for elem in groupEnum: curCol = groups_list[groupEnum.index(elem)] if not header == "": header = header + ', ' readable_elem = painters[curCol].get_readable({curCol: elem}) header = header + str(readable_elem) return header def set_extra_filters(self, filters): self.__view_manager.set_filters(filters) self.update_filters() def set_extra_sorters(self, sorters): self.__view_manager.set_extra_sorters(sorters) def get_filters_name(self): return self.__filters_manager.get_filters_name() def get_view(self): return self.__view_manager.get_view() def group(self, mylist, groups): #if not groups: return mylist def foo(mydict, row): key = tuple(map(lambda x: row[x], groups)) if not key in mydict.keys(): mydict[key] = [] mydict[key].append(row) return mydict return reduce(foo, mylist, {})
class DataRowsManager(): """ A sort of proxy that facilitate gathering data from LiveStatus without knowing implementation details. """ def __init__(self): self.__view_manager = None self.__filters_manager = None ''' Set the view with the link_name. If a view is found, return true, else return false. ''' def set_view(self, link_name): self.__view_manager = ViewManager() if self.__view_manager.set_view(link_name): self.__filters_manager = FiltersManager() self.update_filters() return True else: return False def update_filters(self): self.__filters_manager.set_filters(self.__view_manager.get_filters()) def get_group_columns_list(self): groupers = self.__view_manager.get_groupers() group_list = [] if groupers: for group in groupers: group_list.append(group.column) return group_list def get_rows(self): """ get rows corresponding to the query builded with the view and its options """ # prepare basic parameter datasource and columns_name datasource = datasources[self.__view_manager.get_view().datasource] columns_name = self.get_asked_columns_name() filters_string = self.__filters_manager.get_filter_query() print '\nfilters: ' + filters_string columns_name += self.__view_manager.get_sorters_columns() rows = livestatus_query.get_rows(datasource, columns_name, filters_string) if not rows == []: groupers = self.__view_manager.get_groupers() sort_list = [] for grouper in groupers: sort_list.append((grouper.column,0)) rows = self.group(rows, self.get_group_columns_list()) rows = sorted(list((k, v) for k,v in rows.items())) sorters = self.__view_manager.get_sorters() if sorters: sort_list = [] for sorter in sorters: sort_list.append((sorter.column,sorter.sorter_option)) rows = self.__sorted_rows(rows, sort_list) rows = self.__readable_rows(rows) return rows def get_sorters_dic(self): sorters = {} for sorter in self.__view_manager.get_sorters(): sorters[sorter.column] = sorter.sorter_option return sorters def __readable_rows(self, rows): rows_readable = rows for group in rows_readable: for row in group[1]: for name, value in row.iteritems(): if name in painters.keys(): row[name] = painters[name].get_readable(row) return rows_readable def __sorted_rows(self, rows, sort_list): arguments = [] callers = {} for sorter in sort_list: prefix = '' sort_order = sorter[1] column = sorter[0] if sort_order == '1': prefix = '-' arguments.append(prefix + column) if isinstance(rows[0][1][0][column], basestring): get_lower = compose(itemgetter(column), methodcaller('lower')) callers[column] = get_lower for group in rows: multikeysort(group[1], arguments, callers) return rows def get_asked_columns_name(self): columns_names = [] for column in self.__view_manager.get_columns(): columns_names.append(column.column) return columns_names def get_rows_count(self, rows): count = 0 for group in rows: count = count + len(group[1]) return count def get_asked_columns_title(self): columns_names = [] for column in self.__view_manager.get_columns(): columns_names.append(painters[column.column].title) return columns_names def get_group_header(self, groupEnum): header = "" groups_list = self.get_group_columns_list() for elem in groupEnum: curCol = groups_list[groupEnum.index(elem)] if not header == "": header = header + ', ' readable_elem = painters[curCol].get_readable({curCol: elem}) header = header + str(readable_elem) return header def set_extra_filters(self, filters): self.__view_manager.set_filters(filters) self.update_filters() def set_extra_sorters(self, sorters): self.__view_manager.set_extra_sorters(sorters) def get_filters_name(self): return self.__filters_manager.get_filters_name() def get_view(self): return self.__view_manager.get_view() def group(self, mylist, groups): #if not groups: return mylist def foo(mydict, row): key = tuple(map(lambda x: row[x], groups)) if not key in mydict.keys(): mydict[key] = [] mydict[key].append(row) return mydict return reduce(foo, mylist, {})
def edit_view(): link_name = request.args.get('link_name', '') datasource = request.args.get('datasource', '') form = ViewForm(csrf_enabled=True) view_manager = ViewManager() if request.method=='GET': # Store the link_name parameter to use when we will be in post request session['link_name'] = link_name # We want to edit an existing view or create a new one? if link_name: if view_manager.set_view(link_name): view = view_manager.get_view() form = ViewForm(csrf_enabled=True, obj=view) form.populate_obj(view) form.set_columns_choices(view_manager.get_columns_choices()) form.set_links_choices(view_manager.get_links_choices()) columns = view_manager.get_columns() groupers = view_manager.get_groupers() form.set_columns(columns) if groupers: form.set_groupers(groupers) add_default_groupers(form) sorters = view_manager.get_sorters() if sorters: form.set_sorters(sorters) add_default_sorters(form) filters = view_manager.get_filters() #form.populate_obj(filters) else: flash(_(u'View') + ' \'' + link_name + '\' ' + _(u'doesn\'t exist'), 'error') return redirect('/view') else: if not datasource: datasource = 'hosts' view_manager.set_view_dummy(datasource) view = view_manager.get_view() form = ViewForm(csrf_enabled=True, obj=view) form.populate_obj(view) form.columns.append_entry() add_default_sorters(form) add_default_groupers(form) form.set_columns_choices(view_manager.get_columns_choices(), update=True) form.set_links_choices(view_manager.get_links_choices(), update=True) elif request.method=='POST': link_name = session['link_name'] if link_name: saved_view = view_manager.set_view(link_name) form = ViewForm(csrf_enabled=True, obj=saved_view) #form.populate_obj(saved_view) else: view_manager.set_view_dummy(datasource) form.set_links_choices(view_manager.get_links_choices(), update=True) if form.validate_on_submit(): if not link_name: view = form.get_view() view_manager.add_view(view) link_name = view.link_name saved_view = view_manager.set_view(link_name) view_manager.add_columns(form.get_columns(view.id)) view_manager.add_sorters(form.get_sorters(view.id)) view_manager.add_groupers(form.get_groupers(view.id)) view_manager.add_filters(form.get_filters()) db_session.commit() else: view = view_manager.set_view(link_name) view_manager.update_view(form.get_view()) view_manager.update_filters(form.get_filters()) view_manager.add_columns(form.get_columns(view.id), delete_before=True) view_manager.add_groupers(form.get_groupers(view.id), delete_before=True) view_manager.add_sorters(form.get_sorters(view.id), delete_before=True) flash(_(u'View') + ' \'' + view_manager.get_view().title + '\' ' + _(u'saved successfully!'), 'success') return redirect('/view?link_name='+ view_manager.get_view().link_name) filter_display = view_manager.get_filter_display(form.filters) return snapins.render_sidebar_template('views/edit_view.html', link_name=link_name, form=form, filter_display=filter_display)
def view(): link_name = request.args.get('link_name', '') if link_name: form = ViewFiltersForm() else: form = ViewForm() if request.method == 'GET': data_rows_manager = DataRowsManager() view_manager = ViewManager() if link_name: view_manager.set_view(link_name) if data_rows_manager.set_view(link_name): extra_filter = False # Get filters parameters if any filters_name = data_rows_manager.get_filters_name() filters_url_values = {} url = urlparse.urlparse(request.url) query_param = urlparse.parse_qs(url.query) query_param = dict([(k, v[0]) for k, v in query_param.items()]) for name in filters_name: arg = query_param.get(name, '') if arg: # TODO: find a better way to check if it is a dictionnary if '{' in arg: filters_url_values[name] = dict( ast.literal_eval(arg)) else: filters_url_values[name] = arg if filters_url_values: data_rows_manager.set_extra_filters(filters_url_values) arg = query_param.get('sort_by', '') if arg: sort_by = dict(ast.literal_eval(arg)) data_rows_manager.set_extra_sorters(sort_by) view = data_rows_manager.get_view() viewFilters = view_manager.get_filters() form = ViewFiltersForm(obj=viewFilters) form.populate_obj(viewFilters) filter_display = view_manager.get_filter_display(form) return snapins.render_sidebar_template( 'views/view.html', controller=globals(), form=form, data_rows_manager=data_rows_manager, extra_filter=extra_filter, filter_display=filter_display) else: flash( _(u'View') + ' \'' + link_name + '\' ' + _(u'doesn\'t exist'), 'error') return redirect('/view') else: views = View.query.all() return snapins.render_sidebar_template('views/view.html', views=views, form=form) return snapins.render_sidebar_template('views/view.html', link_name=link_name) elif request.method == 'POST': if link_name: viewFilters = form.get_filters() return redirect(add_to_cur_url(request, viewFilters.get_filters())) else: datasource = form.datasource.data return redirect('/edit_view?datasource=' + datasource)