/* statquery.h
****************************************************************************************************************
****************************************************************************************************************

    Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2020, 2021 Askar
    Almatov

    This file is part of Trafficmeter.

    Trafficmeter is free software: you can redistribute it and/or modify it under the terms of the GNU General
    Public License as published by the Free Software Foundation, either version 3 of the License, or (at your
    option) any later version.

    Trafficmeter is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
    implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
    License for more details.

    You should have received a copy of the GNU General Public License along with Trafficmeter. If not, see
    <http://www.gnu.org/licenses/>.
*/

#ifndef __STATQUERY_H
#define __STATQUERY_H 1

#include <map>
#include <set>
#include <libiclass/ipv4>

#include "groupbytime.h"
#include "timequery.h"
#include "summaryio.h"

namespace trafficmeter
{
    /**********************************************************************************************************/
    class StatQuery : public TimeQuery
    {
    public:
        struct Key;
        struct Search;

                                        StatQuery();
                                        ~StatQuery();

        bool                            timeByHour();
        bool                            timeByDay();
        bool                            timeByMonth();
        bool                            hideTime();
        void                            saveVlans();
        void                            saveHosts();
        void                            addNetwork( iclass::ipnet4t );
        void                            fetch( std::map<Key,SummaryIo>& );
        time_t                          regroup( time_t ) const;
        bool                            shouldSaveVlans() const;
        bool                            shouldSaveHosts() const;
        const iclass::ipset4t&          networks() const;

    protected:
        GroupByTime*                    groupByTime_;
        bool                            shouldSaveVlans_;
        bool                            shouldSaveHosts_;
        iclass::ipset4t                 networks_;
        std::set<int>                   hashes_;
    };

    /**********************************************************************************************************/
    struct StatQuery::Key
    {
        time_t                          group;
        iclass::ipaddr4t                ip;
        int                             vlan;

        bool                            operator<( const Key& ) const;
    };
}

/*
****************************************************************************************************************
****************************************************************************************************************
*/

namespace trafficmeter
{
    //----------------------------------------------------------------------------------------------------------
    inline time_t
    StatQuery::regroup( time_t group ) const
    {
        return groupByTime_? (*groupByTime_)( group ) : group;
    }

    //----------------------------------------------------------------------------------------------------------
    inline bool
    StatQuery::shouldSaveVlans() const
    {
        return shouldSaveVlans_;
    }

    //----------------------------------------------------------------------------------------------------------
    inline bool
    StatQuery::shouldSaveHosts() const
    {
        return shouldSaveHosts_;
    }

    //----------------------------------------------------------------------------------------------------------
    inline const iclass::ipset4t&
    StatQuery::networks() const
    {
        return networks_;
    }

    //----------------------------------------------------------------------------------------------------------
    inline bool
    StatQuery::Key::operator<( const Key& rhs ) const
    {
        if ( group != rhs.group )
        {
            return group < rhs.group;
        }

        if ( ip != rhs.ip )
        {
            return ip < rhs.ip;
        }

        return vlan < rhs.vlan;
    }
}

#endif  // __STATQUERY_H
