Mercurial > hg > orthanc-databases
comparison Framework/Plugins/OptimizedRoutes.cpp @ 161:2ccde9c7311b optimized-routes
added new optimized REST routes. this is a temporary work to try to speed up some routes (used by LRO). This way, we avoid another app to access the Orthanc DB and we skip the plugin SDK update for a very specific route
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Fri, 10 Jul 2020 13:26:47 +0200 |
parents | |
children | 6f83b74373d3 |
comparison
equal
deleted
inserted
replaced
160:84b6fc6f6b9b | 161:2ccde9c7311b |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "OptimizedRoutes.h" | |
23 #include "../../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" | |
24 | |
25 #include <string> | |
26 #include <map> | |
27 #include <list> | |
28 #include <json/writer.h> | |
29 #include <json/value.h> | |
30 #include <boost/algorithm/string.hpp> // for boost::algorithm::split & boost::starts_with | |
31 | |
32 static OrthancDatabases::IndexBackend* backend_; | |
33 static OrthancPluginContext* context_; | |
34 | |
35 extern "C" | |
36 { | |
37 | |
38 // handles url like /optimized-routes/studies/{id)/instances-metadata?types=1,10 | |
39 OrthancPluginErrorCode RestGetStudiesInstancesMetadata(OrthancPluginRestOutput* output, | |
40 const char* /* url */, | |
41 const OrthancPluginHttpRequest* request) | |
42 { | |
43 std::list<int32_t> metadataTypes; | |
44 for (uint32_t i = 0; i < request->getCount; i++) | |
45 { | |
46 if (strcmp(request->getKeys[i], "types") == 0) | |
47 { | |
48 std::string getValue(request->getValues[i]); | |
49 std::vector<std::string> typesString; | |
50 boost::algorithm::split(typesString, getValue, boost::is_any_of(",")); | |
51 | |
52 for (size_t t = 0; t < typesString.size(); t++) | |
53 { | |
54 int32_t metadataType = boost::lexical_cast<int32_t>(typesString[t]); | |
55 metadataTypes.push_back(metadataType); | |
56 } | |
57 } | |
58 } | |
59 | |
60 std::map<std::string, std::map<int32_t, std::string>> result; | |
61 std::string orthancId = std::string(request->groups[0]); | |
62 backend_->GetStudyInstancesMetadata(result, | |
63 orthancId, | |
64 metadataTypes); | |
65 | |
66 Json::Value response(Json::objectValue); | |
67 for (std::map<std::string, std::map<int32_t, std::string>>::const_iterator itInstance = result.begin(); itInstance != result.end(); itInstance++) | |
68 { | |
69 Json::Value instanceMetadatas; | |
70 for (std::map<int32_t, std::string>::const_iterator itMetadata = itInstance->second.begin(); itMetadata != itInstance->second.end(); itMetadata++) | |
71 { | |
72 std::string id = boost::lexical_cast<std::string>(itMetadata->first); | |
73 instanceMetadatas[id] = itMetadata->second; | |
74 } | |
75 | |
76 response[itInstance->first] = instanceMetadatas; | |
77 } | |
78 | |
79 Json::StyledWriter writer; | |
80 std::string outputStr = writer.write(response); | |
81 OrthancPluginAnswerBuffer(context_, output, outputStr.c_str(), outputStr.size(), "application/json"); | |
82 | |
83 return OrthancPluginErrorCode_Success; | |
84 } | |
85 | |
86 // handles url like /optimized-routes/studies/{id)/instances-ids | |
87 OrthancPluginErrorCode RestGetStudiesInstancesIds(OrthancPluginRestOutput* output, | |
88 const char* /*url*/, | |
89 const OrthancPluginHttpRequest* request) | |
90 { | |
91 std::list<std::string> result; | |
92 std::string orthancId = std::string(request->groups[0]); | |
93 backend_->GetStudyInstancesIds(result, | |
94 orthancId); | |
95 | |
96 Json::Value response(Json::arrayValue); | |
97 for (std::list<std::string>::const_iterator itInstance = result.begin(); itInstance != result.end(); itInstance++) | |
98 { | |
99 response.append(*itInstance); | |
100 } | |
101 | |
102 Json::StyledWriter writer; | |
103 std::string outputStr = writer.write(response); | |
104 OrthancPluginAnswerBuffer(context_, output, outputStr.c_str(), outputStr.size(), "application/json"); | |
105 | |
106 return OrthancPluginErrorCode_Success; | |
107 } | |
108 } | |
109 | |
110 namespace OrthancDatabases { | |
111 | |
112 void OptimizedRoutes::EnableOptimizedRoutes(IndexBackend *backend, OrthancPluginContext *context) | |
113 { | |
114 backend_ = backend; | |
115 context_ = context; | |
116 // Register optimized rest routes (temporary) | |
117 OrthancPluginRegisterRestCallbackNoLock(context_, "/optimized-routes/studies/(.*)/instances-metadata", RestGetStudiesInstancesMetadata); | |
118 OrthancPluginRegisterRestCallbackNoLock(context_, "/optimized-routes/studies/(.*)/instances-ids", RestGetStudiesInstancesIds); | |
119 } | |
120 } |