<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = '<NAME> (張道博)'
__copyright__ = 'Copyright (c) 2014, University of Hawaii Smart Energy Project'
__license__ = 'https://raw.github.com/Hawaii-Smart-Energy-Project/Smart-Grid' \
              '-PV-Inverter/master/BSD-LICENSE.txt'

"""
Create meter data partitions based on meter name.
"""

import argparse
from sek.logger import SEKLogger
from si_configer import SIConfiger
from sek.db_util import SEKDBUtil
from sek.db_connector import SEKDBConnector
from si_util import SIUtil

COMMAND_LINE_ARGS = None


def processCommandLineArguments():
    """
    Create command line arguments and parse them.
    """

    global COMMAND_LINE_ARGS
    parser = argparse.ArgumentParser(
        description = 'Perform insertion of data contained in multiple files '
                      'to the SI database.')
    parser.add_argument('--basepath', required = True,
                        help = 'A base path from which to process data files.')
    COMMAND_LINE_ARGS = parser.parse_args()


if __name__ == '__main__':
    processCommandLineArguments()
    tableBase = "MeterData"
    logger = SEKLogger(__name__, 'debug')
    configer = SIConfiger()
    dbUtil = SEKDBUtil()
    conn = SEKDBConnector(
        dbName = configer.configOptionValue('Database', 'db_name'),
        dbHost = configer.configOptionValue('Database', 'db_host'),
        dbPort = configer.configOptionValue('Database', 'db_port'),
        dbUsername = configer.configOptionValue('Database', 'db_username'),
        dbPassword = configer.configOptionValue('Database',
                                                'db_password')).connectDB()
    cursor = conn.cursor()

    for meterName in SIUtil().meters(basepath = COMMAND_LINE_ARGS.basepath):
        logger.log('creating table {}'.format(tableBase + "_" + meterName))
        sql = 'CREATE TABLE "{1}_{0}" ( CHECK ( meter_id = meter_id(\'{' \
              '0}\'))) INHERITS ("{1}")'.format(meterName, tableBase)
        if dbUtil.executeSQL(cursor, sql, exitOnFail = False):
            conn.commit()
        else:
            conn.rollback()
<commit_msg>setting primary key and foreign key of partitions<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = '<NAME> (張道博)'
__copyright__ = 'Copyright (c) 2014, University of Hawaii Smart Energy Project'
__license__ = 'https://raw.github.com/Hawaii-Smart-Energy-Project/Smart-Grid' \
              '-PV-Inverter/master/BSD-LICENSE.txt'

"""
Create meter data partitions based on meter name.
"""

import argparse
from sek.logger import SEKLogger
from si_configer import SIConfiger
from sek.db_util import SEKDBUtil
from sek.db_connector import SEKDBConnector
from si_util import SIUtil

COMMAND_LINE_ARGS = None


def processCommandLineArguments():
    """
    Create command line arguments and parse them.
    """

    global COMMAND_LINE_ARGS
    parser = argparse.ArgumentParser(
        description = 'Perform insertion of data contained in multiple files '
                      'to the SI database.')
    parser.add_argument('--basepath', required = True,
                        help = 'A base path from which to process data files.')
    COMMAND_LINE_ARGS = parser.parse_args()


if __name__ == '__main__':
    processCommandLineArguments()
    tableBase = "MeterData"
    pkey = 'meter_id, time_utc'
    logger = SEKLogger(__name__, 'debug')
    configer = SIConfiger()
    dbUtil = SEKDBUtil()
    conn = SEKDBConnector(
        dbName = configer.configOptionValue('Database', 'db_name'),
        dbHost = configer.configOptionValue('Database', 'db_host'),
        dbPort = configer.configOptionValue('Database', 'db_port'),
        dbUsername = configer.configOptionValue('Database', 'db_username'),
        dbPassword = configer.configOptionValue('Database',
                                                'db_password')).connectDB()
    cursor = conn.cursor()

    tableOwner = configer.configOptionValue('Database', 'table_owner')
    for meterName in SIUtil().meters(basepath = COMMAND_LINE_ARGS.basepath):
        logger.log('creating table {}'.format(tableBase + "_" + meterName))
        sql = 'CREATE TABLE "{1}_{0}" ( CHECK ( meter_id = meter_id(\'{' \
              '0}\'))) INHERITS ("{1}"); ALTER TABLE ONLY "{1}_{0}" ADD ' \
              'CONSTRAINT "{1}_{0}_pkey" PRIMARY KEY ({3}); ALTER TABLE ONLY ' \
              '"{1}_{0}" ADD CONSTRAINT meter_id_fkey FOREIGN KEY (meter_id) ' \
              'REFERENCES "Meters"(meter_id) ON UPDATE CASCADE ON DELETE ' \
              'CASCADE; ALTER TABLE "{1}_{0}" OWNER TO {2}'.format(
            meterName, tableBase, tableOwner, pkey)
        if dbUtil.executeSQL(cursor, sql, exitOnFail = False):
            conn.commit()
        else:
            conn.rollback()

{'fraction_non_alphanumeric': 0.119533527696793, 'fraction_numerical': 0.0058309037900874635, 'mean_word_length': 2.9048780487804877, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '950699', 'n_tokens_mistral': 1448, 'n_tokens_neox': 1392, 'n_words': 356}