<commit_before>#!/usr/bin/env python3
import os
from flask import Flask, jsonify, send_from_directory, abort, Response, request, g
import psycopg2
import psycopg2.extras
import json
import pickle
import numpy as np
import re

app = Flask(__name__)

aggregated_stats_file = 'county_agg_stats.p'
aggdata               = pickle.load(open(aggregated_stats_file, "rb"))

@app.before_request
def before_request():
  g.db = psycopg2.connect("dbname='rivers' user='nelson' host='localhost' password='<PASSWORD>'")

@app.teardown_request
def teardown_request(exception):
  db = getattr(g, 'db', None)
  if db is not None:
      db.close()

<EMAIL>('/')
#def index():
#  return send_from_directory('.', 'index.html')

@app.route('/gauges/reachflow/<string:date>', methods=['GET'])
def show_reachflow(date):
  cur = g.db.cursor(cursor_factory = psycopg2.extras.RealDictCursor)
  cur.execute("SELECT * FROM reach_summary WHERE jday=%(date)s::date-'1970-01-01'::date", {"date":date})
  return json.dumps({"reachflows":cur.fetchall()})

@app.route('/gauges/getvals/<string:date>', methods=['POST'])
def show_getvals(date):
  cur         = g.db.cursor(cursor_factory = psycopg2.extras.RealDictCursor)
  gaugelist   = json.loads(request.form['gauages'])
  non_decimal = re.compile(r'[^\d]+')
  gaugelist   = filter(lambda x: non_decimal.match(x), gaugelist)
  gaugelist   = map(lambda x: "'"+x+"'", gaugelist)
  gaugelist   = ','.join(gaugelist)
  print(gaugelist)
  cur.execute("SELECT * FROM gauge_summary WHERE site_code IN ("+gaugelist+") AND jday=%(date)s::date-'1970-01-01'::date")
  return json.dumps({"gaugevals":cur.fetchall()})

@app.route('/gauges/list/<string:xmin>/<string:ymin>/<string:xmax>/<string:ymax>', methods=['GET'])
def show_gaugelist(xmin,ymin,xmax,ymax):
  cur = g.db.cursor(cursor_factory = psycopg2.extras.RealDictCursor)


  cur.execute("""
SELECT site_code,
      lng,
      lat,
      to_char(sdt, 'YYYY-MM-DD HH24:MI') as sdt,
      svalue,<commit_msg>Server sends gauge vals<commit_after>#!/usr/bin/env python3
import os
from flask import Flask, jsonify, send_from_directory, abort, Response, request, g
import psycopg2
import psycopg2.extras
import json
import pickle
import numpy as np
import re

app = Flask(__name__)

aggregated_stats_file = 'county_agg_stats.p'
aggdata               = pickle.load(open(aggregated_stats_file, "rb"))

@app.before_request
def before_request():
  g.db = psycopg2.connect("dbname='rivers' user='nelson' host='localhost' password='<PASSWORD>'")

@app.teardown_request
def teardown_request(exception):
  db = getattr(g, 'db', None)
  if db is not None:
      db.close()

#<EMAIL>('/')
#def index():
#  return send_from_directory('.', 'index.html')

@app.route('/gauges/reachflow/<string:date>', methods=['GET'])
def show_reachflow(date):
  cur = g.db.cursor(cursor_factory = psycopg2.extras.RealDictCursor)
  cur.execute("SELECT * FROM reach_summary WHERE jday=%(date)s::date-'1970-01-01'::date", {"date":date})
  return json.dumps({"reachflows":cur.fetchall()})

@app.route('/gauges/getvals/<string:date>', methods=['POST'])
def show_getvals(date):
  cur       = g.db.cursor(cursor_factory = psycopg2.extras.RealDictCursor)
  gaugelist = request.form['gauges']
  gaugelist = json.loads(gaugelist)
  print(type(gaugelist))
  print(gaugelist)
  decimal   = re.compile(r'[\d]+')
  gaugelist = list(filter(lambda x: decimal.match(x), gaugelist))
  print(gaugelist)
  gaugelist = map(lambda x: "'"+x+"'", gaugelist)
  gaugelist = ','.join(gaugelist)
  cur.execute("SELECT * FROM gauge_summary WHERE site_code IN ("+gaugelist+") AND jday=%(date)s::date-'1970-01-01'::date", {"date":date})
  return json.dumps({"gaugevals":cur.fetchall()})

@app.route('/gauges/list/<string:xmin>/<string:ymin>/<string:xmax>/<string:ymax>', methods=['GET'])
def show_gaugelist(xmin,ymin,xmax,ymax):
  cur = g.db.cursor(cursor_factory = psycopg2.extras.RealDictCursor)


  cur.execute("""
SELECT site_code,
      lng,
      lat,
      to_char(sdt, 'YYYY-MM-DD HH24:MI') as sdt,
      svalue,
{'fraction_non_alphanumeric': 0.17106573705179282, 'fraction_numerical': 0.01245019920318725, 'mean_word_length': 5.2962382445141065, 'pattern_counts': {'":': 7, '<': 19, '<?xml version=': 0, '>': 19, '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': '7173353', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1501, 'n_words': 267}