|
@@ -372,24 +372,90 @@ void statistics_db::writeStatisticsConv(std::unordered_map<conv, entry_convStat>
|
|
|
query.exec();
|
|
|
query.reset();
|
|
|
}
|
|
|
- else if (e.pkts_count == 1){
|
|
|
- int minDelay = -1;
|
|
|
- int maxDelay = -1;
|
|
|
- e.avg_pkt_rate = (float) -1;
|
|
|
- e.avg_interarrival_time = e.avg_interarrival_time = (std::chrono::microseconds) 0;
|
|
|
+ }
|
|
|
+ transaction.commit();
|
|
|
+ }
|
|
|
+ catch (std::exception &e) {
|
|
|
+ std::cout << "Exception in statistics_db: " << e.what() << std::endl;
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
+
|
|
|
+/**
|
|
|
+ * Writes the stateless conversation statistics into the database.
|
|
|
+ * @param convStatistics The stateless conversation from class statistics.
|
|
|
+ */
|
|
|
+void statistics_db::writeStatisticsConvStateless(std::unordered_map<conv, entry_convStat> convStatistics){
|
|
|
+ try {
|
|
|
+ db->exec("DROP TABLE IF EXISTS conv_statistics_stateless");
|
|
|
+ SQLite::Transaction transaction(*db);
|
|
|
+ const char *createTable = "CREATE TABLE conv_statistics_stateless ("
|
|
|
+ "ipAddressA TEXT,"
|
|
|
+ "portA INTEGER,"
|
|
|
+ "ipAddressB TEXT,"
|
|
|
+ "portB INTEGER,"
|
|
|
+ "pktsCount INTEGER,"
|
|
|
+ "avgPktRate REAL,"
|
|
|
+ "avgDelay INTEGER,"
|
|
|
+ "minDelay INTEGER,"
|
|
|
+ "maxDelay INTEGER,"
|
|
|
+ "PRIMARY KEY(ipAddressA,portA,ipAddressB,portB));";
|
|
|
+ db->exec(createTable);
|
|
|
+ SQLite::Statement query(*db, "INSERT INTO conv_statistics_stateless VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
|
|
+
|
|
|
+ // Calculate average of inter-arrival times and average packet rate
|
|
|
+ for (auto it = convStatistics.begin(); it != convStatistics.end(); ++it) {
|
|
|
+ conv f = it->first;
|
|
|
+ entry_convStat e = it->second;
|
|
|
+ if (e.pkts_count > 0){
|
|
|
query.bind(1, f.ipAddressA);
|
|
|
query.bind(2, f.portA);
|
|
|
query.bind(3, f.ipAddressB);
|
|
|
query.bind(4, f.portB);
|
|
|
- query.bind(5, (int) e.pkts_count);
|
|
|
- query.bind(6, (float) e.avg_pkt_rate);
|
|
|
- query.bind(7, (int) e.avg_interarrival_time.count());
|
|
|
- query.bind(8, minDelay);
|
|
|
- query.bind(9, maxDelay);
|
|
|
- query.exec();
|
|
|
- query.reset();
|
|
|
+
|
|
|
+ if (e.pkts_count == 1){
|
|
|
+ e.avg_pkt_rate = (float) -1;
|
|
|
+ e.avg_interarrival_time = (std::chrono::microseconds) -1;
|
|
|
+
|
|
|
+ query.bind(5, (int) e.pkts_count);
|
|
|
+ query.bind(6, (float) e.avg_pkt_rate);
|
|
|
+ query.bind(7, (int) e.avg_interarrival_time.count());
|
|
|
+ query.bind(8, -1);
|
|
|
+ query.bind(9, -1);
|
|
|
+ query.exec();
|
|
|
+ query.reset();
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ int sumDelay = 0;
|
|
|
+ int minDelay = -1;
|
|
|
+ int maxDelay = -1;
|
|
|
+ for (int i = 0; (unsigned) i < e.interarrival_time.size(); i++) {
|
|
|
+ sumDelay += e.interarrival_time[i].count();
|
|
|
+ if (maxDelay < e.interarrival_time[i].count())
|
|
|
+ maxDelay = e.interarrival_time[i].count();
|
|
|
+ if (minDelay > e.interarrival_time[i].count() || minDelay == -1)
|
|
|
+ minDelay = e.interarrival_time[i].count();
|
|
|
+ }
|
|
|
+ if (e.interarrival_time.size() > 0)
|
|
|
+ e.avg_interarrival_time = (std::chrono::microseconds) sumDelay / e.interarrival_time.size(); // average
|
|
|
+ else e.avg_interarrival_time = (std::chrono::microseconds) 0;
|
|
|
+
|
|
|
+ std::chrono::microseconds start_timesttamp = e.pkts_timestamp[0];
|
|
|
+ std::chrono::microseconds end_timesttamp = e.pkts_timestamp.back();
|
|
|
+ std::chrono::microseconds conn_duration = end_timesttamp - start_timesttamp;
|
|
|
+ e.avg_pkt_rate = (float) e.pkts_count * 1000000 / conn_duration.count(); // pkt per sec
|
|
|
+
|
|
|
+
|
|
|
+ query.bind(5, (int) e.pkts_count);
|
|
|
+ query.bind(6, (float) e.avg_pkt_rate);
|
|
|
+ query.bind(7, (int) e.avg_interarrival_time.count());
|
|
|
+ query.bind(8, minDelay);
|
|
|
+ query.bind(9, maxDelay);
|
|
|
+ query.exec();
|
|
|
+ query.reset();
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
transaction.commit();
|
|
|
}
|
|
@@ -398,6 +464,7 @@ void statistics_db::writeStatisticsConv(std::unordered_map<conv, entry_convStat>
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* Writes the interval statistics into the database.
|
|
|
* @param intervalStatistics The interval entries from class statistics.
|