model_report.py 11.8 KB
Newer Older
1 2 3 4
# -*- coding: utf-8 -*-
"""Definitions of the report tables

"""
5
import numpy as np
6 7


LE GAC Renaud's avatar
LE GAC Renaud committed
8 9
from callbacks import ON_CREATE_LISTS2, ON_UPDATE_LISTS2
from gluon import current, IS_IN_SET
10
from gluon.tools import PluginManager
11
from pydal import Field
12 13


14 15 16 17 18 19
AGGREGATE_FUNCS = {
    "max": np.max,
    "mean": np.min,
    "median": np.median,
    "min": np.min,
    "sem": np.max,
20
    "size": np.size,
21 22 23
    "sum": np.sum,
    "std": np.std,
    "var": np.var}
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
DEF_COLUMNS_LISTS = \
"""[{
    "xtype": "rownumberer"
}, {
    "dbfield": "?.?",
    "flex": 1,
    "text": "?",
    "xtype": "gridcolumn"
}]"""

DEF_COLUMNS_METRIC1D = \
"""[{
    "aggregate": "?",
    "align": "right",
    "dbfield": "?.?",
    "format": "0.0",
    "text": "?",
    "xtype": "numbercolumn"
}]"""

DEF_FEATURES = \
"""[{
    "ftype": "groupingsummary",
    "groupHeaderTpl": "{name}",
    "startCollapsed": false
}, {
    "ftype": "summary"
}]"""

54 55 56 57 58 59 60 61 62 63 64 65
DEF_GRAPH = {
    "colormap": "Pastel1",
    "kind": "",
    "grid": True,
    "linewidth": 0,
    "legend": True,
    "loglog": False,
    "logx": False,
    "logy": False,
    "rot": 0,
    "stacked": True,
    "width": 0.8,
66
    "ylim": ""}
67

68 69 70 71 72 73
DEF_PLOT = \
"""{
    "kind": "?",
    "stacked": false
}"""

74
DEF_SUMMARY = {
75 76
    "functions": "",
    "labels": ""}
77

78 79 80 81 82 83 84 85 86 87 88 89 90
TP_AGG = \
    "The aggregation function applies on the metric field. " \
    "It is computed on the subset of row having a given values for " \
    "the group_field x and y."

TP_COLUMNS = "Configure the column of the grid displayed in the view."

TP_CONDITIONS = \
    "Database query to select history records:<br> " \
    "(db.events.event == 'People') & (db.history.data.like('%cdd%'))<br>"

TP_GROUP_FIELD_LISTS = \
    "Row are grouped according to the value of that field. " \
91
    "It can be any field defined in the source."
92 93 94

TP_GROUP_FIELD_METRICS = \
    "Metric are computed for each value of that field. " \
95
    "It can be any field defined in the source"
96 97 98 99

TP_FEATURES = "Summary value can be computed for columns."

TP_SORTERS = \
LE GAC Renaud's avatar
LE GAC Renaud committed
100
    "Entries are sorted according to the value of these fields. "\
101 102
    "It can be any field defined in the source. "\
    "Descending order is obtained by using the '~field' construct."
103

104 105 106
TP_SUMMARY_X = "To sum, for example, the content of each row."
TP_SUMMARY_Y = "To sum, for example, the content of each column."

107

108
class Report(object):
LE GAC Renaud's avatar
LE GAC Renaud committed
109 110 111 112 113 114
    """Create the tables ``Lists`` and ``metrics2D`` for the reporting

    Note:
        The table ``graphs``, ``lists``, ``metrics1d`` and ``metrics2d``
        are obsolete. They are kept to ease the transition to the new
        tables ``lists2`` and ``metrics2D2`` introduce in version 0.7.0.
115

116
    """
117 118 119 120 121 122 123 124 125
    @staticmethod
    def define_tables(db, T):
        """Define tables for the reporting.

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator

        """
126

127 128
        Report.graphs(db, T)
        Report.lists(db, T)
LE GAC Renaud's avatar
LE GAC Renaud committed
129
        Report.lists2(db, T)
130 131
        Report.metrics1d(db, T)
        Report.metrics2d(db, T)
132
        Report.metrics2d2(db, T)
133 134 135 136 137 138 139 140 141 142 143 144 145

    @staticmethod
    def graphs(db, T):
        """graphs table

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator

        Returns:
            pyDAL.Table

        """
146
        def_plot = (None if db._migrate else DEF_PLOT)
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

        table = db.define_table(
            "graphs",
            Field("name", "string", length=255, notnull=True, unique=True),
            Field("title", "string", length=255),
            Field("report_type", "string", length=255, notnull=True),
            Field("report_name", "string", length=255, notnull=True),
            Field("plot", "text", notnull=True, default=def_plot),
            Field("definition", "text"),
            migrate="graphs.table")

        return table

    @staticmethod
    def lists(db, T):
        """lists table

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator

        Returns:
            pyDAL.Table

        """
172 173
        def_columns = (None if db._migrate else DEF_COLUMNS_LISTS)
        def_features = (None if db._migrate else DEF_FEATURES)
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

        table = db.define_table(
            "lists",
            Field("name", "string", length=255, notnull=True, unique=True),
            Field("title", "string", length=255),
            Field("conditions", "text", comment=T(TP_CONDITIONS)),

            Field("group_field",
                  "string",
                  length=255,
                  comment=T(TP_GROUP_FIELD_LISTS)),

            Field("sorters", "list:string", comment=TP_SORTERS),

            Field("columns",
                  "text",
                  default=def_columns,
                  comment=T(TP_COLUMNS),
                  notnull=True),

            Field("features",
                  "text",
                  default=def_features,
                  comment=T(TP_FEATURES)),

            Field("definition", "text"),
            migrate="lists.table")

        return table

LE GAC Renaud's avatar
LE GAC Renaud committed
204 205 206 207 208 209 210 211 212 213 214 215
    @staticmethod
    def lists2(db, T):
        """lists2 table

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator

        Returns:
            pyDAL.Table

        """
216
        migrate = current.globalenv["MIGRATE"]
LE GAC Renaud's avatar
LE GAC Renaud committed
217 218 219 220 221 222 223

        table = db.define_table(
            "lists2",
            Field("name", "string", length=255, notnull=True, unique=True),
            Field("title", "string", length=255),
            Field("definition", "text"),
            Field("source", "string", length=255, notnull=True),
224 225 226

            Field("id_events",
                  "reference events",
227
                  notnull=True,
228 229
                  label="Event"),

230 231
            Field("eval", "text"),
            Field("query", "text"),
LE GAC Renaud's avatar
LE GAC Renaud committed
232 233 234 235 236 237 238 239

            Field("group_field",
                  "string",
                  length=255,
                  comment=T(TP_GROUP_FIELD_LISTS)),

            Field("sorters", "list:string", comment=TP_SORTERS),

240 241 242
            # NOTE
            #    - notnull when creating the database
            #    - null later on in order to run callback ON_CREATE_LISTS, ...
243
            Field("columns", "text",
244
                  notnull=migrate,
245
                  comment=T(TP_COLUMNS)),
LE GAC Renaud's avatar
LE GAC Renaud committed
246

247 248 249 250 251 252 253 254 255
            Field("summary_group",
                  "boolean",
                  default=True,
                  label=T("Summary row per group")),

            Field("summary_all",
                  "boolean",
                  default=True,
                  label=T("Summary row")),
LE GAC Renaud's avatar
LE GAC Renaud committed
256 257 258

            migrate="lists2.table")

259 260 261 262
        sources = PluginManager("event").event.sources.keys()
        sources.sort()
        db.lists2.source.requires = IS_IN_SET(sources)

263 264 265
        db.lists2._before_insert.append(ON_CREATE_LISTS2)
        db.lists2._before_update.append(ON_UPDATE_LISTS2)

LE GAC Renaud's avatar
LE GAC Renaud committed
266 267
        return table

268 269 270 271 272 273 274 275 276 277 278 279
    @staticmethod
    def metrics1d(db, T):
        """metrics1d table

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator

        Returns:
            pyDAL.Table

        """
280
        def_columns = (None if db._migrate else DEF_COLUMNS_METRIC1D)
281

LE GAC Renaud's avatar
LE GAC Renaud committed
282
        db.define_table(
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
            "metrics1d",
            Field("name", "string", length=255, notnull=True, unique=True),
            Field("title", "string", length=255),
            Field("conditions", "text", comment=T(TP_CONDITIONS)),

            Field("group_field",
                  "string",
                  length=255,
                  comment=T(TP_GROUP_FIELD_METRICS),
                  notnull=True),

            Field("columns",
                  "text",
                  default=def_columns,
                  comment=T(TP_COLUMNS),
                  notnull=True),

            Field("definition", "text"),
            migrate="metrics1d.table")

    @staticmethod
    def metrics2d(db, T):
        """metrics2d table

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator

        Returns:
            pyDAL.Table

        """

        table = db.define_table(
            "metrics2d",
            Field("name", "string", length=255, notnull=True, unique=True),
            Field("title", "string", length=255),
            Field("conditions", "text", comment=T(TP_CONDITIONS)),

            Field("group_field_x",
                  "string",
                  length=255,
                  notnull=True,
                  comment=T(TP_GROUP_FIELD_METRICS)),

            Field("group_field_y",
                  "string",
                  length=255,
                  notnull=True,
                  comment=T(TP_GROUP_FIELD_METRICS)),

            Field("metric_field_z",
                  "string",
                  length=255,
                  notnull=True,
                  comment=T(TP_GROUP_FIELD_METRICS)),

            Field("aggregation_z",
                  "string",
                  length=255,
                  notnull=True,
                  comment=T(TP_AGG)),

            Field("definition", "text"),
            migrate="metrics2d.table")

        return table

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    @staticmethod
    def metrics2d2(db, T):
        """metrics2d2 table

        Args:
            db (pyDAL.DAL): database connection
            T (gluon.languages.translator): language translator

        Returns:
            pyDAL.Table

        """

        table = db.define_table(
            "metrics2d2",
            Field("name", "string", length=255, notnull=True, unique=True),
            Field("title", "string", length=255),
            Field("definition", "text"),
            Field("source", "string", length=255, notnull=True),

            Field("id_events",
                  "reference events",
373 374
                  label="Event",
                  notnull=True),
375 376 377 378 379 380

            Field("eval", "text"),
            Field("query", "text"),

            Field("group_field_x",
                  "string",
381
                  comment=T(TP_GROUP_FIELD_METRICS),
382 383
                  length=255,
                  notnull=True,
LE GAC Renaud's avatar
LE GAC Renaud committed
384
                  label="Field (x)"),
385 386 387

            Field("group_field_y",
                  "string",
388
                  comment=T(TP_GROUP_FIELD_METRICS),
389 390
                  length=255,
                  notnull=True,
LE GAC Renaud's avatar
LE GAC Renaud committed
391
                  label="Field (y)"),
392 393 394 395

            Field("metric_field_z",
                  "string",
                  comment=T(TP_GROUP_FIELD_METRICS),
LE GAC Renaud's avatar
LE GAC Renaud committed
396
                  label="Field (z)",
397 398
                  length=255,
                  notnull=True),
399 400 401

            Field("aggregate_func_z",
                  "string",
402
                  comment=T(TP_AGG),
403
                  default="sum",
404
                  label="Aggregate (z)",
405
                  length=255,
406
                  notnull=True),
407

408
            Field("summary_x", "json", default=DEF_SUMMARY, label="Per row"),
LE GAC Renaud's avatar
LE GAC Renaud committed
409 410 411 412 413 414

            Field("summary_y",
                  "json",
                  default=DEF_SUMMARY,
                  label="Per column"),

415
            Field("graph", "json", default=DEF_GRAPH),
416 417 418 419 420

            migrate="metrics2d2.table")

        sources = PluginManager("event").event.sources.keys()
        sources.sort()
421

422
        table.source.requires = IS_IN_SET(sources)
423

424 425 426
        funcs = AGGREGATE_FUNCS.keys()
        funcs.sort()

427 428 429 430 431 432
        table.aggregate_func_z.requires = IS_IN_SET(funcs)

        # NOTE: we have to remove the json validator to work !!
        table.graph.requires = None
        table.summary_x.requires = None
        table.summary_y.requires = None
433 434 435

        return table

436 437 438 439 440 441 442 443 444 445
    @staticmethod
    def tables():
        """Tool returning the list of table names.

        Returns:
            list

        """
        lst = ["graphs",
               "lists",
446
               "lists2",
447
               "metrics1d",
448 449
               "metrics2d"
               "metrics2d2"]
450 451

        return lst