From 9fbdbbd1bb4de7c9bbceaa3bf460dcc2d6918667 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 4 Nov 2021 10:28:10 -0700 Subject: [PATCH] Fixing stats reset at sim startup. --- build | 2 +- openapi/owls.yaml | 45 +++++----------------- src/RESTAPI/RESTAPI_results_handler.cpp | 2 +- src/RESTObjects/RESTAPI_OWLSobjects.cpp | 44 --------------------- src/RESTObjects/RESTAPI_OWLSobjects.h | 23 ----------- src/SimStats.h | 8 +++- src/Simulation.cpp | 17 +++++++++ src/storage/storage_results.cpp | 51 +++++++++++++++---------- src/storage/storage_results.h | 5 ++- 9 files changed, 69 insertions(+), 128 deletions(-) diff --git a/build b/build index c5b431b..6139554 100644 --- a/build +++ b/build @@ -1 +1 @@ -50 \ No newline at end of file +52 \ No newline at end of file diff --git a/openapi/owls.yaml b/openapi/owls.yaml index 9aa88e4..698263f 100644 --- a/openapi/owls.yaml +++ b/openapi/owls.yaml @@ -348,41 +348,6 @@ components: items: $ref: '#/components/schemas/SimulationDetails' - SimulationResults: - type: object - properties: - id: - type: string - format: uuid - simulationId: - type: string - format: uuid - startTime: - type: integer - format: int64 - endTime: - type: integer - format: int64 - numClientsSuccesses: - type: integer - numClientsErrors: - type: integer - timeToFullDevices: - type: integer - format: int64 - tx: - type: integer - rx: - type: integer - - SimulationResultsList: - type: object - properties: - list: - type: array - items: - $ref: '#/components/schemas/SimulationResults' - SimulationStatus: type: object properties: @@ -423,6 +388,14 @@ components: - canceled - completed + SimulationStatusList: + type: object + properties: + list: + type: array + items: + $ref: '#/components/schemas/SimulationStatus' + paths: /simulation: get: @@ -498,7 +471,7 @@ paths: operationId: getSimulationResults responses: 200: - $ref: '#/components/schemas/SimulationResultsList' + $ref: '#/components/schemas/SimulationStatusList' 400: $ref: '#/components/responses/BadRequest' diff --git a/src/RESTAPI/RESTAPI_results_handler.cpp b/src/RESTAPI/RESTAPI_results_handler.cpp index 904e1b1..10c7e22 100644 --- a/src/RESTAPI/RESTAPI_results_handler.cpp +++ b/src/RESTAPI/RESTAPI_results_handler.cpp @@ -9,7 +9,7 @@ namespace OpenWifi { void RESTAPI_results_handler::DoGet() { - std::vector Results; + std::vector Results; StorageService()->SimulationResultsDB().GetRecords(1,10000,Results); return ReturnObject("list",Results); } diff --git a/src/RESTObjects/RESTAPI_OWLSobjects.cpp b/src/RESTObjects/RESTAPI_OWLSobjects.cpp index 331538a..d7c5c74 100644 --- a/src/RESTObjects/RESTAPI_OWLSobjects.cpp +++ b/src/RESTObjects/RESTAPI_OWLSobjects.cpp @@ -82,50 +82,6 @@ namespace OpenWifi::OWLSObjects { return false; } - void SimulationResults::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"id", id); - field_to_json(Obj,"simulationId", simulationId); - field_to_json(Obj,"startTime", startTime); - field_to_json(Obj,"endTime", endTime); - field_to_json(Obj,"numClientsSuccesses", numClientsSuccesses); - field_to_json(Obj,"numClientsErrors", numClientsErrors); - field_to_json(Obj,"timeToFullDevices", timeToFullDevices); - field_to_json(Obj,"tx", tx); - field_to_json(Obj,"rx", rx); - } - - bool SimulationResults::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"id", id); - field_from_json(Obj,"simulationId", simulationId); - field_from_json(Obj,"startTime", startTime); - field_from_json(Obj,"endTime", endTime); - field_from_json(Obj,"numClientsSuccesses", numClientsSuccesses); - field_from_json(Obj,"numClientsErrors", numClientsErrors); - field_from_json(Obj,"timeToFullDevices", timeToFullDevices); - field_from_json(Obj,"tx", tx); - field_from_json(Obj,"rx", rx); - return true; - } catch(...) { - - } - return false; - } - - void SimulationResultsList::to_json(Poco::JSON::Object &Obj) const { - field_to_json(Obj,"list", list); - } - - bool SimulationResultsList::from_json(const Poco::JSON::Object::Ptr &Obj) { - try { - field_from_json(Obj,"list", list); - return true; - } catch(...) { - - } - return false; - } - void SimulationStatus::to_json(Poco::JSON::Object &Obj) const { field_to_json(Obj,"id", id); field_to_json(Obj,"simulationId", simulationId); diff --git a/src/RESTObjects/RESTAPI_OWLSobjects.h b/src/RESTObjects/RESTAPI_OWLSobjects.h index 77d92d6..8fcf1b1 100644 --- a/src/RESTObjects/RESTAPI_OWLSobjects.h +++ b/src/RESTObjects/RESTAPI_OWLSobjects.h @@ -43,29 +43,6 @@ namespace OpenWifi::OWLSObjects { bool from_json(const Poco::JSON::Object::Ptr &Obj); }; - - struct SimulationResults { - std::string id; - std::string simulationId; - uint64_t startTime; - uint64_t endTime; - uint64_t numClientsSuccesses; - uint64_t numClientsErrors; - uint64_t timeToFullDevices; - uint64_t tx; - uint64_t rx; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - - struct SimulationResultsList { - std::vector list; - - void to_json(Poco::JSON::Object &Obj) const; - bool from_json(const Poco::JSON::Object::Ptr &Obj); - }; - struct SimulationStatus { std::string id; std::string simulationId; diff --git a/src/SimStats.h b/src/SimStats.h index 73261f8..bb2a758 100644 --- a/src/SimStats.h +++ b/src/SimStats.h @@ -85,17 +85,23 @@ namespace OpenWifi { Status_.state = S; } - [[nodiscard]] const std::string & GetState() { + [[nodiscard]] inline const std::string & GetState() { std::lock_guard G(Mutex_); return Status_.state; } + [[nodiscard]] inline const std::string & Id() const { + return Status_.id; + } + inline void Reset() { Status_.liveDevices = Status_.tx = Status_.msgsRx = Status_.msgsTx = Status_.rx = Status_.endTime = Status_.errorDevices = Status_.startTime = Status_.timeToFullDevices = 0; Status_.simulationId = Status_.id = Status_.state = ""; } + [[nodiscard]] uint64_t GetStartTime() const { return Status_.startTime; } + private: static SimStats * instance_; OWLSObjects::SimulationStatus Status_; diff --git a/src/Simulation.cpp b/src/Simulation.cpp index fc91bf5..a87f7fd 100644 --- a/src/Simulation.cpp +++ b/src/Simulation.cpp @@ -41,6 +41,17 @@ namespace OpenWifi { Poco::Thread::trySleep(2000); if(!Running_) break; + + if(SimStats()->GetState()!="running") { + continue; + } + + uint64_t Now = std::time(nullptr); + + if( (Now - SimStats()->GetStartTime()) > CurrentSim_.simulationLength ) { + std::string Error; + StopSim( SimStats()->Id(), Error ); + } } } @@ -142,6 +153,12 @@ namespace OpenWifi { SimRunning_ = false; SimStats()->SetState("stopped"); + SimStats()->EndSim(); + + OWLSObjects::SimulationStatus S; + SimStats()->GetCurrent(S); + StorageService()->SimulationResultsDB().CreateRecord(S); + return true; } diff --git a/src/storage/storage_results.cpp b/src/storage/storage_results.cpp index 1165948..bea8627 100644 --- a/src/storage/storage_results.cpp +++ b/src/storage/storage_results.cpp @@ -9,13 +9,16 @@ namespace OpenWifi { static ORM::FieldVec SimulationResultsDB_Fields{ ORM::Field{"id",64, true}, ORM::Field{"simulationId",ORM::FieldType::FT_TEXT}, + ORM::Field{"state",ORM::FieldType::FT_TEXT}, + ORM::Field{"tx",ORM::FieldType::FT_BIGINT}, + ORM::Field{"rx",ORM::FieldType::FT_BIGINT}, + ORM::Field{"msgsTx",ORM::FieldType::FT_BIGINT}, + ORM::Field{"msgsRx",ORM::FieldType::FT_BIGINT}, + ORM::Field{"liveDevices",ORM::FieldType::FT_BIGINT}, + ORM::Field{"timeToFullDevices",ORM::FieldType::FT_BIGINT}, ORM::Field{"startTime",ORM::FieldType::FT_BIGINT}, ORM::Field{"endTime",ORM::FieldType::FT_BIGINT}, - ORM::Field{"numClientsSuccesses",ORM::FieldType::FT_BIGINT}, - ORM::Field{"numClientsErrors",ORM::FieldType::FT_BIGINT}, - ORM::Field{"timeToFullDevices",ORM::FieldType::FT_BIGINT}, - ORM::Field{"tx",ORM::FieldType::FT_BIGINT}, - ORM::Field{"rx",ORM::FieldType::FT_BIGINT} + ORM::Field{"errorDevices",ORM::FieldType::FT_BIGINT} }; static ORM::IndexVec SimulationResultsDB_Indexes{ @@ -26,26 +29,32 @@ namespace OpenWifi { } } -template<> void ORM::DB::Convert(OpenWifi::SimulationResultsDBRecordType &In, OpenWifi::OWLSObjects::SimulationResults &Out) { +template<> void ORM::DB::Convert(OpenWifi::SimulationResultsDBRecordType &In, OpenWifi::OWLSObjects::SimulationStatus &Out) { Out.id = In.get<0>(); Out.simulationId = In.get<1>(); - Out.startTime = In.get<2>(); - Out.endTime = In.get<3>(); - Out.numClientsSuccesses = In.get<4>(); - Out.numClientsErrors = In.get<5>(); - Out.timeToFullDevices = In.get<6>(); - Out.tx = In.get<7>(); - Out.rx = In.get<8>(); + Out.state = In.get<2>(); + Out.tx = In.get<3>(); + Out.rx = In.get<4>(); + Out.msgsTx = In.get<5>(); + Out.msgsRx = In.get<6>(); + Out.liveDevices = In.get<7>(); + Out.timeToFullDevices = In.get<8>(); + Out.startTime = In.get<9>(); + Out.endTime = In.get<10>(); + Out.errorDevices = In.get<11>(); } -template<> void ORM::DB::Convert(OpenWifi::OWLSObjects::SimulationResults &In, OpenWifi::SimulationResultsDBRecordType &Out) { +template<> void ORM::DB::Convert(OpenWifi::OWLSObjects::SimulationStatus &In, OpenWifi::SimulationResultsDBRecordType &Out) { Out.set<0>(In.id); Out.set<1>(In.simulationId); - Out.set<2>(In.startTime); - Out.set<3>(In.endTime); - Out.set<4>(In.numClientsSuccesses); - Out.set<5>(In.numClientsErrors); - Out.set<6>(In.timeToFullDevices); - Out.set<7>(In.tx); - Out.set<8>(In.rx); + Out.set<2>(In.state); + Out.set<3>(In.tx); + Out.set<4>(In.rx); + Out.set<5>(In.msgsTx); + Out.set<6>(In.msgsRx); + Out.set<7>(In.liveDevices); + Out.set<8>(In.timeToFullDevices); + Out.set<9>(In.startTime); + Out.set<10>(In.endTime); + Out.set<11>(In.errorDevices); } diff --git a/src/storage/storage_results.h b/src/storage/storage_results.h index c144f01..7b0f07b 100644 --- a/src/storage/storage_results.h +++ b/src/storage/storage_results.h @@ -12,6 +12,9 @@ namespace OpenWifi { typedef Poco::Tuple< std::string, std::string, + std::string, + uint64_t, + uint64_t, uint64_t, uint64_t, uint64_t, @@ -21,7 +24,7 @@ namespace OpenWifi { uint64_t > SimulationResultsDBRecordType; - class SimulationResultsDB : public ORM::DB { + class SimulationResultsDB : public ORM::DB { public: SimulationResultsDB( OpenWifi::DBType T, Poco::Data::SessionPool & P, Poco::Logger &L); private: