Gnumed.business.gmDataMining API documentation
Description: GNUmed data mining middleware.

# Module `Gnumed.business.gmDataMining`

GNUmed data mining middleware.

## Functions

`def delete_report_definition(name=None)`
  Expand source code

      def delete_report_definition(name=None):
              queries = [{
                      'sql': 'DELETE FROM cfg.report_query WHERE label = %(name)s',
                      'args': {'name': name}
              }]
              gmPG2.run_rw_queries(queries=queries)
              return True

`def report_exists(name=None)`
  Expand source code

      def report_exists(name=None):
              rows = gmPG2.run_ro_queries(queries = [{
                      'sql': 'SELECT EXISTS(SELECT 1 FROM cfg.report_query WHERE label = %(name)s)',
                      'args': {'name': name}
              }])
              return rows[0][0]

`def run_report_query(query=None, limit=None, pk_identity=None)`
  Expand source code

      def run_report_query(query=None, limit=None, pk_identity=None):
              """Returns (status, hint, cols, rows)"""

              PATIENT_ID_TOKEN = '$<ID_ACTIVE_PATIENT>$'
              if limit is None:
                      limit = ''
              else:
                      limit = 'LIMIT %s' % limit

              # does user want to insert current patient ID ?
              if query.find(PATIENT_ID_TOKEN) == -1:
                      wrapper_query = """
                              SELECT * FROM (
                                      %%s
                              ) AS user_query
                              %s
                      """ % limit
              else:
                      # she does, but is it possible ?
                      if pk_identity is None:
                              gmDispatcher.send('statustext', msg = _('Query needs active patient.'), beep = True)
                              cols = [_('Error')]
                              rows = [
                                      [_('Active patient query')],
                                      [''],
                                      [_('This query requires a patient to be active in the client.')],
                                      [''],
                                      [_('Please activate the patient you are interested')],
                                      [_('in and re-run the query.')]
                              ]
                              return (False, 'pk_identity', cols, rows)

                      query = query.replace(PATIENT_ID_TOKEN, str(pk_identity))
                      wrapper_query = """
                              SELECT %s AS pk_patient, * FROM (
                                      %%s
                              ) AS user_query
                              %s
                      """ % (pk_identity, limit)

              wrapped_query = wrapper_query % query
              _log.debug('running report query:')
              _log.debug(wrapped_query)

              try:
                      # read-only for safety reasons
                      rows = gmPG2.run_ro_queries(queries = [{'sql': wrapped_query}])
              except Exception:
                      _log.exception('report query failed')
                      gmDispatcher.send('statustext', msg = _('The query failed.'), beep = True)
                      cols = [_('Error')]
                      t, v = sys.exc_info()[:2]
                      rows = [
                              [_('The query failed.')],
                              [''],
                              [str(t)]
                      ]
                      for line in str(v).split('\n'):
                              rows.append([line])
                      rows.append([''])
                      for line in query.split('\n'):
                              rows.append([line])
                      return (False, 'query failed', cols, rows)

              if rows:
                      cols = list(rows[0].keys())
              else:
                      cols = [_('No results.')]

              return (True, None, cols, rows)

  Returns (status, hint, cols, rows)

`def save_report_definition(name=None, query=None, overwrite=False)`
  Expand source code

      def save_report_definition(name=None, query=None, overwrite=False):
              if not overwrite:
                      if report_exists(name=name):
                              return False

              args = {'name': name, 'query': query}
              queries = [
                      {'sql': 'DELETE FROM cfg.report_query WHERE label = %(name)s', 'args': args},
                      {'sql': 'INSERT INTO cfg.report_query (label, cmd) VALUES (%(name)s, %(query)s)', 'args': args}
              ]
              gmPG2.run_rw_queries(queries = queries)
              return True