Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 0 additions & 102 deletions hiseqX_run_times.py

This file was deleted.

31 changes: 17 additions & 14 deletions sensorpush_to_statusdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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}"
Expand Down
37 changes: 22 additions & 15 deletions update_exchange_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

import argparse
from couchdb import Server
from ibmcloudant import CouchDbSessionAuthenticator, cloudant_v1
import datetime
import json
import requests
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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')
Expand All @@ -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)

Expand Down