1 #include <wfrest/json.hpp>
5 using namespace wfrest;
6 using Json = nlohmann::json;
11 graphContainer::graphContainer(int32_t n) : m_loopTime(n) {}
14 for (
auto item :
m_graphs) {
delete item; }
21 if (item->getId() == idx) {
return item; }
28 WFGraphTask* graph = WFTaskFactory::create_graph_task([=](WFGraphTask* task) {
29 fmt::print(fg(fmt::color::steel_blue) | fmt::emphasis::italic,
30 "Graph task complete. Wakeup main process\n");
31 fmt::print(fg(fmt::color::steel_blue) | fmt::emphasis::italic,
32 "**************************************************\n");
34 WFTimerTask* timer = WFTaskFactory::create_timer_task(
m_loopTime, 0, [=](WFTimerTask* task) {
35 fmt::print(
"Loops Graphs by {} sec.\n",
m_loopTime);
39 fmt::print(fg(fmt::color::steel_blue) | fmt::emphasis::italic,
40 "Terminated. End loop the graph task. Waiting the graph in queue done.\n");
43 auto timerNode = &graph->create_graph_node(timer);
45 (graph->create_graph_node(item->generateGraphTask()))-- > (*timerNode);
47 fmt::print(fg(fmt::color::steel_blue) | fmt::emphasis::italic,
48 "**************************************************\n");
51 fmt::print(fg(fmt::color::steel_blue) | fmt::emphasis::italic,
52 "Terminated. End loop the graph task. Waiting the graph in queue done.\n");
61 : m_graphs(cfg.graphExecSec), m_web(cfg.webPort, cfg.webRoot), m_rHttp(cfg.rHttpPort) {
81 m_rHttp().POST(
"/add_graph", [
this](
const HttpReq* req, HttpResp* resp) {
82 if (req->content_type() != APPLICATION_JSON) {
83 resp->set_status(HttpStatusBadRequest);
84 resp->set_header_pair(
"Content-Type",
"application/json");
85 resp->append_output_body(
"{\"res\": \"False\"}");
88 Json& kv = req->json();
92 resp->set_status(HttpStatusBadRequest);
93 resp->set_header_pair(
"Content-Type",
"application/json");
94 resp->append_output_body(
"{\"res\": \"False\"}");
98 resp->set_header_pair(
"Content-Type",
"application/json");
99 resp->append_output_body(
"{\"res\": \"True\"}");
102 m_rHttp().POST(
"/add_device", [
this](
const HttpReq* req, HttpResp* resp) {
103 if (req->content_type() != APPLICATION_JSON) {
104 resp->set_status(HttpStatusBadRequest);
105 resp->set_header_pair(
"Content-Type",
"application/json");
106 resp->append_output_body(
"{\"res\": \"False\"}");
109 Json& kv = req->json();
112 std::string deviceType = kv[
"deviceType"].get<std::string>();
114 if (deviceType ==
"MySQL") {
115 std::string host = kv[
"host"].get<std::string>();
116 std::string port = kv[
"port"].get<std::string>();
117 std::string usrName = kv[
"usrName"].get<std::string>();
118 std::string password = kv[
"password"].get<std::string>();
119 std::string databaseName = kv[
"databaseName"].get<std::string>();
123 usrName, password, databaseName));
124 resp->set_header_pair(
"Content-Type",
"application/json");
125 resp->append_output_body(fmt::format(
"{}\"res\": \"{}\" {}",
"{", curIdx,
"}"));
126 }
else if (deviceType ==
"Redis") {
127 std::string host = kv[
"host"].get<std::string>();
128 std::string port = kv[
"port"].get<std::string>();
129 std::string usrName = kv[
"usrName"].get<std::string>();
130 std::string password = kv[
"password"].get<std::string>();
135 m_rHttp().GET(
"/table_at_graph", [=](
const HttpReq* req, HttpResp* resp) {
136 if (!req->has_query(
"idx")) {
137 resp->set_status(500);
140 int32_t _graphId = atoi(req->query(
"idx").c_str());
146 kv[
"col"] = outs->m_shape[1];
147 if (outs->m_info.size() == 0) {
148 kv[
"tableName"] =
"DefaultName";
150 kv[
"tableName"] = outs->m_info[0].getTable();
152 std::vector<std::string> _tmpColName;
153 for (
auto& item : outs->m_info) { _tmpColName.push_back(item.getName()); }
154 kv[
"colName"] = _tmpColName;
158 m_rHttp().GET(
"/num", [=](
const HttpReq* req, HttpResp* resp) {
159 std::string tp = req->query(
"type");
160 if (tp ==
"task" || tp ==
"api") {
164 }
else if (tp ==
"device") {
171 m_rHttp().GET(
"/trans_graph", [=](
const HttpReq* req, HttpResp* resp) {
172 if (!req->has_query(
"idx")) {
173 resp->set_status(500);
176 int32_t _graphId = atoi(req->query(
"idx").c_str());
187 resp->headers[
"Content-Type"] =
"text/html";
189 std::map<cb::graph::cbNode*, cb::trans::opMapStruct> mapOpNode;
190 std::map<cb::graph::cbNode*, cb::trans::leafMapStruct> mapLeafNode;
191 std::map<int, cb::graph::cbNode*> toLeaf, toOpNode;
194 for (
auto item : nodes) {
195 switch (item->nodeT) {
196 case cb::graph::nodeType::Leaf: {
197 mapLeafNode[item].nodeCode = ++leafCodeNow;
198 mapOpNode[item->nextNode].inputNodeCode.push_back(leafCodeNow);
199 mapOpNode[item->nextNode].inputNum++;
200 toLeaf[leafCodeNow] = item;
205 mapOpNode[item].nodeCode = opCodeNow;
206 toOpNode[opCodeNow] = item;
214 for (
int i = 1; i < opCodeNow + 1; i++) {
215 firstPosy = firstPosy + 100;
216 auto opnode = toOpNode[i];
217 mapOpNode[opnode].posy = firstPosy;
218 for (
auto j : mapOpNode[opnode].inputNodeCode) {
219 auto leafNode = toLeaf[j];
220 mapLeafNode[leafNode].posy = firstPosy;
221 firstPosy = firstPosy + 200;
223 mapOpNode[opnode].posy = (firstPosy + mapOpNode[opnode].posy) / 2;
226 int finNodePosy = (mapOpNode[toOpNode[1]].posy + mapOpNode[toOpNode[opCodeNow]].posy) / 2;
228 for (
int i = 1; i < opCodeNow + 1; i++) {
229 auto opnode = toOpNode[i];
231 for (
auto j : mapOpNode[opnode].inputNodeCode) {
232 auto leafNode = toLeaf[j];
234 auto deviceNode = device_n->getDevice();
238 mapOpNode[opnode].inputNumNow++;
246 void app::execMain() {
256 void app::stopMain() {
259 m_graphs.setTerminated(
true);