1from chart.events.db import find_events, count_events
  2from chart.events.eventclass import EventClass
  3from chart.db.model.group import GroupInfo
  4from chart.db.model.field import FieldInfo
  5from chart.db.model.table import TableInfo
  6from chart.db.model.table import TableStorage
  7from chart.project import SID
  8
  9def build_table_tree(table_name, sid_name=None):
 10    """Build a table tree."""
 11
 12    table_info = TableInfo(name=table_name, sid=SID(sid_name))
 13    res = []
 14
 15    for value in table_info.grouped_fields.fields.values():
 16        if isinstance(value, GroupInfo):
 17            res.append({"label": value.name,
 18                        "children": unfold_group(res, table_info.name, value),
 19                        "data": table_name + "_" + value.name,
 20                        "leaf": False,
 21                        "expandedIcon": "pi pi-folder-open",
 22                        "collapsedIcon": "pi pi-folder"})
 23
 24        elif isinstance(value, FieldInfo):
 25            if table_info.description is None:
 26                table_desc = table_name
 27
 28            else:
 29                table_desc = table_info.description
 30
 31            if value.description is None:
 32                field_desc = value.name
 33
 34            else:
 35                field_desc = value.description
 36
 37            res.append({"label": value.name,
 38                        "description": field_desc,
 39                        "data": table_name + "." + value.name,
 40                        "has_choices": value.choices != None,
 41                        "leaf": True,
 42                        "icon": "pi pi-chart-line"
 43                        })
 44
 45    return {"tree": res}
 46
 47
 48def build_events_tree(event_classes=EventClass.all()):
 49    """Build events tree."""
 50    events = []
 51    for cls in event_classes:
 52        desc = cls.description
 53        if desc is None:
 54            desc = "Event"
 55
 56        props = cls.instance_properties
 57
 58        children = []
 59        for k, v in props.items():
 60            # show plottable properties only (no strings)
 61            if v["type"] not in ("int", "uint", "float", "duration"):
 62                continue
 63            children.append({
 64                    "label": k,
 65                    "description": v.get("description", "event property"),
 66                    "data": "EVENT" + "." + cls.name + "." + k,
 67                    "leaf": True,
 68                    "icon": "pi pi-chart-line"})
 69
 70        if len(children) > 0:
 71            events.append({"label": cls.name,
 72                           "description": desc,
 73                           "data": "EVENT" + "." + cls.name,
 74                           "children": children,
 75                           "leaf": False,
 76                           "expandedIcon": "pi pi-chart-bar",
 77                           "collapsedIcon": "pi pi-chart-bar"})
 78
 79        else:
 80            events.append({"label": cls.name,
 81                           "description": desc,
 82                           "data": "EVENT" + "." + cls.name,
 83                           "leaf": True,
 84                           "icon": "pi pi-chart-bar"
 85                           })
 86
 87    return {"tree": events}
 88
 89def build_main_tree(sid_name=None):
 90    """Build a tree structure giving the top level
 91    tables for the hierarchical data tree."""
 92    tables_tree = []
 93
 94    tables_tree.append({"label": "EVENTS",
 95                        "description": "Events with their properties",
 96                        "leaf": False,  # Main tree is only the top level nodes
 97                        "data": "EVENTS",
 98                        "expandedIcon": "pi pi-folder-open",
 99                        "collapsedIcon": "pi pi-folder"
100                        })
101
102    for table in TableInfo.all(visible=True)[0][sid_name]:  # exclude tables where <visible>false</visible>
103        tables_tree.append({"data": table.name,
104                            "label": table.name,
105                            "leaf": False,
106                            "description": table.description,
107                            "expandedIcon": "pi pi-folder-open",
108                            "collapsedIcon": "pi pi-folder"})
109
110    return {"tree": tables_tree}
111
112def unfold_group(res, table_name, group):
113    """Recurse into field groups while building a table tree."""
114
115    res = []
116    for value in group.fields.values():
117        if isinstance(value, GroupInfo):
118            res.append({"label": value.name,
119                        "children": unfold_group(res, table_name, value),
120                        "data": table_name + "_" + value.name,
121                        "leaf": False,
122                        "expandedIcon": "pi pi-folder-open",
123                        "collapsedIcon": "pi pi-folder"})
124
125        elif isinstance(value, FieldInfo):
126            res.append({"label": value.name,
127                        "description": value.description,
128                        "data": table_name + "." + value.name,
129                        "has_choices": value.choices != None,
130                        "leaf": True,
131                        "icon": "pi pi-chart-line"})
132
133    return res