#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2009 RoyaltyShare, Inc.      All Rights Reserved
#---------------------------------------------------------------
package Job::DB::Item::JobHostClass;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use lib '/app/tools/job/lib';
use Common::Assert;
use Common::RSApp;
use Common::DB::Item;
use Job::DB::Item::JobHost;
use base 'Common::DB::Item';

use constant kTable => 'job_host_class';
use constant kDB    => Common::DB::Item::kCommonDB;

sub GetAllForHostname {
    my ( $class, $hostname ) = @_;

    my $sql = "SELECT * FROM " . kTable . " WHERE hostname='$hostname'";
    return $class->SUPER::GetAll($sql);
}

sub GetLiveHostClassCounts {
    my ($class) = @_;

    my $dbo = Common::RSApp::GetCommonDB();

    my %resultSet;
    my $sql =
        "SELECT class, COUNT(*) as count FROM "
      . kTable
      . " WHERE hostname IN"
      . " ( SELECT hostname FROM job_host WHERE (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(job_host.heartbeat) < "
      . Job::DB::Item::JobHost::kStaleHeartbeatThreshold() . ")"
      . "   AND job_host.on_hold = 0)"
      . " GROUP BY class";

    my $sth = $dbo->DoCmd($sql);
    while ( my $hr = $sth->fetchrow_hashref() ) {
        $resultSet{ $hr->{class} } = $hr->{count};
    }

    return \%resultSet;
}

###
1;    #
###

