def list_blackouts(): query = qb.from_params(request.args, customers=g.customers) total = Blackout.count(query) paging = Page.from_params(request.args, total) blackouts = Blackout.find_all(query, page=paging.page, page_size=paging.page_size) if blackouts: return jsonify( status='ok', page=paging.page, pageSize=paging.page_size, pages=paging.pages, more=paging.has_more, blackouts=[blackout.serialize for blackout in blackouts], total=total) else: return jsonify(status='ok', page=paging.page, pageSize=paging.page_size, pages=paging.pages, more=paging.has_more, message='not found', blackouts=[], total=0)
def bulk_set_status(): status = request.json.get('status', None) text = request.json.get('text', 'bulk status update') timeout = request.json.get('timeout', None) if not status: raise ApiError("must supply 'status' as json data", 400) query = qb.from_params(request.args) alerts = Alert.find_all(query) if not alerts: raise ApiError('not found', 404) updated = [] errors = [] for alert in alerts: try: alert, status, text = process_status(alert, status, text) except RejectException as e: errors.append(str(e)) continue except Exception as e: errors.append(str(e)) continue if alert.set_status(status, text, timeout): updated.append(alert.id) if errors: raise ApiError('failed to bulk set alert status', 500, errors=errors) else: return jsonify(status='ok', updated=updated, count=len(updated))
def list_twilio_rules(): query = qb.from_params(request.args, customers=g.customers) total = TwilioRule.count(query) paging = Page.from_params(request.args, total) twilio_rules = TwilioRule.find_all(query, page=paging.page, page_size=paging.page_size) if twilio_rules: return jsonify( status='ok', page=paging.page, pageSize=paging.page_size, pages=paging.pages, more=paging.has_more, twilioRules=[ twilio_rule.serialize for twilio_rule in twilio_rules ], total=total, ) else: return jsonify( status='ok', page=paging.page, pageSize=paging.page_size, pages=paging.pages, more=paging.has_more, message='not found', twilioRules=[], total=0, )
def list_customers(): query = qb.from_params(request.args, customers=g.customers) total = Customer.count(query) paging = Page.from_params(request.args, total) customers = [ c for c in Customer.find_all( query, page=paging.page, page_size=paging.page_size) if Scope.admin in g.scopes or Scope.admin_customers in g.scopes or c.customer in g.customers ] if customers: return jsonify( status='ok', page=paging.page, pageSize=paging.page_size, pages=paging.pages, more=paging.has_more, customers=[customer.serialize for customer in customers], total=total) else: return jsonify(status='ok', page=paging.page, pageSize=paging.page_size, pages=paging.pages, more=paging.has_more, message='not found', customers=[], total=0)
def search_users(): query = qb.from_params(request.args) users = User.find_all(query) # add admins defined in server config if 'admin' in request.args.getlist('roles'): for admin in set(current_app.config['ADMIN_USERS']): user = User.find_by_email(admin) if user: users.append(user) # remove admins whose default role is 'user' if 'admin' not in request.args.getlist('roles'): users = [u for u in users if 'admin' not in u.roles] if users: return jsonify(status='ok', users=[user.serialize for user in users], domains=current_app.config['ALLOWED_EMAIL_DOMAINS'], total=len(users)) else: return jsonify(status='ok', message='not found', users=[], domains=current_app.config['ALLOWED_EMAIL_DOMAINS'], total=0)
def list_groups(): query = qb.from_params(request.args) total = Group.count(query) paging = Page.from_params(request.args, total) groups = Group.find_all(query, page=paging.page, page_size=paging.page_size) if groups: return jsonify(status='ok', page=paging.page, pageSize=paging.page_size, pages=paging.pages, more=paging.has_more, groups=[group.serialize for group in groups], total=total) else: return jsonify(status='ok', page=paging.page, pageSize=paging.page_size, pages=paging.pages, more=paging.has_more, message='not found', groups=[], total=0)
def test_equal_to(self): search_params = MultiDict([('status', 'open'), ('environment', 'Production')]) with self.app.test_request_context(): query = qb.from_params(search_params) # noqa # FIXME
def list_heartbeats(): query = qb.from_params(request.args, customers=g.customers) total = Heartbeat.count(query) paging = Page.from_params(request.args, total) heartbeats = Heartbeat.find_all(query, page=paging.page, page_size=paging.page_size) if heartbeats: return jsonify( status='ok', page=paging.page, pageSize=paging.page_size, pages=paging.pages, more=paging.has_more, heartbeats=[heartbeat.serialize for heartbeat in heartbeats], total=total) else: return jsonify(status='ok', page=paging.page, pageSize=paging.page_size, pages=paging.pages, more=paging.has_more, message='not found', heartbeats=[], total=0)
def list_keys(): query = qb.from_params(request.args, customers=g.customers) total = ApiKey.count(query) paging = Page.from_params(request.args, total) if not current_app.config['AUTH_REQUIRED']: keys = ApiKey.find_all(query, page=paging.page, page_size=paging.page_size) elif Scope.admin in g.scopes or Scope.admin_keys in g.scopes: keys = ApiKey.find_all(query, page=paging.page, page_size=paging.page_size) elif not g.get('login', None): raise ApiError("Must define 'user' to list user keys", 400) else: keys = ApiKey.find_by_user(user=g.login) if keys: return jsonify(status='ok', page=paging.page, pageSize=paging.page_size, pages=paging.pages, more=paging.has_more, keys=[key.serialize for key in keys], total=total) else: return jsonify(status='ok', page=paging.page, pageSize=paging.page_size, pages=paging.pages, more=paging.has_more, message='not found', keys=[], total=0)
def list_users(): query = qb.from_params(request.args) total = User.count(query) paging = Page.from_params(request.args, total) users = User.find_all(query, page=paging.page, page_size=paging.page_size) if users: return jsonify(status='ok', page=paging.page, pageSize=paging.page_size, pages=paging.pages, more=paging.has_more, users=[user.serialize for user in users], domains=current_app.config['ALLOWED_EMAIL_DOMAINS'], total=total) else: return jsonify(status='ok', page=paging.page, pageSize=paging.page_size, pages=paging.pages, more=paging.has_more, message='not found', users=[], domains=current_app.config['ALLOWED_EMAIL_DOMAINS'], total=0)
def list_perms(): query = qb.from_params(request.args) perms = Permission.find_all(query) admin_perm = Permission(match=DEFAULT_ADMIN_ROLE, scopes=[Scope.admin]) user_perm = Permission(match='user', scopes=current_app.config['USER_DEFAULT_SCOPES']) guest_perm = Permission(match='guest', scopes=current_app.config['GUEST_DEFAULT_SCOPES']) # add system-defined roles 'admin', 'user' and 'guest if 'scopes' in request.args: want_scopes = request.args.getlist('scopes') if set(admin_perm.scopes) & set(want_scopes): perms.append(admin_perm) if set(user_perm.scopes) & set(want_scopes): perms.append(user_perm) if set(guest_perm.scopes) & set(want_scopes): perms.append(guest_perm) else: perms.append(admin_perm) perms.append(user_perm) perms.append(guest_perm) if perms: return jsonify(status='ok', permissions=[perm.serialize for perm in perms], total=len(perms)) else: return jsonify(status='ok', message='not found', permissions=[], total=0)
def bulk_untag_alert(): if not request.json.get('tags', None): raise ApiError("must supply 'tags' as json list") query = qb.from_params(request.args) updated = Alert.untag_find_all(query, tags=request.json['tags']) return jsonify(status='ok', updated=updated, count=len(updated))
def get_groups(): query = qb.from_params(request.args, customers=g.customers) groups = Alert.get_groups(query) if groups: return jsonify(status='ok', groups=groups, total=len(groups)) else: return jsonify(status='ok', message='not found', groups=[], total=0)
def get_tags(): query = qb.from_params(request.args, customers=g.customers) tags = Alert.get_tags(query) if tags: return jsonify(status='ok', tags=tags, total=len(tags)) else: return jsonify(status='ok', message='not found', tags=[], total=0)
def bulk_update_attributes(): if not request.json.get('attributes', None): raise ApiError("must supply 'attributes' as json data", 400) query = qb.from_params(request.args) updated = Alert.update_attributes_find_all(query, request.json['attributes']) return jsonify(status='ok', updated=updated, count=len(updated))
def get_top10_flapping(): query = qb.from_params(request.args) top10 = Alert.get_top10_flapping(query) if top10: return jsonify(status='ok', top10=top10, total=len(top10)) else: return jsonify(status='ok', message='not found', top10=[], total=0)
def get_services(): query = qb.from_params(request.args) services = Alert.get_services(query) if services: return jsonify(status="ok", services=services, total=len(services)) else: return jsonify(status="ok", message="not found", services=[], total=0)
def get_services(): query = qb.from_params(request.args, customers=g.customers) services = Alert.get_services(query) if services: return jsonify(status='ok', services=services, total=len(services)) else: return jsonify(status='ok', message='not found', services=[], total=0)
def get_tags(): query = qb.from_params(request.args) tags = Alert.get_tags(query) if tags: return jsonify(status="ok", tags=tags, total=len(tags)) else: return jsonify(status="ok", message="not found", tags=[], total=0)
def get_top10_standing(): query = qb.from_params(request.args) top10 = Alert.get_top10_standing(query) if top10: return jsonify(status="ok", top10=top10, total=len(top10)) else: return jsonify(status="ok", message="not found", top10=[], total=0)
def get_counts(): query = qb.from_params(request.args) severity_count = Alert.get_counts_by_severity(query) status_count = Alert.get_counts_by_status(query) return jsonify(status="ok", total=sum(severity_count.values()), severityCounts=severity_count, statusCounts=status_count)
def get_counts(): query = qb.from_params(request.args, customers=g.customers) severity_count = Alert.get_counts_by_severity(query) status_count = Alert.get_counts_by_status(query) return jsonify(status='ok', total=sum(severity_count.values()), severityCounts=severity_count, statusCounts=status_count, autoRefresh=Switch.find_by_name('auto-refresh-allow').is_on)
def history(): query = qb.from_params(request.args) history = Alert.get_history(query) if history: return jsonify(status="ok", history=[h.serialize for h in history], total=len(history)) else: return jsonify(status="ok", message="not found", history=[], total=0)
def list_groups(): query = qb.from_params(request.args) groups = Group.find_all(query) if groups: return jsonify(status='ok', groups=[group.serialize for group in groups], total=len(groups)) else: return jsonify(status='ok', message='not found', groups=[], total=0)
def oembed(format): try: url = request.args['url'] title = request.args['title'] except Exception as e: return jsonify(status='error', message=str(e)), 400 try: o = urlparse(url) query = qb.from_params(request.args) except Exception as e: return jsonify(status='error', message=str(e)), 500 if o.path.endswith('/alerts/count'): try: qvars = dict() qvars['status'] = 'open' query = Query(where='"status"=%(status)s', vars=qvars, sort='', group='') severity_count = db.get_counts_by_severity(query) except Exception as e: return jsonify(status='error', message=str(e)), 500 max = 'none' if severity_count.get('informational', 0) > 0: max = 'informational' if severity_count.get('warning', 0) > 0: max = 'warning' if severity_count.get('minor', 0) > 0: max = 'minor' if severity_count.get('major', 0) > 0: max = 'major' if severity_count.get('critical', 0) > 0: max = 'critical' html = render_template('oembed/counts.html', title=title, max=max, counts=severity_count) headers = {"Access-Control-Allow-Origin": "*"} return jsonify(version='1.0', type='rich', title=title, provider_name='Alerta', provider_url=request.url_root, html=html), 200, headers elif o.path.endswith('/alerts/top10/count'): # TODO: support top10 oembed widget pass else: return jsonify(status='error', message='unsupported oEmbed URL scheme'), 400
def test_attributes(self): search_params = MultiDict([('attributes.country_code', 'US')]) with self.app.test_request_context(): query = qb.from_params(search_params) if self.app.config['DATABASE_URL'].startswith('postgres'): self.assertIn('AND attributes @> %(attr_country_code)s', query.where) self.assertEqual(query.vars, {'attr_country_code': {'country_code': 'US'}}) else: self.assertEqual(query.where, {'attributes.country_code': 'US'})
def list_customers(): query = qb.from_params(request.args) customers = Customer.find_all(query) if customers: return jsonify( status="ok", customers=[customer.serialize for customer in customers], total=len(customers)) else: return jsonify(status="ok", message="not found", customers=[], total=0)
def oembed(format): if format != 'json': return jsonify(status="error", message="unsupported format: %s" % format), 400 if 'url' not in request.args or 'maxwidth' not in request.args \ or 'maxheight' not in request.args: return jsonify(status="error", message="missing default parameters: url, maxwidth, maxheight"), 400 try: url = request.args['url'] width = int(request.args['maxwidth']) height = int(request.args['maxheight']) title = request.args.get('title', 'Alerts') except Exception as e: return jsonify(status="error", message=str(e)), 400 try: o = urlparse(url) query = qb.from_params(request.args) except Exception as e: return jsonify(status="error", message=str(e)), 500 if o.path.endswith('/alerts/count'): try: severity_count = db.get_counts_by_severity(query) except Exception as e: return jsonify(status="error", message=str(e)), 500 max = 'normal' if severity_count.get('warning', 0) > 0: max = 'warning' if severity_count.get('minor', 0) > 0: max = 'minor' if severity_count.get('major', 0) > 0: max = 'major' if severity_count.get('critical', 0) > 0: max = 'critical' html = render_template( 'oembed/counts.html', title=title, width=width, height=height, max=max, counts=severity_count ) return jsonify(version="1.0", type="rich", width=width, height=height, title=title, provider_name="Alerta", provider_url=request.url_root, html=html) elif o.path.endswith('/alerts/top10/count'): # TODO: support top10 oembed widget pass else: return jsonify(status="error", message="unsupported oEmbed URL scheme"), 400
def get_counts(): query = qb.from_params(request.args) severity_count = Alert.get_counts_by_severity(query) status_count = Alert.get_counts_by_status(query) return jsonify( status="ok", total=sum(severity_count.values()), severityCounts=severity_count, statusCounts=status_count )
def history(): query = qb.from_params(request.args, customers=g.customers) paging = Page.from_params(request.args, items=0) history = Alert.get_history(query, paging.page, paging.page_size) if history: return jsonify(status='ok', history=[h.serialize for h in history], total=len(history)) else: return jsonify(status='ok', message='not found', history=[], total=0)
def list_blackouts(): query = qb.from_params(request.args) blackouts = Blackout.find_all(query) if blackouts: return jsonify( status="ok", blackouts=[blackout.serialize for blackout in blackouts], total=len(blackouts)) else: return jsonify(status="ok", message="not found", blackouts=[], total=0)
def oembed(format): try: url = request.args['url'] title = request.args['title'] except Exception as e: return jsonify(status='error', message=str(e)), 400 try: o = urlparse(url) query = qb.from_params(request.args) except Exception as e: return jsonify(status='error', message=str(e)), 500 if o.path.endswith('/alerts/count'): try: qvars = dict() qvars['status'] = 'open' query = Query(where='"status"=%(status)s', vars=qvars, sort='', group='') severity_count = db.get_counts_by_severity(query) except Exception as e: return jsonify(status='error', message=str(e)), 500 max = 'none' if severity_count.get('informational', 0) > 0: max = 'informational' if severity_count.get('warning', 0) > 0: max = 'warning' if severity_count.get('minor', 0) > 0: max = 'minor' if severity_count.get('major', 0) > 0: max = 'major' if severity_count.get('critical', 0) > 0: max = 'critical' html = render_template( 'oembed/counts.html', title=title, max=max, counts=severity_count ) headers = { "Access-Control-Allow-Origin": "*" } return jsonify(version='1.0', type='rich', title=title, provider_name='Alerta', provider_url=request.url_root, html=html), 200, headers elif o.path.endswith('/alerts/top10/count'): # TODO: support top10 oembed widget pass else: return jsonify(status='error', message='unsupported oEmbed URL scheme'), 400
def list_heartbeats(): query = qb.from_params(request.args, customers=g.customers) heartbeats = Heartbeat.find_all(query) if heartbeats: return jsonify( status='ok', heartbeats=[heartbeat.serialize for heartbeat in heartbeats], total=len(heartbeats) ) else: return jsonify( status='ok', message='not found', heartbeats=[], total=0 )
def list_heartbeats(): query = qb.from_params(request.args) heartbeats = Heartbeat.find_all(query) if heartbeats: return jsonify( status="ok", heartbeats=[heartbeat.serialize for heartbeat in heartbeats], total=len(heartbeats) ) else: return jsonify( status="ok", message="not found", heartbeats=[], total=0 )
def list_blackouts(): query = qb.from_params(request.args) blackouts = Blackout.find_all(query) if blackouts: return jsonify( status="ok", blackouts=[blackout.serialize for blackout in blackouts], total=len(blackouts) ) else: return jsonify( status="ok", message="not found", blackouts=[], total=0 )
def history(): query = qb.from_params(request.args) history = Alert.get_history(query) if history: return jsonify( status="ok", history=[h.serialize for h in history], total=len(history) ) else: return jsonify( status="ok", message="not found", history=[], total=0 )
def list_customers(): query = qb.from_params(request.args) customers = Customer.find_all(query) if customers: return jsonify( status="ok", customers=[customer.serialize for customer in customers], total=len(customers) ) else: return jsonify( status="ok", message="not found", customers=[], total=0 )
def get_tags(): query = qb.from_params(request.args) tags = Alert.get_tags(query) if tags: return jsonify( status="ok", tags=tags, total=len(tags) ) else: return jsonify( status="ok", message="not found", tags=[], total=0 )
def get_services(): query = qb.from_params(request.args) services = Alert.get_services(query) if services: return jsonify( status="ok", services=services, total=len(services) ) else: return jsonify( status="ok", message="not found", services=[], total=0 )
def get_environments(): query = qb.from_params(request.args) environments = Alert.get_environments(query) if environments: return jsonify( status="ok", environments=environments, total=len(environments) ) else: return jsonify( status="ok", message="not found", environments=[], total=0 )
def get_top10_flapping(): query = qb.from_params(request.args) top10 = Alert.get_top10_flapping(query) if top10: return jsonify( status="ok", top10=top10, total=len(top10) ) else: return jsonify( status="ok", message="not found", top10=[], total=0 )
def list_users(): query = qb.from_params(request.args) users = User.find_all(query) if users: return jsonify( status='ok', users=[user.serialize for user in users], domains=current_app.config['ALLOWED_EMAIL_DOMAINS'], total=len(users) ) else: return jsonify( status='ok', message='not found', users=[], domains=current_app.config['ALLOWED_EMAIL_DOMAINS'], total=0 )
def bulk_action_alert(): from alerta.tasks import action_alerts action = request.json.get('action', None) text = request.json.get('text', 'bulk status update') timeout = request.json.get('timeout', None) if not action: raise ApiError("must supply 'action' as json data", 400) query = qb.from_params(request.args) alerts = [alert.id for alert in Alert.find_all(query)] if not alerts: raise ApiError('not found', 404) task = action_alerts.delay(alerts, action, text, timeout) return jsonify(status='ok', message='{} alerts queued for action'.format(len(alerts))), 202, {'Location': absolute_url('/_bulk/task/' + task.id)}
def list_customers(): query = qb.from_params(request.args, customers=g.customers) customers = [ c for c in Customer.find_all(query) if Scope.admin in g.scopes or Scope.admin_customers in g.scopes or c.customer in g.customers ] if customers: return jsonify( status='ok', customers=[customer.serialize for customer in customers], total=len(customers) ) else: return jsonify( status='ok', message='not found', customers=[], total=0 )
def search_alerts(): query_time = datetime.utcnow() query = qb.from_params(request.args, query_time) severity_count = Alert.get_counts_by_severity(query) status_count = Alert.get_counts_by_status(query) total = sum(severity_count.values()) paging = Page.from_params(request.args, total) alerts = Alert.find_all(query, paging.page, paging.page_size) if alerts: return jsonify( status="ok", page=paging.page, pageSize=paging.page_size, pages=paging.pages, more=paging.has_more, alerts=[alert.serialize for alert in alerts], total=total, statusCounts=status_count, severityCounts=severity_count, lastTime=max([alert.last_receive_time for alert in alerts]), autoRefresh=Switch.find_by_name('auto-refresh-allow').is_on ) else: return jsonify( status="ok", message="not found", page=paging.page, pageSize=paging.page_size, pages=0, more=False, alerts=[], total=0, severityCounts=severity_count, statusCounts=status_count, lastTime=query_time, autoRefresh=Switch.find_by_name('auto-refresh-allow').is_on )
def list_perms(): query = qb.from_params(request.args) perms = Permission.find_all(query) admin_perm = Permission( match='admin', scopes=[Scope.admin] ) user_perm = Permission( match='user', scopes=current_app.config['USER_DEFAULT_SCOPES'] ) # add system-defined roles 'admin' and 'user' if 'scopes' in request.args: want_scopes = request.args.getlist('scopes') if set(admin_perm.scopes) & set(want_scopes): perms.append(admin_perm) if set(user_perm.scopes) & set(want_scopes): perms.append(user_perm) else: perms.append(admin_perm) perms.append(user_perm) if perms: return jsonify( status='ok', permissions=[perm.serialize for perm in perms], total=len(perms) ) else: return jsonify( status='ok', message='not found', permissions=[], total=0 )
def bulk_delete_alert(): query = qb.from_params(request.args) deleted = Alert.delete_find_all(query) return jsonify(status='ok', deleted=deleted, count=len(deleted))