# 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):


from collections import defaultdict
from sqlalchemy import create_engine, inspect
from sqlalchemy import event
from sqlalchemy import exc
from sqlalchemy.engine import reflection
from sqlalchemy.orm import sessionmaker
from sqlalchemy.schema import (
    MetaData,
    Table,
    DropTable,
    ForeignKeyConstraint,
    DDL
)
from sqlalchemy.sql.ddl import DropConstraint
from odpw.core.model import Dataset, Base, tab_datasets, tab_resourcesinfo, ResourceInfo, tab_resourcescrawllog
import os
import structlog
import time
        if port:
            conn_string += ":" + str(port)
        conn_string += "/" + db
        log.info("Connecting DB")

        self.engine = create_engine(conn_string, pool_size=20, client_encoding='utf8', echo=debug)
        add_engine_pidguard(self.engine)
        #add_query_time_logging(self.engine)
        # register_after_fork(self.engine, self.engine.dispose)
        log.info("Connected DB")
        # self.engine.connect()


        self.session_factory = sessionmaker(bind=self.engine)  # , expire_on_commit=False

        # self.session = self.Session()



        self.dataset_insert_function = DDL(
            """
            CREATE OR REPLACE FUNCTION dataset_insert_function()
            RETURNS TRIGGER AS $$

            DECLARE
                _snapshot smallint;
                _table_name text;

            BEGIN
                _snapshot := NEW.snapshot;

# 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/db.py
from collections import defaultdict
from sqlalchemy import create_engine, inspect
from sqlalchemy import event
from sqlalchemy import exc
from sqlalchemy.engine import reflection
from sqlalchemy.orm import sessionmaker
from sqlalchemy.schema import (
    MetaData,
    Table,
    DropTable,
    ForeignKeyConstraint,
    DDL
)
from sqlalchemy.sql.ddl import DropConstraint
from odpw.core.model import Dataset, Base, tab_datasets, tab_resourcesinfo, ResourceInfo, tab_resourcescrawllog
import os
import structlog
import time

        if port:
            conn_string += ":" + str(port)
        conn_string += "/" + db
        log.info("Connecting DB")

        self.engine = create_engine(conn_string, pool_size=20, client_encoding='utf8', echo=debug)
        add_engine_pidguard(self.engine)
        #add_query_time_logging(self.engine)
        # register_after_fork(self.engine, self.engine.dispose)
        log.info("Connected DB")
        # self.engine.connect()


        self.session_factory = sessionmaker(bind=self.engine)  # , expire_on_commit=False

        # self.session = self.Session()



        self.dataset_insert_function = DDL(
            """
            CREATE OR REPLACE FUNCTION dataset_insert_function()
            RETURNS TRIGGER AS $$

            DECLARE
                _snapshot smallint;
                _table_name text;

            BEGIN
                _snapshot := NEW.snapshot;

                _table_name := '""" + tab_datasets + """_' || _snapshot;