summaryrefslogtreecommitdiffstats
path: root/bijoe/schemas.py
blob: e4a2fbab039b4d5df05f8097cea66adcbf4b99a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
172
173
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# -*- coding: utf-8 -*-
#
# bijoe - BI dashboard
# Copyright (C) 2015  Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import datetime
import decimal
import collections

from .relative_time import RelativeDate

Point = collections.namedtuple('Point', ['x', 'y'])

TYPE_MAP = {
    'duration': datetime.timedelta,
    'date': datetime.date,
    'integer': int,
    'decimal': decimal.Decimal,
    'percent': float,
    'point': Point,
}


class SchemaError(Exception):
    pass


def type_cast(t):
    if t not in TYPE_MAP:
        raise SchemaError('invalid type: %r' % t)
    return t


class Base(object):
    __types__ = {}

    def __init__(self, **kwargs):
        for k, v in kwargs.iteritems():
            if k in self.__types__ and self.__types__ == 'str':
                v = str(v)
            setattr(self, k, v)

    @classmethod
    def slots(cls):
        s = set()
        for cls in reversed(cls.__mro__):
            if hasattr(cls, '__slots__'):
                s.update(cls.__slots__)
        return s

    @classmethod
    def types(cls):
        d = {}
        for cls in reversed(cls.__mro__):
            if hasattr(cls, '__types__'):
                d.update(cls.__types__)
        return d

    @classmethod
    def from_json(cls, d):
        assert hasattr(d, 'keys')
        slots = cls.slots()
        assert set(d.keys()) <= set(slots), \
            'given keys %r does not match %s.__slots__: %r' % (d.keys(), cls.__name__, slots)
        types = cls.types()

        kwargs = {}
        for key in slots:
            assert key in d or hasattr(cls, key), \
                '%s.%s is is a mandatory attribute' % (cls.__name__, key)
            if not key in d:
                continue
            value = d[key]
            if key in types:
                kls = types[key]
                if isinstance(kls, list):
                    kls = kls[0]
                    if not isinstance(value, list):
                        value = [value]
                    if hasattr(kls, 'from_json'):
                        value = [kls.from_json(v) for v in value]
                    else:
                        value = [kls(v) for v in value]
                elif hasattr(kls, 'from_json'):
                    value = kls.from_json(value)
                else:
                    value = kls(value)
            kwargs[key] = value
        return cls(**kwargs)

    def to_json(self):
        d = {}
        types = self.types()
        for attr in self.slots():
            try:
                v = getattr(self, attr)
            except AttributeError:
                pass
            else:
                if attr in types and hasattr(types[attr], 'to_json'):
                    v = types[attr].to_json(v)
                if isinstance(v, list):
                    v = [x.to_json() if hasattr(x, 'to_json') else x for x in v]
                d[attr] = v
        return d

    def __repr__(self):
        kwargs = ['%s=%r' % (key, getattr(self, key)) for key in self.slots() if hasattr(self, key)]
        return '<%s %s>' % (self.__class__.__name__, ' '.join(kwargs))


class Measure(Base):
    __slots__ = ['name', 'label', 'type', 'expression']
    __types__ = {
        'name': str,
        'label': unicode,
        'type': type_cast,
        'expression': str,
    }


class Dimension(Base):
    __slots__ = ['name', 'label', 'type', 'join', 'value', 'value_label',
                 'order_by', 'group_by', 'filter_in_join', 'filter', 'filter_value',
                 'filter_needs_join', 'filter_expression']
    __types__ = {
        'name': str,
        'label': unicode,
        'type': str,
        'join': [str],
        'value': str,
        'value_label': str,
        'order_by': [str],
        'group_by': str,
        'filter': bool,
        'members_query': str,
        # do filtering need joins ?
        'filter_in_join': bool,
        'filter_value': str,
        'filter_needs_join': bool,
    }

    label = None
    value_label = None
    order_by = None
    group_by = None
    join = None
    filter = True
    filter_in_join = False
    filter_value = None
    filter_expression = None
    filter_needs_join = True
    members_query = None

    @property
    def dimensions(self):
        if self.type == 'date':
            filter_value = self.filter_value or self.value
            return [
                self,
                Dimension(
                    label=u'année (%s)' % self.label,

                    name=self.name + '__year',
                    type='integer',
                    join=self.join,
                    filter_value='EXTRACT(year from %s)::integer' % filter_value,
                    filter_in_join=self.filter_in_join,
                    value='EXTRACT(year from %s)::integer' % self.value,
                    filter=False),
                Dimension(
                    label=u'mois (%s)' % self.label,
                    name=self.name + '__month',
                    type='integer',
                    filter_value='EXTRACT(month from %s)' % filter_value,
                    filter_in_join=self.filter_in_join,
                    join=self.join,
                    value='EXTRACT(month from %s)' % self.value,
                    value_label='to_char(date_trunc(\'month\', %s), \'TMmonth\')' % self.value,
                    group_by='EXTRACT(month from %s), '
                             'to_char(date_trunc(\'month\', %s), \'TMmonth\')'
                             % (self.value, self.value),
                    filter=False),
                Dimension(
                    label=u'jour de la semaine (%s)' % self.label,
                    name=self.name + '__dow',
                    type='integer',
                    join=self.join,
                    filter_value='EXTRACT(dow from %s)' % filter_value,
                    filter_in_join=self.filter_in_join,
                    value='EXTRACT(dow from %s)' % self.value,
                    order_by=['(EXTRACT(dow from %s) + 6)::integer %% 7' % self.value],
                    value_label='to_char(date_trunc(\'week\', current_date)::date '
                                '+ EXTRACT(dow from %s)::integer - 1, \'TMday\')' % self.value,
                    filter=False),
                Dimension(
                    label=u'semaine (%s)' % self.label,
                    name=self.name + '__isoweek',
                    type='integer',
                    join=self.join,
                    filter_value='EXTRACT(isoyear from %s) || \'S\' || EXTRACT(week from %s)'
                          % (filter_value, filter_value),
                    filter_in_join=self.filter_in_join,
                    value='EXTRACT(isoyear from %s) || \'S\' || EXTRACT(week from %s)'
                          % (self.value, self.value),
                    group_by='EXTRACT(isoyear from %s), EXTRACT(week from %s)' % (self.value,
                                                                                  self.value),
                    order_by=['EXTRACT(isoyear from %s), EXTRACT(week from %s)' % (self.value,
                                                                                   self.value)],
                    filter=False)
            ]
        return [self]

    def build_filter(self, filter_values):
        value = self.filter_value or self.value

        if self.type == 'date':
            assert isinstance(filter_values, dict) and set(filter_values.keys()) == set(['start',
                                                                                         'end'])
            filters = []
            values = []

            def date_filter(tpl, filter_value):
                if not isinstance(filter_value, (datetime.date, datetime.datetime)):
                    filter_value = RelativeDate(filter_value)
                filters.append(tpl % (value, '%s'))
                values.append(filter_value)
            try:
                if filter_values['start']:
                    date_filter('%s >= %s', filter_values['start'])
            except IndexError:
                pass
            try:
                if filter_values['end']:
                    date_filter('%s < %s', filter_values['end'])
            except IndexError:
                pass
            return ' AND '.join(filters), values
        else:
            if not filter_values:
                return '', []
            if self.type == 'integer':
                values = map(int, filter_values)
            else:
                values = filter_values
            s = ', '.join(['%s'] * len(values))
            if self.filter_expression:
                return self.filter_expression % s, values
            return '%s IN (%s)' % (value, s), values


def join_kind(kind):
    if kind not in ('inner', 'left', 'right', 'full'):
        raise ValueError('bad join kind: %s' % kind)
    return kind


class Join(Base):
    __slots__ = ['name', 'table', 'master', 'detail', 'kind']
    __types__ = {
        'name': str,
        'table': str,
        'master': str,
        'detail': str,
        'kind': join_kind,
    }

    kind = 'right'

    @property
    def master_table(self):
        if '.' in self.master:
            return self.master.split('.', 1)[0]
        return None


class Cube(Base):
    __slots__ = ['name', 'label', 'fact_table', 'json_field', 'key', 'joins', 'dimensions',
                 'measures']
    __types__ = {
        'name': str,
        'label': unicode,
        'fact_table': str,
        'json_field': str,
        'key': str,
        'joins': [Join],
        'dimensions': [Dimension],
        'measures': [Measure],
    }

    json_field = None
    joins = ()
    dimensions = ()
    measures = ()

    def check(self):
        names = collections.Counter()
        names.update(join.name for join in self.joins)
        names.update(dimension.name for dimension in self.dimensions)
        names.update(measure.name for measure in self.measures)
        duplicates = [k for k, v in names.iteritems() if v > 1]
        if duplicates:
            raise SchemaError(
                'More than one join, dimension or measure with name(s) %s' % ', '.join(duplicates))

    @property
    def all_dimensions(self):
        for dimension in self.dimensions:
            for sub_dimension in dimension.dimensions:
                yield sub_dimension

    def get_dimension(self, name):
        for dimension in self.dimensions:
            for sub_dimension in dimension.dimensions:
                if sub_dimension.name == name:
                    return sub_dimension
        raise KeyError

    def get_join(self, name):
        for join in self.joins:
            if join.name == name:
                return join
        raise KeyError

    def get_measure(self, name):
        for measure in self.measures:
            if measure.name == name:
                return measure
        raise KeyError


class Warehouse(Base):
    __slots__ = ['name', 'label', 'pg_dsn', 'search_path', 'cubes']
    __types__ = {
        'name': str,
        'label': unicode,
        'pg_dsn': str,
        'search_path': [str],
        'cubes': [Cube],
        'search_path': [str],
    }

    def check(self):
        names = collections.Counter(cube.name for cube in self.cubes)
        duplicates = [k for k, v in names.iteritems() if v > 1]
        if duplicates:
            raise SchemaError('More than one cube with name(s) %s' % ', '.join(duplicates))

    def get_cube(self, name):
        for cube in self.cubes:
            if cube.name == name:
                return cube
        raise KeyError