from flask import render_template, g, redirect, request
from db import connect_db, get_all_sum

from statistics import app

@app.before_request
def before_request():
    g.db = connect_db()
    g.fields = ["CPU", "TOTAL", "SQL", "SOLR", "REDIS", "MEMCACHED"]

@app.route("/")
def main_page():
    sort_by = request.args.get('sort_by', None)
    data = get_all_sum(g.db)
    if sort_by:
        data = sorted(data, key=lambda row: row[sort_by])
    return render_template("main_page.html", data=data)

@app.route("/add/")
def add_page():
    key = request.args.get('KEY')
    for field in g.fields:
        new_val = int(request.args.get(field, '0'))
        old_val = int(g.db.hget(key, field) or '0')
        new_val += old_val
        g.db.hset(key, field, new_val)
    g.db.hincrby(key, "REQUESTS", "1")
    return redirect("/")

from flask import render_template, g, redirect, request
from db import connect_db, get_all_sum

from statistics import app

@app.before_request
def before_request():
    g.db = connect_db()
    g.fields = ["CPU", "TOTAL", "SQL", "SOLR", "REDIS", "MEMCACHED"]

@app.route("/")
def main_page():
    sort_by = request.args.get('sort_by', None)
    data = get_all_sum(g.db)
    if sort_by:
        data = sorted(data, key=lambda row: row[sort_by])
    return render_template("main_page.html", data=data)

@app.route("/average/")
def average():
    data = get_all_sum(g.db)
    for row in data:
        req_count = row['REQUESTS']
        for k in row:
            if k != 'NAME' and k != 'REQUESTS':
                row[k] = float(row[k])/req_count
    return render_template("main_page.html", data=data)

@app.route("/add/")
def add_page():
    key = request.args.get('KEY')
    for field in g.fields:
        new_val = int(request.args.get(field, '0'))
        old_val = int(g.db.hget(key, field) or '0')
        new_val += old_val
        g.db.hset(key, field, new_val)
    g.db.hincrby(key, "REQUESTS", "1")
    return redirect("/")

Add proto of average page. Without sorting.
Add proto of average page. Without sorting.

  from flask import render_template, g, redirect, request
  from db import connect_db, get_all_sum
  
  from statistics import app
  
  @app.before_request
  def before_request():
      g.db = connect_db()
      g.fields = ["CPU", "TOTAL", "SQL", "SOLR", "REDIS", "MEMCACHED"]
  
  @app.route("/")
  def main_page():
      sort_by = request.args.get('sort_by', None)
      data = get_all_sum(g.db)
      if sort_by:
          data = sorted(data, key=lambda row: row[sort_by])
      return render_template("main_page.html", data=data)
  
+ @app.route("/average/")
+ def average():
+     data = get_all_sum(g.db)
+     for row in data:
+         req_count = row['REQUESTS']
+         for k in row:
+             if k != 'NAME' and k != 'REQUESTS':
+                 row[k] = float(row[k])/req_count
+     return render_template("main_page.html", data=data)
+ 
  @app.route("/add/")
  def add_page():
      key = request.args.get('KEY')
      for field in g.fields:
          new_val = int(request.args.get(field, '0'))
          old_val = int(g.db.hget(key, field) or '0')
          new_val += old_val
          g.db.hset(key, field, new_val)
      g.db.hincrby(key, "REQUESTS", "1")
      return redirect("/")
  
Add proto of average page. Without sorting.
## Code Before:
from flask import render_template, g, redirect, request
from db import connect_db, get_all_sum

from statistics import app

@app.before_request
def before_request():
    g.db = connect_db()
    g.fields = ["CPU", "TOTAL", "SQL", "SOLR", "REDIS", "MEMCACHED"]

@app.route("/")
def main_page():
    sort_by = request.args.get('sort_by', None)
    data = get_all_sum(g.db)
    if sort_by:
        data = sorted(data, key=lambda row: row[sort_by])
    return render_template("main_page.html", data=data)

@app.route("/add/")
def add_page():
    key = request.args.get('KEY')
    for field in g.fields:
        new_val = int(request.args.get(field, '0'))
        old_val = int(g.db.hget(key, field) or '0')
        new_val += old_val
        g.db.hset(key, field, new_val)
    g.db.hincrby(key, "REQUESTS", "1")
    return redirect("/")

## Instruction:
Add proto of average page. Without sorting.
## Code After:
from flask import render_template, g, redirect, request
from db import connect_db, get_all_sum

from statistics import app

@app.before_request
def before_request():
    g.db = connect_db()
    g.fields = ["CPU", "TOTAL", "SQL", "SOLR", "REDIS", "MEMCACHED"]

@app.route("/")
def main_page():
    sort_by = request.args.get('sort_by', None)
    data = get_all_sum(g.db)
    if sort_by:
        data = sorted(data, key=lambda row: row[sort_by])
    return render_template("main_page.html", data=data)

@app.route("/average/")
def average():
    data = get_all_sum(g.db)
    for row in data:
        req_count = row['REQUESTS']
        for k in row:
            if k != 'NAME' and k != 'REQUESTS':
                row[k] = float(row[k])/req_count
    return render_template("main_page.html", data=data)

@app.route("/add/")
def add_page():
    key = request.args.get('KEY')
    for field in g.fields:
        new_val = int(request.args.get(field, '0'))
        old_val = int(g.db.hget(key, field) or '0')
        new_val += old_val
        g.db.hset(key, field, new_val)
    g.db.hincrby(key, "REQUESTS", "1")
    return redirect("/")
