diff --git a/hiseqX_run_times.py b/hiseqX_run_times.py deleted file mode 100755 index 1782276..0000000 --- a/hiseqX_run_times.py +++ /dev/null @@ -1,102 +0,0 @@ -import os -import couchdb -import glob -import logging -from datetime import datetime -from datetime import date -import argparse -import yaml -try: - import ConfigParser -except ImportError: - import configparser - -CONFIG = {} - -logger = logging.getLogger(__name__) - - - -def main(args): - configuration_file = args.config - load_yaml_config(configuration_file) - couch=setupServer(CONFIG) - flowcell_db = couch["x_flowcells"] - instruments = {} - instruments["ST-E00198"] = [] - instruments["ST-E00201"] = [] - instruments["ST-E00214"] = [] - instruments["ST-E00266"] = [] - instruments["ST-E00269"] = [] - - for fc_doc in flowcell_db: - try: - instrument = flowcell_db[fc_doc]["Runinfo"]["Instrument"] - fcid = flowcell_db[fc_doc]["Runinfo"]["Id"] - except KeyError: - if "RunInfo" in flowcell_db[fc_doc]: - instrument = flowcell_db[fc_doc]["RunInfo"]["Instrument"] - fcid = flowcell_db[fc_doc]["RunInfo"]["Id"] - else: - continue - #check if the instrument is one of the ones I want to check - if instrument in ["ST-E00198", "ST-E00201", "ST-E00214", "ST-E00266", "ST-E00269"]: - try: - time_cycles = flowcell_db[fc_doc]["time cycles"] - except KeyError: - continue - first_cycle_start = time_cycles[0]['start'] - last_cycle_end = time_cycles[-1]['end'] - # the split is done to remove the decimal point in the seconds - first_cycle_date = datetime.strptime(first_cycle_start.split(".")[0], '%Y-%m-%d %H:%M:%S') - last_cycle_date = datetime.strptime(last_cycle_end.split(".")[0], '%Y-%m-%d %H:%M:%S') - delta = last_cycle_date - first_cycle_date - instruments[instrument].append({"{}".format(fcid): delta.total_seconds()/3600 } ) - - for instrument in instruments: - print("time\t{}".format(instrument)) - for run in sorted(instruments[instrument]): - date_illumina_format =list(run.keys())[0].split("_")[0] - date_exel_format="{}/{}/20{}".format(date_illumina_format[4:6] , date_illumina_format[2:4], date_illumina_format[0:2]) - print("{}\t{}".format(date_exel_format, run[list(run.keys())[0]])) - - - - -def setupServer(conf): - db_conf = conf['statusdb'] - url="https://{0}:{1}@{2}".format(db_conf['username'], db_conf['password'], db_conf['url']) - return couchdb.Server(url) - - - -def load_yaml_config(config_file): - """Load YAML config file - - :param str config_file: The path to the configuration file. - - :returns: A dict of the parsed config file. - :rtype: dict - :raises IOError: If the config file cannot be opened. - """ - if type(config_file) is file: - CONFIG.update(yaml.load(config_file) or {}) - return CONFIG - else: - try: - with open(config_file, 'r') as f: - content = yaml.load(f) - CONFIG.update(content) - return content - except IOError as e: - e.message = "Could not open configuration file \"{}\".".format(config_file) - raise e - - - -if __name__ == '__main__': - parser = argparse.ArgumentParser("""Check running times""") - parser.add_argument('--config', help="configuration file", type=str, required=True) - args = parser.parse_args() - - main(args) diff --git a/sensorpush_to_statusdb.py b/sensorpush_to_statusdb.py index 3dcbbc1..97d5456 100644 --- a/sensorpush_to_statusdb.py +++ b/sensorpush_to_statusdb.py @@ -9,13 +9,12 @@ import argparse import yaml import os -import pytz import datetime import numpy as np import pandas as pd import logging import time -from couchdb import Server +from ibmcloudant import CouchDbSessionAuthenticator, cloudant_v1 class SensorPushConnection(object): @@ -470,29 +469,33 @@ def main( with open(statusdb_config) as settings_file: server_settings = yaml.load(settings_file, Loader=yaml.SafeLoader) - url_string = "https://{}:{}@{}".format( - server_settings["statusdb"].get("username"), - server_settings["statusdb"].get("password"), - server_settings["statusdb"].get("url"), + couch = cloudant_v1.CloudantV1( + authenticator=CouchDbSessionAuthenticator( + server_settings["statusdb"].get("username"), server_settings["statusdb"].get("password") + ) ) - couch = Server(url_string) - sensorpush_db = couch["sensorpush"] + couch.set_service_url(server_settings["statusdb"].get("url")) for sd in sensor_documents: # Check if there already is a document for the sensor & date combination - view_call = sensorpush_db.view("entire_document/by_sensor_id_and_date")[ - sd.sensor_id, sd.start_date_midnight - ] + view_call = couch.post_view( + db="sensorpush", + ddoc="entire_document", + view="by_sensor_id_and_date", + key=[sd.sensor_id, sd.start_date_midnight], + ).get_result() sd_dict = sd.format_for_statusdb() - if view_call.rows: - sd_dict = SensorDocument.merge_with(sd_dict, view_call.rows[0].value) + if view_call["rows"]: + sd_dict = SensorDocument.merge_with(sd_dict, view_call["rows"][0]["value"]) if push: logging.info(f'Saving {sd_dict["sensor_name"]} to statusdb') try: - sensorpush_db.save(sd_dict) + couch.post_document( + db="sensorpush", document=sd_dict + ).get_result() except Exception as e: logging.error( f"Error saving {sd_dict['sensor_name']} to statusdb: {e}" diff --git a/update_exchange_rates.py b/update_exchange_rates.py index cd2c1d1..5152bc7 100644 --- a/update_exchange_rates.py +++ b/update_exchange_rates.py @@ -4,7 +4,7 @@ """ import argparse -from couchdb import Server +from ibmcloudant import CouchDbSessionAuthenticator, cloudant_v1 import datetime import json import requests @@ -37,10 +37,16 @@ def get_rate(self, currency): return 1/self.rates[currency] -def get_current(db, item): - rows = db.view("entire_document/by_date", descending=True, limit=1).rows +def get_current(db_conn, item): + rows = db_conn.post_view( + db="pricing_exchange_rates", + ddoc="entire_document", + view="by_date", + descending=True, + limit=1 + ).get_result()["rows"] if len(rows) != 0: - value = rows[0].value + value = rows[0]["value"] return value[item] return None @@ -77,20 +83,18 @@ def main(config, fixer_io_config, push_to_server=False): with open(config) as settings_file: server_settings = yaml.load(settings_file, Loader=yaml.SafeLoader) - url_string = 'https://{}:{}@{}'.format( - server_settings['statusdb'].get('username'), - server_settings['statusdb'].get('password'), - server_settings['statusdb'].get('url') - ) - couch = Server(url_string) - - db = couch['pricing_exchange_rates'] + couch = cloudant_v1.CloudantV1( + authenticator=CouchDbSessionAuthenticator( + server_settings["statusdb"].get("username"), server_settings["statusdb"].get("password") + ) + ) + couch.set_service_url(server_settings["statusdb"].get("url")) # Check that new is not too strange compared to current. # This is a safety measure so that we have lower risk of having erroneus # exchange rates in the db. - current_usd = get_current(db, 'USD_in_SEK') - current_eur = get_current(db, 'EUR_in_SEK') + current_usd = get_current(couch, 'USD_in_SEK') + current_eur = get_current(couch, 'EUR_in_SEK') check_financial_crisis(current_usd, usd_to_sek, 'USD') check_financial_crisis(current_eur, eur_to_sek, 'EUR') @@ -105,7 +109,10 @@ def main(config, fixer_io_config, push_to_server=False): raise Exception("Super stable currencies? Stale api would be my guess.") if push_to_server: - db.save(doc) + couch.post_document( + db="pricing_exchange_rates", + document=doc + ) else: print(doc)