`

python之mongodb报表开发

 
阅读更多
1、python中定义父类base.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ConfigParser
import pymongo
import Constants
import abc

import mongodbInit
from jshy import excelManager


class DataCollectBase(object):
    _metaclass__ = abc.ABCMeta
    @abc.abstractproperty
    def currentTable(self):
        """
             定义当前操作表
           """
        raise NotImplementedError
    def __init__(self):
        self.client = mongodbInit.MongodbData();
        self.db = self.client.db;
        self._currentTable = self.currentTable();

    """
              启动任务:用来生成报表数据
            """
    def start(self):
        """
            实现启动
        """
        self.client.exists_dropTable(self._currentTable)
        # 正式表,不存在则创建
        self.client.not_exists_createTable(self._currentTable);
        # 查询日志中的mac和sn用户数据,保存到临时表
        self.generateTableData();
        print "生成报表数据:%s" %self._currentTable
    """生成数据报表接口"""
    @abc.abstractmethod
    def generateTableData(self):
        """数据表处理中心"""
        raise NotImplementedError

    def out_excel(self,filename,titlerow,query={},project={},**methodDict):
        obj = None;
        if len(project) > 0:
            cursor = self.db[self._currentTable].find(query, project);
        else:
            cursor = self.db[self._currentTable].find(query);
        # result = result.sort(methodDict["sort"])
        if len(methodDict) > 0:
            for key in methodDict:
                if hasattr(cursor ,key):
                   func=  getattr(cursor,key);
                   cursor =func(methodDict[key])
        result = cursor ;
        excelManager.generate_excel(rows=result,titleRow=titlerow,filename=filename)

2、定义数据报表total_package_day_active.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from jshy import dataManagerCenter
from jshy import excelManager
from jshy.Base import DataCollectBase

__author__ = "jiandequn"
import mongodbInit

""""聚视各套餐分别的日活跃度"""
class TotalPackageDayActive(DataCollectBase):

    def currentTable(self):
        return "total_package_day_active";
    """
        创建临时数据表并读取日志表中数据,进行格式化处理,将临时表数据插入到正式表中
    """
    def generateTableData(self):
        group = {"_id":{
                        "mac":'$mac',
                        "sn":'$sn',
                        "package_type":"$package_type",
                        "day_time":{"$substrBytes": ["$time", 0, 10]},
                        }
                 };
        group1 = {"_id":{'day_time':'$_id.day_time',"package_type":"$_id.package_type"},
                  "userCount":{'$sum':1}
                 };
        temp_result = self.db[dataManagerCenter.opentAction_table].aggregate(
            [
                # {"$match": match},
             {"$group":group},
             {"$group": group1},
             {"$project": {
                            "_id": 0,
                            "package_type": "$_id.package_type",
                            "day_time": "$_id.day_time",
                            "userCount": 1
                         }
             },
             {"$out": self._currentTable}]);
        return temp_result;

    def excel(self):
        filename = '总套餐日活跃度.xls';
        titleRow = [{"package_type": "套餐类型"},
                    {"day_time": "日期"},
                    {"userCount": "总用户数量"}];
        self.out_excel(filename,titleRow,sort=[("day_time",1),("package_type",1),("userCount",1)])
if __name__ == "__main__":
    TotalPackageDayActive().start();










分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics