77 lines
2.8 KiB
Python
77 lines
2.8 KiB
Python
from typing import Tuple
|
|
|
|
import sqlalchemy as sa
|
|
import json
|
|
|
|
from fastapi import Depends
|
|
|
|
import pandas as pd
|
|
|
|
from sqlalchemy import func, or_, and_, not_, MetaData
|
|
|
|
import schemas
|
|
from core.config import settings
|
|
from db.redisdb import get_redis_pool, RedisDrive
|
|
|
|
|
|
class XAnalysis:
|
|
def __init__(self, data_in: schemas.Overview, game: str):
|
|
self.data_in = data_in
|
|
self.game = game
|
|
|
|
def to_sql(self):
|
|
# 构建表
|
|
metadata = sa.MetaData(schema=self.game)
|
|
tbl = sa.Table(f'overview', metadata,
|
|
sa.Column('date'),
|
|
sa.Column('#os'),
|
|
sa.Column('#bundle_id'),
|
|
sa.Column('owner_name'),
|
|
sa.Column('channel'),
|
|
sa.Column('active_num'),
|
|
sa.Column('new_account_num'),
|
|
sa.Column('money'),
|
|
sa.Column('pay_account_num'),
|
|
sa.Column('new_pay_account_num'),
|
|
sa.Column('new_money'),
|
|
)
|
|
# bundle_id_col = sa.Column('#bundle_id')
|
|
# channel_col = sa.Column('channel')
|
|
# owner_name_col = sa.Column('owner_name')
|
|
# os_col = sa.Column('#os')
|
|
# date_col = sa.Column('date')
|
|
# active_num_col = sa.Column('active_num')
|
|
# new_account_num_col = sa.Column('new_account_num')
|
|
|
|
where = [
|
|
getattr(tbl.c, 'date') >= self.data_in.sdate,
|
|
getattr(tbl.c, 'date') <= self.data_in.edate,
|
|
]
|
|
|
|
if self.data_in.bundle_id:
|
|
where.append(getattr(tbl.c, '#bundle_id').in_(self.data_in.bundle_id))
|
|
|
|
if self.data_in.channel:
|
|
where.append(getattr(tbl.c, '#bundle_id').in_(self.data_in.channel))
|
|
|
|
if self.data_in.owner_name:
|
|
where.append(getattr(tbl.c, 'owner_name').in_(self.data_in.owner_name))
|
|
|
|
if self.data_in.os:
|
|
where.append(getattr(tbl.c, '#os').in_(self.data_in.os))
|
|
|
|
qry = sa.select(getattr(tbl.c, 'date'), getattr(tbl.c, '#bundle_id'),
|
|
func.sum(getattr(tbl.c, 'active_num')).label('active_num'),
|
|
func.sum(getattr(tbl.c, 'new_account_num')).label('new_account_num'),
|
|
func.sum(getattr(tbl.c, 'pay_account_num')).label('pay_account_num'),
|
|
func.sum(getattr(tbl.c, 'money')).label('money'),
|
|
func.sum(getattr(tbl.c, 'new_pay_account_num')).label('new_pay_account_num'),
|
|
func.sum(getattr(tbl.c, 'new_money')).label('new_money'),
|
|
|
|
) \
|
|
.where(*where) \
|
|
.group_by(getattr(tbl.c, 'date'), getattr(tbl.c, '#bundle_id'))
|
|
sql = str(qry.compile(compile_kwargs={"literal_binds": True}))
|
|
print(sql)
|
|
return sql
|