leadguru-jobs 0.404.0__py3-none-any.whl → 0.405.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. {leadguru_jobs-0.404.0.dist-info → leadguru_jobs-0.405.0.dist-info}/METADATA +5 -10
  2. leadguru_jobs-0.405.0.dist-info/RECORD +26 -0
  3. lgt_jobs/__init__.py +4 -4
  4. lgt_jobs/jobs/analytics.py +1 -1
  5. lgt_jobs/jobs/archive_leads.py +2 -2
  6. lgt_jobs/jobs/bot_stats_update.py +11 -10
  7. lgt_jobs/jobs/chat_history.py +6 -6
  8. lgt_jobs/jobs/inbox_leads.py +6 -5
  9. lgt_jobs/jobs/mass_message.py +2 -2
  10. lgt_jobs/jobs/send_code.py +1 -1
  11. lgt_jobs/jobs/send_slack_message.py +5 -5
  12. lgt_jobs/jobs/update_slack_profile.py +14 -12
  13. lgt_jobs/jobs/user_balance_update.py +5 -5
  14. lgt_jobs/jobs/workspace_connect.py +5 -7
  15. lgt_jobs/main.py +11 -9
  16. lgt_jobs/runner.py +9 -6
  17. lgt_jobs/smtp.py +1 -1
  18. leadguru_jobs-0.404.0.dist-info/RECORD +0 -49
  19. lgt_jobs/lgt_common/__init__.py +0 -0
  20. lgt_jobs/lgt_common/discord_client/__init__.py +0 -0
  21. lgt_jobs/lgt_common/discord_client/discord_client.py +0 -58
  22. lgt_jobs/lgt_common/discord_client/methods.py +0 -15
  23. lgt_jobs/lgt_common/enums/__init__.py +0 -0
  24. lgt_jobs/lgt_common/enums/slack_errors.py +0 -6
  25. lgt_jobs/lgt_common/helpers.py +0 -18
  26. lgt_jobs/lgt_common/lgt_logging.py +0 -15
  27. lgt_jobs/lgt_common/pubsub/__init__.py +0 -0
  28. lgt_jobs/lgt_common/pubsub/command.py +0 -14
  29. lgt_jobs/lgt_common/pubsub/messages.py +0 -37
  30. lgt_jobs/lgt_common/pubsub/pubsubfactory.py +0 -51
  31. lgt_jobs/lgt_common/slack_client/__init__.py +0 -0
  32. lgt_jobs/lgt_common/slack_client/methods.py +0 -46
  33. lgt_jobs/lgt_common/slack_client/slack_client.py +0 -392
  34. lgt_jobs/lgt_common/slack_client/web_client.py +0 -164
  35. lgt_jobs/lgt_data/__init__.py +0 -0
  36. lgt_jobs/lgt_data/analytics.py +0 -723
  37. lgt_jobs/lgt_data/engine.py +0 -223
  38. lgt_jobs/lgt_data/enums.py +0 -68
  39. lgt_jobs/lgt_data/helpers.py +0 -2
  40. lgt_jobs/lgt_data/model.py +0 -964
  41. lgt_jobs/lgt_data/mongo_repository.py +0 -980
  42. {leadguru_jobs-0.404.0.dist-info → leadguru_jobs-0.405.0.dist-info}/WHEEL +0 -0
  43. {leadguru_jobs-0.404.0.dist-info → leadguru_jobs-0.405.0.dist-info}/top_level.txt +0 -0
@@ -1,723 +0,0 @@
1
- import os
2
- from typing import Optional, List, Dict, Union
3
- from collections import OrderedDict
4
- import datetime
5
- from datetime import timedelta
6
- from bson import ObjectId
7
- from dateutil import tz
8
- from lgt_jobs.lgt_data.mongo_repository import to_object_id
9
- from pymongo import MongoClient
10
-
11
- client = MongoClient(os.environ.get('MONGO_CONNECTION_STRING', 'mongodb://127.0.0.1:27017/'))
12
- db = client.lgt_analytics
13
-
14
-
15
- def _build_date_aggregated_analytics_pipeline(source_id=None,
16
- email=None,
17
- started_at: datetime.datetime = None,
18
- ended_at: datetime.datetime = None,
19
- bots_ids: [str] = None):
20
- pipeline = [
21
- {
22
- "$sort": {"created_at": 1}
23
- },
24
- {
25
- "$project": {
26
- "created_at": {"$dateToString": {"format": "%Y-%m-%d", "date": "$created_at"}},
27
- "name": "$name"
28
- }
29
- },
30
- {
31
- "$group":
32
- {
33
- "_id": "$created_at",
34
- "count": {"$sum": 1}
35
- }
36
- },
37
- {
38
- "$project": {
39
- "_id": {"$dateFromString": {"format": "%Y-%m-%d", "dateString": "$_id"}},
40
- "count": "$count"
41
- }
42
- },
43
- {
44
- "$sort": {"_id": 1}
45
- },
46
- {"$limit": 1000}
47
- ]
48
-
49
- if source_id:
50
- pipeline.insert(0, {"$match": {"source.source_id": source_id}})
51
-
52
- if email:
53
- pipeline.insert(0, {"$match": {"name": email}})
54
-
55
- if started_at:
56
- beginning_of_the_day = datetime.datetime(started_at.year, started_at.month, started_at.day, 0, 0, 0, 0)
57
- pipeline.insert(0, {"$match": {"created_at": {"$gte": beginning_of_the_day}}})
58
-
59
- if ended_at:
60
- end_of_the_day = datetime.datetime(ended_at.year, ended_at.month, ended_at.day, 23, 59, 59, 999)
61
- pipeline.insert(0, {"$match": {"created_at": {"$lte": end_of_the_day}}})
62
-
63
- if bots_ids is not None:
64
- pipeline.insert(0, {"$match": {'extra_ids': {'$in': bots_ids}}})
65
-
66
- return pipeline
67
-
68
-
69
- def _create_result_dic(started_at: datetime.datetime = None, ended_at: datetime.datetime = None):
70
- analytics_dict = OrderedDict()
71
-
72
- if started_at and ended_at:
73
- days_range = range(0, (ended_at - started_at).days + 1)
74
- for day in days_range:
75
- cur_date = started_at + datetime.timedelta(days=day)
76
- str_date = f'{cur_date.year}-{cur_date.month:02d}-{cur_date.day:02d}'
77
- analytics_dict[str_date] = 0
78
-
79
- return analytics_dict
80
-
81
-
82
- def _prepare_date_analytics_doc(doc, ordered_result_dict: Dict[str, int]):
83
- for item in doc:
84
- str_date = f'{item["_id"].year}-{item["_id"].month:02d}-{item["_id"].day:02d}'
85
- ordered_result_dict[str_date] = item["count"]
86
- return ordered_result_dict
87
-
88
-
89
- def get_aggregated_user_leads(user_id: Union[ObjectId, str],
90
- from_date: datetime.datetime,
91
- to_date: datetime.datetime = None):
92
- pipeline = [
93
- {
94
- '$match': {
95
- 'created_at': {'$gte': from_date}
96
- }
97
- }, {
98
- '$group': {
99
- '_id': {
100
- '$dateFromParts': {
101
- 'day': {
102
- '$dayOfMonth': '$created_at'
103
- },
104
- 'month': {
105
- '$month': '$created_at'
106
- },
107
- 'year': {
108
- '$year': '$created_at'
109
- }
110
- }
111
- },
112
- 'count': {
113
- '$sum': 1
114
- },
115
- 'leads': {
116
- '$push': {
117
- 'id': '$id',
118
- 'created_at': '$created_at'
119
- }
120
- }
121
- }
122
- }, {
123
- '$sort': {
124
- '_id': 1
125
- }
126
- }
127
- ]
128
-
129
- if user_id:
130
- pipeline[0]["$match"]["user_id"] = to_object_id(user_id)
131
-
132
- if to_date:
133
- end = datetime.datetime(to_date.year, to_date.month, to_date.day, 23, 59, 59, tzinfo=tz.tzutc())
134
- pipeline[0]["$match"]["created_at"]['$lte'] = end
135
-
136
- user_leads_data = list(client.lgt_admin.user_leads.aggregate(pipeline))
137
- result = _create_result_dic(from_date, to_date)
138
-
139
- for item in user_leads_data:
140
- str_date = f'{item["_id"].year}-{item["_id"].month:02d}-{item["_id"].day:02d}'
141
- result[str_date] = item["count"]
142
- return result
143
-
144
-
145
- def get_register_users_analytics(from_date: datetime = None, to_date: datetime = None):
146
- pipeline = [
147
- {
148
- '$addFields': {
149
- 'created_at_formatted': {
150
- '$dateToString': {
151
- 'format': '%Y-%m-%d',
152
- 'date': '$created_at'
153
- }
154
- }
155
- }
156
- }, {
157
- '$group': {
158
- '_id': '$created_at_formatted',
159
- 'count': {
160
- '$sum': 1
161
- }
162
- }
163
- }
164
- ]
165
- if from_date:
166
- beginning_of_the_day = datetime.datetime(from_date.year, from_date.month, from_date.day, 0, 0, 0, 0)
167
- pipeline.insert(0, {"$match": {"created_at": {"$gte": beginning_of_the_day}}})
168
-
169
- if to_date:
170
- end_of_the_day = datetime.datetime(to_date.year, to_date.month, to_date.day, 23, 59, 59, 999)
171
- pipeline.insert(0, {"$match": {"created_at": {"$lte": end_of_the_day}}})
172
-
173
- users = list(client.lgt_admin.users.aggregate(pipeline))
174
- users_dic = OrderedDict()
175
-
176
- for item in users:
177
- users_dic[item["_id"]] = item["count"]
178
-
179
- return users_dic
180
-
181
-
182
- def get_global_saved_leads_analytics(from_date: datetime = None, to_date: datetime = None):
183
- pipeline = [
184
- {
185
- '$addFields': {
186
- 'created_at_formatted': {
187
- '$dateToString': {
188
- 'format': '%Y-%m-%d',
189
- 'date': '$created_at'
190
- }
191
- }
192
- }
193
- }, {
194
- '$group': {
195
- '_id': '$created_at_formatted',
196
- 'count': {
197
- '$sum': 1
198
- }
199
- }
200
- }
201
- ]
202
-
203
- if from_date:
204
- beginning_of_the_day = datetime.datetime(from_date.year, from_date.month, from_date.day, 0, 0, 0, 0)
205
- pipeline.insert(0, {"$match": {"created_at": {"$gte": beginning_of_the_day}}})
206
-
207
- if to_date:
208
- end_of_the_day = datetime.datetime(to_date.year, to_date.month, to_date.day, 23, 59, 59, 999)
209
- pipeline.insert(0, {"$match": {"created_at": {"$lte": end_of_the_day}}})
210
-
211
- saved_leads = list(client.lgt_admin.user_leads.aggregate(pipeline))
212
- saved_leads_dic = OrderedDict()
213
- for item in saved_leads:
214
- saved_leads_dic[str(item["_id"])] = item["count"]
215
- return saved_leads_dic
216
-
217
-
218
- def get_global_uniq_leads_analytics(from_date: datetime = None, to_date: datetime = None):
219
- pipeline = [
220
- {
221
- '$addFields': {
222
- 'created_at_formatted': {
223
- '$dateToString': {
224
- 'format': '%Y-%m-%d',
225
- 'date': '$created_at'
226
- }
227
- }
228
- }
229
- }, {
230
- '$group': {
231
- '_id': {
232
- 'created_at': '$created_at_formatted',
233
- 'message': '$message.message_id'
234
- },
235
- 'uniq_leads': {
236
- '$addToSet': '$message.message_id'
237
- }
238
- }
239
- }, {
240
- '$group': {
241
- '_id': '$_id.created_at',
242
- 'uniq_leads_count': {
243
- '$sum': {
244
- '$size': '$uniq_leads'
245
- }
246
- }
247
- }
248
- }
249
- ]
250
-
251
- if from_date:
252
- beginning_of_the_day = datetime.datetime(from_date.year, from_date.month, from_date.day, 0, 0, 0, 0)
253
- pipeline.insert(0, {"$match": {"created_at": {"$gte": beginning_of_the_day}}})
254
-
255
- if to_date:
256
- end_of_the_day = datetime.datetime(to_date.year, to_date.month, to_date.day, 23, 59, 59, 999)
257
- pipeline.insert(0, {"$match": {"created_at": {"$lte": end_of_the_day}}})
258
-
259
- uniq_leads = list(client.lgt_admin.user_leads.aggregate(pipeline))
260
- uniq_leads_dic = OrderedDict()
261
-
262
- for item in uniq_leads:
263
- uniq_leads_dic[item["_id"]] = item["uniq_leads_count"]
264
- return uniq_leads_dic
265
-
266
-
267
- def get_bots_global_analytics(from_date: datetime = None, to_date: datetime = None):
268
- pipeline = [
269
- {
270
- '$addFields': {
271
- 'created_at_formatted': {
272
- '$dateToString': {
273
- 'format': '%Y-%m-%d',
274
- 'date': '$created_at'
275
- }
276
- }
277
- }
278
- },
279
- {
280
- '$group': {
281
- '_id': {
282
- 'source_id': '$source.source_id',
283
- 'created_at': '$created_at_formatted'
284
- },
285
- 'count': {
286
- '$sum': 1
287
- }
288
- }
289
- }, {
290
- '$group': {
291
- '_id': '$_id.created_at',
292
- 'bots': {
293
- '$sum': '$count'
294
- },
295
- 'sources': {
296
- '$sum': {
297
- '$cond': [
298
- {
299
- '$eq': [
300
- '$count', 1
301
- ]
302
- }, 1, 0
303
- ]
304
- }
305
- }
306
- }
307
- }
308
- ]
309
- if from_date:
310
- beginning_of_the_day = datetime.datetime(from_date.year, from_date.month, from_date.day, 0, 0, 0, 0)
311
- pipeline.insert(0, {"$match": {"created_at": {"$gte": beginning_of_the_day}}})
312
-
313
- if to_date:
314
- end_of_the_day = datetime.datetime(to_date.year, to_date.month, to_date.day, 23, 59, 59, 999)
315
- pipeline.insert(0, {"$match": {"created_at": {"$lte": end_of_the_day}}})
316
-
317
- bots = list(client.lgt_admin.dedicated_bots.aggregate(pipeline))
318
- result = {}
319
- for bot in bots:
320
- result.update({bot['_id']: {'bots': bot['bots'], 'sources': bot['sources']}})
321
- return result
322
-
323
-
324
- def get_date_aggregated_analytics(source_id=None, started_at: datetime.datetime = None,
325
- ended_at: datetime.datetime = None, bots_ids: [str] = None, configs: [str] = None):
326
- pipeline: list = _build_date_aggregated_analytics_pipeline(source_id=source_id, started_at=started_at,
327
- ended_at=ended_at, bots_ids=bots_ids)
328
-
329
- received_messages = list(db[f'received_messages'].aggregate(pipeline))
330
-
331
- if configs is not None:
332
- pipeline.insert(0, {"$match": {"attributes": {'$in': configs}}})
333
-
334
- filtered_messages = list(db[f'filtered_messages'].aggregate(pipeline))
335
-
336
- received_messages_dic = _create_result_dic(started_at, ended_at)
337
- filtered_messages_dic = _create_result_dic(started_at, ended_at)
338
-
339
- return (_prepare_date_analytics_doc(received_messages, received_messages_dic),
340
- _prepare_date_analytics_doc(filtered_messages, filtered_messages_dic))
341
-
342
-
343
- def get_leads_aggregated_analytics(from_date: datetime.datetime = None, to_date: datetime.datetime = None):
344
- pipeline = [
345
- {
346
- '$unwind': {
347
- 'path': '$message.configs',
348
- 'preserveNullAndEmptyArrays': False
349
- }
350
- },
351
- {
352
- '$group': {
353
- '_id': '$message.configs.id',
354
- 'count': {
355
- '$sum': 1
356
- }
357
- }
358
- }
359
- ]
360
-
361
- if from_date:
362
- beginning_of_the_day = datetime.datetime(from_date.year, from_date.month, from_date.day, 0, 0, 0, 0)
363
- pipeline.insert(0, {"$match": {"created_at": {"$gte": beginning_of_the_day}}})
364
-
365
- if to_date:
366
- end_of_the_day = datetime.datetime(to_date.year, to_date.month, to_date.day, 23, 59, 59, 999)
367
- pipeline.insert(0, {"$match": {"created_at": {"$lte": end_of_the_day}}})
368
-
369
- filtered_messages = list(client.lgt_admin.general_leads.aggregate(pipeline))
370
- filtered_messages_dic = OrderedDict()
371
-
372
- for item in filtered_messages:
373
- filtered_messages_dic[str(item["_id"])] = item["count"]
374
-
375
- return filtered_messages_dic
376
-
377
-
378
- def get_contacts_aggregated_analytics(from_date: datetime.datetime = None, to_date: datetime.datetime = None):
379
- pipeline = [
380
- {
381
- '$group': {
382
- '_id': {
383
- 'user_id': '$user_id',
384
- 'sender_id': '$message.sender_id'
385
- },
386
- 'count': {
387
- '$sum': 1
388
- }
389
- }
390
- }, {
391
- '$project': {
392
- 'user_id': '$_id.user_id',
393
- 'sender_id': '$_id.sender_id',
394
- 'count': 1,
395
- '_id': 0
396
- }
397
- }, {
398
- '$group': {
399
- '_id': '$user_id',
400
- 'contacts_count': {
401
- '$sum': 1
402
- }
403
- }
404
- }
405
- ]
406
-
407
- if from_date:
408
- beginning_of_the_day = datetime.datetime(from_date.year, from_date.month, from_date.day, 0, 0, 0, 0)
409
- pipeline.insert(0, {"$match": {"created_at": {"$gte": beginning_of_the_day}}})
410
-
411
- if to_date:
412
- end_of_the_day = datetime.datetime(to_date.year, to_date.month, to_date.day, 23, 59, 59, 999)
413
- pipeline.insert(0, {"$match": {"created_at": {"$lte": end_of_the_day}}})
414
-
415
- contact_saved = list(client.lgt_admin.user_leads.aggregate(pipeline))
416
- contact_saved_dic = OrderedDict()
417
- for item in contact_saved:
418
- contact_saved_dic[str(item["_id"])] = item["contacts_count"]
419
-
420
- return contact_saved_dic
421
-
422
-
423
- def get_total_active_bots_global_analytics(from_date: datetime.datetime = None, to_date: datetime.datetime = None):
424
- active_bot_dates = []
425
- while from_date <= to_date:
426
- active_bot_dates.append(from_date)
427
- from_date += timedelta(days=1)
428
- pipeline = [
429
- {
430
- '$match': {
431
- 'created_at': {
432
- '$lte': to_date
433
- },
434
- 'deleted': False
435
- }
436
- }, {
437
- '$addFields': {
438
- 'created_at_formatted': {
439
- '$dateToString': {
440
- 'format': '%Y-%m-%d',
441
- 'date': '$created_at'
442
- }
443
- }
444
- }
445
- }, {
446
- '$group': {
447
- '_id': {
448
- 'source': '$source.source_id',
449
- 'created_at': '$created_at_formatted'
450
- },
451
- 'count': {
452
- '$sum': 1
453
- }
454
- }
455
- }, {
456
- '$sort': {
457
- '_id': 1
458
- }
459
- }
460
- ]
461
- bots = list(client.lgt_admin.dedicated_bots.aggregate(pipeline))
462
- response = {}
463
- count = 0
464
- name = ""
465
- for date in active_bot_dates:
466
- for bot in bots:
467
- bot_date = datetime.datetime.strptime(bot["_id"]["created_at"], "%Y-%m-%d")
468
- if bot_date <= date:
469
- if bot["_id"]["source"] == name:
470
- continue
471
- else:
472
- count += 1
473
- name = bot["_id"]["source"]
474
- response[str(date.date())] = count
475
- count = 0
476
-
477
- return response
478
-
479
-
480
- def get_bots_aggregated_analytics(from_date: datetime.datetime = None,
481
- to_date: datetime.datetime = None,
482
- bot_ids: Optional[List[str]] = None,
483
- configs: Optional[List[str]] = None):
484
- pipeline = [
485
- {
486
- "$group": {
487
- "_id": "$source.source_id",
488
- "count": {"$sum": 1}
489
- }
490
- },
491
- {"$limit": 1000}
492
- ]
493
-
494
- if from_date:
495
- beginning_of_the_day = datetime.datetime(from_date.year, from_date.month, from_date.day, 0, 0, 0, 0)
496
- pipeline.insert(0, {"$match": {"created_at": {"$gte": beginning_of_the_day}}})
497
-
498
- if to_date:
499
- end_of_the_day = datetime.datetime(to_date.year, to_date.month, to_date.day, 23, 59, 59, 999)
500
- pipeline.insert(0, {"$match": {"created_at": {"$lte": end_of_the_day}}})
501
-
502
- if bot_ids is not None:
503
- pipeline.insert(0, {"$match": {"extra_ids": {"$in": bot_ids}}})
504
-
505
- received_messages = list(db[f'received_messages'].aggregate(pipeline))
506
-
507
- if configs is not None:
508
- pipeline.insert(0, {"$match": {"attributes": {"$in": configs}}})
509
-
510
- filtered_messages = list(db[f'filtered_messages'].aggregate(pipeline))
511
- received_messages_dic = OrderedDict()
512
- filtered_messages_dic = OrderedDict()
513
-
514
- for item in received_messages:
515
- received_messages_dic[item["_id"]] = item["count"]
516
-
517
- for item in filtered_messages:
518
- filtered_messages_dic[item["_id"]] = item["count"]
519
-
520
- return received_messages_dic, filtered_messages_dic
521
-
522
-
523
- def get_channel_aggregated_analytics(from_date: datetime.datetime = None, to_date: datetime.datetime = None,
524
- bot_id: Optional[str | ObjectId] = None):
525
- pipeline = [
526
- {
527
- '$match': {
528
- 'extra_ids': {"$in": [str(bot_id)]}
529
- }
530
- },
531
- {
532
- "$group": {
533
- '_id': {
534
- '$arrayElemAt': [
535
- '$attributes', 0
536
- ]
537
- },
538
- 'count': {"$sum": 1}
539
- }
540
- },
541
- {
542
- '$limit': 1000
543
- }
544
- ]
545
-
546
- if from_date:
547
- beginning_of_the_day = datetime.datetime(from_date.year, from_date.month, from_date.day, 0, 0, 0, 0)
548
- pipeline.insert(0, {"$match": {"created_at": {"$gte": beginning_of_the_day}}})
549
-
550
- if to_date:
551
- end_of_the_day = datetime.datetime(to_date.year, to_date.month, to_date.day, 23, 59, 59, 999)
552
- pipeline.insert(0, {"$match": {"created_at": {"$lte": end_of_the_day}}})
553
-
554
- received_messages = list(db[f'received_messages'].aggregate(pipeline))
555
- filtered_messages = list(db[f'filtered_messages'].aggregate(pipeline))
556
-
557
- received_messages_dic = OrderedDict()
558
- filtered_messages_dic = OrderedDict()
559
-
560
- for item in received_messages:
561
- received_messages_dic[item["_id"]] = item["count"]
562
-
563
- for item in filtered_messages:
564
- filtered_messages_dic[item["_id"]] = item["count"]
565
-
566
- return received_messages_dic, filtered_messages_dic
567
-
568
-
569
- def get_users_aggregated_analytics(event_type: str = 'user-lead-extended',
570
- from_date: datetime.datetime = None,
571
- to_date: datetime.datetime = None,
572
- email: str = None):
573
- pipeline = [
574
- {
575
- "$group": {
576
- "_id": "$name",
577
- "count": {"$sum": 1}
578
- }
579
- },
580
- {"$limit": 1000}
581
- ]
582
-
583
- if email:
584
- pipeline.insert(0, {"$match": {"name": email}})
585
-
586
- if from_date:
587
- beginning_of_the_day = datetime.datetime(from_date.year, from_date.month, from_date.day, 0, 0, 0, 0)
588
- pipeline.insert(0, {"$match": {"created_at": {"$gte": beginning_of_the_day}}})
589
-
590
- if to_date:
591
- end_of_the_day = datetime.datetime(to_date.year, to_date.month, to_date.day, 23, 59, 59, 999)
592
- pipeline.insert(0, {"$match": {"created_at": {"$lte": end_of_the_day}}})
593
-
594
- read_messages = list(db[event_type].aggregate(pipeline))
595
- read_messages_dic = OrderedDict()
596
-
597
- for item in read_messages:
598
- read_messages_dic[item["_id"]] = item["count"]
599
-
600
- return read_messages_dic
601
-
602
-
603
- def get_user_date_aggregated_analytics(email=None, event_type: str = 'user-lead-extended',
604
- started_at: datetime.datetime = None,
605
- ended_at: datetime.datetime = None):
606
- pipeline = _build_date_aggregated_analytics_pipeline(email=email, started_at=started_at, ended_at=ended_at)
607
-
608
- messages = list(db[event_type].aggregate(pipeline))
609
- messages_dic = _create_result_dic(started_at, ended_at)
610
-
611
- for item in messages:
612
- str_date = f'{item["_id"].year}-{item["_id"].month:02d}-{item["_id"].day:02d}'
613
- messages_dic[str_date] = item["count"]
614
-
615
- return messages_dic
616
-
617
-
618
- def get_user_read_count(lead_ids: [str]):
619
- pipeline = [
620
- {
621
- "$group":
622
- {
623
- "_id": "$data",
624
- "count": {"$sum": 1}
625
- }
626
- },
627
- {
628
- "$match": {
629
- "_id": {"$in": lead_ids}
630
- }
631
- }
632
- ]
633
- messages = list(db['user-lead-extended'].aggregate(pipeline))
634
- result = dict()
635
-
636
- for message in messages:
637
- result[message['_id']] = message['count']
638
-
639
- return result
640
-
641
-
642
- def get_events_leads(email, event, from_date, to_date=None):
643
- pipeline = {
644
- 'name': email,
645
- 'created_at': {'$gte': from_date}
646
- }
647
-
648
- if to_date:
649
- end = datetime.datetime(to_date.year, to_date.month, to_date.day, 23, 59, 59, tzinfo=tz.tzutc())
650
- pipeline['created_at']['$lte'] = end
651
-
652
- return list(db[event].find(pipeline))
653
-
654
-
655
- def get_leads_aggregation(email, event, from_date, to_date=None):
656
- pipeline = get_event_pipeline(email, from_date, to_date)
657
- return list(db[event].aggregate(pipeline))
658
-
659
-
660
- def get_event_pipeline(email, from_date, to_date=None):
661
- pipeline = [
662
- {
663
- "$match": {
664
- 'name': email,
665
- 'created_at': {'$gte': from_date}
666
- }
667
- },
668
- {
669
- '$group': {
670
- '_id': {
671
- '$dateFromParts': {
672
- 'day': {
673
- '$dayOfMonth': '$created_at'
674
- },
675
- 'month': {
676
- '$month': '$created_at'
677
- },
678
- 'year': {
679
- '$year': '$created_at'
680
- }
681
- }
682
- },
683
- 'count': {
684
- '$sum': 1
685
- }
686
- }
687
- },
688
- {
689
- "$sort": {"_id": 1}
690
- }
691
- ]
692
-
693
- if to_date:
694
- end = datetime.datetime(to_date.year, to_date.month, to_date.day, 23, 59, 59, tzinfo=tz.tzutc())
695
- pipeline[0]["$match"]["created_at"]['$lte'] = end
696
-
697
- return pipeline
698
-
699
-
700
- def get_total_analytic_followup_up_date(from_date, to_date=None):
701
- beginning_of_the_day = datetime.datetime(from_date.year, from_date.month, from_date.day, 0, 0, 0, 0)
702
- end_of_the_day = datetime.datetime(to_date.year, to_date.month, to_date.day, 23, 59, 59, 999)
703
- pipeline = [
704
- {
705
- "$match": {
706
- "followup_date": {
707
- "$gte": beginning_of_the_day,
708
- "$lte": end_of_the_day
709
- }
710
- }
711
- },
712
- {
713
- "$group": {
714
- "_id": "$_id",
715
- "count": {"$sum": 1}
716
- }
717
- }
718
- ]
719
- followup_analytic = list(client.lgt_admin.user_leads.aggregate(pipeline))
720
-
721
- followup_analytic_dic = {item["_id"]: item["count"] for item in followup_analytic}
722
-
723
- return followup_analytic_dic