Commit 480dbd4d authored by Mouadh's avatar Mouadh

fix (bugs + path prob)

parent 1ac25c8b
...@@ -5,14 +5,15 @@ from __future__ import absolute_import, division, print_function ...@@ -5,14 +5,15 @@ from __future__ import absolute_import, division, print_function
import itertools import itertools
import os import os
import re import re
from os.path import expanduser
from collections import OrderedDict from collections import OrderedDict
import numpy as np import numpy as np
import pandas as pd import pandas as pd
import pandas.io.sql as psql import pandas.io.sql as psql
from ..utils.connection import MyDB from ..tools.config_file_parser import ConfigParser
from ..utils.config_file_parser import ConfigParser from ..tools.connection import MyDB
class MdxEngine: class MdxEngine:
...@@ -31,6 +32,7 @@ class MdxEngine: ...@@ -31,6 +32,7 @@ class MdxEngine:
def __init__(self, def __init__(self,
cube_name, cube_name,
cubes_path=None,
mdx_query=None, mdx_query=None,
cube_folder=CUBE_FOLDER, cube_folder=CUBE_FOLDER,
sep=';', sep=';',
...@@ -41,12 +43,18 @@ class MdxEngine: ...@@ -41,12 +43,18 @@ class MdxEngine:
:param mdx_query: query to execute :param mdx_query: query to execute
:param sep: separator in the csv files :param sep: separator in the csv files
''' '''
self.cube = cube_name
self.cube_folder = cube_folder self.cube_folder = cube_folder
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
self.cube_path = self._get_cube_path()
if cubes_path is None:
self.cube_path = self._get_default_cube_directory()
else:
self.cube_path = cubes_path
# to get cubes in db # to get cubes in db
self._ = self.get_cubes_names() self._ = self.get_cubes_names()
self.tables_loaded = self._load_tables() self.tables_loaded = self._load_tables()
...@@ -59,18 +67,16 @@ class MdxEngine: ...@@ -59,18 +67,16 @@ class MdxEngine:
self.selected_measures = [self.measures[0]] self.selected_measures = [self.measures[0]]
@classmethod @classmethod
def get_cubes_names(self): def get_cubes_names(cls):
''' '''
:return: list cubes name under cubes folder :return: list cubes name under cubes folder
''' '''
# get csv files folders (cubes) # get csv files folders (cubes)
home_directory = expanduser("~")
location = os.path.join(home_directory, 'olapy-data', cls.CUBE_FOLDER)
try: try:
location = os.path.join(
os.path.abspath(
os.path.join(
os.path.dirname(__file__), "..", "..", "..", "..")),
MdxEngine.CUBE_FOLDER)
MdxEngine.csv_files_cubes = [ MdxEngine.csv_files_cubes = [
file for file in os.listdir(location) file for file in os.listdir(location)
if os.path.isdir(os.path.join(location, file)) if os.path.isdir(os.path.join(location, file))
...@@ -94,6 +100,10 @@ class MdxEngine: ...@@ -94,6 +100,10 @@ class MdxEngine:
return MdxEngine.csv_files_cubes + MdxEngine.postgres_db_cubes return MdxEngine.csv_files_cubes + MdxEngine.postgres_db_cubes
def _get_default_cube_directory(self):
home_directory = expanduser("~")
return os.path.join(home_directory, 'olapy-data', self.cube_folder)
def _get_tables_name(self): def _get_tables_name(self):
return self.tables_loaded.keys() return self.tables_loaded.keys()
...@@ -222,10 +232,7 @@ class MdxEngine: ...@@ -222,10 +232,7 @@ class MdxEngine:
db.connection) db.connection)
fusion = fusion.merge( fusion = fusion.merge(
df, df, left_on=fact_key, right_on=dimension_and_key.split('.')[1])
left_on=fact_key,
right_on=dimension_and_key.split('.')[1],
how='outer')
# TODO CHOSE BETWEEN THOSES DF # TODO CHOSE BETWEEN THOSES DF
# if separated dimensions # if separated dimensions
...@@ -302,8 +309,8 @@ class MdxEngine: ...@@ -302,8 +309,8 @@ class MdxEngine:
for cubes in config_file_parser.construct_cubes(): for cubes in config_file_parser.construct_cubes():
# TODO cubes.source == 'csv' # TODO cubes.source == 'csv'
if cubes.source == 'postgres': if cubes.source == 'postgres':
fusion = self._construct_star_schema_config_file(cube_name, fusion = self._construct_star_schema_config_file(
cubes) cube_name, cubes)
elif cube_name in self.csv_files_cubes: elif cube_name in self.csv_files_cubes:
fusion = self._construct_star_schema_csv_files(cube_name) fusion = self._construct_star_schema_csv_files(cube_name)
...@@ -311,8 +318,9 @@ class MdxEngine: ...@@ -311,8 +318,9 @@ class MdxEngine:
elif cube_name in self.postgres_db_cubes: elif cube_name in self.postgres_db_cubes:
fusion = self._construct_star_schema_db(cube_name) fusion = self._construct_star_schema_db(cube_name)
return fusion[ return fusion[[
[col for col in fusion.columns if col.lower()[-3:] != '_id']] col for col in fusion.columns if col.lower()[-3:] != '_id'
]]
def get_all_tables_names(self, ignore_fact=False): def get_all_tables_names(self, ignore_fact=False):
""" """
...@@ -325,16 +333,6 @@ class MdxEngine: ...@@ -325,16 +333,6 @@ class MdxEngine:
return [tab for tab in self.tables_names if self.facts not in tab] return [tab for tab in self.tables_names if self.facts not in tab]
return self.tables_names return self.tables_names
def _get_cube_path(self):
'''
:return: return local cube folder name with full path
'''
return os.path.join(
os.path.abspath(
os.path.join(
os.path.dirname(__file__), '..', "..", '..', '..')),
self.cube_folder)
def get_cube(self): def get_cube(self):
""" """
get path to the cube (example /home/your_user_name/olapy-core/cubes) get path to the cube (example /home/your_user_name/olapy-core/cubes)
...@@ -634,8 +632,9 @@ class MdxEngine: ...@@ -634,8 +632,9 @@ class MdxEngine:
:return: updated columns_to_keep :return: updated columns_to_keep
""" """
if len(tuple_as_list) == 3 and tuple_as_list[-1] in self.tables_loaded[ if len(
tuple_as_list[0]].columns: tuple_as_list
) == 3 and tuple_as_list[-1] in self.tables_loaded[tuple_as_list[0]].columns:
# in case of [Geography].[Geography].[Country] # in case of [Geography].[Geography].[Country]
cols = [tuple_as_list[-1]] cols = [tuple_as_list[-1]]
else: else:
...@@ -737,7 +736,7 @@ class MdxEngine: ...@@ -737,7 +736,7 @@ class MdxEngine:
# TODO margins=True for columns total !!!!! # TODO margins=True for columns total !!!!!
return { return {
'result': 'result':
df.drop_duplicates().replace(np.nan, -1).groupby(cols).sum(), df.groupby(cols).sum()[self.selected_measures],
'columns_desc': 'columns_desc':
tables_n_columns tables_n_columns
} }
......
...@@ -7,8 +7,12 @@ class MyDB(object): ...@@ -7,8 +7,12 @@ class MyDB(object):
self.connection = pg.connect( self.connection = pg.connect(
"user={0} password={1}".format(username, password)) "user={0} password={1}".format(username, password))
else: else:
self.connection = pg.connect("user={0} password={1} dbname='{2}'". try:
self.connection = pg.connect("user={0} password={1} dbname='{2}'".
format(username, password, db)) format(username, password, db))
except:
print("can't connect")
def __del__(self): def __del__(self):
self.connection.close() if hasattr(self, 'connection'):
self.connection.close()
\ No newline at end of file
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
from __future__ import absolute_import, division, print_function from __future__ import absolute_import, division, print_function
import os
from datetime import datetime from datetime import datetime
from os.path import expanduser
from lxml import etree from lxml import etree
from spyne import AnyXml, Application, ServiceBase, rpc from spyne import AnyXml, Application, ServiceBase, rpc
...@@ -222,7 +224,12 @@ def start_server(write_on_file=False): ...@@ -222,7 +224,12 @@ def start_server(write_on_file=False):
# log to the file # log to the file
# TODO FIX it with os # TODO FIX it with os
if write_on_file: if write_on_file:
logging.basicConfig(level=logging.DEBUG, filename="C:\\logs\\xmla.log") home_directory = expanduser("~")
if not os.path.isdir(os.path.join(home_directory, 'logs')):
os.makedirs(os.path.join(home_directory, 'logs'))
logging.basicConfig(
level=logging.DEBUG,
filename=os.path.join(home_directory, 'logs', 'xmla.log'))
else: else:
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG) logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)
......
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