|
4 | 4 |
|
5 | 5 | from __future__ import print_function
|
6 | 6 | from collections import OrderedDict
|
7 |
| -from couchdb import Server |
8 | 7 | import logging
|
| 8 | +from ibmcloudant import CouchDbSessionAuthenticator, cloudant_v1, ViewQuery |
| 9 | +from ibm_cloud_sdk_core import ApiException |
9 | 10 | import json
|
10 | 11 | import os
|
11 | 12 | import re
|
12 | 13 | import requests
|
13 |
| -import socket |
14 | 14 | import subprocess
|
15 | 15 | import yaml
|
16 | 16 |
|
@@ -215,11 +215,11 @@ def get_ngi_project_metadata(self, pid):
|
215 | 215 | if self.couch is None:
|
216 | 216 | return None
|
217 | 217 | try:
|
218 |
| - p_view = self.couch['projects'].view('project/summary') |
219 |
| - except socket.error: |
| 218 | + p_view = self.couch.post_view(db="projects", ddoc="project", view="summary").get_result() |
| 219 | + except ConnectionError: |
220 | 220 | log.error('CouchDB Operation timed out')
|
221 | 221 | p_summary = None
|
222 |
| - for row in p_view: |
| 222 | + for row in p_view['rows']: |
223 | 223 | if row['key'][1] == pid:
|
224 | 224 | p_summary = row
|
225 | 225 |
|
@@ -281,15 +281,15 @@ def get_ngi_samples_metadata(self, pid, s_names=None):
|
281 | 281 | """ Get project sample metadata from statusdb """
|
282 | 282 | if self.test_data is not None:
|
283 | 283 | report.ngi['sample_meta'] = self.test_data['samples']
|
284 |
| - elif self.couch is not None: |
285 |
| - p_view = self.couch['projects'].view('project/samples') |
286 |
| - p_samples = p_view[pid] |
287 |
| - if not len(p_samples.rows) == 1: |
288 |
| - log.error(f"statusdb returned {len(p_samples.rows)} rows when querying {pid}") |
| 284 | + elif self.couch is not None: |
| 285 | + p_view_results = self.couch.post_view_queries(db="projects", ddoc="project", queries=[ViewQuery(key=pid)], |
| 286 | + view="samples").get_result() |
| 287 | + if not len(p_view_results['results'][0]['rows']) == 1: |
| 288 | + log.error(f"statusdb returned {len(p_view_results['results'][0]['rows'])} rows when querying {pid}") |
289 | 289 | else:
|
290 | 290 | if 'sample_meta' not in report.ngi:
|
291 | 291 | report.ngi['sample_meta'] = dict()
|
292 |
| - report.ngi['sample_meta'].update(p_samples.rows[0]['value']) |
| 292 | + report.ngi['sample_meta'].update(p_view_results['results'][0]['rows'][0]['value']) |
293 | 293 |
|
294 | 294 | if 'ngi_names' not in report.ngi:
|
295 | 295 | report.ngi['ngi_names'] = dict()
|
@@ -483,18 +483,17 @@ def push_statusdb_multiqc_data(self):
|
483 | 483 | if self.couch is None:
|
484 | 484 | return None
|
485 | 485 | try:
|
486 |
| - db = self.couch['analysis'] |
487 |
| - p_view = db.view('project/project_id') |
488 |
| - except socket.error: |
| 486 | + p_view_results = self.couch.post_view_queries(db="analysis", ddoc="project", |
| 487 | + queries=[ViewQuery(key=report.ngi['pid'])], |
| 488 | + view="project_id").get_result() |
| 489 | + except ConnectionError: |
489 | 490 | log.error('CouchDB Operation timed out')
|
490 | 491 | return None
|
491 | 492 |
|
492 | 493 | # Try to get an existing document if one exists
|
493 | 494 | doc = {}
|
494 |
| - for row in p_view: |
495 |
| - if row['key'] == report.ngi['pid']: |
496 |
| - doc = row.value |
497 |
| - break |
| 495 | + if p_view_results['results'][0]['rows']: |
| 496 | + doc = p_view_results['results'][0]['rows'][0]['value'] |
498 | 497 |
|
499 | 498 | # Start fresh unless the existing doc looks similar
|
500 | 499 | newdoc = {
|
@@ -530,12 +529,12 @@ def push_statusdb_multiqc_data(self):
|
530 | 529 |
|
531 | 530 | # Save object to the database
|
532 | 531 | try:
|
533 |
| - db.save(doc) |
534 |
| - except ValueError as e: |
535 |
| - if e.args[0] == 'Out of range float values are not JSON compliant': |
536 |
| - log.debug('Error saving to StatusDB: Out of range float values are not JSON compliant, might be NaNs, trying again...') |
| 532 | + self.couch.post_document(db="analysis", document=doc).get_result() |
| 533 | + except ApiException as e: |
| 534 | + if e.message[0] == 'bad_request: invalid UTF-8 JSON': |
| 535 | + log.debug('Error saving to StatusDB: bad_request: invalid UTF-8 JSON, might be NaNs, trying again...') |
537 | 536 | doc = json.loads(utils.util_functions.dump_json(doc, filehandle=None))
|
538 |
| - db.save(doc) |
| 537 | + self.couch.post_document(db="analysis", document=doc).get_result() |
539 | 538 | log.debug('Saved to StatusDB after converting NaNs to nulls')
|
540 | 539 | else:
|
541 | 540 | log.error(f'Error saving to StatusDB: {e}')
|
@@ -576,7 +575,10 @@ def connect_statusdb(self):
|
576 | 575 | log.warning("Cannot contact statusdb - skipping NGI metadata stuff")
|
577 | 576 | return None
|
578 | 577 |
|
579 |
| - return Server(server_url) |
| 578 | + couch_server = cloudant_v1.CloudantV1(authenticator=CouchDbSessionAuthenticator(couch_user, password)) |
| 579 | + couch_server.set_service_url(f"https://{couch_url}") |
| 580 | + |
| 581 | + return couch_server |
580 | 582 |
|
581 | 583 |
|
582 | 584 |
|
|
0 commit comments