<commit_before>###
# DZ Smart Energy Project Library for Perl
# This module provides data processing functions for the
# Forest City Military Community Energy Audit and the
# Maui Smart Grid project.
#
# @author <NAME> (張道博)
# @copyright Copyright (c) 2013, University of Hawaii Smart Energy Project
# @license https://raw.github.com/Hawaii-Smart-Energy-Project/Maui-Smart-Grid/master/BSD-LICENSE.txt
##

package DZSEPLib;
use strict;
use warnings;

require Exporter;
our @ISA       = qw(Exporter);
our @EXPORT_OK = qw();

use DBI;

my $AUTOCOMMIT = 0;
my $RAISEERROR = 1;

# Global variables.
my $DBH;

###
# Constructor
#
# @return object reference
##
sub new {
    my $self = {};
    $self->{DBH} = undef;
    bless($self);
    return $self;
}

###
# Connect to the database.
#
# @param database name
# @param database host
# @param database port
# @param database user
# @param database password
##
sub connectDatabase {
    my ( $dbName, $dbHost, $dbPort, $dbUser, $dbPass ) = @_;
    if ( !$dbName ) { die "dbName $dbName is invalid!"; }

    $DBH = DBI->connect( "dbi:Pg:dbname=$dbName;host=$dbHost;port=$dbPort",
        "$dbUser", "$dbPass",
        { AutoCommit => $AUTOCOMMIT, RaiseError => $RAISEERROR } );

    return $DBH;
}

###
# Map the house numbers to the egauge numbers
#
# House numbers are referenced by egauge numbers as in
#
#     $egaugeHouseMap->{EGAUGE_NUMBER}
#
# This is not used in the MSG eGauge Service.
#
# @return hash reference
##
sub mapEgaugeNumbersToHouseID {

  # Grab house data and make a hash that maps the house numbers to eGauge IDs.
    my $sql            = "SELECT house_id, egauge_no FROM house";
    my $sth            = $DBH->prepare($sql);
    my $result         = $sth->execute;
    my %egaugeHouseMap = ();

    # Process each row.
    while ( my $row = $sth->fetchrow_hashref ) {
        $egaugeHouseMap{ $row->{egauge_no} } = $row->{house_id};
    }
    return \%egaugeHouseMap;
}

###
# Map header columns to DB columns for eGauge energy data.
#
# Data header columns are matched to $colAssoc->{HEADER_NAME}
#
# @return hash reference
##
sub mapCSVColumnsToDatabaseColumns {
    my %colAssoc = ();

    # column associations
    $colAssoc{"Date & Time"} = "datetime";

    # some have whole house, others have grid
    $colAssoc{"Whole House [kW]"} = "grid_kw";
    $colAssoc{"Grid [kW]"}        = "grid_kw";

    $colAssoc{"AC [kW]"}                  = "ac_kw";
    $colAssoc{"AC+ [kW]"}                 = "acplus_kw";
    $colAssoc{"DHW [kW]"}                 = "dhw_kw";
    $colAssoc{"Dishwasher [kW]"}          = "dishwasher_kw";
    $colAssoc{"Dryer [kW]"}               = "dryer_kw";
    $colAssoc{"Dryer.Usage [kW]"}         = "dryer_usage_kw";
    $colAssoc{"Fan [kW]"}                 = "fan_kw";
    $colAssoc{"Garage AC [kW]"}           = "garage_ac_kw";
    $colAssoc{"Garage AC.Usage [kW]"}     = "garage_ac_usage_kw";
    $colAssoc{"gen [kW]"}                 = "gen_kw";
    $colAssoc{"Large AC [kW]"}            = "large_ac_kw";
    $colAssoc{"Large AC.Usage [kW]"}      = "large_ac_usage_kw";
    $colAssoc{"Oven [kW]"}                = "oven_kw";
    $colAssoc{"Oven.Usage [kW]"}          = "oven_usage_kw";
    $colAssoc{"Range [kW]"}               = "range_kw";
    $colAssoc{"Range.Usage [kW]"}         = "range_usage_kw";
    $colAssoc{"Refrigerator [kW]"}        = "refrigerator_kw";
    $colAssoc{"Refrigerator.Usage [kW]"}  = "refrigerator_usage_kw";
    $colAssoc{"Rest of House.Usage [kW]"} = "rest_of_house_usage_kw";
    $colAssoc{"Solar [kW]"}               = "solarpump_kw";
    $colAssoc{"Solar Pump [kW]"}          = "solarpump_kw";
    $colAssoc{"Stove [kW]"}               = "stove_kw";
    $colAssoc{"use [kW]"}                 = "use_kw";
    $colAssoc{"Washer [kW]"}              = "clotheswasher_kw";
    $colAssoc{"Washer.Usage [kW]"}        = "clotheswasher_usage_kw";

    #$colAssoc{""} = "microwave_kw"; # only one house has microwave data

    return \%colAssoc;
}

###
# Get last unix timestamp where energy_autoload data was retrieved.
#
# @param house ID
# @return unix timestamp or 0 if nothing was found
##<commit_msg>adding columns: #MSG-345 fixed<commit_after>###
# DZ Smart Energy Project Library for Perl
# This module provides data processing functions for the
# Forest City Military Community Energy Audit and the
# Maui Smart Grid project.
#
# @author <NAME> (張道博)
# @copyright Copyright (c) 2013, University of Hawaii Smart Energy Project
# @license https://raw.github.com/Hawaii-Smart-Energy-Project/Maui-Smart-Grid/master/BSD-LICENSE.txt
##

package DZSEPLib;
use strict;
use warnings;

require Exporter;
our @ISA       = qw(Exporter);
our @EXPORT_OK = qw();

use DBI;

my $AUTOCOMMIT = 0;
my $RAISEERROR = 1;

# Global variables.
my $DBH;

###
# Constructor
#
# @return object reference
##
sub new {
    my $self = {};
    $self->{DBH} = undef;
    bless($self);
    return $self;
}

###
# Connect to the database.
#
# @param database name
# @param database host
# @param database port
# @param database user
# @param database password
##
sub connectDatabase {
    my ( $dbName, $dbHost, $dbPort, $dbUser, $dbPass ) = @_;
    if ( !$dbName ) { die "dbName $dbName is invalid!"; }

    $DBH = DBI->connect( "dbi:Pg:dbname=$dbName;host=$dbHost;port=$dbPort",
        "$dbUser", "$dbPass",
        { AutoCommit => $AUTOCOMMIT, RaiseError => $RAISEERROR } );

    return $DBH;
}

###
# Map the house numbers to the egauge numbers
#
# House numbers are referenced by egauge numbers as in
#
#     $egaugeHouseMap->{EGAUGE_NUMBER}
#
# This is not used in the MSG eGauge Service.
#
# @return hash reference
##
sub mapEgaugeNumbersToHouseID {

  # Grab house data and make a hash that maps the house numbers to eGauge IDs.
    my $sql            = "SELECT house_id, egauge_no FROM house";
    my $sth            = $DBH->prepare($sql);
    my $result         = $sth->execute;
    my %egaugeHouseMap = ();

    # Process each row.
    while ( my $row = $sth->fetchrow_hashref ) {
        $egaugeHouseMap{ $row->{egauge_no} } = $row->{house_id};
    }
    return \%egaugeHouseMap;
}

###
# Map header columns to DB columns for eGauge energy data.
#
# Data header columns are matched to $colAssoc->{HEADER_NAME}
#
# @return hash reference
##
sub mapCSVColumnsToDatabaseColumns {
    my %colAssoc = ();

    # column associations
    $colAssoc{"Date & Time"} = "datetime";

    # some have whole house, others have grid
    $colAssoc{"Whole House [kW]"} = "grid_kw";
    $colAssoc{"Grid [kW]"}        = "grid_kw";

    $colAssoc{"AC [kW]"}                  = "ac_kw";
    $colAssoc{"AC+ [kW]"}                 = "acplus_kw";
    $colAssoc{"DHW [kW]"}                 = "dhw_kw";
    $colAssoc{"Dishwasher [kW]"}          = "dishwasher_kw";
    $colAssoc{"Dryer [kW]"}               = "dryer_kw";
    $colAssoc{"Dryer.Usage [kW]"}         = "dryer_usage_kw";
    $colAssoc{"Fan [kW]"}                 = "fan_kw";
    $colAssoc{"Garage AC [kW]"}           = "garage_ac_kw";
    $colAssoc{"Garage AC.Usage [kW]"}     = "garage_ac_usage_kw";
    $colAssoc{"gen [kW]"}                 = "gen_kw";
    $colAssoc{"Large AC [kW]"}            = "large_ac_kw";
    $colAssoc{"Large AC.Usage [kW]"}      = "large_ac_usage_kw";
    $colAssoc{"Oven [kW]"}                = "oven_kw";
    $colAssoc{"Oven.Usage [kW]"}          = "oven_usage_kw";
    $colAssoc{"Range [kW]"}               = "range_kw";
    $colAssoc{"Range.Usage [kW]"}         = "range_usage_kw";
    $colAssoc{"Refrigerator [kW]"}        = "refrigerator_kw";
    $colAssoc{"Refrigerator.Usage [kW]"}  = "refrigerator_usage_kw";
    $colAssoc{"Rest of House.Usage [kW]"} = "rest_of_house_usage_kw";
    $colAssoc{"Solar [kW]"}               = "solarpump_kw";
    $colAssoc{"Solar Pump [kW]"}          = "solarpump_kw";
    $colAssoc{"Stove [kW]"}               = "stove_kw";
    $colAssoc{"Stove Top [kW]"}           = "stove_kw";
    $colAssoc{"use [kW]"}                 = "use_kw";
    $colAssoc{"Washer [kW]"}              = "clotheswasher_kw";
    $colAssoc{"Washer.Usage [kW]"}        = "clotheswasher_usage_kw";
    $colAssoc{"Oven and Microwave [kW]"}  = "oven_and_microwave_kw";
    $colAssoc{"Oven and Microwave+ [kW]"} = "oven_and_microwave_plus_kw";

    #$colAssoc{""} = "microwave_kw"; # only one house has microwave data

    return \%colAssoc;
}

###
# Get last unix timestamp where energy_autoload data was retrieved.
#
# @param house ID
# @return unix timestamp or 0 if nothing was found
##
{'fraction_non_alphanumeric': 0.15323724007561437, 'fraction_numerical': 0.002008506616257089, 'mean_word_length': 2.8355233348436792, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 2, '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': '4493662', 'n_tokens_mistral': 3148, 'n_tokens_neox': 3046, 'n_words': 731}