def sendSSNormal(self,consumer,items): try: uid = str(items[0]) ServerID = str(items[1]) LoginDate = str(items[2]) LoginChanelID=str(items[3]) IPAddress=str(items[4]) CurrentDiamond=str(items[5]) sa = sensorsanalytics.SensorsAnalytics(consumer) distinct_id =ServerID+'_'+ uid expire_time = LoginDate d = datetime.datetime.strptime(expire_time,"%Y-%m-%d %H:%M:%S") time_sec_float = time.mktime(d.timetuple()) properties = { 'uid' :uid, 'ServerID' : ServerID, '$time' :time_sec_float, 'LoginChanelID':LoginChanelID, '$ip':IPAddress, 'CurrentDiamond':int(CurrentDiamond) } sa.track(distinct_id, 'Login', properties) sa.close() return 1 except Exception as e: Log.error("Login error code: %s"%(str(e))) if '10060' in str(str(e)): return 10060 else: return -1
def sendSSNormal(self, consumer, items): try: uid = str(items[0]) ServerID = str(items[1]) StageID = str(items[2]) EnterDate = str(items[3]) CurrentExp = str(items[4]) CurrentDiamond = str(items[5]) CurrentCoin = str(items[6]) sa = sensorsanalytics.SensorsAnalytics(consumer) distinct_id = ServerID + '_' + uid expire_time = EnterDate d = datetime.datetime.strptime(expire_time, "%Y-%m-%d %H:%M:%S") time_sec_float = time.mktime(d.timetuple()) properties = { 'uid': uid, 'ServerID': ServerID, 'StageID': StageID, '$time': time_sec_float, 'CurrentExp': int(CurrentExp), 'CurrentDiamond': int(CurrentDiamond), 'CurrentCoin': int(CurrentCoin) } sa.track(distinct_id, 'EnterMap', properties) sa.close() return 1 except Exception as e: Log.error("Entermap error code: %s" % (str(e))) if '10060' in str(str(e)): return 10060 else: return -1
def sendSSNormal(self, consumer, items): try: uid = str(items[0]) ServerID = str(items[1]) RechargeOrderID = str(items[2]) Money = str(items[3]) RechargeType = str(items[4]) ArrivalDate = str(items[5]) OrderCreatedDate = str(items[6]) Diamond = str(items[7]) sa = sensorsanalytics.SensorsAnalytics(consumer) distinct_id = ServerID + '_' + uid expire_time = ArrivalDate d = datetime.datetime.strptime(expire_time, "%Y-%m-%d %H:%M:%S") time_sec_float = time.mktime(d.timetuple()) properties = { 'uid': uid, 'ServerID': ServerID, 'RechargeOrderID': RechargeOrderID, 'Price': int(Money), 'RechargeType': RechargeType, '$time': time_sec_float, 'OrderCreatedDate': OrderCreatedDate, 'Diamond': int(Diamond) } sa.track(distinct_id, 'Recharge', properties) sa.close() return 1 except Exception as e: Log.error("Recharge error code: %s" % (str(e))) if '10060' in str(str(e)): return 10060 else: return -1
def sendSSNormal(self, consumer, items): try: ServerID = str(items[0]) RegistCount = str(items[1]) OnlineCount = str(items[2]) CurDate = str(items[3]) sa = sensorsanalytics.SensorsAnalytics(consumer) distinct_id = ServerID + '_onlinecount' expire_time = CurDate d = datetime.datetime.strptime(expire_time, "%Y-%m-%d %H:%M:%S") time_sec_float = time.mktime(d.timetuple()) properties = { 'ServerID': ServerID, 'RegistCount': int(RegistCount), 'OnlineCount': int(OnlineCount), '$time': time_sec_float } sa.track(distinct_id, 'Online', properties) sa.close() return 1 except Exception as e: Log.error("Online error code: %s" % (str(e))) if '10060' in str(str(e)): return 10060 else: return -1
def sendSSNormal(self,consumer,items): try: uid = str(items[0]) ServerID = str(items[1]) AccountID = str(items[2]) AccountName=str(items[3]) RoleName = str(items[4]) RoleCreatedDate=str(items[5]) sa = sensorsanalytics.SensorsAnalytics(consumer) distinct_id =ServerID+'_'+ uid expire_time = RoleCreatedDate.replace("\n","").replace("\r","") d = datetime.datetime.strptime(expire_time,"%Y-%m-%d %H:%M:%S") time_sec_float = time.mktime(d.timetuple()) properties = { 'uid' :uid, 'ServerID' : ServerID, 'AccountID':AccountID, 'AccountName':AccountName, 'RoleName':RoleName, '$time':time_sec_float } sa.track(distinct_id, 'CreateChar', properties) sa.close() return 1 except Exception as e: Log.error("CreateChar error code: %s"%(str(e))) if '10060' in str(str(e)): return 10060 else: return -1
def init_sensor(cls, **kwargs): """ 初始化连接 :param kwargs: :return: """ consumer = sensorsanalytics.DefaultConsumer(cls.SA_SERVER_URL, 800) cls.SA = sensorsanalytics.SensorsAnalytics(consumer)
def __init__(self): self.row_start = 0 self.row_stop = 0 self.recourd_count = 0 self.l = list() self.SA_SERVER_URL = 'https://datax-api.huatu.com/sa?project=production' # 初始化一个 Consumer,用于数据发送 # DefaultConsumer 是同步发送数据,因此不要在任何线上的服务中使用此 Consumer consumer = sensorsanalytics.DefaultConsumer(self.SA_SERVER_URL) # 使用 Consumer 来构造 SensorsAnalytics 对象 sa = sensorsanalytics.SensorsAnalytics(consumer) self.sa = sa self.zero_count = 0 today = datetime.today() self.to = today.strftime('%Y%m%d')
#!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'fjl' import sensorsanalytics # 从神策分析配置页面中获取数据接收的 URL SA_SERVER_URL = 'https://datax-api.huatu.com/sa?project=default' # 初始化一个 Consumer,用于数据发送 # DefaultConsumer 是同步发送数据,因此不要在任何线上的服务中使用此 Consumer consumer = sensorsanalytics.DefaultConsumer(SA_SERVER_URL) # 使用 Consumer 来构造 SensorsAnalytics 对象 sa = sensorsanalytics.SensorsAnalytics(consumer) # 记录用户登录事件 distinct_id = 'hahah233' properties = { # 用户性别属性(Sex)为男性 'Sex': 'Male', # 用户等级属性(Level)为 VIP 'UserLevel': 'Elite VIP', } # 设置用户属性 sa.profile_set(distinct_id, properties, is_login_id=True) sa.close()
def run(self): # 从文件里读取 SQL try: fh = open(self._sql_file, 'r') sql = fh.read() fh.close() except IOError: logging.error(traceback.format_exc()) logging.error("fail to read sql from file") return False # 获取 Meta 信息,判断是否是第一次导入 meta_sql = 'SELECT * FROM users LIMIT 1' ret_code, result = self._execute_sql(meta_sql) if ret_code != 200: logging.error("fail to execute meta sql. [msg=%r]", result.decode('utf8')) return False columns = result.readline().decode('utf8').strip().split('\t') is_new_profile = True if self._profile_name.upper() in (column.upper() for column in columns): is_new_profile = False result.close() # 构造 SQL,需要和 users 表进行 JOIN if is_new_profile: filter_sql = 'b.segment IS NOT NULL' else: filter_sql = 'a.%s IS NOT NULL OR b.segment IS NOT NULL ' % self._profile_name final_sql = ''' SELECT DISTINCT COALESCE(b.distinct_id, a.first_id), b.segment FROM users a LEFT OUTER JOIN (%s) b ON a.id = b.user_id WHERE %s LIMIT 100000000 ''' % (sql, filter_sql) logging.info("start running segmenter sql. [sql=%r, first=%r]", sql.strip(), is_new_profile) # 执行 SQL ret_code, result = self._execute_sql(final_sql) if ret_code != 200: logging.error("fail to execute segmenter sql. [msg=%r]", result.decode('utf8')) return False # 打开临时文件待写入 tmp_data_file_path = self._temp_dir + '/segmenter.dat' try: tmp_data_file_handler = open(tmp_data_file_path, 'w+') except IOError: logging.error(traceback.format_exc()) logging.error("fail to open tmp file for write") return False # 写入临时文件,因为后面的发送可能需要比较长的时间 logging.info("write segmenter result to tmp file. [path=%r]", tmp_data_file_path) first = True row_num = 0 for record in result: if first: first = False continue tmp_data_file_handler.write(record.decode('utf8')) row_num += 1 result.close() logging.info("write segmenter result success. [size=%r]", row_num) # 使用 SDK 开始发送数据 logging.info("start sending data to Sensors Analytics.") consumer = sensorsanalytics.BatchConsumer(self._logging_url, max_size=100) sa = sensorsanalytics.SensorsAnalytics(consumer) tmp_data_file_handler.seek(0, 0) for record in tmp_data_file_handler: distinct_id, segment = record.split('\t') if len(distinct_id) == 0: continue segment = segment.strip() if len(segment) == 0: sa.profile_unset(distinct_id, [self._profile_name]) else: sa.profile_set(distinct_id, {self._profile_name: segment}) sa.close() tmp_data_file_handler.close() logging.info("send data success.")