上代码,主要是整个流程。细节忽略
1 #! /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 """ 4 @author: zhoujiebing 5 @contact: zhoujiebing@maimiaotech.com 6 @date: 2012-11-16 11:30 7 @version: 1.0.0 8 @license: Copyright maimiaotech.com 9 @copyright: Copyright maimiaotech.com 10 11 """ 12 import sys 13 sys.path.append('http://www.cnblogs.com/') 14 from shop_info_db import ShopInfoDB 15 from database_operator import DBOpsCampaignSelect, DBOpsItemsSelect, DBOpsKeyword, DBOpsActionLog, DBOpsShopInfo 16 MONGODB_HOST = 'localhost' 17 MONGODB_PORT = 2006 18 from pymongo import Connection 19 mongoConn = Connection(host = MONGODB_HOST, port = MONGODB_PORT) 20 21 class Rule: 22 """ 23 Base Rule Class 24 """ 25 def __init__(self): 26 self.child_rule = [] 27 28 def judge_rule(self, shop_id, result): 29 return False 30 31 class AccessTokenRule(Rule): 32 """ 33 judge AccessToken 34 """ 35 def __init__(self, shop_status_dict): 36 Rule.__init__(self) 37 self.shop_status_dict = shop_status_dict 38 39 def judge_rule(self, shop_id, result): 40 shop_status = self.shop_status_dict[shop_id] 41 if shop_status['session_expired']: 42 resultinfo = ['Invalid AccessToken', shop_id] 43 result.append(resultinfo) 44 return True 45 if shop_status['insuff_level']: 46 resultinfo = ['R1 AccessToken', shop_id] 47 result.append(resultinfo) 48 return True 49 return False 50 51 class CampaignRule(Rule): 52 """ 53 judge campaign self 54 """ 55 def __init__(self): 56 Rule.__init__(self) 57 58 def judge_rule(self, shop_id, result): 59 campaign_select_db_ops = DBOpsCampaignSelect(mongoConn, str(shop_id)) 60 campaigns = campaign_select_db_ops.get_all() 61 if len(campaigns) != 1: 62 resultinfo = ['No Campaign', shop_id] 63 result.append(resultinfo) 64 return True 65 66 if not campaigns[0]['init_success']: 67 resultinfo = ['Campaign init Fail', shop_id] 68 result.append(resultinfo) 69 return True 70 return False 71 72 class ItemRule(Rule): 73 """ 74 judge items 75 """ 76 def __init__(self): 77 Rule.__init__(self) 78 79 def judge_rule(self, shop_id, result): 80 item_select_db_ops = DBOpsItemsSelect(mongoConn, str(shop_id)) 81 items_num = len(item_select_db_ops.get_active_items_all()) 82 if items_num <= 10: 83 resultinfo = ['Item few', shop_id, str(items_num)] 84 result.append(resultinfo) 85 return True 86 return False 87 88 class KeywordRule(Rule): 89 """ 90 judge keyword 91 """ 92 def __init__(self): 93 Rule.__init__(self) 94 95 def judge_rule(self, shop_id, result): 96 keyword_db_ops = DBOpsKeyword(mongoConn, str(shop_id)) 97 keywords_num = keyword_db_ops.coll.find().count() 98 if keywords_num <= 100: 99 resultinfo = ['Keyword few', shop_id, str(keywords_num)] 100 result.append(resultinfo) 101 return True 102 return False 103 104 class ActionRule(Rule): 105 """ 106 judge action 107 """ 108 def __init__(self): 109 Rule.__init__(self) 110 111 def judge_rule(self, shop_id, result): 112 action_db_ops = DBOpsActionLog(mongoConn, str(shop_id)) 113 accumulate = action_db_ops.get_last_week_action() 114 action_num = accumulate['add_words'] + accumulate['delete_words'] + accumulate['change_price'] 115 if action_num <= 100: 116 resultinfo = ['Action few', shop_id, str(action_num)] 117 return True 118 return False 119 120 def judge_rule(shop_id, unnormal_result, rule_relation): 121 """ 122 judge the shop 123 """ 124 while 1: 125 if rule_relation.judge_rule(shop_id, unnormal_result): 126 break 127 if not rule_relation.child_rule: 128 break 129 if len(rule_relation.child_rule) == 1: 130 rule_relation = rule_relation.child_rule[0] 131 else: 132 for rule_relation in rule_relation.child_rule: 133 if judge_rule(shop_id, unnormal_result, rule_relation): 134 return 135 136 if __name__ == '__main__': 137 file_name = 'unnormal_data.csv' 138 file_obj = file(file_name, 'w') 139 140 shop_info = ShopInfoDB(mongoConn) 141 shop_id_list = shop_info.get_all_shop_id_list() 142 shop_status_dict = {} 143 for shop in shop_info.shop_status.find(): 144 shop_status_dict[int(shop['_id'])] = shop 145 #rule init 146 rule_relation = Rule() 147 access_token_rule = AccessTokenRule(shop_status_dict) 148 campaign_rule = CampaignRule() 149 item_rule = ItemRule() 150 keyword_rule = KeywordRule() 151 action_rule = ActionRule() 152 153 #rule relation init 154 rule_relation.child_rule.append(access_token_rule) 155 access_token_rule.child_rule.append(campaign_rule) 156 campaign_rule.child_rule.append(item_rule) 157 campaign_rule.child_rule.append(keyword_rule) 158 campaign_rule.child_rule.append(action_rule) 159 160 unnormal_result = [] 161 for shop_id in shop_id_list: 162 judge_rule(shop_id, unnormal_result, rule_relation) 163 unnormal_result.sort(key = lambda x:x[0]) 164 for info in unnormal_result: 165 shop_id = info[1] 166 info[1] = str(shop_id) 167 nick = shop_status_dict[shop_id]['nick'] 168 info.insert(2, nick) 169 file_obj.write(','.join(info)) 170 file_obj.write('\n') 171 file_obj.close()