# Path: odpw/core/model.py
# class ResourceCrawlLog(Base):
#     __tablename__ = tab_resourcescrawllog
# 
#     uri= Column(String, primary_key=True)
#     snapshot= Column(SmallInteger, primary_key=True)
#     timestamp= Column(TIMESTAMP, primary_key=True)
#     status=Column(SmallInteger, index=True)
# 
#     exc=Column(String)
#     header=Column(JSONB)
#     mime=Column(String)
#     size=Column(BigInteger)
#     crawltime=Column(BigInteger)
# 
#     referrer=Column( String)
#     disklocation=Column( String)
#     digest=Column( String)
#     contentchanged=Column( Integer)
#     domain=Column( String, index=True)
# 
# Path: odpw/web_rest/rest/odpw_restapi.py
# def default_error_handler(error):
# def database_not_found_error_handler(e):
# 
# Path: odpw/utils/datamonitor_utils.py
# def parseDate(date):
#     date_parts = []
#     # YYYY<MM|DD|HH|MM|SS>
#     if len(date) < 4:
#         # error
#         pass
#     else:
#         date_parts_length = [4, 2, 2, 2, 2, 2]
#         date_parts = []
#         for l in date_parts_length:
#             if len(date) >= l:
#                 date_parts.append(int(date[0:l]))
#                 date = date[l:]
# 
#     if len(date_parts) == 0:
#         return None
#     elif len(date_parts) == 1:
#         date_parts.append(1)
#         date_parts.append(1)
#     elif len(date_parts) == 2:
#         date_parts.append(1)
# 
#     return datetime.datetime(*map(int, date_parts))


import logging
import os
import functools
from flask import after_this_request
from odpw.core.model import ResourceCrawlLog
from flask import request
from flask import send_file
from flask import url_for
from flask_restplus import Resource
from odpw.web_rest.rest.odpw_restapi import api
from odpw.utils.datamonitor_utils import parseDate
from flask import Blueprint, current_app, render_template
from flask import jsonify,Response
from datetime import datetime


@ns.route('/list')
class List(Resource):

    def get(self):
        session = current_app.config['dbsession']
        q=session.query(ResourceCrawlLog.uri).distinct(ResourceCrawlLog.uri)
        urls=[]
        for r in q:
           urls.append(r[0])

        resp = jsonify(urls)
        return resp

@ns.route('/timemap/json/<path:url>')
@ns.doc(params={'url': 'URL (HTTP URL)', 'date': 'Date as \"YYYY<MM|DD|HH|MM|SS>\"'})
class Timemap(Resource):
    def get(self, url):
        session = current_app.config['dbsession']
        res = {
                "original_uri": url
                ,"timemap_uri": {
                                "json_format": url_for('api.data_timemap', url=url),
                                #"link_format": "http://labs.mementoweb.org/timemap/link/http://cnn.com/"
                                }
                , "mementos": {'list':[]}
                }

        d={}

# Path: odpw/core/model.py
# class ResourceCrawlLog(Base):
#     __tablename__ = tab_resourcescrawllog
# 
#     uri= Column(String, primary_key=True)
#     snapshot= Column(SmallInteger, primary_key=True)
#     timestamp= Column(TIMESTAMP, primary_key=True)
#     status=Column(SmallInteger, index=True)
# 
#     exc=Column(String)
#     header=Column(JSONB)
#     mime=Column(String)
#     size=Column(BigInteger)
#     crawltime=Column(BigInteger)
# 
#     referrer=Column( String)
#     disklocation=Column( String)
#     digest=Column( String)
#     contentchanged=Column( Integer)
#     domain=Column( String, index=True)
# 
# Path: odpw/web_rest/rest/odpw_restapi.py
# def default_error_handler(error):
# def database_not_found_error_handler(e):
# 
# Path: odpw/utils/datamonitor_utils.py
# def parseDate(date):
#     date_parts = []
#     # YYYY<MM|DD|HH|MM|SS>
#     if len(date) < 4:
#         # error
#         pass
#     else:
#         date_parts_length = [4, 2, 2, 2, 2, 2]
#         date_parts = []
#         for l in date_parts_length:
#             if len(date) >= l:
#                 date_parts.append(int(date[0:l]))
#                 date = date[l:]
# 
#     if len(date_parts) == 0:
#         return None
#     elif len(date_parts) == 1:
#         date_parts.append(1)
#         date_parts.append(1)
#     elif len(date_parts) == 2:
#         date_parts.append(1)
# 
#     return datetime.datetime(*map(int, date_parts))


# Path: odpw/web_rest/rest/datamonitor_namespace.py
import logging
import os
import functools
from flask import after_this_request
from odpw.core.model import ResourceCrawlLog
from flask import request
from flask import send_file
from flask import url_for
from flask_restplus import Resource
from odpw.web_rest.rest.odpw_restapi import api
from odpw.utils.datamonitor_utils import parseDate
from flask import Blueprint, current_app, render_template
from flask import jsonify,Response
from datetime import datetime



@ns.route('/list')
class List(Resource):

    def get(self):
        session = current_app.config['dbsession']
        q=session.query(ResourceCrawlLog.uri).distinct(ResourceCrawlLog.uri)
        urls=[]
        for r in q:
           urls.append(r[0])

        resp = jsonify(urls)
        return resp

@ns.route('/timemap/json/<path:url>')
@ns.doc(params={'url': 'URL (HTTP URL)', 'date': 'Date as \"YYYY<MM|DD|HH|MM|SS>\"'})
class Timemap(Resource):
    def get(self, url):
        session = current_app.config['dbsession']
        res = {
                "original_uri": url
                ,"timemap_uri": {
                                "json_format": url_for('api.data_timemap', url=url),
                                #"link_format": "http://labs.mementoweb.org/timemap/link/http://cnn.com/"
                                }
                , "mementos": {'list':[]}
                }

        d={}

        q=session.query(ResourceCrawlLog.timestamp).filter(ResourceCrawlLog.uri == url).filter(ResourceCrawlLog.contentchanged!=0).order_by(ResourceCrawlLog.timestamp)