<commit_before>from __future__ import absolute_import

import logging

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column, String, DateTime, Integer

from sqlalchemy.dialects.postgresql import JSON, BYTEA

from scrapi import events
from scrapi.processing.base import BaseProcessor

logger = logging.getLogger(__name__)

engine = create_engine('postgresql://localhost/scrapi', echo=True)
session = sessionmaker(bind=engine)()
Base = declarative_base(bind=engine)


class PostgresProcessor(BaseProcessor):
    NAME = 'postgres'

    def __init__(self, *args, **kwargs):
        Base.metadata.create_all()
        session.commit()
        super(PostgresProcessor, self).__init__(*args, **kwargs)

    @events.logged(events.PROCESSING, 'raw.postgres')
    def process_raw(self, raw_doc):
        source, docID = raw_doc['source'], raw_doc['docID']
        document = self._get_by_source_id(Document, source, docID) or Document(source=source, docID=docID)

        document.raw = raw_doc
        session.add(document)
        session.commit()

    @events.logged(events.PROCESSING, 'normalized.postgres')
    def process_normalized(self, raw_doc, normalized):
        source, docID = raw_doc['source'], raw_doc['docID']
        document = self._get_by_source_id(Document, source, docID) or Document(source=source, docID=docID)

        document.normalized = normalized.attributes
        document.providerUpdatedDateTime = normalized['providerUpdatedDateTime']

        session.add(document)
        session.commit()

        session.close()

    def _get_by_source_id(self, model, source, docID):
        return session.query(model).filter_by(source=source, docID=docID).first()


class Document(Base):
    __tablename__ = 'documents'

    source = Column(String, primary_key=True)
    docID = Column(String, primary_key=True)
    providerUpdatedDateTime = Column(DateTime, primary_key=True)

    raw = Column(BYTEA)
    normalized = Column(JSON)
<commit_msg>Save Raw Doc as json too for now<commit_after>from __future__ import absolute_import

import logging

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column, String, DateTime

from sqlalchemy.dialects.postgresql import JSON

from scrapi import events
from scrapi.processing.base import BaseProcessor

logger = logging.getLogger(__name__)

engine = create_engine('postgresql://localhost/scrapi', echo=True)
session = sessionmaker(bind=engine)()
Base = declarative_base(bind=engine)


class PostgresProcessor(BaseProcessor):
    NAME = 'postgres'

    def __init__(self, *args, **kwargs):
        Base.metadata.create_all()
        session.commit()
        super(PostgresProcessor, self).__init__(*args, **kwargs)

    @events.logged(events.PROCESSING, 'raw.postgres')
    def process_raw(self, raw_doc):
        source, docID = raw_doc['source'], raw_doc['docID']
        document = self._get_by_source_id(Document, source, docID) or Document(source=source, docID=docID)

        document.raw = raw_doc.attributes
        session.add(document)
        session.commit()

    @events.logged(events.PROCESSING, 'normalized.postgres')
    def process_normalized(self, raw_doc, normalized):
        source, docID = raw_doc['source'], raw_doc['docID']
        document = self._get_by_source_id(Document, source, docID) or Document(source=source, docID=docID)

        document.normalized = normalized.attributes
        document.providerUpdatedDateTime = normalized['providerUpdatedDateTime']

        session.add(document)
        session.commit()

        session.close()

    def _get_by_source_id(self, model, source, docID):
        return session.query(model).filter_by(source=source, docID=docID).first()


class Document(Base):
    __tablename__ = 'documents'

    source = Column(String, primary_key=True)
    docID = Column(String, primary_key=True)
    providerUpdatedDateTime = Column(DateTime, primary_key=True)

    raw = Column(JSON)
    normalized = Column(JSON)

{'fraction_non_alphanumeric': 0.1019522776572668, 'fraction_numerical': 0.0, 'mean_word_length': 4.3686934023285895, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4600141', 'n_tokens_mistral': 1249, 'n_tokens_neox': 1210, 'n_words': 263}