publish.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import copy
  2. from . import util
  3. import transport
  4. import numpy as np
  5. import time
  6. import pandas as pd
  7. from multiprocessing import Process
  8. def build (**_args):
  9. """
  10. This function will build SQL statements to create a table (perhaps not needed)
  11. :plugins loaded plugins
  12. :x12 837|835 file types
  13. """
  14. _plugins=_args['plugins']
  15. _x12 = _args['x12']
  16. _template = util.template(plugins=_plugins)[_x12]
  17. _primaryKey = util.getPrimaryKey(plugins=_plugins,x12=_x12)
  18. _tables = []
  19. _main = {}
  20. for _name in _template :
  21. _item = _template[_name] #copy.deepcopy(_template[_name])
  22. if _primaryKey not in _item and type(_item) == dict:
  23. _item[_primaryKey] = ''
  24. _tables.append({_name:_item})
  25. else:
  26. _main[_name] = ''
  27. _name = getContext(_x12)
  28. _tables += [{_name:_main}]
  29. _template[_name] = _main
  30. return _template #_tables
  31. def getContext(_x12) :
  32. return 'claims' if _x12 == '837' else 'remits'
  33. def format(**_args) :
  34. """
  35. :rows rows for the
  36. :primary_key primary_key field name
  37. :x12 file format
  38. """
  39. # _name = _args['table']
  40. _rows = _args['rows']
  41. _primary_key = _args['primary_key']
  42. _x12 = _args['x12']
  43. _mainTableName = getContext(_x12)
  44. _tables = {_mainTableName:[]}
  45. for _claim in _rows :
  46. # #
  47. # # Turn the claim into a relational model ...
  48. # #
  49. _main = {}
  50. _pkvalue = None
  51. if _primary_key in _claim :
  52. _pkvalue = _claim[_primary_key]
  53. for _attrName in _claim :
  54. _item = _claim[_attrName]
  55. _item = update(_item,_primary_key,_pkvalue)
  56. if _attrName not in _tables and type(_item) in [dict,list]:
  57. _tables[_attrName] = []
  58. if type(_item) in [dict,list] :
  59. _tables[_attrName] += _item if type(_item) == list else [_item]
  60. else:
  61. #
  62. # This section suggests we found a main table attribute
  63. _main[_attrName] = _item
  64. _tables[_mainTableName].append(_main)
  65. return _tables
  66. def update (_item,key,value):
  67. if type(_item) not in [dict,list] :
  68. return _item
  69. if type(_item) == dict :
  70. _item[key] = value
  71. else:
  72. #
  73. # List, we will go through every item and update accordingly
  74. _index = 0
  75. for _row in _item :
  76. if type(_row) == dict :
  77. _row['_index'] = _index
  78. _row[key] = value
  79. return _item
  80. def init(**_args):
  81. """
  82. This function will kick off the export process provided claims/remits and the loaded plugins (not sure why)
  83. It requires the data it is pulling to be consistently formatted (otherwise nothing can be done)
  84. :plugins
  85. :store data store information i.e {source,target} specifications for data-transport
  86. :x12 file type i.e 837|835
  87. """
  88. _file_type = _args['x12']
  89. _plugins = _args['plugins']
  90. _store = _args['store']
  91. _default = build(plugins=_plugins,x12=_file_type)
  92. _df = read(store = _store['source'],x12=_file_type)
  93. _pkey = util.getPrimaryKey(plugins = _plugins, x12=_file_type)
  94. SEGMENTS = 4 # arbitrary choice
  95. _indexes = np.array_split(np.arange(_df.shape[0]),SEGMENTS)
  96. jobs = []
  97. for _ii in _indexes :
  98. try:
  99. _data = format(rows= _df.iloc[_ii].to_dict(orient='records'),x12=_file_type,primary_key=_pkey)
  100. _thread = Process(target=post,args=({'store':_store['target'],'data':_data,'default':_default,'x12':_file_type},))
  101. jobs.append(_thread)
  102. except Exception as e:
  103. #
  104. # Log: sigment,
  105. pass
  106. if jobs :
  107. jobs[0].start()
  108. jobs[0].join()
  109. while jobs :
  110. jobs = [thread for thread in jobs if thread.is_alive()]
  111. time.sleep(1)
  112. def read (**_args):
  113. _store = copy.copy(_args['store'])
  114. _x12 = _args['x12']
  115. _store['table'] = getContext(_x12) #'claims' if _x12 == '837' else 'remits'
  116. reader = transport.factory.instance(**_store)
  117. #
  118. # @TODO: reading should support streaming (for scalability)
  119. _df = reader.read()
  120. return _df
  121. def post(_args):
  122. _data = _args['data']
  123. _store = _args['store']
  124. _default = _args['default']
  125. _prefix = 'clm_' if _args['x12'] == '837' else 'rem_'
  126. for _name in _data :
  127. _tablename = _prefix+_name
  128. _store['table'] = _tablename if _name not in ['remits','claims'] else _name
  129. _store['context']='write'
  130. writer = transport.factory.instance(**_store)
  131. if len(_data[_name]) == 0 and _name in _default and not writer.has(table=_tablename):
  132. _rows = [_default[_name]]
  133. else:
  134. _rows = _data[_name]
  135. writer.write(pd.DataFrame(_rows).fillna(''))
  136. if hasattr(writer,'close') :
  137. writer.close()
  138. # _xwriter = trasnport.factory.instance(**_store)
  139. # _xwriter.write(_df)
  140. # _info = format()
  141. pass