<commit_before>import os
from re import compile as re, M

import json
import logging
import psycopg2
from tornado import ioloop, log, web

from .alerts import BaseAlert
from .utils import parse_interval
from .handlers import registry


LOGGER = log.gen_log

COMMENT_RE = re('//\s+.*$', M)



class Reactor(object):
    class UpdateHandler(web.RequestHandler):
        # react is the overall reactor object such that options can be changed
        def initialize(self, react):
            self.reactor = react
        
        # Supports upsert. Adds new alerts or updates existing alerts with data
        def put(self, arg):
            info = json.loads(self.request.body)
            for i in range(len(self.reactor.options.get('alerts'))):
                if self.reactor.options.get('alerts')[i].get('query').strip() == info.get('query').strip():
                    self.reactor.options.get('alerts')[i] = info
                    print "replaced"
                    break
            else:
                print "nothing happened"
            self.reactor.reinit()
            conn = psycopg2.connect(self.reactor.options.get('database'))
            cur  = conn.cursor()
            #Upsert
            cur.execute("UPDATE alerts SET name = %s, source = %s, format = %s, interval = %s, history_size = %s, rules = %s WHERE query = %s;", (info['name'], info['source'], info['format'], info['interval'], info['history_size'], ', '.join(info['rules']), info['query']))
            cur.execute("INSERT INTO alerts (query, name, source, format, interval, history_size, rules) SELECT %s, %s, %s, %s, %s, %s, %s WHERE NOT EXISTS (SELECT 1 FROM alerts WHERE query = %s);", (info['query'], info['name'], info['source'], info['format'], info['interval'], info['history_size'], ', '.join(info['rules']), info['query']))
            conn.commit()
            cur.close()
            conn.close()
            self.write("All good")
            
        # Deletes alert with arg-specified query 
        def delete(self, arg):
            for i in range(len(self.reactor.options.get('alerts'))):
                if self.reactor.options.get('alerts')[i].get('query') == arg:
                    break
            self.reactor.options.get('alerts').pop(i)
            self.reactor.reinit()
            conn = psycopg2.connect(self.reactor.options.get('database'))
            cur  = conn.cursor()
            try:
                cur.execute("DELETE FROM alerts WHERE query = %s;", (arg,))
                cur.execute("DELETE FROM cache WHERE original_query = %s;", (arg,))
            except Exception as e:
                print e
            #    self.write(e)
            conn.commit()
            cur.close()
            conn.close()
            self.write("All good")
            
        # Adds new alert. Unnecessary, use PUT    
        def post(self, arg):
            info = json.loads(self.request.body)
            self.reactor.options.get('alerts').append(info)
            self.reactor.reinit()
            conn = psycopg2.connect(self.reactor.options.get('database'))
            cur  = conn.cursor()
            try:
                cur.execute("INSERT INTO alerts (name, query, source, format, interval, history_size, rules) VALUES (%s, %s, %s, %s, %s, %s, %s);", (info['name'], info['query'], info['source'], info['format'], info['interval'], info['history_size'], ', '.join(info['rules'])))
            except Exception as e:
                self.write(e)
            conn.commit()
            cur.close()
            conn.close()
            self.write("All good")
            
        # No arguments: Return current options (including setup of alerts)
        # Query Specified: Returns the alert settings of the given query
        def get(self, arg):
            if arg == "":
                tempDict = dict(self.reactor.options)
                if not 'alerts' in tempDict:
                    tempDict['alerts'] = []
                conn = psycopg2.connect(self.reactor.options.get('database'))
                cur  = conn.cursor()
                for alert in tempDict['alerts']:
                    cur.execute("SELECT * FROM cache WHERE original_query=%s", (alert['query'],))
                    alert['events'] = []
                    for item in cur.fetchall():
                        a = {}
                        a['resolved_query'] = item[1]
                        a['description'] = item[3]
                        a['level'] = item[2]
                        alert['events'].append(a)
                self.write(json.dumps(tempDict))
            else:
                for alert in self.reactor.options.get('alerts'):
                    if alert['query'] == arg:
                        self.write(json.dumps(alert))
                        break
                else:
                    self.write("Query not found")
    """ Class description. """

    defaults = {
        'auth_password': <PASSWORD>,
        'auth_username': None,
        'config': 'config.json',
        'critical_handlers': ['log', 'smtp'],
        'debug': False,
        'format': 'short',
        'graphite_url': 'http://localhost',
        'history_size': '1day',
        'interval': '10minute',
        'logging': 'info',
        'method': 'average',
        'normal_handlers': ['log', 'smtp'],
        'pidfile': None,
        'prefix': '[BEACON]',
        'repeat_interval': '2hour',
        'request_timeout': 20.0,
        'send_initial': False,
        'warning_handlers': ['log', 'smtp'],<commit_msg>resolved query finder get request<commit_after>import os
from re import compile as re, M

import json
import logging
import psycopg2
from tornado import ioloop, log, web

from .alerts import BaseAlert
from .utils import parse_interval
from .handlers import registry


LOGGER = log.gen_log

COMMENT_RE = re('//\s+.*$', M)



class Reactor(object):
    class UpdateHandler(web.RequestHandler):
        # react is the overall reactor object such that options can be changed
        def initialize(self, react):
            self.reactor = react
        
        # Supports upsert. Adds new alerts or updates existing alerts with data
        def put(self, arg):
            info = json.loads(self.request.body)
            for i in range(len(self.reactor.options.get('alerts'))):
                if self.reactor.options.get('alerts')[i].get('query').strip() == info.get('query').strip():
                    self.reactor.options.get('alerts')[i] = info
                    print "replaced"
                    break
            else:
                print "nothing happened"
            self.reactor.reinit()
            conn = psycopg2.connect(self.reactor.options.get('database'))
            cur  = conn.cursor()
            #Upsert
            cur.execute("UPDATE alerts SET name = %s, source = %s, format = %s, interval = %s, history_size = %s, rules = %s WHERE query = %s;", (info['name'], info['source'], info['format'], info['interval'], info['history_size'], ', '.join(info['rules']), info['query']))
            cur.execute("INSERT INTO alerts (query, name, source, format, interval, history_size, rules) SELECT %s, %s, %s, %s, %s, %s, %s WHERE NOT EXISTS (SELECT 1 FROM alerts WHERE query = %s);", (info['query'], info['name'], info['source'], info['format'], info['interval'], info['history_size'], ', '.join(info['rules']), info['query']))
            conn.commit()
            cur.close()
            conn.close()
            self.write("All good")
            
        # Deletes alert with arg-specified query 
        def delete(self, arg):
            for i in range(len(self.reactor.options.get('alerts'))):
                if self.reactor.options.get('alerts')[i].get('query') == arg:
                    break
            self.reactor.options.get('alerts').pop(i)
            self.reactor.reinit()
            conn = psycopg2.connect(self.reactor.options.get('database'))
            cur  = conn.cursor()
            try:
                cur.execute("DELETE FROM alerts WHERE query = %s;", (arg,))
                cur.execute("DELETE FROM cache WHERE original_query = %s;", (arg,))
            except Exception as e:
                print e
            #    self.write(e)
            conn.commit()
            cur.close()
            conn.close()
            self.write("All good")
            
        # Adds new alert. Unnecessary, use PUT    
        def post(self, arg):
            info = json.loads(self.request.body)
            self.reactor.options.get('alerts').append(info)
            self.reactor.reinit()
            conn = psycopg2.connect(self.reactor.options.get('database'))
            cur  = conn.cursor()
            try:
                cur.execute("INSERT INTO alerts (name, query, source, format, interval, history_size, rules) VALUES (%s, %s, %s, %s, %s, %s, %s);", (info['name'], info['query'], info['source'], info['format'], info['interval'], info['history_size'], ', '.join(info['rules'])))
            except Exception as e:
                self.write(e)
            conn.commit()
            cur.close()
            conn.close()
            self.write("All good")
            
        # No arguments: Return current options (including setup of alerts)
        # Query Specified: Returns the alert settings of the given query
        def get(self, arg):
            if arg == "":
                tempDict = dict(self.reactor.options)
                if not 'alerts' in tempDict:
                    tempDict['alerts'] = []
                conn = psycopg2.connect(self.reactor.options.get('database'))
                cur  = conn.cursor()
                for alert in tempDict['alerts']:
                    cur.execute("SELECT * FROM cache WHERE original_query=%s", (alert['query'],))
                    alert['events'] = []
                    for item in cur.fetchall():
                        a = {}
                        a['resolved_query'] = item[1]
                        a['description'] = item[3]
                        a['level'] = item[2]
                        alert['events'].append(a)
                self.write(json.dumps(tempDict))
            else:
                for alert in self.reactor.options.get('alerts'):
                    for event in alert['events']:
                        if event['resolved_query'] == arg:
                            self.write(json.dumps(alert))
                            break
                    else:
                        continue
                    break
                else:
                    self.write("Query not found")
    """ Class description. """

    defaults = {
        'auth_password': <PASSWORD>,
        'auth_username': None,
        'config': 'config.json',
        'critical_handlers': ['log', 'smtp'],
        'debug': False,
        'format': 'short',
        'graphite_url': 'http://localhost',
        'history_size': '1day',
        'interval': '10minute',
        'logging': 'info',
        'method': 'average',
        'normal_handlers': ['log', 'smtp'],
        'pidfile': None,
        'prefix': '[BEACON]',
        'repeat_interval': '2hour',
        'request_timeout': 20.0,
        'send_initial': False,
        'warning_handlers': ['log', 'smtp'],
{'fraction_non_alphanumeric': 0.1428826257581163, 'fraction_numerical': 0.0028540849090260435, 'mean_word_length': 1.794865403788634, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '885781', 'n_tokens_mistral': 3148, 'n_tokens_neox': 3022, 'n_words': 772}