00001
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef _CALLACCOUNTING_H_
00033 #define _CALLACCOUNTING_H_
00034
00035
00036
00037
00038 #include <map>
00039 #include <set>
00040 #include <vector>
00041
00042
00043 #include "fraction.h"
00044 #include "osbid.h"
00045 #include "modules.h"
00046
00047
00048 namespace OSB_DB {
00049 class Session;
00050 class CallAccountingGw;
00051 }
00052
00053 namespace OSB_LIB {
00054 class Cdr;
00055 class CdrList;
00056 class RecordStreamInfo;
00057 }
00058
00059
00060
00061 namespace OSB_LIB {
00062
00063
00064
00065
00069 class CallAccounting {
00070 public:
00074 enum Result {
00075 caOk = 'o',
00076 caError = 'e',
00077 caFiltered = 'f'
00078 };
00079
00087 static char toChar(Result result);
00088
00096 static Result mkResult(char r);
00097
00098
00100 typedef std::pair< Id<RecordStreamInfo>, Result > Key;
00102 typedef std::map<Key, Fraction> Counts;
00103
00104 public:
00106 CallAccounting(ModuleId module);
00107
00109 ModuleId module() const { return module_; }
00110
00116 void reset();
00117
00124 void add(
00125 Result result,
00126 const CdrList& cdrs
00127 );
00128
00135 void subtract(
00136 Result result,
00137 const CdrList& cdrs
00138 );
00139
00146 void add(
00147 Result result,
00148 const Cdr& cdr
00149 );
00150
00157 void subtract(
00158 Result result,
00159 const Cdr& cdr
00160 );
00161
00173 void add(
00174 Result result,
00175 const Id<RecordStreamInfo>& rsId
00176 );
00177
00186 void subtract(
00187 Result result,
00188 const Id<RecordStreamInfo>& rsId
00189 );
00190
00200 void save(const OSB_DB::Session& session);
00201
00202 private:
00204 inline Key key(
00205 Result result,
00206 const Id<RecordStreamInfo>& rsId
00207 )
00208 {
00209 return std::make_pair(rsId, result);
00210 }
00211
00212 private:
00214 ModuleId module_;
00216 Counts counts_;
00217 };
00218
00220 struct CaResultDef {
00222 typedef Id<RecordStreamInfo> RsId;
00232 typedef std::map<CallAccounting::Result, double> CaCounts;
00233 };
00234
00241 struct CaCounts : public CaResultDef::CaCounts {
00243 explicit CaCounts(
00244 const CaResultDef::CaCounts& caCounts
00245 ) : CaResultDef::CaCounts(caCounts)
00246 {
00247
00248 }
00249 };
00250
00255 class CaStreamResult : public CaResultDef {
00257 friend class OSB_DB::CallAccountingGw;
00258
00259 public:
00261 typedef std::map<ModuleId, CaCounts> ModuleCounts;
00262
00263 public:
00273 explicit CaStreamResult(const RsId& rsId = RsId());
00274
00275 public:
00277 const RsId rsId() const
00278 {
00279 return rsId_;
00280 }
00281
00283 long numRecords() const
00284 {
00285 return numRecords_;
00286 }
00287
00289 const ModuleCounts& result() const
00290 {
00291 return result_;
00292 }
00293
00297 const ModuleCounts resNotZero() const;
00298
00304 void read(
00305 const OSB_DB::Session& session
00306 );
00307
00308 private:
00310 RsId rsId_;
00312 long numRecords_;
00314 ModuleCounts result_;
00315 };
00316
00318 struct CaModuleResult : public CaResultDef {
00320 struct StreamInfo {
00322 RsId rsId_;
00324 long numRecords_;
00329 StreamInfo(
00330 const RsId& rsId,
00331 long numRecords
00332 ) : rsId_(rsId), numRecords_(numRecords)
00333 {
00334
00335 }
00336
00338 bool operator<(const StreamInfo& rhs) const
00339 {
00340 return rsId_ < rhs.rsId_;
00341 }
00342 };
00343
00345 typedef std::map<StreamInfo, CaCounts> StreamCounts;
00346
00347 public:
00349 ModuleId module_;
00355 StreamCounts result_;
00356 public:
00358 explicit CaModuleResult(
00359 ModuleId module = mdUnknown
00360 ) : module_(module)
00361 {
00362
00363 }
00364 };
00365
00369 class CaResults : public CaResultDef {
00370 public:
00372 typedef std::set<ModuleId> Modules;
00373
00379 typedef std::map<RsId, CaStreamResult> Results;
00380
00381 public:
00383 const Results& results() const;
00385 const Modules& modules() const;
00387 const CaStreamResult& result(const RsId& rsId);
00395 bool result(CaModuleResult& modRes) const;
00396
00398 void clear();
00411 const CaStreamResult& add(const CaStreamResult& caRes);
00422 const CaStreamResult& read(
00423 const OSB_DB::Session& session,
00424 const RsId& rsId
00425 );
00426 private:
00428 Results results_;
00430 Modules modules_;
00431 };
00432 }
00433 #endif // #ifndef _CALLACCOUNTING_H_