Skip to content

Commit 5b9f120

Browse files
authored
Merge pull request #111 from aanil/master
Update to cloudant to see if it fixes the 500 Conn err
2 parents 146fab8 + 34337e9 commit 5b9f120

3 files changed

Lines changed: 39 additions & 131 deletions

File tree

hiseqX_run_times.py

Lines changed: 0 additions & 102 deletions
This file was deleted.

sensorpush_to_statusdb.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
import argparse
1010
import yaml
1111
import os
12-
import pytz
1312
import datetime
1413
import numpy as np
1514
import pandas as pd
1615
import logging
1716
import time
18-
from couchdb import Server
17+
from ibmcloudant import CouchDbSessionAuthenticator, cloudant_v1
1918

2019

2120
class SensorPushConnection(object):
@@ -470,29 +469,33 @@ def main(
470469
with open(statusdb_config) as settings_file:
471470
server_settings = yaml.load(settings_file, Loader=yaml.SafeLoader)
472471

473-
url_string = "https://{}:{}@{}".format(
474-
server_settings["statusdb"].get("username"),
475-
server_settings["statusdb"].get("password"),
476-
server_settings["statusdb"].get("url"),
472+
couch = cloudant_v1.CloudantV1(
473+
authenticator=CouchDbSessionAuthenticator(
474+
server_settings["statusdb"].get("username"), server_settings["statusdb"].get("password")
475+
)
477476
)
478-
couch = Server(url_string)
479-
sensorpush_db = couch["sensorpush"]
477+
couch.set_service_url(server_settings["statusdb"].get("url"))
480478

481479
for sd in sensor_documents:
482480
# Check if there already is a document for the sensor & date combination
483-
view_call = sensorpush_db.view("entire_document/by_sensor_id_and_date")[
484-
sd.sensor_id, sd.start_date_midnight
485-
]
481+
view_call = couch.post_view(
482+
db="sensorpush",
483+
ddoc="entire_document",
484+
view="by_sensor_id_and_date",
485+
key=[sd.sensor_id, sd.start_date_midnight],
486+
).get_result()
486487

487488
sd_dict = sd.format_for_statusdb()
488489

489-
if view_call.rows:
490-
sd_dict = SensorDocument.merge_with(sd_dict, view_call.rows[0].value)
490+
if view_call["rows"]:
491+
sd_dict = SensorDocument.merge_with(sd_dict, view_call["rows"][0]["value"])
491492

492493
if push:
493494
logging.info(f'Saving {sd_dict["sensor_name"]} to statusdb')
494495
try:
495-
sensorpush_db.save(sd_dict)
496+
couch.post_document(
497+
db="sensorpush", document=sd_dict
498+
).get_result()
496499
except Exception as e:
497500
logging.error(
498501
f"Error saving {sd_dict['sensor_name']} to statusdb: {e}"

update_exchange_rates.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55

66
import argparse
7-
from couchdb import Server
7+
from ibmcloudant import CouchDbSessionAuthenticator, cloudant_v1
88
import datetime
99
import json
1010
import requests
@@ -37,10 +37,16 @@ def get_rate(self, currency):
3737
return 1/self.rates[currency]
3838

3939

40-
def get_current(db, item):
41-
rows = db.view("entire_document/by_date", descending=True, limit=1).rows
40+
def get_current(db_conn, item):
41+
rows = db_conn.post_view(
42+
db="pricing_exchange_rates",
43+
ddoc="entire_document",
44+
view="by_date",
45+
descending=True,
46+
limit=1
47+
).get_result()["rows"]
4248
if len(rows) != 0:
43-
value = rows[0].value
49+
value = rows[0]["value"]
4450
return value[item]
4551
return None
4652

@@ -77,20 +83,18 @@ def main(config, fixer_io_config, push_to_server=False):
7783
with open(config) as settings_file:
7884
server_settings = yaml.load(settings_file, Loader=yaml.SafeLoader)
7985

80-
url_string = 'https://{}:{}@{}'.format(
81-
server_settings['statusdb'].get('username'),
82-
server_settings['statusdb'].get('password'),
83-
server_settings['statusdb'].get('url')
84-
)
85-
couch = Server(url_string)
86-
87-
db = couch['pricing_exchange_rates']
86+
couch = cloudant_v1.CloudantV1(
87+
authenticator=CouchDbSessionAuthenticator(
88+
server_settings["statusdb"].get("username"), server_settings["statusdb"].get("password")
89+
)
90+
)
91+
couch.set_service_url(server_settings["statusdb"].get("url"))
8892

8993
# Check that new is not too strange compared to current.
9094
# This is a safety measure so that we have lower risk of having erroneus
9195
# exchange rates in the db.
92-
current_usd = get_current(db, 'USD_in_SEK')
93-
current_eur = get_current(db, 'EUR_in_SEK')
96+
current_usd = get_current(couch, 'USD_in_SEK')
97+
current_eur = get_current(couch, 'EUR_in_SEK')
9498

9599
check_financial_crisis(current_usd, usd_to_sek, 'USD')
96100
check_financial_crisis(current_eur, eur_to_sek, 'EUR')
@@ -105,7 +109,10 @@ def main(config, fixer_io_config, push_to_server=False):
105109
raise Exception("Super stable currencies? Stale api would be my guess.")
106110

107111
if push_to_server:
108-
db.save(doc)
112+
couch.post_document(
113+
db="pricing_exchange_rates",
114+
document=doc
115+
)
109116
else:
110117
print(doc)
111118

0 commit comments

Comments
 (0)