# 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
               }
        columns = row['f']
        an_info['startDate'] = an_info['endDate'] = happened_date
        check_link = columns[8]['v'].split("#")
        if len(check_link) > 0:
            an_info['headline'] = "<a href = '" + columns[8]['v'] + "' target='_blank'>" + columns[0]['v'] + "</a>"
        else:
            an_info['headline'] = "<a href = '" + columns[8]['v'] + "' target='_blank'>" + columns[0]['v'] + "</a>"
        an_info['text'] = columns[4]['v']  + "-" + columns[7]['v']
        an_info['tag'] = columns[0]['v']
        an_info['asset']['caption'] = "<a href = '" + columns[8]['v'] + "' target='_blank'></a>"
        an_info['asset']['credit'] = columns[6]['v']
        #+ columns[5]['v'] + " - " + columns[6]['v']
        split_date = columns[9]['v'].split(' ')
        happened_date1 = ",".join(split_date[0].split("-"))
        happened_date = happened_date1 + "," + ",".join(split_date[1].split(":"))
        info.append(an_info)
        
    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")
        

# 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

               }
        columns = row['f']
        an_info['startDate'] = an_info['endDate'] = happened_date
        check_link = columns[8]['v'].split("#")
        if len(check_link) > 0:
            an_info['headline'] = "<a href = '" + columns[8]['v'] + "' target='_blank'>" + columns[0]['v'] + "</a>"
        else:
            an_info['headline'] = "<a href = '" + columns[8]['v'] + "' target='_blank'>" + columns[0]['v'] + "</a>"
        an_info['text'] = columns[4]['v']  + "-" + columns[7]['v']
        an_info['tag'] = columns[0]['v']
        an_info['asset']['caption'] = "<a href = '" + columns[8]['v'] + "' target='_blank'></a>"
        an_info['asset']['credit'] = columns[6]['v']
        #+ columns[5]['v'] + " - " + columns[6]['v']
        split_date = columns[9]['v'].split(' ')
        happened_date1 = ",".join(split_date[0].split("-"))
        happened_date = happened_date1 + "," + ",".join(split_date[1].split(":"))
        info.append(an_info)
        
    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):