from flask import Flask
from flask import jsonify
import psycopg2
import os
import json
app = Flask(__name__)

# Database access constants
DB_PASSWORD = os.environ.get('DB_PASS', '')
DB_NAME = os.environ.get('DB_NAME', '')
DB_USER = os.environ.get('DB_USER', DB_NAME)
DB_PORT = int(os.environ.get('DB_PORT', '54322'))
DB_HOST = 'localhost'

# Connect to database
conn = psycopg2.connect(dbname=DB_NAME, user=DB_USER, password=DB_PASSWORD, port=DB_PORT, host=DB_HOST)
cur = conn.cursor()

# Returns the training data
# TODO: Populate using DB data instead of hardcoding
@app.route('/<int:job_id>')
def training_data(job_id):
    # pre_vector is dict. of index:count that can be reconstructed into a vector
    # num_reductions is the dimension of that vector
    offset = 0
    limit = 5000
    # TODO: Actually include offset, limit as parameters
    cur.execute('SELECT vector,label FROM vectors WHERE job_id=%s OFFSET %s LIMIT %s', (job_id, offset, limit))
    training_examples = [(v,l) for v,l in cur]
    data = {
        'training_examples': training_examples
    }
    return jsonify(data)

# Returns length of each input vector
# TODO: Populate using DB data instead of hardcoding
@app.route('/input_vector_length')
def input_vector_length():
    cur.execute('SELECT vector FROM vectors LIMIT 1')
    vector_len = json.loads(cur.fetchone()[0])['reductions']
    data = {
        'input_vector_length': vector_len
    }
    return jsonify(data)

# Returns number of classes we are classifying
# TODO: Populate using DB data instead of hardcoding
@app.route('/num_classes')
def num_classes():
    data = {
        'num_classes': 2
    }
    return jsonify(data)

# Returns the number of total examples
# TODO: Populate using DB data instead of hardcoding
@app.route('/num_examples')
def num_examples():
    cur.execute('SELECT count(*) FROM vectors')
    num_examples = cur.fetchone()[0]
    data = {
        'num_examples': num_examples
    }
    return jsonify(data)

@@ -43,16 +43,42 @@
 jsonify%0A
+from flask import request%0A
 import p
@@ -521,20 +521,16 @@
 rns 
-the 
 training
  dat
@@ -529,66 +529,57 @@
 ning
- data%0A# TODO: Populate using DB data instead of hardcoding
+_examples for a given job_id from offset to limit
 %0A@ap
@@ -638,226 +638,93 @@
     
-# pre_vector is dict. of index:count that can be reconstructed into a vector%0A    # num_reductions is the dimension of that vector%0A    offset = 0%0A    limit = 5000%0A    # TODO: Actually include offset, limit as parameters
+offset = request.args.get('offset', None)%0A    limit = request.args.get('limit', None)
 %0A   
@@ -1828,28 +1828,263 @@
  %7D%0A    return jsonify(data)%0A
+%0A# An endpoint solely for testing, returns all vectors in table%0A@app.route('/all_vectors')%0Adef all_vectors():%0A    cur.execute('SELECT * FROM vectors')%0A    data = %7B%0A        'all_vectors': %5Bx for x in cur%5D%0A    %7D%0A    return jsonify(data)%0A

Add limit, offset to training_examples endpoint