This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
publik-bi/wcs_olap/feeder.py

119 lines
4.0 KiB
Python

from utils import Whatever
import psycopg2
class WcsOlapFeeder(object):
def __init__(self, api, dsn, schema, logger=None):
self.api = api
self.logger = logger or Whatever()
self.schema = schema
self.connection = psycopg2.connect(dsn)
self.cur = self.connection.cursor()
self.formdefs = api.get_formdefs()
self.roles = api.roles
@property
def default_ctx(self):
return {
'schema': self.schema,
'role_table': 'role',
'channel_table': 'channel',
}
def ex(self, query, ctx=None, vars=None):
ctx = ctx or {}
ctx.update(self.default_ctx)
self.cur.execute(query.format(**(ctx or {})), vars=vars)
def exmany(self, query, varslist, ctx=None):
ctx = ctx or {}
ctx.update(self.default_ctx)
self.cur.executemany(query.format(**(ctx or {})), varslist)
def do_schema(self):
self.ex('SET search_path = public')
self.ex('DROP SCHEMA {schema} IF EXISTS')
self.ex('CREATE SCHEMA {schema} IF EXISTS')
self.ex('SET search_path = {schema},public')
channels = [
[1, 'web', u'web']
[2, 'mail', u'courrier'],
[3, 'phone', u'téléphone'],
[4, 'counter', u'guichet'],
]
channel_to_id = dict((c[1], c[0]) for c in channels)
id_to_channel = dict((c[0], c[1]) for c in channels)
def do_base_table(self):
self.ex('CREATE TABLE {channel_table} (id serial PRIMARY KEY, label varchar)')
self.exmany('INSERT INTO {channel_table} (id, label) VALUES (%s, %s)',
[[c[0], c[2]] for c in self.channels])
self.ex('CREATE TABLE {role_table} (id serial PRIMARY KEY, label varchar)')
self.exmany('INSERT INTO {role_table} (label) VALUES (%s) RETURNING (id)',
[[role.name] for role in self.roles])
self.roles_mapping = []
for row, role in zip(self.cur.fetchall(), self.roles):
self.roles_mappin[role.id] = row[0]
def feed(self):
self.do_schema()
self.do_base_table()
for formdef in self.api.get_formdefs():
self.feed_formdef(formdef)
class WcsFormdefFeeder(object):
def __init__(self, olap_feeder, formdef):
self.olap_feeder = olap_feeder
self.formdef = formdef
self.status_mapping = {}
@property
def table_name(self):
return 'formdata_%s' % self.formdef.slug.replace('-', '_')
@property
def status_table_name(self):
return 'formdata_status_%s' % self.formdef.slug.replace('-', '_')
@property
def default_ctx(self):
return {
'table_name': self.table_name,
'status_table_name': self.status_table_name,
}
def __getattr__(self, name):
return getattr(self.olap_feeder, name)
def ex(self, query, ctx=None, vars=None):
ctx = ctx or {}
ctx.update(self.default_ctx)
self.olap_feeder.ex(query, ctx=ctx, vars=vars)
def formdef_exmany(self, formdef, statement, varslist, ctx=None):
ctx = ctx or {}
ctx.update(self.default_ctx)
self.olap_feeder.exmany(statement, varslist, ctx=ctx)
def do_statuses(self):
self.ex('CREATE TABLE {status_table_name} (id serial PRIMARY KEY, '
'submission_backoffice label varchar)')
statuses = self.formdef.schema.workflow['statuses'].items()
labels = [[defn['name']] for status, defn in statuses]
self.exmany('INSERT INTO {status_mapping} (label) VALUES (%s) RETURNING (id)',
varslist=labels)
for status_sql_id, (status_id, defn) in zip(self.cur.fetchall(), statuses):
self.status_mapping['wf-%s' % status_id] = status_sql_id
def do_data_table(self):
self.ex('CREATE TABLE {table_name} (id serial PRIMARY KEY, '
'receipt_date date, '
'status integer REFERENCES {status_table_name} (id))')
def feed(self):
self.logger.info('feed formdef %s', self.formdef.slug)
self.do_statuses()
self.do_data_table()