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
52 changes: 41 additions & 11 deletions GeoHealthCheck/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import csv
import logging
import json
from datetime import datetime, timedelta
from StringIO import StringIO
from itertools import chain
Expand All @@ -46,7 +47,7 @@
from healthcheck import sniff_test_resource, run_test_resource
from init import App
from enums import RESOURCE_TYPES
from models import Resource, Run, ProbeVars, CheckVars, Tag, User
from models import Resource, Run, ProbeVars, CheckVars, Tag, User, Recipient
from factory import Factory
from util import render_template2, send_email
import views
Expand Down Expand Up @@ -252,7 +253,8 @@ def export():
'average_response_time': round(r.average_response_time, 2),
'max_response_time': round(r.max_response_time, 2),
'reliability': round(r.reliability, 2),
'last_report': r.last_run.report
'last_report': r.last_run.report,
'recipients': r.dump_recipients()
})
return jsonify(json_dict)
elif request.url_rule.rule == '/csv':
Expand All @@ -261,13 +263,18 @@ def export():
header = [
'resource_type', 'title', 'url', 'ghc_url', 'ghc_json', 'ghc_csv',
'first_run', 'last_run', 'status', 'min_response_time',
'average_response_time', 'max_response_time', 'reliability'
'average_response_time', 'max_response_time', 'reliability',
'recipients'
]
writer.writerow(header)
for r in response['resources']:
ghc_url = '%s%s' % (CONFIG['GHC_SITE_URL'],
url_for('get_resource_by_id',
identifier=r.identifier))
# serialize recipients into a string
recipients = r.dump_recipients()
recipients_str = json.dumps(recipients)

writer.writerow([
r.resource_type,
r.title,
Expand All @@ -281,7 +288,8 @@ def export():
'%Y-%m-%dT%H:%M:%SZ'),
r.last_run.success,
round(r.average_response_time, 2),
round(r.reliability, 2)
round(r.reliability, 2),
recipients_str
])
return output.getvalue(), 200, {'Content-type': 'text/csv'}

Expand Down Expand Up @@ -324,7 +332,8 @@ def export_resource(identifier):
'%Y-%m-%dT%H:%M:%SZ'),
'history_csv': history_csv,
'history_json': history_json,
'last_report': resource.last_run.report
'last_report': resource.last_run.report,
'recipients': resource.dump_recipients()
}
return jsonify(json_dict)
elif 'csv' in request.url_rule.rule:
Expand All @@ -334,8 +343,13 @@ def export_resource(identifier):
'identifier', 'title', 'url', 'resource_type', 'owner',
'min_response_time', 'average_response_time', 'max_response_tie',
'reliability', 'status', 'first_run', 'last_run', 'history_csv',
'history_json'
'history_json', 'recipients',
]

# serialize recipients into a string
recipients = resource.dump_recipients()
recipients_str = json.dumps(recipients)

writer.writerow(header)
writer.writerow([
resource.identifier,
Expand All @@ -353,7 +367,8 @@ def export_resource(identifier):
resource.last_run.checked_datetime.strftime(
'%Y-%m-%dT%H:%M:%SZ'),
history_csv,
history_json
history_json,
recipients_str
])
return output.getvalue(), 200, {'Content-type': 'text/csv'}

Expand Down Expand Up @@ -552,6 +567,9 @@ def add():
run_to_add = Run(resource_to_add, result)

DB.session.add(resource_to_add)
# prepopulate notifications for current user
resource_to_add.set_recipients('email', [g.user.email])

if probe_to_add:
DB.session.add(probe_to_add)
for check_to_add in checks_to_add:
Expand Down Expand Up @@ -635,13 +653,19 @@ def update(resource_identifier):
resource.probe_vars.append(probe_vars)

update_counter += 1

elif key == 'notify_emails':
resource.set_recipients('email',
[v for v in value if v.strip()])
elif key == 'notify_webhooks':
resource.set_recipients('webhook',
[v for v in value if v.strip()])
elif getattr(resource, key) != resource_identifier_dict[key]:
# Update other resource attrs, mainly 'name'
setattr(resource, key, resource_identifier_dict[key])
update_counter += 1

except Exception as err:
LOGGER.error("Cannot update resource: %s", err, exc_info=err)
DB.session.rollback()
status = str(err)
update_counter = 0
Expand Down Expand Up @@ -695,8 +719,14 @@ def edit_resource(resource_identifier):

probes_avail = views.get_probes_avail(resource.resource_type, resource)

return render_template('edit_resource.html', lang=g.current_lang,
resource=resource, probes_avail=probes_avail)
suggestions = json.dumps(Recipient.get_suggestions('email',
g.user.username))

return render_template('edit_resource.html',
lang=g.current_lang,
resource=resource,
suggestions=suggestions,
probes_avail=probes_avail)


@APP.route('/resource/<int:resource_identifier>/delete')
Expand All @@ -718,7 +748,7 @@ def delete(resource_identifier):

for run in runs:
DB.session.delete(run)

resource.clear_recipients()
DB.session.delete(resource)

try:
Expand Down
Empty file.
5 changes: 1 addition & 4 deletions GeoHealthCheck/migrations/versions/2638c2a40625_.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@
import sqlalchemy as sa
import imp
import os

from GeoHealthCheck.migrations import alembic_helpers

# revision identifiers, used by Alembic.
revision = '2638c2a40625'
down_revision = '992013af402f'
branch_labels = None
depends_on = None

alembic_helpers = imp.load_source('alembic_helpers', (
os.getcwd() + '/' + op.get_context().script.dir + '/alembic_helpers.py'))


def upgrade():
if not alembic_helpers.table_has_column('resource', 'active'):
Expand Down
4 changes: 1 addition & 3 deletions GeoHealthCheck/migrations/versions/496427d03f87_.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import sqlalchemy as sa
import imp
import os

alembic_helpers = imp.load_source('alembic_helpers', (
os.getcwd() + '/' + op.get_context().script.dir + '/alembic_helpers.py'))
from GeoHealthCheck.migrations import alembic_helpers

# revision identifiers, used by Alembic.
revision = '496427d03f87'
Expand Down
4 changes: 1 addition & 3 deletions GeoHealthCheck/migrations/versions/992013af402f_.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import sqlalchemy as sa
import imp
import os

alembic_helpers = imp.load_source('alembic_helpers', (
os.getcwd() + '/' + op.get_context().script.dir + '/alembic_helpers.py'))
from GeoHealthCheck.migrations import alembic_helpers

# revision identifiers, used by Alembic.
revision = '992013af402f'
Expand Down
Empty file.
46 changes: 46 additions & 0 deletions GeoHealthCheck/migrations/versions/bb91fb332c36_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""empty message

Revision ID: bb91fb332c36
Revises: f2bac0a1cdaa
Create Date: 2018-02-13 17:08:09.313113

"""
from alembic import op
import sqlalchemy as sa
from GeoHealthCheck.migrations import alembic_helpers


# revision identifiers, used by Alembic.
revision = 'bb91fb332c36'
down_revision = '2638c2a40625'
branch_labels = None
depends_on = None


def upgrade():
if not alembic_helpers.tables_exist(['recipient']):
print('creating Recipient table(s)')

# ### commands auto generated by Alembic - please adjust! ###
op.create_table('recipient',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('channel', sa.Enum('email', 'webhook', name='recipient_channel_types'), nullable=False),
sa.Column('location', sa.Text(), nullable=False),
sa.PrimaryKeyConstraint('id')
)

op.create_table('resourcenotification',
sa.Column('resource_id', sa.Integer(), nullable=False),
sa.Column('recipient_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['resource_id'], ['resource.identifier'], ),
sa.ForeignKeyConstraint(['recipient_id'], ['recipient.id'], ),
sa.PrimaryKeyConstraint('resource_id', 'recipient_id')
)
# ### end Alembic commands ###
else:
print('Tables for recipient support already present, will not create them')


def downgrade():
op.drop_table('resourcenotification')
op.drop_table('recipient')
Loading