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

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):
    # Handles get requests for historical data
    class HistoryHandler(web.RequestHandler):
        def initialize(self, react):
            self.reactor = react
        def get(self):
            info = {}
            try:
                info["startdate"] = self.get_argument('startdate')
            except:
                print "no start date"
            try:
                info["enddate"] = self.get_argument('enddate')
            except:
                print "no end date"
            try:
                info["interval"] = self.get_argument('interval')
            except:
                print "no interval"
            try:
                info["query"] = self.get_argument('query')
            except:
                print "no query"
            conn = psycopg2.connect(self.reactor.options.get('database'))
            cur  = conn.cursor()
            if not "query" in info:
                self.write("no query")
            elif "startdate" in info and "enddate" in info:
                #make DB call for range startdate to enddate
                cur.execute("SELECT * FROM history WHERE day >= %s AND day <= %s AND query = %s AND hour = 24;", (info["startdate"], info["enddate"], info["query"]))
                self.write(json.dumps(cur.fetchall()))
            elif "interval" in info:
                if "startdate" in info:
                    #start from there and move forward
                    cur.execute("SELECT * FROM history WHERE day >= %s AND day <= date %s + integer \' %s \' AND query = %s  AND hour = 24;", (info["startdate"], info["startdate"], info["interval"], info["query"]))
                    self.write(json.dumps(cur.fetchall()))
                elif "enddate" in info:
                    #start from there and move backward
                    cur.execute("SELECT * FROM history WHERE day >= date %s - integer \' %s \' AND day <= date %s AND query = %s  AND hour = 24;", (info["enddate"], info["interval"], info["enddate"], info["query"]))
                    self.write(json.dumps(cur.fetchall()))
                else:
                    #Default ending date is current day
                    curDate = str(datetime.now().date().year) + "-" + str(datetime.now().date().month) + "-" + str(datetime.now().date().day)
                    cur.execute("SELECT * FROM history where day >= date %s - integer \' %s \' AND day <= date %s AND query = %s AND hour = 24;", (curDate, info["interval"], curDate, info["query"]))
                    self.write(json.dumps(cur.fetchall()))
            else:
                #dump all data with no regard to dates
                cur.execute("SELECT * FROM history WHERE query = %s  AND hour = 24;", (info["query"],))
                self.write(json.dumps(cur.fetchall()))
            conn.commit();
            cur.close();
            conn.close();
    # Handles web requests for updating alerts
    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'))<commit_msg>history get changes<commit_after>import os
from re import compile as re, M
from datetime import datetime

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):
    # Handles get requests for historical data
    class HistoryHandler(web.RequestHandler):
        def initialize(self, react):
            self.reactor = react
        def get(self):
            def format(s):
                ret = []
                for a in s:
                    ret.append(dict(value=a[1], day=str(a[2]), hour=a[3]))
                return json.dumps(ret)
            info = {}
            try:
                info["startdate"] = self.get_argument('startdate')
            except:
                print "no start date"
            try:
                info["enddate"] = self.get_argument('enddate')
            except:
                print "no end date"
            try:
                info["interval"] = self.get_argument('interval')
            except:
                print "no interval"
            try:
                info["query"] = self.get_argument('query')
            except:
                print "no query"
            conn = psycopg2.connect(self.reactor.options.get('database'))
            cur  = conn.cursor()
            if not "query" in info:
                self.write("no query")
            elif "startdate" in info and "enddate" in info:
                #make DB call for range startdate to enddate
                cur.execute("SELECT * FROM history WHERE day >= %s AND day <= %s AND query = %s AND hour = 24;", (info["startdate"], info["enddate"], info["query"]))
                self.write(format(cur.fetchall()))
            elif "interval" in info:
                if "startdate" in info:
                    #start from there and move forward
                    cur.execute("SELECT * FROM history WHERE day >= %s AND day <= date %s + integer \' %s \' AND query = %s  AND hour = 24;", (info["startdate"], info["startdate"], info["interval"], info["query"]))
                    self.write(format(cur.fetchall()))
                elif "enddate" in info:
                    #start from there and move backward
                    cur.execute("SELECT * FROM history WHERE day >= date %s - integer \' %s \' AND day <= date %s AND query = %s  AND hour = 24;", (info["enddate"], info["interval"], info["enddate"], info["query"]))
                    self.write(format(cur.fetchall()))
                else:
                    #Default ending date is current day
                    curDate = str(datetime.now().date().year) + "-" + str(datetime.now().date().month) + "-" + str(datetime.now().date().day)
                    cur.execute("SELECT * FROM history where day >= date %s - integer \' %s \' AND day <= date %s AND query = %s AND hour = 24;", (curDate, info["interval"], curDate, info["query"]))
                    self.write(format(cur.fetchall()))
            else:
                #dump all data with no regard to dates
                cur.execute("SELECT * FROM history WHERE query = %s  AND hour = 24;", (info["query"],))
                self.write(format(cur.fetchall()))
            conn.commit();
            cur.close();
            conn.close();
    # Handles web requests for updating alerts
    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'))
{'fraction_non_alphanumeric': 0.12490983409473431, 'fraction_numerical': 0.0034864150036066363, 'mean_word_length': 1.8219131614654003, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3064544', 'n_tokens_mistral': 2269, 'n_tokens_neox': 2148, 'n_words': 663}