from datetime import timedelta, timezone from celery.utils.log import get_task_logger from django.utils import timezone as django_timezone from pingcap_sso.celery import app as celery_app from pingcap_sso.user_behavior_track.models import TrackEvent from pingcap_sso.models import ( LarkMessage, User, CompanyVerifyApplication, CommuntiyDailySummaryReport, UserInfoMisc, ) from pingcap_sso.discourse.models import ( DiscourseTopic, DiscoursePost, DiscourseUserAction ) logger = get_task_logger(__name__) @celery_app.task def send_asktug_daily_report_to_lark(lark_webhook): now = django_timezone.now().astimezone( timezone(timedelta(hours=8)) ) today =now.replace( hour=0, minute=0, second=0, microsecond=0 ) yesterday = today - timedelta(days=1) last_30_days = today - timedelta(days=30) new_user_count = User.objects.filter( date_joined__gte=yesterday, date_joined__lt=today, ).count() new_topic_count = DiscourseTopic.objects.filter( deleted_at__isnull=True, archetype='regular', visible=True, created_at__gte=yesterday, created_at__lt=today ).count() new_post_count = DiscoursePost.objects.filter( topic__deleted_at__isnull=True, topic__visible=True, topic__archetype='regular', deleted_at__isnull=True, hidden=False, post_type=1, created_at__gte=yesterday, created_at__lt=today ).count() new_solved_count = DiscourseUserAction.objects.filter( created_at__gte=yesterday, created_at__lt=today, action_type=DiscourseUserAction.ACTION_SOLVED ).count() new_company_verified_count = CompanyVerifyApplication.objects.filter( created_at__gte=yesterday, created_at__lt=today, status=CompanyVerifyApplication.STATUS_APPROVED ).count() dau = TrackEvent.objects.filter( openid__isnull=False, event_time__gte=yesterday, event_time__lt=today ).values('openid').distinct().count() total_topic_count = DiscourseTopic.objects.filter( deleted_at__isnull=True, archetype='regular', visible=True, ).count() total_post_count = DiscoursePost.objects.filter( topic__deleted_at__isnull=True, topic__visible=True, topic__archetype='regular', deleted_at__isnull=True, hidden=False, post_type=1, ).count() total_user_count = User.objects.all().count() total_company_verified_count = User.objects.filter( company_verified_status=User.COMPANY_VERIFIED_PASS ).count() total_company_name_count = User.objects.filter( company_name__isnull=False ).exclude( company_name__exact='' ).count() last_30_days_active_user_count = TrackEvent.objects.filter( openid__isnull=False, event_time__gte=last_30_days, event_time__lt=today ).values('openid').distinct().count() company_count = UserInfoMisc.objects.filter( company__isnull=False ).values('company').distinct().count() yesterday_str = yesterday.strftime('%m/%d') CommuntiyDailySummaryReport.objects.create( sequence=yesterday.strftime('%Y-%m-%d'), report_date=yesterday, time_dimension=CommuntiyDailySummaryReport.TIME_DAILY, new_user_count=new_user_count, new_topic_count=new_topic_count, new_post_count=new_post_count, new_solved_count=new_solved_count, new_company_verified_count=new_company_verified_count, dau=dau, total_topic_count=total_topic_count, total_post_count=total_post_count, total_user_count=total_user_count, total_company_verified_count=total_company_verified_count, total_company_name_count=total_company_name_count, last_30_days_active_user_count=last_30_days_active_user_count, ) message = f"""**{yesterday_str} 00:00 ~ 24:00 的新增数据** - 注册用户: {new_user_count} - 主题(全部分类): {new_topic_count} - 回复(全部分类): {new_post_count} - 解决方案(全部分类): {new_solved_count} - 认证用户: {new_company_verified_count} - DAU: {dau} **总计数据** - 主题数: {total_topic_count} - 回复数: {total_post_count} - 用户数: {total_user_count} - 认证用户数: {total_company_verified_count} - 填写公司名的用户数: {total_company_name_count} - 累计公司数: {company_count} - 近 30 日用户活跃数: {last_30_days_active_user_count}""" LarkMessage.send( webhook=lark_webhook, message_type=LarkMessage.MESSAGE_TYPE_MARKDOWN, content=message, remark='AskTUG 日报', _async=False )