# Path: mainhandler.py
# class Handler(webapp2.RequestHandler):
#     def write(self, *a, **kw):
#         self.response.out.write(*a, **kw)
# 
#     def render_str(self, template, **params):
#         t = jinja_env.get_template(template)
#         return t.render(params)
# 
#     def render(self, template, **kw):
#         self.write(self.render_str(template, **kw))
# 
#     def render_front(self, template):
#         self.render(template)
# 
# Path: app/utilities/github_archive_bigquery_handler.py
# def execute_bigquery_githubarchive(service):
#     query = """SELECT type, actor_attributes_login, payload_action, payload_pull_request_title, repository_name, repository_url, repository_description, repository_language, url, created_at as date
#                 FROM [githubarchive:github.timeline]
#                 WHERE (type="PullRequestReviewCommentEvent" OR type="IssuesEvent" OR type="IssueCommentEvent" OR type="PullRequestEvent") AND repository_owner!="sagarsane"
#                 AND actor_attributes_login="sagarsane"
#                 AND PARSE_UTC_USEC(created_at) >= PARSE_UTC_USEC('2012-07-01 00:00:00')
#                 GROUP BY type, actor_attributes_login, payload_action, payload_pull_request_title, repository_url, repository_description, repository_name, repository_language,  url, date 
#                 ORDER BY date DESC LIMIT 100;"""
#     #return runSyncQuery(service, '<project_id>', 'test', query)
#     return queryTableData(service, '<project_id>', 'test', 'github_query_result', startIndex=0)
# 
# def execute_bigquery_toprepos(service):
#     return queryTableData(service, "<project_id>", 'windows8app', 'top_repos', startIndex=0)
# 
# def execute_bigquery_toplanguages(service):
#     return queryTableData(service, "<project_id>", 'windows8app', 'top_languages', startIndex=0)
# 
# def execute_bigquery_mostactive_users(service):
#     return queryTableData(service, "<project_id>", 'windows8app', 'mostactive_users', startIndex=0)


import httplib2
import json
from apiclient.discovery import build
from oauth2client.appengine import AppAssertionCredentials
from mainhandler import Handler
from ..utilities.github_archive_bigquery_handler import execute_bigquery_githubarchive, execute_bigquery_toprepos, execute_bigquery_toplanguages, execute_bigquery_mostactive_users
        
    timeline['date'] = info
    timeline['era'] = era_info
    timeline['chart'] = chart_info
    data['timeline'] = timeline
    return data


class GithubArchiveHTML(Handler):
    def get(self):
        #self.render_front("timeline.html")                
        self.render("metro_github_archive.html")
        
class GithubArchiveBigQuery(Handler):
    def get(self):
        self.response.headers['Content-Type'] = 'application/json'
        """Local_parser_result
        self.write(json.dumps(local_archive))
        """
        """Remote_parser_result"""
        service = build("bigquery", "v2", http=http)        
        parsed_archive = execute_bigquery_githubarchive(service)
        self.write(json.dumps(parsed_archive))
        


class MostActiveUsers(Handler):
    def get(self):
        self.response.headers['Content-Type'] = 'application/json'
        service = build("bigquery", "v2", http=http)

# Path: mainhandler.py
# class Handler(webapp2.RequestHandler):
#     def write(self, *a, **kw):
#         self.response.out.write(*a, **kw)
# 
#     def render_str(self, template, **params):
#         t = jinja_env.get_template(template)
#         return t.render(params)
# 
#     def render(self, template, **kw):
#         self.write(self.render_str(template, **kw))
# 
#     def render_front(self, template):
#         self.render(template)
# 
# Path: app/utilities/github_archive_bigquery_handler.py
# def execute_bigquery_githubarchive(service):
#     query = """SELECT type, actor_attributes_login, payload_action, payload_pull_request_title, repository_name, repository_url, repository_description, repository_language, url, created_at as date
#                 FROM [githubarchive:github.timeline]
#                 WHERE (type="PullRequestReviewCommentEvent" OR type="IssuesEvent" OR type="IssueCommentEvent" OR type="PullRequestEvent") AND repository_owner!="sagarsane"
#                 AND actor_attributes_login="sagarsane"
#                 AND PARSE_UTC_USEC(created_at) >= PARSE_UTC_USEC('2012-07-01 00:00:00')
#                 GROUP BY type, actor_attributes_login, payload_action, payload_pull_request_title, repository_url, repository_description, repository_name, repository_language,  url, date 
#                 ORDER BY date DESC LIMIT 100;"""
#     #return runSyncQuery(service, '<project_id>', 'test', query)
#     return queryTableData(service, '<project_id>', 'test', 'github_query_result', startIndex=0)
# 
# def execute_bigquery_toprepos(service):
#     return queryTableData(service, "<project_id>", 'windows8app', 'top_repos', startIndex=0)
# 
# def execute_bigquery_toplanguages(service):
#     return queryTableData(service, "<project_id>", 'windows8app', 'top_languages', startIndex=0)
# 
# def execute_bigquery_mostactive_users(service):
#     return queryTableData(service, "<project_id>", 'windows8app', 'mostactive_users', startIndex=0)


# Path: app/utilities/githubarchive.py
import httplib2
import json
from apiclient.discovery import build
from oauth2client.appengine import AppAssertionCredentials
from mainhandler import Handler
from ..utilities.github_archive_bigquery_handler import execute_bigquery_githubarchive, execute_bigquery_toprepos, execute_bigquery_toplanguages, execute_bigquery_mostactive_users

        
    timeline['date'] = info
    timeline['era'] = era_info
    timeline['chart'] = chart_info
    data['timeline'] = timeline
    return data


class GithubArchiveHTML(Handler):
    def get(self):
        #self.render_front("timeline.html")                
        self.render("metro_github_archive.html")
        
class GithubArchiveBigQuery(Handler):
    def get(self):
        self.response.headers['Content-Type'] = 'application/json'
        """Local_parser_result
        self.write(json.dumps(local_archive))
        """
        """Remote_parser_result"""
        service = build("bigquery", "v2", http=http)        
        parsed_archive = execute_bigquery_githubarchive(service)
        self.write(json.dumps(parsed_archive))
        


class MostActiveUsers(Handler):
    def get(self):
        self.response.headers['Content-Type'] = 'application/json'
        service = build("bigquery", "v2", http=http)

        parsed_archive = execute_bigquery_mostactive_users(service)