示例#1
0
    def transfer_from_hot_wallet(self,
                                 ticker,
                                 quantity=None,
                                 destination="multisigcash"):
        contract = self.session.query(
            models.Contract).filter_by(ticker=ticker).one()

        if destination == "offlinecash" or not contract.multisig_wallet_address:
            address = contract.cold_wallet_address
            to_account = "offlinecash"
        else:
            address = contract.multisig_wallet_address
            to_account = "multisigcash"

        if quantity is None:
            try:
                online_cash = self.session.query(models.Positions).filter_by(
                    username="******").filter_by(contract=contract).one()
            except NoResultFound:
                log.msg("No position in %s for onlinecash" % ticker)
                returnValue(None)
            else:
                # If we exceed the limit by 10%, so we're not always sending small amounts to the cold wallet
                if online_cash.position > contract.hot_wallet_limit * 1.1:
                    quantity = online_cash.position - contract.hot_wallet_limit
                else:
                    returnValue(None)

        log.msg("Transferring %d from hot to %s wallet at %s" %
                (quantity, destination, address))
        result = yield self.send_to_address(ticker, address, quantity)
        txid = result['txid']

        # Charge the fee to 'customer'
        fee = result['fee']
        fee_uid = util.get_uid()
        note = "%s: %s" % (address, txid)

        # CREDIT online cash (decrease)
        fee_d1 = self.accountant.transfer_position('onlinecash', ticker,
                                                   "credit", fee, note,
                                                   fee_uid)

        # DEBIT the customer (decrease)
        fee_d2 = self.accountant.transfer_position('customer', ticker, "debit",
                                                   fee, note, fee_uid)

        uid = util.get_uid()

        # CREDIT THE FROM ACCOUNT (decrease)
        d1 = self.accountant.transfer_position('onlinecash', ticker, 'credit',
                                               quantity, note, uid)
        # DEBIT THE TO ACCOUNT (increase)
        d2 = self.accountant.transfer_position(to_account, ticker, 'debit',
                                               quantity, note, uid)
        yield defer.gatherResults([d1, d2, fee_d1, fee_d2], consumeErrors=True)
        returnValue(txid)
示例#2
0
    def transfer_from_hot_wallet(self, ticker, quantity=None, destination="multisigcash"):
        contract = self.session.query(models.Contract).filter_by(ticker=ticker).one()

        if destination == "offlinecash" or not contract.multisig_wallet_address:
            address = contract.cold_wallet_address
            to_account = "offlinecash"
        else:
            address = contract.multisig_wallet_address
            to_account = "multisigcash"

        if quantity is None:
            try:
                online_cash = self.session.query(models.Positions).filter_by(username="******").filter_by(contract=contract).one()
            except NoResultFound:
                log.msg("No position in %s for onlinecash" % ticker)
                returnValue(None)
            else:
                # If we exceed the limit by 10%, so we're not always sending small amounts to the cold wallet
                if online_cash.position > contract.hot_wallet_limit * 1.1:
                    quantity = online_cash.position - contract.hot_wallet_limit
                else:
                    returnValue(None)

        log.msg("Transferring %d from hot to %s wallet at %s" % (quantity, destination, address))
        result = yield self.send_to_address(ticker, address, quantity)
        txid = result['txid']

        # Charge the fee to 'customer'
        fee = result['fee']
        fee_uid = util.get_uid()
        note = "%s: %s" % (address, txid)

        # CREDIT online cash (decrease)
        fee_d1 = self.accountant.transfer_position('onlinecash', ticker, "credit", fee,
                                               note, fee_uid)

        # DEBIT the customer (decrease)
        fee_d2 = self.accountant.transfer_position('customer', ticker, "debit", fee,
                                                   note, fee_uid)

        uid = util.get_uid()

        # CREDIT THE FROM ACCOUNT (decrease)
        d1=self.accountant.transfer_position('onlinecash', ticker, 'credit', quantity,
                                          note, uid)
        # DEBIT THE TO ACCOUNT (increase)
        d2=self.accountant.transfer_position(to_account, ticker, 'debit', quantity, note, uid)
        yield defer.gatherResults([d1, d2, fee_d1, fee_d2], consumeErrors=True)
        returnValue(txid)
示例#3
0
    def transfer_from_multisig_wallet(self, ticker, quantity, destination,
                                      multisig):
        contract = util.get_contract(self.session, ticker)
        if destination == "onlinecash":
            address = yield self.get_current_address("multisigcash", ticker)
        elif destination == "offlinecash":
            address = contract.cold_wallet_address
        else:
            raise NotImplementedError

        result = yield self.send_to_address(ticker,
                                            address,
                                            quantity,
                                            multisig=multisig)
        txid = result['txid']
        fee = result['fee']

        # Record fees
        fee_uid = util.get_uid()
        note = "%s: %s" % (address, txid)

        # CREDIT the from account
        fee_d1 = self.accountant.transfer_position('multisigcash', ticker,
                                                   'credit', fee, note,
                                                   fee_uid)

        # DEBIT the customer account
        fee_d2 = self.accountant.transfer_position('customer', ticker, 'debit',
                                                   fee, note, fee_uid)
        yield defer.gatherResults([fee_d1, fee_d2], consumeErrors=True)

        if destination == "offlinecash":
            # Record the transfer
            uid = util.get_uid()
            # CREDIT the from account
            d1 = self.accountant.transfer_position('multisigcash', ticker,
                                                   'credit', quantity, note,
                                                   uid)
            # DEBIT the to account
            d2 = self.accountant.transfer_position('offlinecash', ticker,
                                                   'debit', quantity, note,
                                                   uid)
            yield defer.gatherResults([d1, d2])
        else:
            # If going to online cash the transfer will get recorded when the btc arrives
            pass

        returnValue(txid)
示例#4
0
        def logic_func(*args):

            #make generator
            generator = func(*args)

            #gen uid
            uid = None
            if new_logic:
                uid = get_uid()

            #join schedule
            g_logic_schedule.join(generator, uid)

            if new_logic:
                if DEBUG:
                    g_logic_schedule.record(uid, logic_func.__name__, 'Begin')
                result = g_logic_schedule.run(uid, None)
            else:  #avoid generator nested
                if DEBUG:
                    g_logic_schedule.record(None, logic_func.__name__,
                                            'first run')
                result = generator.next()
                if DEBUG and g_logic_schedule.return_flag == False:
                    g_logic_schedule.record(uid, logic_func.__name__, 'pause')

            return result
示例#5
0
    def on_trade_success(self, order, passive_order, price, quantity):
        uid = util.get_uid()
        self.accountant.post_transaction(order.username,
                                         {
                                             'username': order.username,
                                             'aggressive': True,
                                             'contract': self.ticker,
                                             'order': order.id,
                                             'other_order': passive_order.id,
                                             'side': OrderSide.name(order.side),
                                             'quantity': quantity,
                                             'price': price,
                                             'timestamp': order.timestamp,
                                             'uid': uid
                                         }
        )

        self.accountant.post_transaction(passive_order.username,
                                         {
                                             'username': passive_order.username,
                                             'aggressive': False,
                                             'contract': self.ticker,
                                             'order': passive_order.id,
                                             'other_order': order.id,
                                             'side': OrderSide.name(passive_order.side),
                                             'quantity': quantity,
                                             'price': price,
                                             'timestamp': order.timestamp,
                                             'uid': uid
                                         }
        )
示例#6
0
def handle(item):
    try:
        # 需手动创建dest_dir. cd /Users/xujiayu/python, mkdir -p 24hours/date
        date = get_date(item)
        user_id = get_uid(item)
        dest_dir = get_dest_dir(DESTDIR, item)
        print("date: %s, user_id: %s, dest_dir: %s" %
              (date, user_id, dest_dir))
    except Exception as e:
        raise e
    try:
        hour_set = set()
        with open(item, 'r') as fr:
            for line in fr:
                # user_id = line.split("|")[0]
                ts = int(line.split("|")[1])
                day = str(time.localtime(ts)[2])
                hour = str(time.localtime(ts)[3])
                hour_set.add(",".join([day, hour]))
        if len(hour_set) >= 24:
            shutil.move(item, os.path.join(dest_dir, user_id))
            print("hour_set: %s, user_id: %s, filename: %s" %
                  (hour_set, user_id, item))
    except Exception as e:
        raise e
示例#7
0
def get_trajectory(item):
	try:
		try:
			user_id = get_uid(item)
			date = get_date(item)
			ts_0_0_0 = time.mktime(time.strptime(date, DATEFORMAT))
			lines_list_0 = []
			lines_list_1 = []
			ts_traj_map = {}
			sorted_map = {}
			traj_list = []
			inside_tup = ('1')

			come_cnt_list = [None] * time_index_nums
			go_cnt_list = [None] * time_index_nums
			stay_cnt_list = [None] * time_index_nums
		except Exception as e:
			raise e
		try:
			df = pd.read_csv(item, sep='|', names=['user_id', 'ts', 'rssi', 'AP'])
			df_0 = df[df['AP'] == '0C8268F90E64']
			df_1 = df[df['AP'] == '0C8268C7D504']
			df_2 = df[df['AP'] == '14E6E4E1C510']
			df_3 = df[df['AP'] == '0C8268C7DD6C']
			print("len: %s" % len(df))
		except Exception as e:
			raise e
	except Exception as e:
		raise e
示例#8
0
def run():
	try:
		paths_list = get_paths('20170501')
		i = 0
		while i < 100:
			# df = pd.read_csv(paths_list[i], sep='|', names=['user_id', 'ts', 'rssi', 'AP'])
			# df = get_df_with_index(df, df['ts'])
			# df_0 = df[df['AP'] == '14E4E6E186A4']
			# df_1 = df[df['AP'] == 'EC172FE3B340']
			# print("df: %s, df_0: %s, df_1: %s" % (df, df_0, df_1))
			# i += 1
			# plt.plot(df_0['rssi'])
			# plt.plot(df_1['rssi'])
			# plt.xlabel('time')
			# plt.ylabel('rssi')
			# plt.show()

			with open(paths_list[i], 'r') as fr:
				length = len(fr.readlines())
				date = get_date(paths_list[i])
				user_id = get_uid(paths_list[i])
			with open(paths_list[i], 'r') as fr:
				time_slices_list = []
				prev_line = fr.readline()
				prev_list = prev_line.split("|")
				user_id = prev_list[0]
				prev_ts = int(prev_list[1])
				prev_rssi = int(prev_list[2])
				prev_AP = prev_list[-1].strip()
				time_slices_list.append((prev_ts, prev_rssi, prev_AP))
				i += 1
				j = 1
				while j < length:
					j += 1
					cur_line = fr.readline()
					cur_list = cur_line.split("|")
					cur_ts = int(cur_list[1])
					cur_rssi = int(cur_list[2])
					cur_AP = cur_list[-1].strip()
					if cur_ts - prev_ts <= 120:
						time_slices_list.append((cur_ts, cur_rssi, cur_AP))
					else:
						df = pd.DataFrame(time_slices_list)
						df = get_df_with_index(df, df[0])
						df_0 = df[df[2] == '14E4E6E186A4']
						df_1 = df[df[2] == 'EC172FE3B340']
						print("df: %s, df_0: %s, df_1: %s, user_id: %s" % (df, df_0, df_1, user_id))
						plt.plot(df_0[1])
						plt.plot(df_1[1])
						plt.title(user_id)
						plt.xlabel('time')
						plt.ylabel('rssi')
						plt.show()
						time_slices_list[:] = []
						time_slices_list.append((cur_ts, cur_rssi, cur_AP))
					prev_ts = cur_ts
	except Exception as e:
		raise e
示例#9
0
文件: agent.py 项目: kyrias/mrw
def record_to_dict(record):
	login = {
		'host': platform.node(),
		'user': record.user,
		'uid': get_uid(record.user),
		'rhost': record.host,
		'line': record.line,
		'time': record.sec,
	}
	return login
示例#10
0
    def transfer_from_multisig_wallet(self, ticker, quantity, destination, multisig):
        contract = util.get_contract(self.session, ticker)
        if destination == "onlinecash":
            address = yield self.get_current_address("multisigcash", ticker)
        elif destination == "offlinecash":
            address = contract.cold_wallet_address
        else:
            raise NotImplementedError

        result = yield self.send_to_address(ticker, address, quantity, multisig=multisig)
        txid = result['txid']
        fee = result['fee']

        # Record fees
        fee_uid = util.get_uid()
        note = "%s: %s" % (address, txid)

        # CREDIT the from account
        fee_d1 = self.accountant.transfer_position('multisigcash', ticker, 'credit', fee, note, fee_uid)

        # DEBIT the customer account
        fee_d2 = self.accountant.transfer_position('customer', ticker, 'debit', fee, note, fee_uid)
        yield defer.gatherResults([fee_d1, fee_d2], consumeErrors=True)

        if destination == "offlinecash":
            # Record the transfer
            uid = util.get_uid()
            # CREDIT the from account
            d1=self.accountant.transfer_position('multisigcash', ticker, 'credit', quantity,
                                              note, uid)
            # DEBIT the to account
            d2=self.accountant.transfer_position('offlinecash', ticker, 'debit', quantity, note, uid)
            yield defer.gatherResults([d1, d2])
        else:
            # If going to online cash the transfer will get recorded when the btc arrives
            pass

        returnValue(txid)
示例#11
0
    def process_withdrawal(self, withdrawal_id, online=False, cancel=False, admin_username=None, multisig={}):
        # Mark a withdrawal as complete, send the money from the BTC wallet if online=True
        # and tell the accountant that the withdrawal has happened
        # If cancel=True, then return the money to the user
        log.msg("Processing withdrawal: %d online=%s cancel=%s" % (withdrawal_id, online, cancel))
        try:
            withdrawal = self.session.query(models.Withdrawal).filter_by(id=withdrawal_id).one()
        except NoResultFound:
            log.err("No withdrawal found for id %d" % withdrawal_id)
            raise WITHDRAWAL_NOT_FOUND

        log.msg("Withdrawal found: %s" % withdrawal)
        if not withdrawal.pending:
            raise WITHDRAWAL_COMPLETE

        # Figure out what to do with this withdrawal, and send to address if an online withdrawal
        if cancel:
            txid = "cancel"
            to_user = withdrawal.username
            fee = None
        else:
            if online:
                # Actually process via the hot or warm wallet
                if withdrawal.contract.ticker in self.bitcoinrpc:
                    if not multisig:
                        to_user = "******"
                    else:
                        to_user = "******"

                    result = yield self.send_to_address(withdrawal.contract.ticker, withdrawal.address, withdrawal.amount,
                                                      multisig=multisig)
                    txid = result['txid']
                    fee = result['fee']
                else:
                    raise NO_AUTOMATIC_WITHDRAWAL
            else:
                fee = None
                txid = "offline"
                to_user = "******"

        # Notify the accountant
        try:
            if admin_username is not None:
                note = "%s: %s (%s)" % (withdrawal.address, txid, admin_username)
            else:
                note = "%s: %s" % (withdrawal.address, txid)

            # If there was a fee
            if fee is not None:
                fee_uid = util.get_uid()
                fee_d1 = self.accountant.transfer_position(to_user, withdrawal.contract.ticker, 'credit', fee,
                                                           note, fee_uid)
                fee_d2 = self.accountant.transfer_position('customer', withdrawal.contract.ticker, 'debit', fee,
                                                           note, fee_uid)
                yield defer.gatherResults([fee_d1, fee_d2], consumeErrors=True)

            uid = util.get_uid()


            d1 = self.accountant.transfer_position('pendingwithdrawal', withdrawal.contract.ticker, 'debit',
                                              withdrawal.amount,
                                              note, uid)
            d2 = self.accountant.transfer_position(to_user, withdrawal.contract.ticker, 'credit', withdrawal.amount,
                                              note, uid)
            yield defer.gatherResults([d1, d2], consumeErrors=True)
        except Exception as e:
            log.err(e)
            self.alerts.send_alert(str(e), "Transfer position failed in process_withdrawal")
            raise e

        # Update the DB
        try:
            withdrawal.pending = False
            withdrawal.completed = datetime.utcnow()
            self.session.add(withdrawal)
            self.session.commit()
        except Exception as e:
            log.err("Exception when trying to mark withdrawal complete: %s" % e)
            self.session.rollback()
            raise e

        returnValue(txid)
示例#12
0
def get_trajectory(item):
    try:
        try:
            user_id = get_uid(item)
            date = get_date(item)
            ts_0_0_0 = time.mktime(time.strptime(date, DATEFORMAT))
            lines_list_0 = []
            lines_list_1 = []
            lines_list_2 = []
            lines_list_3 = []
            ts_traj_map = {}
            sorted_map = {}
            traj_list = []
            inside_tup = ('1')
            AP_list = [
                '0C8268F90E64', '0C8268C7D504', '14E6E4E1C510', '0C8268C7DD6C'
            ]

            time_index_nums = int(24 * 60 / 5)
            come_cnt_list = [None] * time_index_nums
            go_cnt_list = [None] * time_index_nums
            stay_cnt_list = [None] * time_index_nums

        except Exception as e:
            raise e
        try:
            df = pd.read_csv(item,
                             sep='|',
                             names=['user_id', 'ts', 'rssi', 'AP'])
            with open(item, 'r') as fr:
                for line in fr:
                    line_list = line.split("|")
                    AP = line_list[-1].strip()
                    # print("line_list: %s, filename: %s" % (line_list, item))
                    line_list = [line_list[0], line_list[1], line_list[2], AP]
                    if AP == AP_list[0]:
                        lines_list_0.append(line_list)
                    elif AP == AP_list[1]:
                        lines_list_1.append(line_list)
                    elif AP == AP_list[2]:
                        lines_list_2.append(line_list)
                    elif AP == AP_list[3]:
                        lines_list_3.append(line_list)
        except Exception as e:
            raise e
        try:
            i = 1
            while i < (len(lines_list_0) - 1):
                rssi = int(lines_list_0[i][2])
                if rssi > int(lines_list_0[i - 1][2]) and rssi > int(
                        lines_list_0[i + 1][2]):
                    ts = time.strftime(DATETIMEFORMAT,
                                       time.localtime(int(lines_list_0[i][1])))
                    if ts in ts_traj_map:
                        traj = ts_traj_map[ts]
                        ts_traj_map[ts] = [traj, '0']
                        # ts_traj_map[ts] = [traj, ['0', rssi]]
                        # print("outside AP: 0. dup ts. ts_traj_map[ts]: %s, ts: %s, user_id: %s" % (ts_traj_map[ts], ts, user_id))
                        # ts_traj_map[ts] = '1'
                    else:
                        ts_traj_map[ts] = '0'
                        # ts_traj_map[ts] = ['0', rssi]
                i += 1
            i = 1
            while i < (len(lines_list_1) - 1):
                rssi = int(lines_list_1[i][2])
                if rssi > int(lines_list_1[i - 1][2]) and rssi > int(
                        lines_list_1[i + 1][2]):
                    ts = time.strftime(DATETIMEFORMAT,
                                       time.localtime(int(lines_list_1[i][1])))
                    if ts in ts_traj_map:
                        traj = ts_traj_map[ts]
                        ts_traj_map[ts] = [traj, '1']
                        # ts_traj_map[ts] = [traj, ['1', rssi]]
                        # print("inside AP: 1. dup ts. ts_traj_map[ts]: %s, ts: %s, user_id: %s" % (ts_traj_map[ts], ts, user_id))
                        # continue
                        # ts_traj_map[ts] = '1'
                    else:
                        ts_traj_map[ts] = '1'
                        # ts_traj_map[ts] = ['1', rssi]
                i += 1
            i = 1
            while i < (len(lines_list_2) - 1):
                rssi = int(lines_list_2[i][2])
                if rssi > int(lines_list_2[i - 1][2]) and rssi > int(
                        lines_list_2[i + 1][2]):
                    ts = time.strftime(DATETIMEFORMAT,
                                       time.localtime(int(lines_list_2[i][1])))
                    if ts in ts_traj_map:
                        traj = ts_traj_map[ts]
                        ts_traj_map[ts] = [traj, '2']
                        # ts_traj_map[ts] = [traj, ['2', rssi]]
                        # print("outside AP: 2. dup ts. ts_traj_map[ts]: %s, ts: %s, user_id: %s" % (ts_traj_map[ts], ts, user_id))
                        # ts_traj_map[ts] = '1'
                    else:
                        ts_traj_map[ts] = '2'
                        # ts_traj_map[ts] = ['2', rssi]
                i += 1
            i = 1
            while i < (len(lines_list_3) - 1):
                rssi = int(lines_list_3[i][2])
                if rssi > int(lines_list_3[i - 1][2]) and rssi > int(
                        lines_list_3[i + 1][2]):
                    ts = time.strftime(DATETIMEFORMAT,
                                       time.localtime(int(lines_list_3[i][1])))
                    if ts in ts_traj_map:
                        traj = ts_traj_map[ts]
                        ts_traj_map[ts] = [traj, '3']
                        # ts_traj_map[ts] = [traj, ['3', rssi]]
                        # print("inside AP: 3. dup ts. ts_traj_map[ts]: %s, ts: %s, user_id: %s" % (ts_traj_map[ts], ts, user_id))
                        # ts_traj_map[ts] = '1'
                    else:
                        ts_traj_map[ts] = '3'
                        # ts_traj_map[ts] = ['3', rssi]
                i += 1
            length = len(ts_traj_map)
            if length > 1:
                sorted_map = sorted(ts_traj_map.items())
                for tup in sorted_map:
                    if isinstance(tup[1], list):
                        print("sorted_map: %s, user_id: %s" %
                              (sorted_map, user_id))
                        continue
                # print("sorted_map: %s, user_id: %s" % (sorted_map, user_id))
        except Exception as e:
            raise e
    except Exception as e:
        raise e
示例#13
0
def run():
    try:
        try:
            paths_list = get_paths_0('20170502')
            i = 0
        except Exception as e:
            raise e
        try:
            while i < len(paths_list):
                lines_list_0 = []
                lines_list_1 = []
                ts_traj_map = {}
                user_id = get_uid(paths_list[i])
                with open(paths_list[i], 'r') as fr:
                    for line in fr:
                        line_list = line.split("|")
                        AP = line_list[-1].strip()
                        # print("line_list: %s, filename: %s" % (line_list, item))
                        line_list = [
                            line_list[0], line_list[1], line_list[2], AP
                        ]
                        if AP == '14E4E6E186A4':
                            lines_list_0.append(line_list)
                        elif AP == 'EC172FE3B340':
                            lines_list_1.append(line_list)
                j = 1
                while j < len(lines_list_0) - 1:
                    rssi = int(lines_list_0[j][2])
                    if rssi > int(lines_list_0[j - 1][2]) and rssi > int(
                            lines_list_0[j + 1][2]):
                        # ts = time.strftime(DATETIMEFORMAT, time.localtime(int(lines_list_0[j][1])))
                        ts = int(lines_list_0[j][1])
                        if ts in ts_traj_map:
                            ts_traj_map[ts] = '1'
                        else:
                            ts_traj_map[ts] = '0'
                    j += 1
                j = 1
                while j < len(lines_list_1) - 1:
                    rssi = int(lines_list_1[j][2])
                    if rssi > int(lines_list_1[j - 1][2]) and rssi > int(
                            lines_list_1[j + 1][2]):
                        # ts = time.strftime(DATETIMEFORMAT, time.localtime(int(lines_list_1[j][1])))
                        ts = int(lines_list_1[j][1])
                        ts_traj_map[ts] = '1'
                    j += 1
                length = len(ts_traj_map)
                if length > 1:
                    sorted_map = sorted(ts_traj_map.items())
                    # print("sorted_map: %s, user_id: %s" % (sorted_map, user_id))
                    prev_ts = sorted_map[0][0]
                    # print("prev_ts: %s, type: %s" % (prev_ts, type(prev_ts)))
                    j = 1
                    while j < len(sorted_map):
                        cur_ts = sorted_map[j][0]
                        peak_interval = cur_ts - prev_ts
                        # print("peak_interval: %s" % peak_interval)
                        BIG_LIST.append(peak_interval)
                        prev_ts = cur_ts
                        j += 1
                i += 1
            # print("BIG_LIST: %s, len: %s" % (BIG_LIST, len(BIG_LIST)))
            kmeans = KMeans(n_clusters=2).fit(
                np.array(BIG_LIST).reshape(-1, 1))
            center = kmeans.cluster_centers_
            print("center: %s" % center)
        except Exception as e:
            raise e
    except Exception as e:
        raise e
示例#14
0
def get_trajectory(item):
	try:
		try:
			user_id = get_uid(item)
			date = get_date(item)
			ts_0_0_0 = time.mktime(time.strptime(date, DATEFORMAT))
			lines_list_0 = []
			lines_list_1 = []
			ts_traj_map = {}
			sorted_map = {}
			traj_list = []
			inside_tup = ('1')

			come_cnt_list = [None] * TIME_INDEX_NUMS
			go_cnt_list = [None] * TIME_INDEX_NUMS
			stay_cnt_list = [None] * TIME_INDEX_NUMS

		except Exception as e:
			raise e
		try:
			with open(item, 'r') as fr:
				for line in fr:
					line_list = line.split("|")
					AP = line_list[-1].strip()
					line_list = [line_list[0], line_list[1], line_list[2], AP]
					if AP == '14E4E6E186A4':
						lines_list_0.append(line_list)
					elif AP == 'EC172FE3B340':
						lines_list_1.append(line_list)
		except Exception as e:
			raise e
		try:
			i = 1
			while i < (len(lines_list_0) - 1):
				rssi = int(lines_list_0[i][2])
				if rssi > int(lines_list_0[i-1][2]) and rssi > int(lines_list_0[i+1][2]):
					ts = time.strftime(DATETIMEFORMAT, time.localtime(int(lines_list_0[i][1])))
					if ts in ts_traj_map:
						# traj = ts_traj_map[ts]
						# ts_traj_map[ts] = [traj, '0']
						# ts_traj_map[ts] = [traj, ['0', rssi]]
						print("outside AP. dup ts. ts_traj_map: %s, ts: %s, user_id: %s" % (ts_traj_map, ts, user_id))
						ts_traj_map[ts] = '1'
					else:
						ts_traj_map[ts] = '0'
						# ts_traj_map[ts] = ['0', rssi]
				i += 1
			i = 1
			while i < (len(lines_list_1) - 1):
				rssi = int(lines_list_1[i][2])
				if rssi > int(lines_list_1[i-1][2]) and rssi > int(lines_list_1[i+1][2]):
					ts = time.strftime(DATETIMEFORMAT, time.localtime(int(lines_list_1[i][1])))
					if ts in ts_traj_map:
						# traj = ts_traj_map[ts]
						# ts_traj_map[ts] = [traj, '1']
						# ts_traj_map[ts] = [traj, ['1', rssi]]
						print("inside AP. dup ts. ts_traj_map: %s, ts: %s, user_id: %s" % (ts_traj_map, ts, user_id))
						# continue
						ts_traj_map[ts] = '1'
					else:
						ts_traj_map[ts] = '1'
						# ts_traj_map[ts] = ['1', rssi]
				i += 1
			length = len(ts_traj_map)
			if length > 1:
				sorted_map = sorted(ts_traj_map.items())
				# print("sorted_map: %s, user_id: %s" % (sorted_map, user_id))
				prev_ts = convert_datetime_to_timestamp(sorted_map[0][0], DATETIMEFORMAT)
				traj_list.append(sorted_map[0])
				i = 1
				while i < length:
					cur_ts = convert_datetime_to_timestamp(sorted_map[i][0], DATETIMEFORMAT)
					if cur_ts - prev_ts <= THRESHOLD:
						traj_list.append(sorted_map[i])
					else:
						come_cnt_list, go_cnt_list, stay_cnt_list = handle(traj_list, inside_tup, user_id, ts_0_0_0, come_cnt_list, go_cnt_list, stay_cnt_list)
						# print("traj_list: %s, user_id: %s" % (traj_list, user_id))
						traj_list[:] = []
						traj_list.append(sorted_map[i])
					i += 1
					prev_ts = cur_ts
				come_cnt_list, go_cnt_list, stay_cnt_list = handle(traj_list, inside_tup, user_id, ts_0_0_0, come_cnt_list, go_cnt_list, stay_cnt_list)
				# print("traj_list: %s, user_id: %s" % (traj_list, user_id))
			with lock:
				global RES_LIST
				i = 0
				while i < TIME_INDEX_NUMS:
					come_cnt = come_cnt_list[i] if come_cnt_list[i] else 0
					go_cnt = go_cnt_list[i] if go_cnt_list[i] else 0
					stay_cnt = stay_cnt_list[i] if stay_cnt_list[i] else 0
					RES_LIST[i] = RES_LIST[i] + come_cnt - go_cnt + stay_cnt
					i += 1
			# for cnt in come_cnt_list:
			# 	if cnt:
			# 		print("come_cnt_list: %s, filename: %s" % (come_cnt_list, item))
			# 		break
			# print("come_cnt_list: %s, filename: %s" % (come_cnt_list, item))
			# print("go_cnt_list: %s, filename: %s" % (go_cnt_list, item))
			# print("stay_cnt_list: %s, filename: %s" % (stay_cnt_list, item))
		except Exception as e:
			raise e
	except Exception as e:
		raise e
示例#15
0
    def process_withdrawal(self,
                           withdrawal_id,
                           online=False,
                           cancel=False,
                           admin_username=None,
                           multisig={}):
        # Mark a withdrawal as complete, send the money from the BTC wallet if online=True
        # and tell the accountant that the withdrawal has happened
        # If cancel=True, then return the money to the user
        log.msg("Processing withdrawal: %d online=%s cancel=%s" %
                (withdrawal_id, online, cancel))
        try:
            withdrawal = self.session.query(
                models.Withdrawal).filter_by(id=withdrawal_id).one()
        except NoResultFound:
            log.err("No withdrawal found for id %d" % withdrawal_id)
            raise WITHDRAWAL_NOT_FOUND

        log.msg("Withdrawal found: %s" % withdrawal)
        if not withdrawal.pending:
            raise WITHDRAWAL_COMPLETE

        # Figure out what to do with this withdrawal, and send to address if an online withdrawal
        if cancel:
            txid = "cancel"
            to_user = withdrawal.username
            fee = None
        else:
            if online:
                # Actually process via the hot or warm wallet
                if withdrawal.contract.ticker in self.bitcoinrpc:
                    if not multisig:
                        to_user = "******"
                    else:
                        to_user = "******"

                    result = yield self.send_to_address(
                        withdrawal.contract.ticker,
                        withdrawal.address,
                        withdrawal.amount,
                        multisig=multisig)
                    txid = result['txid']
                    fee = result['fee']
                else:
                    raise NO_AUTOMATIC_WITHDRAWAL
            else:
                fee = None
                txid = "offline"
                to_user = "******"

        # Notify the accountant
        try:
            if admin_username is not None:
                note = "%s: %s (%s)" % (withdrawal.address, txid,
                                        admin_username)
            else:
                note = "%s: %s" % (withdrawal.address, txid)

            # If there was a fee
            if fee is not None:
                fee_uid = util.get_uid()
                fee_d1 = self.accountant.transfer_position(
                    to_user, withdrawal.contract.ticker, 'credit', fee, note,
                    fee_uid)
                fee_d2 = self.accountant.transfer_position(
                    'customer', withdrawal.contract.ticker, 'debit', fee, note,
                    fee_uid)
                yield defer.gatherResults([fee_d1, fee_d2], consumeErrors=True)

            uid = util.get_uid()

            d1 = self.accountant.transfer_position('pendingwithdrawal',
                                                   withdrawal.contract.ticker,
                                                   'debit', withdrawal.amount,
                                                   note, uid)
            d2 = self.accountant.transfer_position(to_user,
                                                   withdrawal.contract.ticker,
                                                   'credit', withdrawal.amount,
                                                   note, uid)
            yield defer.gatherResults([d1, d2], consumeErrors=True)
        except Exception as e:
            log.err(e)
            self.alerts.send_alert(
                str(e), "Transfer position failed in process_withdrawal")
            raise e

        # Update the DB
        try:
            withdrawal.pending = False
            withdrawal.completed = datetime.utcnow()
            self.session.add(withdrawal)
            self.session.commit()
        except Exception as e:
            log.err("Exception when trying to mark withdrawal complete: %s" %
                    e)
            self.session.rollback()
            raise e

        returnValue(txid)
示例#16
0
 def maps_save(self, fd):
     """ save fd:uid, uid:fd """
     uid = get_uid()
     self.maps[fd] = uid
     self.maps[uid] = fd
     return uid
示例#17
0
 def maps_save(self, fd):
     """ save fd:uid, uid:fd """
     uid = get_uid()
     self.maps[fd] = uid
     self.maps[uid] = fd
     return uid