ParserJson.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2022-04-12 15:40:03
  6. * @LastEditors: lishengyin
  7. * @LastEditTime: 2022-06-27 15:26:26
  8. */
  9. #ifndef __PARSERJSON_HPP_
  10. #define __PARSERJSON_HPP_
  11. #include <iostream>
  12. #include "ParserBase.hpp"
  13. using namespace std;
  14. namespace ins
  15. {
  16. class ParserJson:public ParserBase
  17. {
  18. private:
  19. public:
  20. ParserJson():ParserBase(){}
  21. ~ParserJson(){}
  22. /**
  23. * @description: 解析配置
  24. * @param {shared_ptr<InsPacket>} packet
  25. * @return {*}
  26. */
  27. bool ParseConfig(std::shared_ptr<InsPacket> packet){
  28. ApolloServerResultMsg<ApolloAppConfig> msg;
  29. if(msg.jsonToObject(packet->getData()->data)){
  30. InsUniversalData::Ptr toData = std::make_shared<InsUniversalData>();
  31. msg.configurations.USERID = Disposition::getPtr()->getUserId();
  32. toData->format = InsDataFormat::INS_DATA_FORMAT_SQL;
  33. msg.ObjectToSql(toData->data);
  34. packet->setToData(toData);
  35. return true;
  36. }
  37. return false;
  38. }
  39. /**
  40. * @description: 解析 KsTemplate
  41. * @param {*}
  42. * @return {*}
  43. */
  44. bool ParseKsTemplate(std::shared_ptr<InsPacket> packet){
  45. return false;
  46. }
  47. /**
  48. * @description: 获取对应类型的解析器
  49. * @param {std::string} DescriptionId 描述的ID
  50. * @return {*}
  51. */
  52. MsgBase::Ptr getParserType(std::string DescriptionId){
  53. MsgBase::Ptr msg = nullptr;
  54. SqlStream stream("SELECT TYPE,PARSERTYPE FROM InsServerDB.`InsDataDescription` WHERE USERID = 2 AND DESCRIPTIONID = '?'");
  55. stream << DescriptionId;
  56. vector<vector<std::string>> res;
  57. std::string sql = (std::string)stream;
  58. res = GetCenter::getPtr()->ExecuteSql(sql);
  59. if(res.empty()) return msg;
  60. if(res.size() > 1) return msg;
  61. for(auto &line: res){
  62. if(line[1] == "ApolloAppConfig") msg = std::make_shared<ApolloServerResultMsg<ApolloAppConfig>>();
  63. else if(line[1] == "ApolloKsTemplate") msg = std::make_shared<ApolloServerResultMsg<ApolloKsTemplate>>();
  64. else if(line[1] == "KubeSphereNodes") msg = std::make_shared<KubeSphereResourcesMsg<KubeSphereNodes>>();
  65. else if(line[1] == "KubeSpherePods") msg = std::make_shared<KubeSphereResourcesMsg<KubeSpherePods>>();
  66. else if(line[1] == "KubeSphereServers") msg = std::make_shared<KubeSphereResourcesMsg<KubeSphereServers>>();
  67. else if(line[1] == "KubeSphereDeployments") msg = std::make_shared<KubeSphereResourcesMsg<KubeSphereDeployments>>();
  68. }
  69. return msg;
  70. }
  71. /**
  72. * @description: 解析数据
  73. * @param {*}
  74. * @return {*}
  75. */
  76. virtual bool ParserData(std::shared_ptr<InsPacket> packet) override{
  77. MsgBase::Ptr msg = nullptr;
  78. msg = getParserType(packet->getData()->attr1);
  79. if(msg == nullptr) return false;
  80. if(msg->jsonToObject(packet->getData()->data)){
  81. InsUniversalData::Ptr toData = std::make_shared<InsUniversalData>();
  82. msg->DATATIME = packet->getCreateTime();
  83. msg->USERID = Disposition::getPtr()->getUserId();
  84. msg->ID = std::atoi(packet->getData()->attr1.c_str());
  85. msg->DATATIME = packet->getCreateTime();
  86. if(packet->getBournType() == InsPacketTypes::INS_MYSQL_PACKET){
  87. if(!msg->ObjectToSql(toData->data)) return false;
  88. msg->ObjectToSqlBack(toData->attr2);
  89. toData->format = InsDataFormat::INS_DATA_FORMAT_SQL;
  90. }
  91. packet->setToData(toData);
  92. return true;
  93. }
  94. return false;
  95. }
  96. };
  97. } // namespace ins
  98. #endif