Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import re
- import flask
- import settings
- import pg
- # some aliasses
- request = flask.request
- Response = flask.Response
- json = flask.json
- app = flask.Flask(__name__)
- @app.route('/', methods=['POST'])
- def receive_mattermost():
- """We only have 1 incoming hook"""
- form = request.form
- command = form['command']
- token = form['token']
- run_id = form['text']
- if run_id is None:
- return ''
- if command == '/runstatus':
- if settings.MATTERMOST_RUNSTATUS_TOKEN:
- if not token == settings.MATTERMOST_RUNSTATUS_TOKEN:
- app.logger.error('Received wrong token, received [%s]', token)
- return ''
- resp = get_run_status_json(run_id, False)
- elif command == '/runcheck':
- if settings.MATTERMOST_WHISPER_TOKEN:
- if not token == settings.MATTERMOST_WHISPER_TOKEN:
- app.logger.error('Received wrong token, received [%s]', token)
- return ''
- resp = get_run_status(run_id, True)
- else:
- resp = None
- if resp is None:
- return ''
- return resp
- def get_run_status(run_id, whisper):
- resp = execute_pg_req(run_id)
- if resp['percentage_done'] < 0.25:
- color = '#ff5400'
- elif resp['percentage_done'] < 0.5:
- color = '#ff9000'
- elif resp['percentage_done'] < 0.75:
- color = '#ffe400'
- else:
- color = 'good'
- if whisper:
- resp_type = 'ephemeral'
- else:
- resp_type= 'in_channel'
- jresp = json.dumps({'response_type': resp_type,
- 'text': ('#### Run '+ run_id +' status on ' +
- settings.user +
- '\n| Run duration | Completed | Sources processed | Total Sources | Sources/min | Estimated Remaining time |\n'
- '| :----------: | :-------: | :---------------: | :-----------: | :---------: | :----------------------: |\n'
- '| ' + resp['run_duration'] + ' | ' + str('%.2f' % (resp['percentage_done'] * 100)) +
- '% | ' + str('%.0f' % resp['processed']) + ' | '
- '' + str('%.0f' % resp['total_sources']) + ' | ' +
- str('%.2f' % resp['sources_per_min']) + ' | ' +
- resp['time_remaining_estimate'] + ' |\n'
- )
- })
- return Response(jresp, status=200, mimetype='application/json')
- def get_run_status_json(run_id, whisper):
- resp = execute_pg_req(run_id)
- if whisper:
- resp_type = 'ephemeral'
- else:
- resp_type= 'in_channel'
- if resp['percentage_done'] < 0.25:
- color = '#ff5400'
- elif resp['percentage_done'] < 0.5:
- color = '#ff9000'
- elif resp['percentage_done'] < 0.75:
- color = '#ffe400'
- else:
- color = 'good'
- jresp = json.dumps({'response_type': resp_type,
- 'text': ('Run status on ' + settings.user),
- 'attachments': [
- {
- 'color': color,
- 'pretext': 'Run progress',
- 'title': ('Run ' + run_id),
- 'fields': [
- {
- 'title': 'Time since created',
- 'value': str(resp['run_duration']),
- 'short': 'true'
- },
- {
- 'title': 'Completed',
- 'value': str('%.2f' % (resp['percentage_done'] * 100)),
- 'short': 'true'
- }
- ]
- },
- {
- 'color': '#888888',
- 'pretext': 'Detailed statistics',
- 'title': run_id,
- 'fields': [
- {
- 'title': 'Sources Processed',
- 'value': str('%.f' % resp['processed']),
- 'short': 'true'
- },
- {
- 'title': 'Total Sources',
- 'value': str('%.f' % resp['total_sources']),
- 'short': 'true'
- },
- {
- 'title': 'Sources per minute',
- 'value': str('%.f' % resp['sources_per_min']),
- 'short': 'true'
- },
- {
- 'title': 'Estimated Completion',
- 'value': str(resp['time_remaining_estimate']),
- 'short': 'true'
- }
- ]
- }
- ]
- }
- )
- print(jresp)
- response = Response(jresp, status=200, mimetype='application/json')
- return response
- def execute_pg_req(runid):
- connection = pg.connect(dbname=settings.db, host=settings.host, port=settings.port, user=settings.user,
- passwd=settings.passwd)
- query = connection.query('select * from getactiverunstatistics(' + runid + ');')
- speeds = query.dictresult()[0]
- nquery = connection.query('select fname, size from catalog where catalogid=getruncatalogid(' + runid + ')')
- connection.close()
- speeds.update(nquery.dictresult()[0])
- return speeds
- # def search_token(text):
- # """Search in the provided text for a match on the regexp, and return"""
- # match = re.search('(%s)' % settings.TICKET_REGEXP, text)
- # if match:
- # return match.group(0)
- # else:
- # return None
- if __name__ == '__main__':
- app.run(host='0.0.0.0', debug=True)
- # vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement