diff --git a/openapi/owanalytics.yaml b/openapi/owanalytics.yaml index 3095dbd..db50510 100644 --- a/openapi/owanalytics.yaml +++ b/openapi/owanalytics.yaml @@ -969,15 +969,31 @@ paths: default: false required: false + delete: + tags: + - Board data + summary: delete board data for a given time period. + operationId: deleteBoardTimepoints + parameters: + - in: path + name: id + schema: + type: string + format: uuid + required: true + - in: query + name: fromDate + schema: + type: integer + required: false + - in: query + name: endDate + schema: + type: integer + required: false responses: 200: - description: Successfull retrieval of timepoints - content: - application/json: - schema: - oneOf: - - $ref: '#/components/schemas/DeviceTimePointList' - - $ref: '#/components/schemas/DeviceTimePointStats' + $ref: '#/components/responses/Success' 400: $ref: '#/components/responses/BadRequest' 403: diff --git a/src/RESTAPI/RESTAPI_board_timepoint_handler.cpp b/src/RESTAPI/RESTAPI_board_timepoint_handler.cpp index 22c6a59..e24587b 100644 --- a/src/RESTAPI/RESTAPI_board_timepoint_handler.cpp +++ b/src/RESTAPI/RESTAPI_board_timepoint_handler.cpp @@ -91,7 +91,7 @@ namespace OpenWifi { void RESTAPI_board_timepoint_handler::DoGet() { auto id = GetBinding("id",""); - if(id.empty()) { + if(id.empty() || !Utils::ValidUUID(id)) { return BadRequest(RESTAPI::Errors::MissingUUID); } @@ -205,15 +205,25 @@ namespace OpenWifi { Answer->set("stats", *Stats_Array); } -/* static int f=0; - std::ostringstream OO; - Answer.stringify(OO); - std::ofstream of("msg"+std::to_string(f++)+".json", std::ios_base::trunc ); - of << OO.str(); -*/ - - - return ReturnObject(*Answer); } + + void RESTAPI_board_timepoint_handler::DoDelete() { + auto id = GetBinding("id",""); + if(id.empty() || !Utils::ValidUUID(id)) { + return BadRequest(RESTAPI::Errors::MissingUUID); + } + + AnalyticsObjects::BoardInfo B; + if(!StorageService()->BoardsDB().GetRecord("id",id,B)) { + return NotFound(); + } + + auto fromDate = GetParameter("fromDate",0); + auto endDate = GetParameter("endDate",0); + + StorageService()->TimePointsDB().DeleteTimeLine(id,fromDate,endDate); + return OK(); + } + } \ No newline at end of file diff --git a/src/RESTAPI/RESTAPI_board_timepoint_handler.h b/src/RESTAPI/RESTAPI_board_timepoint_handler.h index 27f9056..693ab57 100644 --- a/src/RESTAPI/RESTAPI_board_timepoint_handler.h +++ b/src/RESTAPI/RESTAPI_board_timepoint_handler.h @@ -15,6 +15,7 @@ namespace OpenWifi { : RESTAPIHandler(bindings, L, std::vector{ Poco::Net::HTTPRequest::HTTP_GET, + Poco::Net::HTTPRequest::HTTP_DELETE, Poco::Net::HTTPRequest::HTTP_OPTIONS}, Server, TransactionId, @@ -27,6 +28,6 @@ namespace OpenWifi { void DoGet() final; void DoPost() final {}; void DoPut() final {}; - void DoDelete() final {}; + void DoDelete() final; }; } diff --git a/src/storage/storage_timepoints.cpp b/src/storage/storage_timepoints.cpp index 30d0e95..6f8453a 100644 --- a/src/storage/storage_timepoints.cpp +++ b/src/storage/storage_timepoints.cpp @@ -78,6 +78,21 @@ namespace OpenWifi { bool TimePointDB::DeleteBoard(const std::string &boardId) { return DeleteRecords(fmt::format(" boardId='{}' ", boardId)); } + + bool TimePointDB::DeleteTimeLine(const std::string &boardId, uint64_t FromDate, uint64_t LastDate) { + std::string WhereClause; + + if(FromDate && LastDate) { + WhereClause = fmt::format(" boardId='{}' and (timestamp >= {} ) and ( timestamp <= {} ) ", boardId, FromDate, LastDate); + } else if (FromDate) { + WhereClause = fmt::format(" boardId='{}' and (timestamp >= {}) ", boardId, FromDate); + } else if (LastDate) { + WhereClause = fmt::format(" boardId='{}' and (timestamp <= {}) ", boardId, LastDate); + } + DeleteRecords(WhereClause); + return true; + } + } template<> void ORM::DB::Convert(const OpenWifi::TimePointDBRecordType &In, OpenWifi::AnalyticsObjects::DeviceTimePoint &Out) { diff --git a/src/storage/storage_timepoints.h b/src/storage/storage_timepoints.h index 9e33af4..5a326a9 100644 --- a/src/storage/storage_timepoints.h +++ b/src/storage/storage_timepoints.h @@ -25,6 +25,7 @@ namespace OpenWifi { bool GetStats(const std::string &id, AnalyticsObjects::DeviceTimePointStats &S); bool SelectRecords(const std::string &boardId, uint64_t FromDate, uint64_t LastDate, uint64_t MaxRecords, DB::RecordVec & Recs); bool DeleteBoard(const std::string &boardId); + bool DeleteTimeLine(const std::string &boardId, uint64_t fromDate, uint64_t endDate); virtual ~TimePointDB() {}; private: bool Upgrade(uint32_t from, uint32_t &to) override;