Commit f6879c40 authored by mouadh's avatar mouadh

fix config file bug

parent 82c27c2d
...@@ -42,17 +42,18 @@ class MdxEngine: ...@@ -42,17 +42,18 @@ class MdxEngine:
def __init__(self, def __init__(self,
cube_name, cube_name,
client_type='excel',
cubes_path=None, cubes_path=None,
mdx_query=None, mdx_query=None,
cube_folder=CUBE_FOLDER, cube_folder=CUBE_FOLDER,
sep=';', sep=';',
fact_table_name="Facts"): fact_table_name="Facts"):
self.cube_folder = cube_folder self.cube_folder = cube_folder
self.cube = cube_name self.cube = cube_name
self.sep = sep self.sep = sep
self.facts = fact_table_name self.facts = fact_table_name
self.mdx_query = mdx_query self.mdx_query = mdx_query
if cubes_path is None: if cubes_path is None:
self.cube_path = self._get_default_cube_directory() self.cube_path = self._get_default_cube_directory()
else: else:
...@@ -60,10 +61,11 @@ class MdxEngine: ...@@ -60,10 +61,11 @@ class MdxEngine:
# to get cubes in db # to get cubes in db
self._ = self.get_cubes_names() self._ = self.get_cubes_names()
self.client = client_type
self.tables_loaded = self.load_tables() self.tables_loaded = self.load_tables()
# all measures # all measures
self.measures = self.get_measures()
self.load_star_schema_dataframe = self.get_star_schema_dataframe() self.load_star_schema_dataframe = self.get_star_schema_dataframe()
self.measures = self.get_measures()
self.tables_names = self._get_tables_name() self.tables_names = self._get_tables_name()
# default measure is the first one # default measure is the first one
self.selected_measures = [self.measures[0]] self.selected_measures = [self.measures[0]]
...@@ -128,13 +130,13 @@ class MdxEngine: ...@@ -128,13 +130,13 @@ class MdxEngine:
def load_tables(self): def load_tables(self):
""" """
Load all tables { Table name : DataFrame } of the current cube instance. Load all tables { Table name : DataFrame } of the current cube instance.
:return: dict with key as table name and DataFrame as value :return: dict with key as table name and DataFrame as value
""" """
config_file_parser = ConfigParser(self.cube_path) config_file_parser = ConfigParser(self.cube_path)
tables = {} tables = {}
if config_file_parser.config_file_exist( if config_file_parser.config_file_exist(
) and self.cube in config_file_parser.get_cubes_names(): ) and self.cube in config_file_parser.get_cubes_names() and self.client != 'web':
for cubes in config_file_parser.construct_cubes(): for cubes in config_file_parser.construct_cubes():
# TODO working with cubes.source == 'csv' # TODO working with cubes.source == 'csv'
...@@ -158,7 +160,7 @@ class MdxEngine: ...@@ -158,7 +160,7 @@ class MdxEngine:
include=[np.number]).columns if col.lower()[-2:] != 'id' include=[np.number]).columns if col.lower()[-2:] != 'id'
] ]
def get_star_schema_dataframe(self, client_type='excel'): def get_star_schema_dataframe(self):
""" """
Merge all DataFrames as star schema. Merge all DataFrames as star schema.
...@@ -169,13 +171,13 @@ class MdxEngine: ...@@ -169,13 +171,13 @@ class MdxEngine:
config_file_parser = ConfigParser(self.cube_path) config_file_parser = ConfigParser(self.cube_path)
if config_file_parser.config_file_exist( if config_file_parser.config_file_exist(
client_type self.client
) and self.cube in config_file_parser.get_cubes_names(): ) and self.cube in config_file_parser.get_cubes_names(client_type='web'):
for cubes in config_file_parser.construct_cubes(client_type): for cubes in config_file_parser.construct_cubes(self.client):
# TODO cubes.source == 'csv' # TODO cubes.source == 'csv'
if cubes.source == 'postgres': if cubes.source == 'postgres':
# TODO one config file (I will try to merge dimensions between them in web part) # TODO one config file (I will try to merge dimensions between them in web part)
if client_type == 'web': if self.client == 'web':
fusion = _construct_web_star_schema_config_file( fusion = _construct_web_star_schema_config_file(
self, cubes) self, cubes)
else: else:
...@@ -232,7 +234,7 @@ class MdxEngine: ...@@ -232,7 +234,7 @@ class MdxEngine:
FROM {sales} FROM {sales}
it returns : it returns :
[ [
['Geography','Geography','Continent'], ['Geography','Geography','Continent'],
['Geography','Geography','Continent','Europe'], ['Geography','Geography','Continent','Europe'],
...@@ -260,9 +262,9 @@ class MdxEngine: ...@@ -260,9 +262,9 @@ class MdxEngine:
tup_att.replace('All ', '').replace('[', "").replace("]", "") tup_att.replace('All ', '').replace('[', "").replace("]", "")
for tup_att in tup[0].replace('.Members', '').split('.') for tup_att in tup[0].replace('.Members', '').split('.')
] ]
for tup in re.compile(regex).findall( for tup in re.compile(regex).findall(
query.encode("utf-8")[start:stop]) query.encode("utf-8")[start:stop])
if len(tup[0].split('.')) > 1] if len(tup[0].split('.')) > 1]
# TODO temporary function # TODO temporary function
def decorticate_query(self, query): def decorticate_query(self, query):
...@@ -313,13 +315,13 @@ class MdxEngine: ...@@ -313,13 +315,13 @@ class MdxEngine:
def change_measures(tuples_on_mdx): def change_measures(tuples_on_mdx):
""" """
Set measures to which exists in the query. Set measures to which exists in the query.
:param tuples_on_mdx: list of tuples: :param tuples_on_mdx: list of tuples:
example : [ '[Measures].[Amount]' , '[Geography].[Geography].[Continent]' ] example : [ '[Measures].[Amount]' , '[Geography].[Geography].[Continent]' ]
:return: measures column's names :return: measures column's names
""" """
return [ return [
...@@ -362,7 +364,7 @@ class MdxEngine: ...@@ -362,7 +364,7 @@ class MdxEngine:
else: else:
tables_columns.update({ tables_columns.update({
tupl[0]: tupl[0]:
self.tables_loaded[tupl[0]].columns[:len(tupl[2:])] self.tables_loaded[tupl[0]].columns[:len(tupl[2:])]
}) })
axes.update({axis: tables_columns}) axes.update({axis: tables_columns})
...@@ -374,12 +376,12 @@ class MdxEngine: ...@@ -374,12 +376,12 @@ class MdxEngine:
Filter a DataFrame (Dataframe_in) with one tuple. Filter a DataFrame (Dataframe_in) with one tuple.
Example :: Example ::
tuple = ['Geography','Geography','Continent','Europe','France','olapy'] tuple = ['Geography','Geography','Continent','Europe','France','olapy']
Dataframe_in in : Dataframe_in in :
+-------------+----------+---------+---------+---------+ +-------------+----------+---------+---------+---------+
| Continent | Country | Company | Article | Amount | | Continent | Country | Company | Article | Amount |
+=============+==========+=========+=========+=========+ +=============+==========+=========+=========+=========+
...@@ -389,9 +391,9 @@ class MdxEngine: ...@@ -389,9 +391,9 @@ class MdxEngine:
+-------------+----------+---------+---------+---------+ +-------------+----------+---------+---------+---------+
| ..... | ..... | ...... | ..... | ..... | | ..... | ..... | ...... | ..... | ..... |
+-------------+----------+---------+---------+---------+ +-------------+----------+---------+---------+---------+
out : out :
+-------------+----------+---------+---------+---------+ +-------------+----------+---------+---------+---------+
| Continent | Country | Company | Article | Amount | | Continent | Country | Company | Article | Amount |
+=============+==========+=========+=========+=========+ +=============+==========+=========+=========+=========+
...@@ -513,47 +515,47 @@ class MdxEngine: ...@@ -513,47 +515,47 @@ class MdxEngine:
If we have multiple dimensions, with many columns like: If we have multiple dimensions, with many columns like:
columns_to_keep : columns_to_keep :
Geo -> Continent,Country Geo -> Continent,Country
Prod -> Company Prod -> Company
Time -> Year,Month,Day Time -> Year,Month,Day
we have to use only dimension's columns of current dimension that exist in tuple_as_list and keep other dimensions columns we have to use only dimension's columns of current dimension that exist in tuple_as_list and keep other dimensions columns
so if tuple_as_list = ['Geography','Geography','Continent'] so if tuple_as_list = ['Geography','Geography','Continent']
columns_to_keep will be: columns_to_keep will be:
columns_to_keep : columns_to_keep :
Geo -> Continent Geo -> Continent
Prod -> Company Prod -> Company
Time -> Year,Month,Day Time -> Year,Month,Day
we need columns_to_keep for grouping our columns in the DataFrame we need columns_to_keep for grouping our columns in the DataFrame
:param tuple_as_list: example : ['Geography','Geography','Continent'] :param tuple_as_list: example : ['Geography','Geography','Continent']
:param columns_to_keep: :param columns_to_keep:
example : example :
{ {
'Geography': 'Geography':
['Continent','Country'], ['Continent','Country'],
'Time': 'Time':
['Year','Month','Day'] ['Year','Month','Day']
} }
:return: updated columns_to_keep :return: updated columns_to_keep
""" """
if len( if len(
......
...@@ -10,180 +10,180 @@ from .models import Cube, Dimension, Facts, Table, Dashboard ...@@ -10,180 +10,180 @@ from .models import Cube, Dimension, Facts, Table, Dashboard
class ConfigParser: class ConfigParser:
""" """
Parse olapy config files. Parse olapy config files.
Config file used if you want to show only some measures, dimensions, columns... in excel Config file used if you want to show only some measures, dimensions, columns... in excel
Config file should be under 'home-directory/olapy-data/cubes/cubes-config.xml' Config file should be under 'home-directory/olapy-data/cubes/cubes-config.xml'
Excel Config file Structure:: Excel Config file Structure::
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<cubes> <cubes>
<!-- if you want to set an authentication mechanism in excel so to access cube, <!-- if you want to set an authentication mechanism in excel so to access cube,
user must set a token with login url like 'http://127.0.0.1/admin --> user must set a token with login url like 'http://127.0.0.1/admin -->
<!-- default password = admin --> <!-- default password = admin -->
<xmla_authentication>False</xmla_authentication> <xmla_authentication>False</xmla_authentication>
<cube> <cube>
<!-- cube name => db name --> <!-- cube name => db name -->
<name>labster</name> <name>labster</name>
<!-- source : postgres | csv --> <!-- source : postgres | csv -->
<source>postgres</source> <source>postgres</source>
<!-- star building customized star schema --> <!-- star building customized star schema -->
<facts> <facts>
<!-- facts table name --> <!-- facts table name -->
<table_name>stats_line</table_name> <table_name>stats_line</table_name>
<keys> <keys>
<!-- ref = table_name.column --> <!-- ref = table_name.column -->
<column_name ref="orgunit.id">departement_id</column_name> <column_name ref="orgunit.id">departement_id</column_name>
</keys> </keys>
<!-- specify measures explicitly --> <!-- specify measures explicitly -->
<measures> <measures>
<!-- by default, all number type columns in facts table, or you can specify them here --> <!-- by default, all number type columns in facts table, or you can specify them here -->
<name>montant</name> <name>montant</name>
<name>salaire_brut_mensuel</name> <name>salaire_brut_mensuel</name>
<name>cout_total_mensuel</name> <name>cout_total_mensuel</name>
</measures> </measures>
</facts> </facts>
<!-- end building customized star schema --> <!-- end building customized star schema -->
<!-- star building customized dimensions display in excel from the star schema --> <!-- star building customized dimensions display in excel from the star schema -->
<dimensions> <dimensions>
<dimension> <dimension>
<!-- if you want to keep the same name for excel display, just use the same name in name and displayName --> <!-- if you want to keep the same name for excel display, just use the same name in name and displayName -->
<name>stats_line</name> <name>stats_line</name>
<displayName>Demande</displayName> <displayName>Demande</displayName>
<columns> <columns>
<!-- columns order matter --> <!-- columns order matter -->
<name>type_demande</name> <name>type_demande</name>
<name>financeur</name> <name>financeur</name>
<name>wf_state</name> <name>wf_state</name>
<name>type_recrutement</name> <name>type_recrutement</name>
</columns> </columns>
</dimension> </dimension>
<dimension> <dimension>
<!-- if you want to keep the same name for excel display, just use the same name in name and displayName --> <!-- if you want to keep the same name for excel display, just use the same name in name and displayName -->
<name>orgunit</name> <name>orgunit</name>
<displayName>Organisation</displayName> <displayName>Organisation</displayName>
<columns> <columns>
<!-- columns order matter --> <!-- columns order matter -->
<name>type</name> <name>type</name>
<name>nom</name> <name>nom</name>
<name>sigle</name> <name>sigle</name>
</columns> </columns>
</dimension> </dimension>
</dimensions> </dimensions>
<!-- end building customized dimensions display in excel from the star schema --> <!-- end building customized dimensions display in excel from the star schema -->
</cube> </cube>
</cubes> </cubes>
WEB Config file Structure:: WEB Config file Structure::
<cubes> <cubes>
<cube> <cube>
<!-- cube name => db name --> <!-- cube name => db name -->
<name>mpr</name> <name>mpr</name>
<!-- source : postgres | csv --> <!-- source : postgres | csv -->
<source>postgres</source> <source>postgres</source>
<!-- star building customized star schema --> <!-- star building customized star schema -->
<facts> <facts>
<!-- facts table name --> <!-- facts table name -->
<table_name>projet</table_name> <table_name>projet</table_name>
<keys> <keys>
<!-- ref = table_name.column --> <!-- ref = table_name.column -->
<column_name ref="vocabulary_crm_status.id">status_id</column_name> <column_name ref="vocabulary_crm_status.id">status_id</column_name>
<column_name ref="vocabulary_crm_pole_leader.id">pole_leader_id</column_name> <column_name ref="vocabulary_crm_pole_leader.id">pole_leader_id</column_name>
<column_name ref="contact.id">contact_id</column_name> <column_name ref="contact.id">contact_id</column_name>
<column_name ref="compte.id">compte_porteur_id</column_name> <column_name ref="compte.id">compte_porteur_id</column_name>
<column_name ref="vocabulary_crm_aap_type.id">aap_name_id</column_name> <column_name ref="vocabulary_crm_aap_type.id">aap_name_id</column_name>
</keys> </keys>
<!-- specify measures explicitly --> <!-- specify measures explicitly -->
<measures> <measures>
<!-- by default, all number type columns in facts table, or you can specify them here --> <!-- by default, all number type columns in facts table, or you can specify them here -->
<name>budget_total</name> <name>budget_total</name>
<name>subvention_totale</name> <name>subvention_totale</name>
<name>duree_projet</name> <name>duree_projet</name>
</measures> </measures>
</facts> </facts>
<!-- end building customized star schema --> <!-- end building customized star schema -->
<tables> <tables>
<!-- Table name --> <!-- Table name -->
<table name="vocabulary_crm_status"> <table name="vocabulary_crm_status">
<!-- Columns to keep (INCLUDING id)--> <!-- Columns to keep (INCLUDING id)-->
<!-- They must be seperated with comma ',' --> <!-- They must be seperated with comma ',' -->
<columns>id,label</columns> <columns>id,label</columns>
<!-- Change insignificant table columns names --> <!-- Change insignificant table columns names -->
<!-- {IMPORTANT} Renaming COMMUN columns between dimensions and other columns if you want, other than ids column --> <!-- {IMPORTANT} Renaming COMMUN columns between dimensions and other columns if you want, other than ids column -->
<new_name old_column_name="label">Status</new_name> <new_name old_column_name="label">Status</new_name>
</table> </table>
<table name="contact"> <table name="contact">
<columns>id,nom,prenom,fonction</columns> <columns>id,nom,prenom,fonction</columns>
</table> </table>
</tables> </tables>
</cube> </cube>
</cubes> </cubes>
""" """
...@@ -194,7 +194,7 @@ class ConfigParser: ...@@ -194,7 +194,7 @@ class ConfigParser:
file_name='cubes-config.xml', file_name='cubes-config.xml',
web_config_file_name='web_cube_config.xml'): web_config_file_name='web_cube_config.xml'):
""" """
:param cube_path: path to cube (csv folders) :param cube_path: path to cube (csv folders)
:param file_name: config file name (DEFAULT = cubes-config.xml) :param file_name: config file name (DEFAULT = cubes-config.xml)
""" """
...@@ -205,7 +205,7 @@ class ConfigParser: ...@@ -205,7 +205,7 @@ class ConfigParser:
def config_file_exist(self, client_type='excel'): def config_file_exist(self, client_type='excel'):
""" """
Check whether the config file exists or not. Check whether the config file exists or not.
:return: True | False :return: True | False
""" """
if client_type == 'web': if client_type == 'web':
...@@ -216,27 +216,35 @@ class ConfigParser: ...@@ -216,27 +216,35 @@ class ConfigParser:
def xmla_authentication(self): def xmla_authentication(self):
""" """
Check if excel need authentication to access cubes or not. (xmla_authentication tag in the config file). Check if excel need authentication to access cubes or not. (xmla_authentication tag in the config file).
:return: True | False :return: True | False
""" """
with open(os.path.join(self.cube_path, self.file_name)) as config_file: if self.config_file_exist():
with open(os.path.join(self.cube_path, self.file_name)) as config_file:
parser = etree.XMLParser() parser = etree.XMLParser()
tree = etree.parse(config_file, parser) tree = etree.parse(config_file, parser)
try: try:
return tree.xpath('/cubes/xmla_authentication')[ return tree.xpath('/cubes/xmla_authentication')[
0].text == 'True' 0].text == 'True'
except: except:
return False return False
else:
return False
def get_cubes_names(self): def get_cubes_names(self, client_type='excel'):
""" """
Get all cubes names in the config file. Get all cubes names in the config file.
:return: dict of cube name as key and cube source as value (csv or postgres) (right now only postgres is supported) :return: dict of cube name as key and cube source as value (csv or postgres) (right now only postgres is supported)
""" """
with open(os.path.join(self.cube_path, self.file_name)) as config_file: if client_type == 'excel':
file_name = self.file_name
elif client_type == 'web':
file_name = self.web_config_file_name
with open(os.path.join(self.cube_path, file_name)) as config_file:
parser = etree.XMLParser() parser = etree.XMLParser()
tree = etree.parse(config_file, parser) tree = etree.parse(config_file, parser)
...@@ -315,7 +323,6 @@ class ConfigParser: ...@@ -315,7 +323,6 @@ class ConfigParser:
# try: # try:
with open(os.path.join(self.cube_path, with open(os.path.join(self.cube_path,
self.web_config_file_name)) as config_file: self.web_config_file_name)) as config_file:
parser = etree.XMLParser() parser = etree.XMLParser()
tree = etree.parse(config_file, parser) tree = etree.parse(config_file, parser)
...@@ -358,7 +365,6 @@ class ConfigParser: ...@@ -358,7 +365,6 @@ class ConfigParser:
# try: # try:
with open(os.path.join(self.cube_path, with open(os.path.join(self.cube_path,
self.web_config_file_name)) as config_file: self.web_config_file_name)) as config_file:
parser = etree.XMLParser() parser = etree.XMLParser()
tree = etree.parse(config_file, parser) tree = etree.parse(config_file, parser)
...@@ -370,11 +376,11 @@ class ConfigParser: ...@@ -370,11 +376,11 @@ class ConfigParser:
'rows': dashboard.find('Global_table/rows').text.split(',') 'rows': dashboard.find('Global_table/rows').text.split(',')
}, },
pie_charts=dashboard.find('PieCharts').text.split(','), pie_charts=dashboard.find('PieCharts').text.split(','),
bar_chats=dashboard.find('BarCharts').text.split(','), bar_charts=dashboard.find('BarCharts').text.split(','),
line_charts={ line_charts={
table.find('name').text: table.find('name').text:
(table.find('columns').text.split(',') (table.find('columns').text.split(',')
if table.find('columns') is not None else 'ALL') if table.find('columns') is not None else 'ALL')
for table in dashboard.findall('LineCharts/table') for table in dashboard.findall('LineCharts/table')
}) })
for dashboard in tree.xpath('/cubes/cube/Dashboards/Dashboard') for dashboard in tree.xpath('/cubes/cube/Dashboards/Dashboard')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment