def plainview(self, head, data): """ Represent data in DAS plain view for queries with filters. """ dasquery = head['dasquery'] fields = dasquery.mongo_query.get('fields', []) filters = [f for f in dasquery.filters if f != 'unique' and \ f.find('=') == -1 and f.find('<') == -1 and \ f.find('>') == -1] results = "" status = head.get('status', None) if status == 'fail': reason = head.get('reason', '') if reason: results += 'ERROR: %s' % reason lookup_items = [i for i in fields if i not in das_record_keys()] for row in data: if filters: for flt in filters: try: for obj in DotDict(row).get_values(flt): results += str(obj) + ' ' except: pass results += '\n' else: for item in lookup_items: if item != lookup_items[0]: results += ', ' try: systems = row['das']['system'] mapkey = self.dasmapping.find_mapkey(systems[0], item) if not mapkey: mapkey = '%s.name' % item key, att = mapkey.split('.') if key in row: val = row[key] if isinstance(val, dict): results += val.get(att, '') elif isinstance(val, list): results += \ ' '.join(set([str(i.get(att, '')) for i in val])) except: pass results += '\n' # use DAS sort_rows function instead of python set, since we need to # preserve the order of records in final output rows = [r for r in results.split('\n') if r] results = '\n'.join([r for r in sort_rows(rows)]) return results
def test_sort_rows(self): "Test sort_rows function" rows = [5, 1, 1, 1, 2, 2, 3, 3, 3, 4] expect = [5, 1, 2, 3, 4] result = [r for r in sort_rows(rows)] self.assertEqual(result, expect)