1
0

bindmorecard.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. #ifndef BIND_MORE_CARD_HPP__
  2. #define BIND_MORE_CARD_HPP__
  3. #include <time.h>
  4. #include <thread>
  5. #include <chrono>
  6. #include <functional>
  7. #include <unordered_map>
  8. #include <mutex>
  9. #include <sstream>
  10. #include <map>
  11. #include <functional>
  12. #include "card.h"
  13. #include "zlist.h"
  14. #include "cardMgr.h"
  15. #include "log.h"
  16. #define TIME_WIN_MILLISEC (15*60*1000)
  17. #define TIME_LIM_SEC (3*1000)
  18. struct MyHash
  19. {
  20. typedef uint64_t result_type;
  21. result_type operator ()(uint64_t c1,uint64_t c2) const
  22. {
  23. std::string s;
  24. if(c1<c2)
  25. s = std::to_string(c1) + std::to_string(c2);
  26. else
  27. s = std::to_string(c2) + std::to_string(c1);
  28. result_type st1 = std::hash<std::string>{}(s);
  29. return st1;
  30. }
  31. };
  32. struct Data
  33. {
  34. Data(uint64_t ct)
  35. {
  36. reset();
  37. m_timestamp = ct;
  38. }
  39. void reset()
  40. {
  41. m_index = 0;
  42. m_ct.fill(0);
  43. m_totaldistance = 0;
  44. m_flag = false;
  45. }
  46. void increase(const int index)
  47. {
  48. m_ct[index]++;
  49. }
  50. uint32_t m_index;
  51. std::array<uint16_t,2> m_ct;
  52. double m_totaldistance;
  53. uint64_t m_timestamp;
  54. bool m_flag;
  55. };
  56. struct TcardInterface
  57. {
  58. // TcardInterface()=default;
  59. TcardInterface(std::pair<uint64_t,uint64_t> sp,uint64_t ct,uint64_t owner,int size)
  60. :m_cardid(sp)
  61. ,m_timestamp(ct)
  62. ,m_owner(owner)
  63. ,SIZE(size)
  64. {}
  65. std::tuple<bool,std::string> setindex(uint64_t tsp,const uint64_t cid,const double d=0,bool ctflag =false)
  66. {
  67. bool flag = false;
  68. assert(m_timestamp != 0||tsp != 0);
  69. std::tuple<bool,std::string> stp(flag,"");
  70. if (empty())
  71. grow(tsp);
  72. uint64_t tmp_tsp_min = back(0).m_timestamp;
  73. uint64_t tmp_tsp_max = back(0).m_timestamp + TIME_WIN_MILLISEC;
  74. //assert(tsp >= tmp_tsp_min);
  75. do
  76. {
  77. if(tsp>=tmp_tsp_min && tsp < tmp_tsp_max)
  78. {
  79. if(!ctflag)
  80. {
  81. back(0).m_index++;
  82. back(0).m_totaldistance+=d;
  83. }
  84. else
  85. {
  86. int arrindex = getstrindex(cid);
  87. if(arrindex == -1)
  88. {
  89. log_error("bindmorecard error.not match the cardid");
  90. return stp;
  91. }
  92. back(0).increase(arrindex);
  93. }
  94. break;
  95. }
  96. else if(tsp>=tmp_tsp_max)
  97. {
  98. if(timeout())
  99. {
  100. auto tp = get();
  101. stp = check(std::get<0>(tp),std::get<1>(tp));
  102. skip(1);
  103. }
  104. grow(tsp);
  105. tmp_tsp_min = back(0).m_timestamp;
  106. tmp_tsp_max = back(0).m_timestamp + TIME_WIN_MILLISEC;
  107. }
  108. else
  109. {
  110. tsp=tmp_tsp_min;
  111. }
  112. }while(1);
  113. return stp;
  114. }
  115. std::tuple<bool,std::string> checkLast()
  116. {
  117. // std::tuple<bool,std::string> stp(false,std::string(""));
  118. // if (!timeout() && size()>=ConfStruct::getCs().remote_slot)
  119. // {
  120. // auto tp = get();
  121. // stp = check(std::get<0>(tp),std::get<1>(tp));
  122. // }
  123. // return stp;
  124. return std::make_tuple(false,"");
  125. }
  126. void skip(int count)
  127. {
  128. for(int i=0;i<count&&i<size();i++)
  129. m_arr.pop_front();
  130. }
  131. bool empty()
  132. {
  133. return m_arr.empty();
  134. }
  135. int size()
  136. {
  137. return m_arr.size();
  138. }
  139. void grow(uint64_t ct)
  140. {
  141. Data d(ct);
  142. m_arr.push_back(d);
  143. }
  144. Data &back(int index)
  145. {
  146. assert(index<(int)size() && index >= 0);
  147. return m_arr.at(size()-index-1);
  148. }
  149. std::tuple<int,int,double> get()
  150. {
  151. int total_ix=0,total_ct=0;
  152. double dis = 0;
  153. std::for_each(m_arr.begin(),m_arr.end(),[&total_ix,&total_ct,&dis](const Data &d){total_ix += d.m_index;total_ct+=(d.m_ct[0]+d.m_ct[1]);dis+=d.m_totaldistance;});
  154. return std::make_tuple(total_ix,total_ct,dis);
  155. }
  156. bool timeout()
  157. {
  158. return size() == SIZE;
  159. }
  160. std::string getInfo_1()
  161. {
  162. std::stringstream ss;
  163. ss<<m_cardid.first<<"&"<<m_cardid.second<<","<<m_arr[0].m_timestamp/1000<<","<<(back(0).m_timestamp+TIME_WIN_MILLISEC)/1000 <<",";
  164. std::for_each(m_arr.begin(),m_arr.end(),[&ss](Data& d){
  165. if (!d.m_flag)
  166. {
  167. ss<<d.m_timestamp/1000<<"&"<<d.m_totaldistance<<"&"<<d.m_index<<",";
  168. d.m_flag = true;
  169. }
  170. });
  171. return ss.str();
  172. }
  173. std::string getInfo()
  174. {
  175. auto tp = get();
  176. std::stringstream ss;
  177. ss<<"{T:"<<std::get<0>(tp)<<","<<std::get<1>(tp)<<","<<std::get<2>(tp)<<"}";
  178. //ss<<"{Total index:"<<std::get<0>(tp)<<","<<std::get<1>(tp)<<"},";
  179. //m_arr.for_each([&ss](const Data&x){
  180. // ss<<"["<<x.m_index<<","<<x.m_ct[0]+x.m_ct[1]<<"]";
  181. //});
  182. return std::move(ss.str());
  183. }
  184. inline std::tuple<bool,std::string> check(int index,int ct)
  185. {
  186. if(index*1.0/ct>=0.72)
  187. {
  188. std::string s = getInfo_1();
  189. return std::make_tuple(true,s);
  190. }
  191. return std::make_tuple(false,std::string(""));
  192. }
  193. inline int getstrindex(const uint64_t cardid)
  194. {
  195. if(m_cardid.first == cardid)
  196. return 0;
  197. else
  198. return 1;
  199. return -1;
  200. }
  201. public:
  202. std::pair<uint64_t,uint64_t> m_cardid;
  203. uint64_t m_timestamp;
  204. uint64_t m_owner;
  205. std::deque<Data> m_arr;
  206. int SIZE;
  207. virtual ~TcardInterface(){}
  208. };
  209. struct CardFactory
  210. {
  211. CardFactory(cardMgr*owner)
  212. :m_owner(owner)
  213. {}
  214. std::map<uint64_t,std::string> setCT(uint64_t cid)
  215. {
  216. std::map<uint64_t,std::string> tmpvec;
  217. auto opcard=card_list::instance()->get(cid);
  218. uint64_t cttime = opcard->time_();
  219. auto vec = m_owner->getcard(cid);
  220. for(const auto cardid:vec)
  221. {
  222. uint64_t id = MyHash{}(cardid,cid);
  223. auto iter = m_map.find(id);
  224. if(iter != m_map.end())
  225. {
  226. std::shared_ptr<TcardInterface> ptr = iter->second;
  227. auto sp = ptr->setindex(cttime,cid,0,true);
  228. if (std::get<0>(sp))
  229. {
  230. tmpvec.insert(std::make_pair(id,std::get<1>(sp)));
  231. }
  232. }
  233. }
  234. handle_event(tmpvec);
  235. return std::move(tmpvec);
  236. }
  237. std::map<uint64_t,std::string> handlecard(const std::vector<uint64_t>& vec,uint64_t cid)
  238. {
  239. std::map<uint64_t,std::string> tempvec;
  240. auto opcard = card_list::instance()->get(cid);
  241. uint64_t cttime = opcard->time_();
  242. std::shared_ptr<TcardInterface> ptr = nullptr;
  243. for(const auto cardid:vec)
  244. {
  245. uint64_t id = MyHash{}(cardid,cid);
  246. auto iter = m_map.find(id);
  247. if(iter != m_map.end())
  248. ptr = iter->second;
  249. else
  250. {
  251. log_info("handlecard..%lu,%d,%d",id,cardid,cid);
  252. ptr = make_shared_(cardid,cid,cttime,id);
  253. m_map.insert({id,ptr});
  254. backup(cid,cardid);
  255. }
  256. auto npcard=card_list::instance()->get(cardid);
  257. double dis = opcard->dist(*npcard);
  258. auto sp=ptr->setindex(cttime,cid,dis);
  259. if (std::get<0>(sp))
  260. {
  261. tempvec.insert(std::make_pair(id,std::get<1>(sp)));
  262. }
  263. }
  264. handle_event(tempvec);
  265. return std::move(tempvec);
  266. }
  267. void erase(uint64_t id)
  268. {
  269. auto it = m_map.find(id);
  270. if(it != m_map.end())
  271. {
  272. std::pair<uint64_t,uint64_t> p = it->second->m_cardid;
  273. m_owner->remove_edge(p.first,p.second);
  274. reset(id);
  275. m_map.erase(it);
  276. }
  277. }
  278. inline std::string InfoMessage()
  279. {
  280. std::stringstream ss;
  281. ss<<"S: "<<m_map.size();
  282. for(auto it : m_map)
  283. {
  284. //ss<< " hash info["<<it.first <<"]first cardid:["<<it.second->m_cardid.first<<"]second cardid:["<<it.second->m_cardid.second<<"]Info:{total_size:"<<it.second->size()<<","<<it.second->getInfo()<<"}";
  285. ss<< "["<<(uint32_t)(it.second->m_cardid.first)<<"]["<<(uint32_t)(it.second->m_cardid.second)<<"]{s:"<<it.second->size()<<","<<it.second->getInfo()<<"}";
  286. }
  287. return std::move(ss.str());
  288. }
  289. virtual std::map<uint64_t,std::string> selectcard(std::vector<uint64_t> &v,uint64_t cid) = 0;
  290. virtual std::shared_ptr<TcardInterface> make_shared_(const uint64_t cid1,const uint64_t cid2,const uint64_t ctime,uint64_t key) = 0;
  291. virtual void backup (uint64_t cid1,uint64_t cid2){}
  292. virtual void reset (uint64_t cid){}
  293. virtual std::tuple<bool,std::string> checkLast(std::shared_ptr<TcardInterface> &ti)=0;
  294. void handle_event(std::map<uint64_t,std::string> m);
  295. virtual void create_event(uint32_t key,double lv,double cv,const std::string &desc)=0;
  296. virtual void write_data(std::vector<std::string> &v,const std::string & card1,const std::string &card2){}
  297. virtual ~CardFactory(){}
  298. public:
  299. std::unordered_map<uint64_t,std::shared_ptr<TcardInterface>> m_map;
  300. cardMgr * m_owner;
  301. };
  302. struct CloserCardFactory : CardFactory
  303. {
  304. CloserCardFactory(cardMgr*);
  305. int m_closer_slot=0;
  306. std::map<uint64_t,int> m_count;
  307. bool getAccess(uint64_t cid1,uint64_t cid2)
  308. {
  309. uint64_t cid = MyHash{}(cid1,cid2);
  310. if(m_count[cid] < 600)
  311. {
  312. m_count[cid]++;
  313. return false;
  314. }
  315. else
  316. return true;
  317. }
  318. void reset(uint64_t id)
  319. {
  320. auto it = m_count.find(id);
  321. if(it != m_count.end())
  322. m_count.erase(it);
  323. }
  324. //查找临近卡数据信息,并进行筛选
  325. virtual std::map<uint64_t,std::string> selectcard(std::vector<uint64_t> &v,uint64_t cid)
  326. {
  327. std::map<uint64_t,std::string> vec;
  328. if(v.empty()) return vec;
  329. std::vector<uint64_t> rc(v.begin(),v.end());
  330. auto opcard=card_list::instance()->get(cid);
  331. //去除俩卡600次临近不计入数据,防止路过
  332. //卡号相同,类型不同得过滤掉
  333. //时间差超过3s得过滤掉
  334. rc.erase(std::remove_if(rc.begin(),rc.end(),[&](uint64_t cardid){
  335. if(!getAccess(cid,cardid))
  336. return true;
  337. if(cardid == cid)
  338. return true;
  339. auto npcard = card_list::instance()->get(cardid);
  340. if(npcard->type_()!=opcard->type_())
  341. return true;
  342. uint64_t ct1 = npcard->time_();
  343. uint64_t ct2 = opcard->time_();
  344. uint64_t ct3 = ct1>ct2?ct1-ct2:ct2-ct1;
  345. return ct3 > TIME_LIM_SEC;
  346. }),rc.end());
  347. vec = handlecard(rc,cid);
  348. return std::move(vec);
  349. }
  350. virtual void write_data(std::vector<std::string> &v,const std::string & card1,const std::string &card2);
  351. virtual void backup (uint64_t cid1,uint64_t cid2)
  352. {
  353. m_owner->addVertex(cid1,cid2);
  354. }
  355. virtual std::shared_ptr<TcardInterface> make_shared_(const uint64_t cid1,const uint64_t cid2,const uint64_t ctime,uint64_t key)
  356. {
  357. log_info("handlecard_closer:%d,%d",cid1,cid2);
  358. return std::make_shared<TcardInterface>(std::make_pair(cid1,cid2),ctime,key,m_closer_slot);
  359. }
  360. std::tuple<bool,std::string> checkLast(std::shared_ptr<TcardInterface> &ti)
  361. {
  362. return ti->checkLast();
  363. }
  364. virtual void create_event(uint32_t key,double lv,double cv,const std::string &desc);
  365. };
  366. struct RemoteCardFactory : CardFactory
  367. {
  368. RemoteCardFactory(cardMgr*);
  369. int m_remote_slot=0;
  370. virtual std::map<uint64_t,std::string> selectcard(std::vector<uint64_t> &ov,uint64_t cid)
  371. {
  372. //从boost图中找到与之有过临近记录得卡
  373. auto vcard=m_owner->getcard(cid);
  374. log_info("selectcard_remote.card_id:%d,%d...",cid,vcard.size());
  375. std::sort(vcard.begin(),vcard.end());
  376. std::sort(ov.begin(),ov.end());
  377. std::vector<uint64_t> v(ov.size()+vcard.size());
  378. //找到之前临近卡,现在非临近得卡
  379. auto it = std::set_difference(vcard.begin(),vcard.end(),ov.begin(),ov.end(),v.begin());
  380. v.resize(it-v.begin());
  381. // auto opcard = card_list::instance()->get(cid);
  382. //过滤掉超过3s得卡信息
  383. // v.erase(std::remove_if(v.begin(),v.end(),[&](uint64_t cardid){
  384. // auto npcard = card_list::instance()->get(cardid);
  385. // uint64_t ct1 = opcard->time_();
  386. // uint64_t ct2 = npcard->time_();
  387. // uint64_t ct3 = ct1>ct2?ct1-ct2:ct2-ct1;
  388. // return ct3 > TIME_LIM_SEC;
  389. // }),v.end());
  390. //执行卡
  391. log_info("selectcard_remote.card_id:%d,%d...",cid,v.size());
  392. auto vec = handlecard(v,cid);
  393. return std::move(vec);
  394. }
  395. virtual std::shared_ptr<TcardInterface> make_shared_(const uint64_t cid1,const uint64_t cid2,const uint64_t ctime,uint64_t key)
  396. {
  397. log_info("handlecard_remote:%d,%d",cid1,cid2);
  398. return std::make_shared<TcardInterface>(std::make_pair(cid1,cid2),ctime,key,m_remote_slot);
  399. }
  400. std::tuple<bool,std::string> checkLast(std::shared_ptr<TcardInterface> &ti)
  401. {
  402. return std::make_tuple(false,std::string(""));
  403. }
  404. virtual void create_event(uint32_t key,double lv,double cv,const std::string &desc);
  405. };
  406. #endif