#Source: https://github.com/mganache/helloapp/

from flask import Flask
from flask import render_template
from datetime import datetime, timedelta
import json
import os.path, time
import operator
from pymongo import MongoClient

#TODO: https://pythonhosted.org/Flask-Cache/
#from flask.ext.cache import Cache

MONGO_URL = os.environ['connectURL']
connection = MongoClient(MONGO_URL)
#TODO: Remove hardcoded value + read from settings
db = connection.githublive.pushevent


#Global variables
LimitActiveLanguages=5
LimitActiveRepositories=5
LimitActiveUsers=5

def TotalEntries ():
    return db.count()

def FindOneTimeStamp(type):
    pipeline= [
           { '$match': {} }, 
           { '$project': { '_id': 0, 'created_at': '$created_at'}},
           { '$sort' : { 'created_at': type }},
           { '$limit': 1}
           ]
    mycursor = db.aggregate(pipeline)
    for record in mycursor["result"]:
        return (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(record["created_at"]/1000)))

def ActiveLanguages ():
    pipeline= [
           { '$match': {'$and': [{"language":{"$ne":"null"}},{"language":{"$ne":None}}] }},  
           { '$group': {'_id': {'language': '$language'}, 'count': { '$sum' : 1 }}},
           { '$project': { '_id': 0, 'language': '$_id.language', 'count': '$count' } },
           { '$sort' : { 'count': -1 }},
           { '$limit': LimitActiveLanguages}
           ]
    mycursor = db.aggregate(pipeline)
    return mycursor

def ActiveRepositories ():
    pipeline= [
           { '$match': {}}, 
           { '$group': {'_id': {'url': '$url',  'name': "$name", 'language': "$language"}, 'count': { '$sum' : 1 }}},
           { '$project': { '_id': 0, 'url': '$_id.url', 'count': '$count',  'name': "$_id.name", 'language': "$_id.language" } },
           { '$sort' : { 'count': -1 }},
           { '$limit': LimitActiveRepositories}
           ]
    mycursor = db.aggregate(pipeline)
    #print "#############" , mycursor['result']
    return mycursor

def ActiveUsers ():
    pipeline= [
           { '$match': {}}, 
           { '$group': {'_id': {'actorlogin': '$actorlogin',  'actorname': "$actorname"}, 'count': { '$sum' : 1 }}},
           { '$project': { '_id': 0, 'actorname': '$_id.actorname', 'count': '$count',  'actorlogin': "$_id.actorlogin"} },
           { '$sort' : { 'count': -1 }},
           { '$limit': LimitActiveUsers}
           ]
    mycursor = db.aggregate(pipeline)
    return mycursor

def CloseDB():
    connection.close()


#http://stackoverflow.com/questions/850795/clearing-python-lists    
#def refresh_data():

app = Flask(__name__)
 
@app.route('/')
@app.route('/index')
def index():
    #refresh_data()
    return render_template("index.html",
        title = 'GitHub Analytics',
	AL = ActiveLanguages(),
        AR = ActiveRepositories(),
        AU = ActiveUsers(),
        total = TotalEntries(),
        start = FindOneTimeStamp(1),
        end = FindOneTimeStamp(-1)
	)
@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404
@app.route('/hello')
def hello(name=None):
     return render_template('hello.html', name=name)


if __name__ == '__main__':
	port = int(os.environ.get("PORT", 5000))
	app.run(host='0.0.0.0', port=os.environ['PORT'])

@@ -39,16 +39,92 @@
 elloapp/
+%0A#References%0A#https://realpython.com/blog/python/primer-on-jinja-templating/
 %0A%0Afrom f
@@ -296,16 +296,44 @@
 goClient
+%0Afrom jinja2 import Template
 %0A%0A#TODO:
@@ -569,16 +569,92 @@
 hevent%0A%0A
+#DEV settings%0A#MONGO_URL = %22%22%0A#connection = MongoClient(MONGO_URL)%0A#db =%22%22%0A%0A
 %0A#Global
@@ -709,24 +709,24 @@
 ositories=5%0A
-
 LimitActiveU
@@ -733,16 +733,17 @@
 sers=5%0A%0A
+%0A
 def Tota
@@ -2674,16 +2674,22 @@
 ose()%0A%0A%0A
+#TODO%0A
 #http://
@@ -2792,16 +2792,169 @@
 _name__)
+%0A%0A#Format integers with comma%0A@app.template_filter()%0Adef numformat(value):%0A    return %22%7B:,%7D%22.format(value)%0Aapp.jinja_env.filters%5B'numformat'%5D = numformat
 %0A %0A@app.
@@ -3597,8 +3597,50 @@
 PORT'%5D)%0A
+        #DEV%0A        #app.run(debug=True)%0A

Use Jinja templating language to format numbers and add code for testing