def create_bill(user_uuid, name, host_uuid, run_time, month, total_fee): try: new_bill = Bill() new_bill.user_uuid = user_uuid new_bill.host_uuid = host_uuid new_bill.run_time = run_time new_bill.month = month new_bill.name = name new_bill.total_fee = total_fee new_bill.bill_account_time = datetime.datetime.now() new_bill.save() return new_bill except Exception as e: log.error('[bill] create_bill error.{}'.format(e.__str__())) return None
def check_host_bill(host, user_name, choice): """ 通过主机id来生成账单,如果是用户查询不需要结算,如果是生成月账单,则需要结算 :param kwargs: :return: """ cpu = host['hostInfo']['cpuCores'] mem = host['hostInfo']['memSize'] disk = host['hostInfo']['diskSize'] name = host['hostInfo'].get('hostname', '') # 获取上次结算后的lifetime,不存在则返回新的账单记录表 bill_record = get_record_bill(host_uuid=host['id'], name=name, cpu=cpu, disk=disk, mem=mem) current_month = datetime.datetime.now().month last_month = 12 if current_month == 1 else current_month - 1 # 当主机还在运行时候需要生成账单,如果是用户查询不需要结算,如果是生成月账单,则需要结算 if host['lifetime'] > bill_record.lifetime: # 只统计没有删除的主机 price = gain_price_for_rancher(cpu=cpu, mem=mem, disk=disk) if price: # 主机从上次结帐到当前还没结帐的时间 run_time = math.ceil( (host['lifetime'] - bill_record.lifetime) / 60) + 1 fee = run_time * price # 当是月账单定时任务,需要结算 if choice == "month_account": # 先查看有没有该主机的上个月账单(当用户在查看账单时候会根据用户查询时间生成账单,没有扣费),并且是没有支付的,bill_status=False try: check_bill = Bill.objects.get(user_uuid=user_name, host_uuid=host['id'], month=last_month, \ pay_status=False, bill_status=True) except Bill.DoesNotExist: log.info( '[bill] build month bill. bill not exist. create new bill ' ) # mutex.acquire(1) # if mutex.acquire(1): new_bill = Bill(user_uuid=user_name, name=name, host_uuid=host['id'], run_time=run_time, month=last_month, total_fee=fee) new_bill.save() log.info('[bill] create a new month bill:{}'.format( new_bill.bill_uuid)) status = change_balance(user_name, 'bill', -fee, new_bill.bill_uuid) if status is True: pay_status = True log.info( '[bill] change_balance successful.modify balance:{}' .format(-fee)) else: pay_status = False log.info('[bill] change_balance faild') new_bill.pay_status = pay_status new_bill.bill_account_time = datetime.datetime.now() new_bill.bill_status = False new_bill.save() change_recordbill(host_uuid=host['id'], lifetime=host['lifetime']) return except Exception as e: log.error('[bill] check_host_bill other error:{}'.format( e.__str__())) return status = change_balance(user_name, 'bill', -fee, check_bill.bill_uuid) if status is True: pay_status = True log.info('[bill] no pay bill:{}'.format( check_bill.bill_uuid)) log.info( '[bill] change_balance successful.modify balance:{}'. format(-fee)) else: pay_status = False log.info('[bill] change_balance faild') # mutex.acquire() check_bill.pay_status = pay_status check_bill.run_time = run_time check_bill.total_fee = fee check_bill.bill_account_time = datetime.datetime.now() check_bill.bill_status = False check_bill.save() change_recordbill(host_uuid=host['id'], lifetime=host['lifetime']) else: # 先查看有没有该主机的当月账单(当用户在查看账单时候会根据用户查询时间生成账单,没有扣费),并且是没有支付的,bill_status=False try: check_bill = Bill.objects.get(host_uuid=host['id'], month=current_month, pay_status=False, bill_status=True) check_bill.run_time = run_time check_bill.total_fee = fee check_bill.save() except Bill.DoesNotExist: new_bill = Bill(user_uuid=user_name, host_uuid=host['id'], name=name, run_time=run_time, month=current_month, total_fee=fee) new_bill.save() log.info('[bill] user see bill.create a new month bill') except Exception as e: log.error( '[bill] check_host_bill more than one bill meet the condition:{}' .format(e.__str__())) return else: log.error('[bill] the price startegy is not existed.cpu=%d,mem=%d,disk=%d', \ cpu, mem, disk)