-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_model.h
More file actions
755 lines (635 loc) · 14.8 KB
/
data_model.h
File metadata and controls
755 lines (635 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
#ifndef _DATA_MODEL_H_
#define _DATA_MODEL_H_
/* OS headers */
#include <sys/time.h>
#include <time.h>
#include <map>
#include <list>
#include <set>
#include "list.h"
#include <algorithm>
#include <string>
/* TFC headers */
#include "tfc_object.h"
#include "tfc_base_fast_timer.h"
#include "tfc_base_http.h"
#include "tfc_debug_log.h"
/* module headers */
#include "debug.h"
#include "common_api.h"
using namespace std;
namespace statsvr
{
struct Session
{
Session();
~Session();
Session(const string& strSession);
void toJson(Json::Value &value) const;
string toString() const;
bool has_refreshed() const;
string sessionID;
string userID;
string cpIP; //用户的cpIP
unsigned short cpPort; //用户的cpPort
string serviceID; //坐席ID
long long atime; //活跃时间,即有消息时间到达就更新
long long btime; //建立连接的时间
int notified; //欢迎语通知
//bool toIM; //标记数据流向,默认为0
//string whereFrom; //标记使用http还是websocket
//string channel;
//unsigned userCount;
};
struct UserInfo
{
UserInfo();
~UserInfo();
UserInfo(const string& strUserInfo);
void toJson(Json::Value &value) const;
string toString() const;
string userID;
string cpIP;
unsigned short cpPort;
string tag;
string status;
long long atime;
long long qtime;
string sessionID;
string lastServiceID;
string priority;
//string userInfo;
unsigned queuePriority;
string channel;
string extends;
};
struct ServiceInfo
{
ServiceInfo();
~ServiceInfo();
ServiceInfo(const string& strServiceInfo);
void toJson(Json::Value &value) const;
string toString() const;
int delete_user(const string &raw_userID);
int find_user(const string &raw_userID);
int add_user(const string &raw_userID);
unsigned user_count() const;
bool is_available(int maxConvNum) const;
bool is_busy(int maxConvNum) const;
string serviceID;
string status;
long long atime;
string cpIP;
unsigned cpPort;
set<string> tags;
set<string> userList;
string serviceName;
string serviceAvatar;
//string whereFrom;
};
//仅存储serviceID
struct ServiceHeap
{
ServiceHeap();
~ServiceHeap();
ServiceHeap(const string& strServiceHeap);
string toString() const;
unsigned size();
int find_service(const string &app_serviceID);
int add_service(const string &app_serviceID);
int delete_service(const string &app_serviceID);
//按tag对service进行分组,方便转人工时根据user的tag找到service
set<string> _servlist;
};
//带超时时间的用户信息
struct UserTimer
{
string userID;
long long expire_time;
};
//某个tag下的用户排队队列
struct UserQueue
{
list<UserTimer> _user_list;
UserQueue()
{
_user_list.clear();
}
~UserQueue()
{
_user_list.clear();
}
string toString()
{
Json::Value obj;
Json::Value queueList;
Json::Value arr;
UserTimer ut;
queueList.resize(0);
for (list<UserTimer>::iterator it = _user_list.begin(); it != _user_list.end(); ++it)
{
ut = (*it);
obj["userID"] = ut.userID;
obj["expire_time"] = ut.expire_time;
queueList.append(obj);
}
arr["queueList"] = queueList;
Json::FastWriter writer;
return writer.write(arr);
}
int set(string userID, long long expire_time/* seconds */)
{
UserTimer ut;
ut.userID = userID;
ut.expire_time = expire_time;
_user_list.push_back(ut);
LogTrace("Success to add new userID[%s] on queue.", userID.c_str());
return 0;
}
// delete first user
int pop()
{
if (_user_list.size() > 0)
{
_user_list.pop_front();
return 0;
}
LogError("Failed to pop user from queue, size <= 0!");
return -1;
}
// get first user on queue
int get_first(string& userID, long long &expire_time)
{
list<UserTimer>::iterator it = _user_list.begin();
if (_user_list.size() > 0)
{
userID = it->userID;
expire_time = it->expire_time;
return 0;
}
LogError("Failed to get first user from queue, size <= 0!");
return -1;
}
// get last user on queue
int get_last(string& userID, long long &expire_time)
{
list<UserTimer>::reverse_iterator it = _user_list.rbegin();
if (_user_list.size() > 0)
{
userID = it->userID;
expire_time = it->expire_time;
return 0;
}
LogError("Failed to get last user from queue, size <= 0!");
return -1;
}
// return number before user
int find(string userID)
{
list<UserTimer>::iterator it;
int pos = 0;
for (it = _user_list.begin(); it != _user_list.end(); it++)
{
if (it->userID == userID)
{
return pos;
}
else
{
pos++;
}
}
return -1;
}
int delete_user(string userID)
{
list<UserTimer>::iterator it;
for (it = _user_list.begin(); it != _user_list.end(); it++)
{
if (it->userID == userID)
{
it = _user_list.erase(it);
return 0;
}
}
LogError("Failed to delete user[%s] from queue!", userID.c_str());
return -1;
}
//just get number, do not delete
int check_expire()
{
list<UserTimer>::iterator it;
int expireNum = 0;
long long nowTime = (long long)time(NULL);
for (it = _user_list.begin(); it != _user_list.end(); it++)
{
if (nowTime >= it->expire_time)
{
LogTrace("[nowTime: %ld] Find queue user timeout -- userID: %s, expire_time: %ld",
nowTime, it->userID.c_str(), it->expire_time);
expireNum++;
}
}
return expireNum;
}
unsigned size()
{
return _user_list.size();
}
};
class TagUserQueue
{
public:
TagUserQueue()
{
_tag_queue.clear();
}
~TagUserQueue()
{
_tag_queue.clear();
}
int add_tag(string tag)
{
map<string, UserQueue*>::iterator it;
it = _tag_queue.find(tag);
if (it == _tag_queue.end())
{
_tag_queue[tag] = new UserQueue();
LogTrace("Success to add UserQueue for tag: %s", tag.c_str());
}
return 0;
}
int del_tag(string tag)
{
map<string, UserQueue*>::iterator it;
it = _tag_queue.find(tag);
if (it != _tag_queue.end())
{
delete it->second;
_tag_queue.erase(tag);
return 0;
}
return -1;
}
int get_tag(string tag, UserQueue* &uq)
{
map<string, UserQueue*>::iterator it;
it = _tag_queue.find(tag);
if(it != _tag_queue.end())
{
uq = it->second;
return 0;
}
return -1;
}
int add_user(string tag, string userID, long long expire_time/* seconds */)
{
map<string, UserQueue*>::iterator it;
UserQueue *uq = NULL;
it = _tag_queue.find(tag);
if (it == _tag_queue.end())
{
return -1;
}
if (NULL == (uq = it->second))
{
return -1;
}
return uq->set(userID, expire_time);
}
int find_user(string userID)
{
map<string, UserQueue*>::iterator it;
UserQueue *uq = NULL;
int pos = 0;
for (it = _tag_queue.begin(); it != _tag_queue.end(); it++)
{
uq = it->second;
pos = uq->find(userID);
if (pos >= 0)
{
return pos;
}
}
return -1;
}
int del_user(string tag, string userID)
{
map<string, UserQueue*>::iterator it;
UserQueue *uq = NULL;
it = _tag_queue.find(tag);
if (it == _tag_queue.end())
{
return -1;
}
if (NULL == (uq = it->second))
{
return -1;
}
return uq->delete_user(userID);
}
/*
tags: service所属的分组
direct: 从队头还是队尾查找,0代表队头,1代表队尾
num: 第几个 (TODO:暂时忽略该参数)
*/
int get_target_user(string &userID, const string& serviceID, std::set<string> tags, unsigned num, int direct)
{
//UserInfo user;
UserQueue *uq = NULL;
//bool tag_match = false;
bool found = false;
string temp_userID;
long long temp_expire;
long long expire;
LogTrace("====>direct = %d", direct);
map<string, UserQueue*>::iterator it;
for (it = _tag_queue.begin(); it != _tag_queue.end(); it++)
{
//坐席不能拉取不在它服务范围内的用户
if (tags.end() == tags.find(it->first))
{
continue;
}
uq = it->second;
if (direct == 0) //from queue head
{
if (0 == uq->get_first(temp_userID, temp_expire))
{
if (false == found || temp_expire < expire)
{
expire = temp_expire;
userID = temp_userID;
found = true;
}
}
}
else //from queue tail
{
if (0 == uq->get_last(temp_userID, temp_expire))
{
if (false == found || temp_expire > expire)
{
expire = temp_expire;
userID = temp_userID;
found = true;
}
}
}
}
if (true == found)
{
LogTrace("====> pull out user[%s], expire_time[%ld]", userID.c_str(), expire);
return 0;
}
else
{
LogError("====> not matched user!");
return -1;
}
}
unsigned total_queue_count()
{
map<string, UserQueue*>::iterator it;
UserQueue *uq = NULL;
unsigned total = 0;
for (it = _tag_queue.begin(); it != _tag_queue.end(); it++)
{
uq = it->second;
total += uq->size();
}
return total;
}
unsigned queue_count(string tag)
{
map<string, UserQueue*>::iterator it;
UserQueue *uq = NULL;
it = _tag_queue.find(tag);
if (it == _tag_queue.end())
{
return 0;
}
if (NULL == (uq = it->second))
{
return 0;
}
return uq->size();
}
map<string, UserQueue*> _tag_queue;
};
class SessionTimer
{
public:
string userID;
Session session;
bool isWarn;
long long warn_time;
long long expire_time;
void toJson(Json::Value &value) const
{
session.toJson(value);
value["warn_time"] = warn_time;
value["expire_time"] = expire_time;
}
string toString() const
{
Json::Value value;
toJson(value);
return value.toStyledString();
}
};
class SessionQueue
{
public:
list<SessionTimer> _sess_list;
SessionQueue()
{
_sess_list.clear();
};
~SessionQueue()
{
_sess_list.clear();
};
int set(string userID, Session* sess,
long long gap_warn = 10 * 60, long long gap_expire = 20 * 60 /* seconds */)
{
list<SessionTimer>::iterator it;
for (it = _sess_list.begin(); it != _sess_list.end(); it++)
{
if ((*it).userID == userID)
{
(*it).session = *sess;
(*it).isWarn = 0;
(*it).warn_time = (sess->atime/1000) + gap_warn;
(*it).expire_time = (sess->atime/1000) + gap_expire;
//_sess_list.push_back(st);
LogTrace("Success to set session: %s", sess->toString().c_str());
return 0;
}
}
LogError("Failed to set session: %s!", sess->toString().c_str());
return -1;
}
int insert(string userID, Session* sess,
long long gap_warn = 10 * 60, long long gap_expire = 20 * 60 /* seconds */)
{
SessionTimer st;
list<SessionTimer>::iterator it;
st.userID = userID;
st.session = *sess;
st.isWarn = 0;
st.warn_time = (sess->atime/1000) + gap_warn;
st.expire_time = (sess->atime/1000) + gap_expire;
LogDebug("Success to insert session: %s", sess->toString().c_str());
LogDebug("Session atime: %ld, warn_time: %ld, expire_time: %ld", sess->atime, st.warn_time, st.expire_time);
for (it = _sess_list.begin(); it != _sess_list.end(); it++)
{
if (st.expire_time < (*it).expire_time)
{
_sess_list.insert(it, st);
return 0;
}
}
_sess_list.push_back(st);
return 0;
}
int pop()
{
if (_sess_list.size() > 0)
{
_sess_list.pop_front();
return 0;
}
LogError("Failed to pop from SessionQueue, its size <= 0!");
return -1;
}
int get_first_timer(SessionTimer& sessTimer)
{
list<SessionTimer>::iterator it;
long long nowTime = (long long)time(NULL);
for (it = _sess_list.begin(); it != _sess_list.end(); it++)
{
if (nowTime >= it->expire_time)
{
sessTimer = (*it);
return 0;
}
}
LogError("Failed to get an expired session from SessionQueue, its size <= 0!");
return -1;
}
int get_first(Session& sess)
{
list<SessionTimer>::iterator it;
long long nowTime = (long long)time(NULL);
for (it = _sess_list.begin(); it != _sess_list.end(); it++)
{
if (nowTime >= it->expire_time)
{
sess = it->session;
return 0;
}
}
LogError("Failed to get an expired session from SessionQueue, its size <= 0!");
return -1;
}
int get(string userID, Session& sess)
{
list<SessionTimer>::iterator it;
for (it = _sess_list.begin(); it != _sess_list.end(); it++)
{
if (it->userID == userID)
{
sess = it->session;
return 0;
}
}
return -1;
}
int delete_session(string userID)
{
list<SessionTimer>::iterator it;
for (it = _sess_list.begin(); it != _sess_list.end(); it++)
{
if (it->userID == userID)
{
it = _sess_list.erase(it);
LogDebug("Success to delete session of user[%s]", userID.c_str());
return 0;
}
}
return -1;
}
int check_warn(Session& sess)
{
list<SessionTimer>::iterator it;
long long nowTime = (long long)time(NULL);
for (it = _sess_list.begin(); it != _sess_list.end(); it++)
{
//it->isWarn==1表示之前提醒过,不需要再提醒
if (nowTime >= it->warn_time && it->isWarn == 0)
{
LogDebug("[nowTime: %ld] Find session timewarn, session:%s", nowTime, it->toString().c_str());
sess = it->session;
it->isWarn = 1;
return 0;
}
/*else if (nowTime < it->warn_time)
{
return -1;
}*/
}
return -1;
}
//返回超时断开的数量
int check_expire()
{
list<SessionTimer>::iterator it;
int expireNum = 0;
long long nowTime = (long long)time(NULL);
for (it = _sess_list.begin(); it != _sess_list.end(); it++)
{
if (nowTime >= it->expire_time)
{
//LogDebug("[nowTime: %ld] Find session timeout, session:%s", nowTime, it->toString().c_str());
expireNum++;
}
}
return expireNum;
}
void check_expire_yibot(long long gap_expire, vector<string> &app_userID_list)
{
long long nowTime = (long long)time(NULL);
app_userID_list.clear();
list<SessionTimer>::iterator it;
for (it = _sess_list.begin(); it != _sess_list.end(); ++it)
{
//only check yibot session
if ("" == it->session.serviceID
&& nowTime >= ((it->session.atime/1000) + gap_expire))
{
app_userID_list.push_back(it->userID);
}
}
}
unsigned size()
{
return _sess_list.size();
}
unsigned get_usernum_in_service()
{
list<SessionTimer>::iterator it;
unsigned userNum = 0;
for (it = _sess_list.begin(); it != _sess_list.end(); it++)
{
SessionTimer st = *it;
if ("" != st.session.serviceID)
{
++userNum;
}
}
return userNum;
}
};
}
#endif //_DATA_MODEL_H_