Skip to content

Commit

Permalink
erdl#119 update api_webctrl to use orm_lonoa
Browse files Browse the repository at this point in the history
instead of orm_webctrl
  • Loading branch information
matthew-schultz committed Dec 22, 2019
1 parent 2b69595 commit df275ed
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions webctrl/script/api_webctrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
import configparser
import logging
import logging.handlers
import orm_webctrl
import os
import pendulum
import requests
import sys

# use grandparent_directory with sys.path.append() to import orm_lonoa from ../../ directory relatieve to this script's location
grandparent_directory = os.path.abspath(os.path.join((os.path.join(os.path.join(__file__, os.pardir), os.pardir)), os.pardir))
sys.path.append(grandparent_directory)
import orm_lonoa

SCRIPT_NAME = os.path.basename(__file__)

Expand Down Expand Up @@ -94,7 +98,7 @@ def get_data_from_api(sensor, conn):
raise Exception('No last_updated_datetime found')
#get webctrl user information
#returns an IndexError if there is no webctrl user in database
webctrl_user_row = conn.query(orm_webctrl.ApiAuthentication.username, orm_webctrl.ApiAuthentication.password).filter_by(script_folder=orm_webctrl.SensorInfo.ScriptFolderEnum.webctrl)[0]
webctrl_user_row = conn.query(orm_lonoa.ApiAuthentication.username, orm_lonoa.ApiAuthentication.password).filter_by(script_folder=orm_lonoa.SensorInfo.ScriptFolderEnum.webctrl)[0]
api_user = webctrl_user_row[0]
api_pass = webctrl_user_row[1]
host = 'http://www.soest.hawaii.edu/hneienergy/bulktrendserver/read'
Expand All @@ -109,7 +113,7 @@ def get_data_from_api(sensor, conn):
readings = requests.post(host, params=params, auth=tuple(auth))
if readings.status_code == requests.codes.ok:
logging.info('[' + str(current_time) + '] ' + 'Data acquisition API request was successful for ' + sensor.query_string)
error_log_row = orm_webctrl.ErrorLog(datetime=current_time, was_success=True, purpose_id=sensor.purpose_id, pipeline_stage=orm_webctrl.ErrorLog.PipelineStageEnum.data_acquisition)
error_log_row = orm_lonoa.ErrorLog(datetime=current_time, was_success=True, purpose_id=sensor.purpose_id, pipeline_stage=orm_lonoa.ErrorLog.PipelineStageEnum.data_acquisition)
conn.add(error_log_row)
conn.commit()
return readings
Expand Down Expand Up @@ -154,7 +158,7 @@ def insert_readings_into_database(conn, readings, sensor):
# 1) reading_time is after last_updated_datetime (e.g. not already in database) (need to explicitly set last_updated_datetime to HST for proper time comparison)
# 2) the current reading_time is not a duplicate of previous reading_time
if reading_time > pendulum.instance(sensor.last_updated_datetime, tz='Pacific/Honolulu') and reading_time != previous_reading_time:
reading_row = orm_webctrl.Reading(purpose_id=sensor.purpose_id, datetime=reading_time, reading=reading_value, units=sensor.unit, upload_timestamp=current_time)
reading_row = orm_lonoa.Reading(purpose_id=sensor.purpose_id, datetime=reading_time, reading=reading_value, units=sensor.unit, upload_timestamp=current_time)
conn.add(reading_row)
rows_inserted += 1
# after a reading is successfully inserted, update this datetime value
Expand All @@ -167,17 +171,17 @@ def insert_readings_into_database(conn, readings, sensor):
# with open("output.txt", 'w') as outfile:
# json.dump(sensor_json_data, outfile, indent=4)
if new_last_updated_datetime:
conn.query(orm_webctrl.SensorInfo).filter(orm_webctrl.SensorInfo.purpose_id == sensor.purpose_id).update(
conn.query(orm_lonoa.SensorInfo).filter(orm_lonoa.SensorInfo.purpose_id == sensor.purpose_id).update(
{"last_updated_datetime": new_last_updated_datetime})
error_log_row = orm_webctrl.ErrorLog(datetime=current_time, was_success=True, purpose_id=sensor.purpose_id, pipeline_stage=orm_webctrl.ErrorLog.PipelineStageEnum.database_insertion)
error_log_row = orm_lonoa.ErrorLog(datetime=current_time, was_success=True, purpose_id=sensor.purpose_id, pipeline_stage=orm_lonoa.ErrorLog.PipelineStageEnum.database_insertion)
conn.add(error_log_row)
# need to flush and refresh to get error_log_row.log_id
conn.flush()
conn.refresh(error_log_row)
# update current set of readings with related log_id
conn.query(orm_webctrl.Reading.log_id). \
filter(orm_webctrl.Reading.purpose_id == sensor.purpose_id,
orm_webctrl.Reading.upload_timestamp == current_time). \
conn.query(orm_lonoa.Reading.log_id). \
filter(orm_lonoa.Reading.purpose_id == sensor.purpose_id,
orm_lonoa.Reading.upload_timestamp == current_time). \
update({'log_id': error_log_row.log_id})
conn.commit()
logging.info(str(rows_inserted) + ' row(s) inserted for purpose_id ' + str(sensor.purpose_id))
Expand All @@ -188,7 +192,7 @@ def log_failure_to_connect_to_api(conn, exception, sensor):
current_time = pendulum.now('Pacific/Honolulu')
current_time = current_time.set(microsecond=current_time.microsecond - (current_time.microsecond % 10000))
logging.exception('log_failure_to_connect_to_api')
error_log_row = orm_webctrl.ErrorLog(datetime=current_time, error_type=exception.__class__.__name__, pipeline_stage=orm_webctrl.ErrorLog.PipelineStageEnum.data_acquisition, purpose_id=sensor.purpose_id, was_success=False)
error_log_row = orm_lonoa.ErrorLog(datetime=current_time, error_type=exception.__class__.__name__, pipeline_stage=orm_lonoa.ErrorLog.PipelineStageEnum.data_acquisition, purpose_id=sensor.purpose_id, was_success=False)
conn.add(error_log_row)
conn.commit()

Expand All @@ -200,7 +204,7 @@ def log_failure_to_connect_to_database(conn, exception, sensor):
current_time = pendulum.now('Pacific/Honolulu')
current_time = current_time.set(microsecond=current_time.microsecond - (current_time.microsecond % 10000))
logging.exception('log_failure_to_connect_to_database')
error_log_row = orm_webctrl.ErrorLog(datetime=current_time, error_type=exception.__class__.__name__, pipeline_stage=orm_webctrl.ErrorLog.PipelineStageEnum.database_insertion, purpose_id=sensor.purpose_id, was_success=False)
error_log_row = orm_lonoa.ErrorLog(datetime=current_time, error_type=exception.__class__.__name__, pipeline_stage=orm_lonoa.ErrorLog.PipelineStageEnum.database_insertion, purpose_id=sensor.purpose_id, was_success=False)
conn.add(error_log_row)
conn.commit()

Expand All @@ -209,7 +213,7 @@ def log_failure_to_connect_to_database(conn, exception, sensor):
set_logging_settings()
# connect to the database
conn = get_db_handler()
sensors = conn.query(orm_webctrl.SensorInfo.purpose_id, orm_webctrl.SensorInfo.query_string, orm_webctrl.SensorInfo.last_updated_datetime, orm_webctrl.SensorInfo.unit).filter_by(script_folder=orm_webctrl.SensorInfo.ScriptFolderEnum.webctrl, is_active=True)
sensors = conn.query(orm_lonoa.SensorInfo.purpose_id, orm_lonoa.SensorInfo.query_string, orm_lonoa.SensorInfo.last_updated_datetime, orm_lonoa.SensorInfo.unit).filter_by(script_folder=orm_lonoa.SensorInfo.ScriptFolderEnum.webctrl, is_active=True)
for sensor in sensors:
try:
readings = get_data_from_api(sensor, conn)
Expand Down

0 comments on commit df275ed

Please sign in to comment.