# Path: odpw/core/db.py
# def row2dict(r):
#     if hasattr(r, '_fields'):
#         d = {}
#         for field in r._fields:
#             rf = r.__getattribute__(field)
#             if isinstance(rf, Base):
#                 d.update(_row2dict(rf))
#             else:
#                 d[field] = rf
#         return d
#     if isinstance(r, Base):
#         return _row2dict(r)
# 
# Path: odpw/core/model.py
# class PortalSnapshotQuality(Base):
#     __tablename__ = tab_portalsnapshotquality
# 
#     portalid      = Column(String, ForeignKey(tab_portals+'.id'), primary_key=True, index=True,nullable=False)
#     snapshot= Column( SmallInteger, primary_key=True)
#     portal  = relationship("Portal", back_populates="snapshotsquality")
# 
#     cocu = Column(Float)
#     cocuN = Column(Integer)
#     coce = Column(Float)
#     coceN = Column(Integer)
#     coda = Column(Float)
#     codaN = Column(Integer)
#     cofo = Column(Float)
#     cofoN = Column(Integer)
#     coli = Column(Float)
#     coliN = Column(Integer)
#     coac = Column(Float)
#     coacN = Column(Integer)
#     exda = Column(Float)
#     exdaN = Column(Integer)
#     exri = Column(Float)
#     exriN = Column(Integer)
#     expr = Column(Float)
#     exprN = Column(Integer)
#     exac = Column(Float)
#     exacN = Column(Integer)
#     exdi = Column(Float)
#     exdiN = Column(Integer)
#     exte = Column(Float)
#     exteN = Column(Integer)
#     exsp = Column(Float)
#     exspN = Column(Integer)
#     exco = Column(Float)
#     excoN = Column(Integer)
#     opfo = Column(Float)
#     opfoN = Column(Integer)
#     opma = Column(Float)
#     opmaN = Column(Integer)
#     opli = Column(Float)
#     opliN = Column(Integer)
#     datasets=Column(Integer)
# 
#     def __repr__(self):
#         return "<PortalSnapshotQuality(id=%s, snapshot=%s, agg=%s)>" % (
#             self.portalid, self.snapshot,  any([self.exda,self.coac,self.coce,self.cocu]))
# 
# 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)
# 
# class ResourceHistory(Base):
#     __tablename__ = tab_resourceshistory
# 
#     uri = Column(String, primary_key=True)
#     snapshot = Column(SmallInteger, primary_key=True)
#     md5 = Column(String,ForeignKey(DatasetData.md5), primary_key=True)
#     modified = Column(TIMESTAMP)
#     source = Column(String, primary_key=True)
# 
# Path: odpw/utils/plots.py
# def hm():
# def getFetchProcessChart(db, snapshot, n=3):
# def getData(db, snapshot, n=3):
# def fetchProcessChart(data,cnts):
# def portalsScatter(df):
#     def get_dataset(df, name):
# def qualityChart(df):
#     def rad(mic):
# def evolSize(source,df):
# def evolutionCharts(df):
# def getWeekStringTick():
# def systemEvolutionBarPlot(df, yLabel, values):
# def systemEvolutionPlot(df):
# def portalDynamicity(df):
#     def getWeekString(yearweek):
#         LINE_ARGS = dict(color=c, line_color=None)
# 
# Path: odpw/core/model.py
# class Portal(Base):
# class PortalSnapshot(Base):
# class Serializable(object):
# class PortalSnapshotDynamicity(Base,Serializable):
# class PortalSnapshotQuality(Base):
# class Dataset(Base):
# class DatasetData(Base):
# class DatasetQuality(Base):
# class MetaResource(Base):
# class ResourceInfo(Base):
# class ResourceCrawlLog(Base):
# class ResourceHistory(Base):
# class ResourceFreshness(Base):
# class FormatDist(Base):
#     def snapshot_count(self):
#     def snapshot_count(cls):
#     def first_snapshot(self):
#     def first_snapshot(cls):
#     def last_snapshot(self):
#     def last_snapshot(cls):
#     def datasetcount(self):
#     def datasetcount(cls):
#     def resourcecount(self):
#     def resourcecount(cls):
#     def __repr__(self):
#     def fetchtime(self):
#     def __repr__(self):
#     def to_dict(self):
#     def dyratio(self):
#     def adddelratio(self):
#     def addRatio(self):
#     def delRatio(self):
#     def updatedRatio(self):
#     def staticRatio(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def info(cls):
#     def __repr__(self):


import pandas as pd
from sqlalchemy import and_
from sqlalchemy import func, exists
from sqlalchemy.orm import scoped_session
from odpw.core.db import row2dict
from odpw.core.model import PortalSnapshotQuality, ResourceCrawlLog, ResourceHistory
from odpw.utils.plots import qa
from odpw.core.model import DatasetData, DatasetQuality, Dataset, Base, Portal, PortalSnapshotQuality, PortalSnapshot, \
    tab_datasets, tab_resourcesinfo, ResourceInfo, MetaResource, PortalSnapshotDynamicity
    from contextlib import contextmanager





class DBClient(object):

    def __init__(self, dbm=None, Session=None):
        if dbm is not None:
            self.dbm=dbm

# Path: odpw/core/db.py
# def row2dict(r):
#     if hasattr(r, '_fields'):
#         d = {}
#         for field in r._fields:
#             rf = r.__getattribute__(field)
#             if isinstance(rf, Base):
#                 d.update(_row2dict(rf))
#             else:
#                 d[field] = rf
#         return d
#     if isinstance(r, Base):
#         return _row2dict(r)
# 
# Path: odpw/core/model.py
# class PortalSnapshotQuality(Base):
#     __tablename__ = tab_portalsnapshotquality
# 
#     portalid      = Column(String, ForeignKey(tab_portals+'.id'), primary_key=True, index=True,nullable=False)
#     snapshot= Column( SmallInteger, primary_key=True)
#     portal  = relationship("Portal", back_populates="snapshotsquality")
# 
#     cocu = Column(Float)
#     cocuN = Column(Integer)
#     coce = Column(Float)
#     coceN = Column(Integer)
#     coda = Column(Float)
#     codaN = Column(Integer)
#     cofo = Column(Float)
#     cofoN = Column(Integer)
#     coli = Column(Float)
#     coliN = Column(Integer)
#     coac = Column(Float)
#     coacN = Column(Integer)
#     exda = Column(Float)
#     exdaN = Column(Integer)
#     exri = Column(Float)
#     exriN = Column(Integer)
#     expr = Column(Float)
#     exprN = Column(Integer)
#     exac = Column(Float)
#     exacN = Column(Integer)
#     exdi = Column(Float)
#     exdiN = Column(Integer)
#     exte = Column(Float)
#     exteN = Column(Integer)
#     exsp = Column(Float)
#     exspN = Column(Integer)
#     exco = Column(Float)
#     excoN = Column(Integer)
#     opfo = Column(Float)
#     opfoN = Column(Integer)
#     opma = Column(Float)
#     opmaN = Column(Integer)
#     opli = Column(Float)
#     opliN = Column(Integer)
#     datasets=Column(Integer)
# 
#     def __repr__(self):
#         return "<PortalSnapshotQuality(id=%s, snapshot=%s, agg=%s)>" % (
#             self.portalid, self.snapshot,  any([self.exda,self.coac,self.coce,self.cocu]))
# 
# 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)
# 
# class ResourceHistory(Base):
#     __tablename__ = tab_resourceshistory
# 
#     uri = Column(String, primary_key=True)
#     snapshot = Column(SmallInteger, primary_key=True)
#     md5 = Column(String,ForeignKey(DatasetData.md5), primary_key=True)
#     modified = Column(TIMESTAMP)
#     source = Column(String, primary_key=True)
# 
# Path: odpw/utils/plots.py
# def hm():
# def getFetchProcessChart(db, snapshot, n=3):
# def getData(db, snapshot, n=3):
# def fetchProcessChart(data,cnts):
# def portalsScatter(df):
#     def get_dataset(df, name):
# def qualityChart(df):
#     def rad(mic):
# def evolSize(source,df):
# def evolutionCharts(df):
# def getWeekStringTick():
# def systemEvolutionBarPlot(df, yLabel, values):
# def systemEvolutionPlot(df):
# def portalDynamicity(df):
#     def getWeekString(yearweek):
#         LINE_ARGS = dict(color=c, line_color=None)
# 
# Path: odpw/core/model.py
# class Portal(Base):
# class PortalSnapshot(Base):
# class Serializable(object):
# class PortalSnapshotDynamicity(Base,Serializable):
# class PortalSnapshotQuality(Base):
# class Dataset(Base):
# class DatasetData(Base):
# class DatasetQuality(Base):
# class MetaResource(Base):
# class ResourceInfo(Base):
# class ResourceCrawlLog(Base):
# class ResourceHistory(Base):
# class ResourceFreshness(Base):
# class FormatDist(Base):
#     def snapshot_count(self):
#     def snapshot_count(cls):
#     def first_snapshot(self):
#     def first_snapshot(cls):
#     def last_snapshot(self):
#     def last_snapshot(cls):
#     def datasetcount(self):
#     def datasetcount(cls):
#     def resourcecount(self):
#     def resourcecount(cls):
#     def __repr__(self):
#     def fetchtime(self):
#     def __repr__(self):
#     def to_dict(self):
#     def dyratio(self):
#     def adddelratio(self):
#     def addRatio(self):
#     def delRatio(self):
#     def updatedRatio(self):
#     def staticRatio(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def info(cls):
#     def __repr__(self):


# Path: odpw/core/api.py
import pandas as pd
from sqlalchemy import and_
from sqlalchemy import func, exists
from sqlalchemy.orm import scoped_session
from odpw.core.db import row2dict
from odpw.core.model import PortalSnapshotQuality, ResourceCrawlLog, ResourceHistory
from odpw.utils.plots import qa
from odpw.core.model import DatasetData, DatasetQuality, Dataset, Base, Portal, PortalSnapshotQuality, PortalSnapshot, \
    tab_datasets, tab_resourcesinfo, ResourceInfo, MetaResource, PortalSnapshotDynamicity
    from contextlib import contextmanager






class DBClient(object):

    def __init__(self, dbm=None, Session=None):
        if dbm is not None:
            self.dbm=dbm

            self.Session = scoped_session(dbm.session_factory)