Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
Fixes #34 Running autopep8 to fix up formatting (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonespm authored Apr 22, 2019
1 parent 69500d9 commit 56ef6ec
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 52 deletions.
111 changes: 59 additions & 52 deletions caliper_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@

logger.info("Connect to database...")
conn = psycopg2.connect(
dbname = os.getenv("DB_NAME", "runestone"),
user = os.getenv("DB_USER", "runestone"),
password = os.getenv("DB_PASS", "runestone"),
host = os.getenv("DB_HOST", "localhost"),
port = os.getenv("DB_PORT", 5432),
)
dbname=os.getenv("DB_NAME", "runestone"),
user=os.getenv("DB_USER", "runestone"),
password=os.getenv("DB_PASS", "runestone"),
host=os.getenv("DB_HOST", "localhost"),
port=os.getenv("DB_PORT", 5432),
)

cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)

Expand All @@ -47,8 +47,10 @@ def create_runtime_table():
except Exception as err:
print(err)


create_runtime_table()


def get_last_event_time(cron_job, cron_status):
"""
Return the last successfully sent event timestamp from the previous job
Expand All @@ -57,18 +59,19 @@ def get_last_event_time(cron_job, cron_status):
cur.execute("""
SELECT last_sent_event_time FROM cron_run_info
WHERE cron_job = '{cron_job}' AND last_run_status = '{cron_status}'
ORDER BY last_sent_event_time DESC LIMIT 1 """.format(cron_job = cron_job, cron_status = cron_status))
ORDER BY last_sent_event_time DESC LIMIT 1 """.format(cron_job=cron_job, cron_status=cron_status))
last_event = cur.fetchone()
if last_event:
last_event_time = last_event[0]
else:
else:
last_event_time = os.getenv("FIRST_EVENT_TIME", '2019-01-01T00:00:00').replace('T', ' ')

except Exception as err:
logger.error(err)
last_event_time = os.getenv("FIRST_EVENT_TIME", '2019-01-01T00:00:00').replace('T', ' ')
return last_event_time


def fetch_events(last_event_time, target_events, target_acts):
"""
Return all the events happened after the last_event_time
Expand All @@ -78,20 +81,21 @@ def fetch_events(last_event_time, target_events, target_acts):

if not target_acts:
raise Exception("No target_acts found/defined")

target_events = [f"'{event}'" for event in target_events]
target_acts = [f"'{act}'" for act in target_acts]

cur.execute("""
SELECT * FROM useinfo
WHERE useinfo.event IN ({events})
AND useinfo.act IN ({acts})
AND useinfo.timestamp > CAST('{last_event_time}' AS TIMESTAMP);""".format(events = ', '.join(target_events), acts = ', '.join(target_acts), last_event_time = last_event_time))
AND useinfo.timestamp > CAST('{last_event_time}' AS TIMESTAMP);""".format(events=', '.join(target_events), acts=', '.join(target_acts), last_event_time=last_event_time))

events = cur.fetchall()
logger.info(f"Fetched {len(events)} events")
return events


def send_caliper_event():
cron_job = os.getenv('CRON_NAME', "runestone_caliper_job")
last_event_sent_time = get_last_event_time(cron_job, 'success')
Expand All @@ -113,11 +117,11 @@ def send_caliper_event():
if event.get('event') == 'page' and event.get('act') == 'view':
caliper_event = get_caliper_event(event, "ViewEvent", "Viewed")
batch.append(caliper_event)

if len(batch) == batch_size:
send_event_batch(batch)
batch = []

if len(batch) != 0:
send_event_batch(batch)
# Return last event time
Expand All @@ -128,6 +132,7 @@ def send_caliper_event():
cron_status = "failure"
return last_event_sent_time, cron_status


def get_caliper_event(event, event_type, event_action):
nav_path = document_path = chapter_path = ""
rsc = {}
Expand All @@ -143,17 +148,17 @@ def get_caliper_event(event, event_type, event_action):
rsc['page'] = nav_path[5]

resource = caliper.entities.Page(
id = '/'.join(nav_path),
name = rsc.get('page'),
isPartOf = caliper.entities.Chapter(
id = chapter_path,
name = rsc.get('chapter'),
isPartOf = caliper.entities.Document(
id = document_path,
name = rsc.get('document'),
)
)
)
id='/'.join(nav_path),
name=rsc.get('page'),
isPartOf=caliper.entities.Chapter(
id=chapter_path,
name=rsc.get('chapter'),
isPartOf=caliper.entities.Document(
id=document_path,
name=rsc.get('document'),
)
)
)

actor = caliper.entities.Person(id=event.get('sid'))
course_id = os.getenv("COURSE_ID")
Expand All @@ -167,60 +172,61 @@ def get_caliper_event(event, event_type, event_action):
event_time = event.get('timestamp')
if event_type == "NavigationEvent":
the_event = caliper.events.NavigationEvent(
actor = actor,
edApp = edApp,
group = organization,
object = resource,
eventTime = event_time.strftime('%Y-%m-%dT%H:%M:%S') + event_time.strftime('.%f')[:4] + 'Z',
action = event_action
)
actor=actor,
edApp=edApp,
group=organization,
object=resource,
eventTime=event_time.strftime('%Y-%m-%dT%H:%M:%S') + event_time.strftime('.%f')[:4] + 'Z',
action=event_action
)
elif event_type == "ViewEvent":
the_event = caliper.events.ViewEvent(
actor = actor,
edApp = edApp,
group = organization,
object = resource,
eventTime = event_time.strftime('%Y-%m-%dT%H:%M:%S') + event_time.strftime('.%f')[:4] + 'Z',
action = event_action
actor=actor,
edApp=edApp,
group=organization,
object=resource,
eventTime=event_time.strftime('%Y-%m-%dT%H:%M:%S') + event_time.strftime('.%f')[:4] + 'Z',
action=event_action
)
return the_event


def send_event_batch(batch):
# Multiple LRW support: https://github.com/tl-its-umich-edu/python-caliper-tester
lrw_type = os.getenv('LRW_TYPE',"").lower()
token = os.getenv('LRW_TOKEN',"")
lrw_type = os.getenv('LRW_TYPE', "").lower()
token = os.getenv('LRW_TOKEN', "")
lrw_server = os.getenv('LRW_SERVER', "")

if lrw_type == 'unizin':
lrw_endpoint = lrw_server
elif lrw_type == 'ltitool':
lrw_endpoint = "{lrw_server}/caliper/event?key={token}".format(lrw_server = lrw_server, token = token)
lrw_endpoint = "{lrw_server}/caliper/event?key={token}".format(lrw_server=lrw_server, token=token)
else:
sys.exit("LRW Type {lrw_type} not supported".format(lrw_type = lrw_type))
sys.exit("LRW Type {lrw_type} not supported".format(lrw_type=lrw_type))

the_config = caliper.HttpOptions(
host="{0}".format(lrw_endpoint),
auth_scheme='Bearer',
api_key=token,
debug=True)

the_sensor = caliper.build_simple_sensor(
sensor_id = os.getenv("SENSOR_ID", "{0}/test_caliper".format(lrw_server)),
config_options = the_config )
sensor_id=os.getenv("SENSOR_ID", "{0}/test_caliper".format(lrw_server)),
config_options=the_config)

logger.info("Sending {} events".format(len(batch)))
the_sensor.send(batch)

logger.info (the_sensor.status_code)
logger.info(the_sensor.status_code)
if (the_sensor.status_code != 200):
if (the_sensor.debug):
logger.info(pformat(the_sensor.debug[0].content))
raise Exception(f"Exception sending events code is {the_sensor.status_code}")

logger.info("event batch completed successfully!")


def update_runtime_table(last_event_time, cron_status):
def update_runtime_table(last_event_time, cron_status):
# Insert now into the runtime table after sending event
now = datetime.utcnow()
event_time = now.strftime('%Y-%m-%d %H:%M:%S')
Expand All @@ -229,11 +235,12 @@ def update_runtime_table(last_event_time, cron_status):
INSERT INTO cron_run_info (cron_job, last_run_time, last_run_status, last_sent_event_time)
VALUES ('{cron_job}', '{last_run_time}', '{last_run_status}', '{last_sent_event_time}');
""".format(
cron_job = cron_name,
last_run_time = event_time,
last_run_status = cron_status,
last_sent_event_time = last_event_time))
cron_job=cron_name,
last_run_time=event_time,
last_run_status=cron_status,
last_sent_event_time=last_event_time))
conn.commit()


last_event_time, cron_status = send_caliper_event()
update_runtime_table(last_event_time, cron_status)
update_runtime_table(last_event_time, cron_status)
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[pycodestyle]
statistics = True
count = True
ignore = E401
max-line-length = 120

0 comments on commit 56ef6ec

Please sign in to comment.