<s> from __future__ import absolute_import <EOL> from ... server . logging import RequestLogHandler , REQUEST_LOG_ITEMS <EOL> from sqlalchemy import create_engine , Table , MetaData , Column <EOL> from sqlalchemy import Integer , Sequence , DateTime , String , Float <EOL> from sqlalchemy . exc import NoSuchTableError <EOL> from ... browser import Drilldown <EOL> from . store import create_sqlalchemy_engine <EOL> import logging <EOL> class SQLRequestLogHandler ( RequestLogHandler ) : <EOL> def __init__ ( self , url = None , table = None , dimensions_table = None , ** options ) : <EOL> self . url = url <EOL> self . engine = create_sqlalchemy_engine ( url , options ) <EOL> metadata = MetaData ( bind = self . engine ) <EOL> logging . getLogger ( '<STR_LIT>' ) . setLevel ( "<STR_LIT>" ) <EOL> logging . getLogger ( '<STR_LIT>' ) . setLevel ( "<STR_LIT>" ) <EOL> try : <EOL> self . table = Table ( table , metadata , autoload = True ) <EOL> except NoSuchTableError : <EOL> columns = [ <EOL> Column ( '<STR_LIT:id>' , Integer , Sequence ( table + "<STR_LIT>" ) , <EOL> primary_key = True ) , <EOL> Column ( '<STR_LIT>' , DateTime ) , <EOL> Column ( '<STR_LIT>' , String ( <NUM_LIT:50> ) ) , <EOL> Column ( '<STR_LIT>' , String ( <NUM_LIT> ) ) , <EOL> Column ( '<STR_LIT>' , String ( <NUM_LIT> ) ) , <EOL> Column ( '<STR_LIT>' , String ( <NUM_LIT> ) ) , <EOL> Column ( '<STR_LIT>' , Float ) , <EOL> Column ( '<STR_LIT>' , String ( <NUM_LIT> ) ) , <EOL> Column ( '<STR_LIT>' , String ( <NUM_LIT> ) ) , <EOL> Column ( '<STR_LIT>' , String ( <NUM_LIT> ) ) , <EOL> Column ( '<STR_LIT>' , Integer ) , <EOL> Column ( '<STR_LIT>' , Integer ) , <EOL> Column ( '<STR_LIT>' , String ( <NUM_LIT:50> ) ) , <EOL> Column ( '<STR_LIT>' , String ( <NUM_LIT:50> ) ) , <EOL> ] <EOL> self . table = Table ( table , metadata , extend_existing = True , * columns ) <EOL> self . table . create ( ) <EOL> if dimensions_table : <EOL> try : <EOL> self . dims_table = Table ( dimensions_table , metadata , autoload = True ) <EOL> except NoSuchTableError : <EOL> columns = [ <EOL> Column ( '<STR_LIT:id>' , Integer , Sequence ( table + "<STR_LIT>" ) , <EOL> primary_key = True ) , <EOL> Column ( '<STR_LIT>' , Integer ) , <EOL> Column ( '<STR_LIT>' , String ( <NUM_LIT> ) ) , <EOL> Column ( '<STR_LIT>' , String ( <NUM_LIT> ) ) , <EOL> Column ( '<STR_LIT>' , String ( <NUM_LIT> ) ) , <EOL> Column ( '<STR_LIT>' , String ( <NUM_LIT:50> ) ) , <EOL> Column ( '<STR_LIT:value>' , String ( <NUM_LIT> ) ) , <EOL> ] <EOL> self . dims_table = Table ( dimensions_table , metadata , extend_existing = True , * columns ) <EOL> self . dims_table . create ( ) <EOL> else : <EOL> self . dims_table = None <EOL> def write_record ( self , cube , cell , record ) : <EOL> drilldown = record . get ( "<STR_LIT>" ) <EOL> if drilldown is not None : <EOL> if cell : <EOL> drilldown = Drilldown ( drilldown , cell ) <EOL> record [ "<STR_LIT>" ] = str ( drilldown ) <EOL> else : <EOL> drilldown = [ ] <EOL> record [ "<STR_LIT>" ] = None <EOL> connection = self . engine . connect ( ) <EOL> trans = connection . begin ( ) <EOL> insert = self . table . insert ( ) . values ( record ) <EOL> result = connection . execute ( insert ) <EOL> query_id = result . inserted_primary_key [ <NUM_LIT:0> ] <EOL> if self . dims_table is not None : <EOL> uses = [ ] <EOL> cuts = cell . cuts if cell else [ ] <EOL> cuts = cuts or [ ] <EOL> for cut in cuts : <EOL> dim = cube . dimension ( cut . dimension ) <EOL> depth = cut . level_depth ( ) <EOL> if depth : <EOL> level = dim . hierarchy ( cut . hierarchy ) [ depth - <NUM_LIT:1> ] <EOL> level_name = str ( level ) <EOL> else : <EOL> level_name = None <EOL> use = { <EOL> "<STR_LIT>" : query_id , <EOL> "<STR_LIT>" : str ( dim ) , <EOL> "<STR_LIT>" : str ( cut . hierarchy ) , <EOL> "<STR_LIT>" : str ( level_name ) , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:value>" : str ( cut ) <EOL> } <EOL> uses . append ( use ) <EOL> if drilldown : <EOL> for item in drilldown : <EOL> ( dim , hier , levels ) = item [ <NUM_LIT:0> : <NUM_LIT:3> ] <EOL> if levels : <EOL> level = str ( levels [ - <NUM_LIT:1> ] ) <EOL> else : <EOL> level = None <EOL> use = { <EOL> "<STR_LIT>" : query_id , <EOL> "<STR_LIT>" : str ( dim ) , <EOL> "<STR_LIT>" : str ( hier ) , <EOL> "<STR_LIT>" : str ( level ) , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:value>" : None <EOL> } <EOL> uses . append ( use ) <EOL> if uses : <EOL> insert = self . dims_table . insert ( ) . values ( uses ) <EOL> connection . execute ( insert ) <EOL> trans . commit ( ) <EOL> connection . close ( ) </s>
