//===========================================================================
//  @(#) $Name: arts++-1-1-a8 $
//  @(#) $Id: ArtsAsMatrixAggregator.hh,v 1.7 2000/10/02 17:05:14 dwm Exp $
//===========================================================================
//  Copyright Notice
//
//  By accessing this software, arts++, you are duly informed
//  of and agree to be bound by the conditions described below in this
//  notice:
//
//  This software product, arts++, is developed by Daniel W. McRobb, and
//  copyrighted(C) 1998 by the University of California, San Diego
//  (UCSD), with all rights reserved.  UCSD administers the CAIDA grant,
//  NCR-9711092, under which part of this code was developed.
//
//  There is no charge for arts++ software.  You can redistribute it
//  and/or modify it under the terms of the GNU Lesser General Public
//  License, Version 2.1, February 1999, which is incorporated by
//  reference herein.
//
//  arts++ is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS, OF
//  MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that the use
//  of it will not infringe on any third party's intellectual
//  property rights.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with arts++.  Copies can also be obtained from:
//
//    http://www.gnu.org/copyleft/lesser.html
//
//  or by writing to:
//
//  Free Software Foundation, Inc.
//  59 Temple Place, Suite 330
//  Boston, MA 02111-1307
//  USA
//
//  Or contact:
//
//    info@caida.org
//===========================================================================

#ifndef _ARTSASMATRIXAGGREGATOR_HH_
#define _ARTSASMATRIXAGGREGATOR_HH_

#include <map>
#include <vector>

#include "ArtsAsMatrix.hh"

//----------------------------------------------------------------------------
//     class ArtsAsMatrixKeyValue 
//----------------------------------------------------------------------------
//!  This class is used as a key in a map of counters used by the
//!  ArtsAsMatrixAggregator class.
//----------------------------------------------------------------------------
class ArtsAsMatrixKeyValue
{
public:
  //--------------------------------------------------------------------------
  //       bool operator < (const ArtsAsMatrixKeyValue keyValue) const 
  //..........................................................................
  //! less-than comparison.  This is used by the ArtsAsMatrixAggregator
  //! class as the comparison class for the key in an STL map of counters.
  //! In other words, it determines ordering of elements in the map.
  //--------------------------------------------------------------------------
  bool operator < (const ArtsAsMatrixKeyValue keyValue) const;
  
  //--------------------------------------------------------------------------
  //       bool operator == (const ArtsAsMatrixKeyValue keyValue) const 
  //..........................................................................
  //!  Returns true if *this is equal to keyValue.  This is used by the
  //!  find() member of an STL map of counters in the ArtsAsMatrixAggregator
  //!  class.
  //--------------------------------------------------------------------------
  bool operator == (const ArtsAsMatrixKeyValue keyValue) const;

  uint16_t  srcAs;
  uint16_t  dstAs;
};

//---------------------------------------------------------------------------
//  class ArtsAsMatrixAggregator
//---------------------------------------------------------------------------
//!  This class is used to aggregate AS matrix objects in the time
//!  domain.
//---------------------------------------------------------------------------
class ArtsAsMatrixAggregator
{
public:
  //--------------------------------------------------------------------------
  //!  Used to hold the counter values in our map of counters.
  //--------------------------------------------------------------------------
  typedef struct {
    uint64_t  Pkts;
    uint64_t  Bytes;
  } counter_t;

  //-------------------------------------------------------------------------
  //               ArtsAsMatrixAggregator(const Arts & arts)
  //.........................................................................
  //!  construct from an Arts object.
  //-------------------------------------------------------------------------
  ArtsAsMatrixAggregator(const Arts & arts);
  
  //-------------------------------------------------------------------------
  //                       void Add(const Arts & arts)
  //.........................................................................
  //!  Add the contents of an Arts object.  The input Arts object must be
  //!  an AS matrix object.  The effect here is to add any new AS matrix
  //!  entries and add to the counters for existing AS matrix entries.
  //-------------------------------------------------------------------------
  void Add(const Arts & arts);
  
  //-------------------------------------------------------------------------
  //              ArtsAsMatrix *ConvertToArtsAsMatrix() const
  //.........................................................................
  //!  Return an ArtsAsMatrix object (dynamically allocated) containing
  //!  the aggregate data.  The caller is responsible for calling delete()
  //!  on the returned ArtsAsMatrix pointer.
  //-------------------------------------------------------------------------
  ArtsAsMatrix *ConvertToArtsAsMatrix() const;

  //-------------------------------------------------------------------------
  //                   vector<pair<uint16_t,counter_t> > *                   
  //                   TopSourcesByBytes(uint16_t numSources)
  //.........................................................................
  //!  Returns a vector of ArtsAsMatrixEntry containing the top numSources
  //!  source AS numbers and their corresponding pkt/byte counters.  Note
  //!  that instead of adding to our type space, we use the
  //!  ArtsAsMatrixEntry class.  In the returned vector, the Dst()
  //!  for each ArtsAsMatrixEntry will always be 0 since we're adding up
  //!  data to every destination AS for each source AS.
  //!
  //!  This member is what lets us easily pick out the top N source ASes
  //!  in an aggregate object, usually so we can then go back over the
  //!  time-series data and create a stacked bar chart of bits/sec vs.
  //!  time for each of the top N source ASes.
  //-------------------------------------------------------------------------
  vector<ArtsAsMatrixEntry> *TopSourcesByBytes(uint16_t numDests);
  
  //-------------------------------------------------------------------------
  //            vector<ArtsAsMatrixEntry> *                       
  //            TopDestinationsByBytes(uint16_t numDests)
  //.........................................................................
  //!  Returns a vector of ArtsAsMatrixEntry containing the top numDests
  //!  destination AS numbers and their corresponding pkt/byte counters.
  //!  Note that instead of adding to our type space, we use the
  //!  ArtsAsMatrixEntry class.  In the returned vector, the Src()
  //!  for each ArtsAsMatrixEntry will always be 0 since we're adding up
  //!  traffic from every source AS for each destination AS.
  //!
  //!  This member is what lets us easily pick out the top N destination
  //!  ASes in an aggregate object, usually so we can then go back over the
  //!  time-series data and create a stacked bar chart of bits/sec vs.
  //!  time for each of the top N destination ASes.
  //-------------------------------------------------------------------------
  vector<ArtsAsMatrixEntry> *TopDestinationsByBytes(uint16_t numDests);

  //-------------------------------------------------------------------------
  //                       uint64_t TotalPkts() const
  //.........................................................................
  //!  Returns the sum of all packets in the ArtsAsMatrixAggregator
  //!  object.  This is the sum of all packets for all contained AS
  //!  matrix entries.
  //-------------------------------------------------------------------------
  uint64_t TotalPkts() const;

  //-------------------------------------------------------------------------
  //                       uint64_t TotalBytes() const                      
  //.........................................................................
  //!  Returns the sum of all bytes in the ArtsAsMatrixAggregator
  //!  object.  This is the sum of all bytes for all contained AS
  //!  matrix entries.
  //-------------------------------------------------------------------------
  uint64_t TotalBytes() const;

  //-------------------------------------------------------------------------
  //            const vector<ArtsAttribute> & Attributes() const;            
  //.........................................................................
  //!  Returns a const reference to the vector of ArtsAttribute objects
  //!  contained in the aggregate.
  //-------------------------------------------------------------------------
  const vector<ArtsAttribute> & Attributes() const;
  
private:
  ArtsHeader                               _header;
  vector<ArtsAttribute>                    _attributes;
  map<ArtsAsMatrixKeyValue,counter_t,less<ArtsAsMatrixKeyValue> >  _asCounters;
  uint64_t                                 _totalPkts;
  uint64_t                                 _totalBytes;
};

#endif  // _ARTSASMATRIXAGGREGATOR_HH_
