pythonのmongodbレポート開発
4680 ワード
詳細
1、pythonで親baseを定義.py
2、データレポートtotal_の定義package_day_active.py
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();