Django Model Objects Bulk Update - django

I have reset_company_limits module for updating limit of a company, this task is running on daily basis in morning. But somehow some company limit is unable to update. I am not able to find any error in my code
def reset_company_limits(comp_id=None, reset_type=['daily', 'monthly']):
if comp_id:
company_accounts = CompanyAccount.objects.filter(id=comp_id, enabled=1)
else:
company_accounts = CompanyAccount.objects.filter(enabled=1)
reset, rcache, cache_rec = ({} for l in xrange(3))
error = []
for rtype in reset_type:
for account in company_accounts:
try:
counts = account.get_feature_reset_counts(feature_type=reset_type)
limits = account.get_limits()
recruiters = account.recruiter_set.filter(is_active=True).values('pk')
for limit in limits:
key = RECRUITER_LOOKUP[limit.type]
if key in counts[rtype]:
if rtype == 'daily':
limit.daily = counts[rtype][key]
elif rtype == 'monthly':
limit.monthly = counts[rtype][key]
limit.save()
except Exception, e:
error.append(account.pk)
print "Error occurred for company id %s :: %s" % (account.pk, e)
return reset, rcache, cache_rec, error

Related

Assert operator to count before and after from various variable

i have codes to get database records, send test data (not shown in below codes),
and count before/after size.
can i do below assertion to do simple math calculation directly using the printIn result?
assert responseListAfter.size() == responseListBefore.size + results
this is the full codes
//count the DB records which State = New
String dbQuery = "SELECT COUNT(*) as count FROM dispenseorders.dispenseorder where status = 'true'"
List results = CustomKeywords.'swisslog.database.getSQLResults'(GlobalVariable.dbConnString , GlobalVariable.dbUsername , GlobalVariable.dbPassword ,GlobalVariable.dbDriver ,dbQuery )
println results
//Before Refresh. count number of records in Kafka Topic A
def TopicBefore = 'A'
def NewMessageBefore = consumeMessageBefore(TopicBefore)
def responseListBefore = new JsonSlurper().parseText(consumeMessageBefore.getResponseText())
def consumeMessageBefore(def topic) {
WS.sendRequest(findTestObject('Object Repository/Kafka/subscribe_to_topic_A', [('topic_name') : topic, ('KafkaRestName') : GlobalVariable.KafkaRestName
, ('KafkaRestPort') : GlobalVariable.KafkaRestPort]))
return consumeMessageBefore = WS.sendRequestAndVerify(findTestObject('Object Repository/Kafka/consume_message_from_topic_for_DR',
[('KafkaRestName') : GlobalVariable.KafkaRestName, ('KafkaRestPort') : GlobalVariable.KafkaRestPort]))
}
println('before Request Refresh: \n' + responseListBefore.size)
WebUI.delay(10)
//after Refresh. count number of records in Kafka Topic A
def TopicAfter = 'A'
def NewMessageAfter = consumeMessageAfter(TopicAfter)
def responseListAfter = new JsonSlurper().parseText(consumeMessageAfter.getResponseText())
def consumeMessageAfter(def topic) {
WS.sendRequest(findTestObject('Object Repository/Kafka/subscribe_to_topic_for_DR', [('topic_name') : topic, ('KafkaRestName') : GlobalVariable.KafkaRestName
, ('KafkaRestPort') : GlobalVariable.KafkaRestPort]))
return consumeMessageAfter = WS.sendRequestAndVerify(findTestObject('Object Repository/Kafka/consume_message_from_topic_for_DR',
[('KafkaRestName') : GlobalVariable.KafkaRestName, ('KafkaRestPort') : GlobalVariable.KafkaRestPort]))
}
println('after Request Refresh: \n' + responseListAfter.size)
//check total messages. After Refresh count = Before Refresh count + DB records
assert responseListAfter.size() == responseListBefore.size + results
results is a list, and you need a list size (int) to add it to other ints:
assert responseListAfter.size() == responseListBefore.size() + results.size()

django handle slowly when doing Stress Testing

i had built django server to handle some work.And when i sent 10000 requests to django,django will became slow after 4000th requests,it will report socket timeout randomly.And i take this one data to test it alone,it is ok.So,how can i fix this bug?
i have no idea to fix it
def handle(request) :
sys.stderr.write( "[info]request come in {} \n".format(sys._getframe().f_code.co_name))
ret_dict = dict()
ret_str = ""
if ("vid" in request.GET) and ("title" in request.GET):
id = request.GET["id"]
title = urllib.parse.unquote(request.GET["title"],encoding ='utf-8')
title = request.GET["title"]
req_video = dict()
req_video["vid"] = vid
req_video["title"] = title
tags = handle_function(req_video)
ret_dict["vid"] = vid
ret_dict["tags"] = tags
ret_str = json.dumps(ret_dict)
return HttpResponse(ret_str)
client codes:
for line in sys.stdin:
line = line.rstrip('\n')
fields = line.split("\t")
vid = fields[0]
title = fields[1]
page_params_mq = {'vid':vid,'title':title}
try :
rec_mq_res = get_rec_mingqiang(server_ip_mq, server_port_mq, web_page_mq, page_params_mq)
except:
print (line)
continue
vid = rec_mq_res["vid"]
tags = rec_mq_res["tags"]
fo.write(vid+"\t"+tags+"\n")
cat test_data|python client.py, there are 10000 rows in test_data,and it send request sequentially,and will be hunt randomly after 4000.
django can handle these requests quickly

How to catch the stack status in Openstack

I have following conditions
1. stackCreate
2. stackUpdate
3. stackCreate
What I am trying to do is, while the stackCreate/Update/Delete is triggered, I need to check on the progress. How can I do that? I know of 2 wayts
1. openstack stack event list .
2. I have below python code.
stack_id = str(hc.stacks.get(stack_name).id)
hc.stacks.delete(stack_id=stack_id)
try:
evntsdata = hc.events.list(stack_name)[0].to_dict()
event_handle = evntsdata['resource_status']
if event_handle == 'DELETE_IN_PROGRESS':
loopcontinue = True
while loopcontinue:
evntsdata = hc.events.list(stack_name)[0].to_dict()
event_handle = evntsdata['resource_status']
if event_handle == 'DELETE_COMPLETE':
loopcontinue = False
print(str(timestamp()) + " " + "Delete is Completed!")
elif event_handle == 'DELETE_FAILED':
print("Failed") # this needs a proper error msg
sys.exit(0)
else:
print(str(timestamp()) + " " + "Delete in Progress!")
time.sleep(5)
elif event_handle == 'DELETE_COMPLETE':
print(str(timestamp()) + " " + "Delete is Completed!")
sys.exit(0)
elif event_handle == 'DELETE_FAILED':
print("Failed")
sys.exit(0)
except AttributeError as e:
print(str(timestamp()) + " " + "ERROR: Stack Delete Failure")
raise
except (RuntimeError, heatclient.exc.NotFound):
print("Stack doesnt exist:", stack_name)
The first method is shell command in which I am not very good. (or lets say I dont know how to best integrate the shell command in python)
The problem with both the methods is that I am putting these many steps to identify whether the stack delete is successful. And I am repeating the same for stackupdate and create which is not best practice I am thinking. Anyone has any idea how I can minimize this logic? Any help is greatly appreciated.
You can write simple functions to create/update/delete stack and also to check the status of stack.
Please check below sample code to create a stack and poll the status of the stack.
from keystoneauth1 import loading
from keystoneauth1 import session
from heatclient import client
tenant_id = 'ab3fd9ca29e149acb25161ec8053da9c'
heat_url = 'http://10.26.12.31:8004/v1/%s' % tenant_id
auth_token = 'gAAAAABZYxfjz88XNXnfoCPkNLVeVtqtJ9o8qEtgFhI2GJ-ewSCuiypdwt3K5evgQeICVRqMa2jXgzVlENAUB19ZNyQfVCxSX4_lMBKyChM76SGuQUP8U-xJ9EKIfFaVwRGBkk4Ow9OO-iNINfMs0B5-LzJvxTFybi8yZw4EiagQpNpfu1onYfc'
heat = client.Client('1', endpoint=heat_url, token=auth_token)
def create_stack(stack_file_path, stack_name, parameters=None):
template = open(stack_file_path)
if parameters:
stack = heat.stacks.create(stack_name=stack_name, template=template.read(), parameters=parameters)
else:
stack = heat.stacks.create(stack_name=stack_name, template=template.read())
template.close()
return stack
def get_stack_status(stack_id):
stack = heat.stacks.get(stack_id)
return stack.stack_status
def poll_stack_status(stack_id, poll_time=5):
stack_status = get_stack_status(stack_id)
while stack_status != 'CREATE_COMPLETE':
if stack_status == 'CREATE_FAILED':
return 1
time.sleep(poll_time)
stack_status = get_stack_status(stack_id)
return 0
I worked it with below for now. It's not the best I think but satisfies what I need to do.
def stackStatus(status):
evntsdata = hc.events.list(stack_name)[0].to_dict()
event_handle = evntsdata['resource_status'].split("_")
event_handle = '_'.join(event_handle[1:])
if event_handle == 'IN_PROGRESS':
loopcontinue = True
while loopcontinue:
evntsdata = hc.events.list(stack_name)[0].to_dict()
event_handle = evntsdata['resource_status'].split("_")
event_handle = '_'.join(event_handle[1:])
if event_handle == 'COMPLETE':
loopcontinue = False
print(str(timestamp()) + status + " IS COMPLETED!")
elif event_handle == 'FAILED':
print("Failed")
exit(1)
else:
print(str(timestamp()) + status + " IN PROGRESS!")
time.sleep(5)
Call this function
stackStatus("DELETE")
stackStatus("CREATE")
stackStatus("UPDATE")

Re-using Queued Data in Python

I have a data stream that adds price information to a queue where it is consumed while processing an order. Could someone advise on how I could save the last price streamed and only send to the queue IF the price were (for instance) higher than the last, or vice versa?
Here is my code, thanks in advance for any help!
def stream_to_queue(self):
response = self.connect_to_stream()
if response.status_code != 200:
return
for line in response.iter_lines(1):
if line:
try:
msg = json.loads(line)
except Exception as e:
print "Caught exception when converting message into json\n" + str(e)
return
if msg.has_key("instrument") or msg.has_key("tick"):
print msg["tick"]
instrument = msg["tick"]["instrument"]
time = msg["tick"]["time"]
bid = msg["tick"]["bid"]
ask = msg["tick"]["ask"]
stop = msg["tick"]["ask"]
tev = TickEvent(instrument, time, bid, ask)
self.events_queue.put(tev)

Denormalising field in django

I'm finding it difficult to denormalise a field in a django model. I have:
class AnswerSet(models.Model):
title = models.CharField(max_length=255)
num_answers = models.PositiveIntegerField(editable=False, default=0)
answers = models.ManyToManyField(Answer, through='AnswerSetAnswer')
...
class AnswerSetAnswer(models.Model):
answer = models.ForeignKey(Answer)
answer_set = models.ForeignKey(AnswerSet)
...
I want num_answers to contain a count of the number of answers in the set.
If 5 answers are initially associated with the AnswerSet "Food" and I edit one so it becomes associated with the AnswerSet "Colours", how can I recalculate the number of answers in the AnswerSet with "Food"? All the signals only seem to send the new data so I can't just override the save method.
I've tried using the m2m_changed signal, but it never gets called when I edit relationships through the admin form.
Here's my code anyway:
def update_answer_set_num_answers(sender, **kwargs):
"""
Updates the num_answers field to reflect the number of answers
associated with this AnswerSet
"""
instance = kwargs.get('instance', False)
print "no instance" # never gets here
if not instance:
return
action = kwargs.get('action')
print "action: ", action
if (action != 'pre_remove' and action != 'pre_add' and action != 'clear'):
return
reverse = kwargs.get('reverse')
if reverse:
answer_set = instance.answer_set
else:
answer_set = instance.answer_set
num_answers = AnswerSetAnswer.objects.filter(answer_set=answer_set.id).count()
if (action == 'pre_remove'):
num_answers -= int(kwargs.get('pk_set'))
elif (action == 'pre_add'):
num_answers += int(kwargs.get('pk_set'))
elif (action == 'clear'):
num_answers = 0
answer_set.num_answers = num_answers
print 'n a: ', answer_set.num_answers
answer_set.save()
m2m_changed.connect(update_answer_set_num_answers, \
AnswerSet.answers.through, weak=False)
Do you really need to denormalise this? You can calculate it with a simple aggregate:
from django.db.models import Count
answersets = AnswerSet.objects.all().annotate(num_answers=Count('answers')