Query Optimization and profiling for celery tasks

2021-07-24 Thread Hamidreza Mahmoodian

Hello everyone

Django-debug-toolbar and Django-silk are two famous tools for profiling 
HTTP requests. Is it possible to analyze a celery task or any other 
function inside the code? for instance, I have a celery task like this:

def main_task(): 
while _condition: 
logic() 
sleep(_t) 
return True

I need some information like duplicate and similar queries in logic() for 
every loop.
Unfortunately no one answered similar question to me in Stackoverflow.
thanks






-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7ee5aa4a-9860-45b6-ab50-76bad4a668cdn%40googlegroups.com.


Re: complex math calculation from query set # SOLVED

2021-07-24 Thread mab.mo...@gmail.com
Thank you Derek for the information. For those that are having similar 
problems this is the solution that worked for me based on Derek's lead

## METHOD 1 ##

# view

def position_view(request):

packet_data = TncData.objects.exclude(lat=0).order_by('-dt_heard')[:100]

# loop through query set and add new dictionary key-value pair

for row in packet_data:

row.distance = row.lat*row.lon # example only of calculation  only 
not real 

return render(request, 'main/position.html', 
{'packet_data':packet_data,})

# html template snippet

{% for row in packet_data %}


{{row.callsign}}
{{row.lat|floatformat:2}} N 
{{row.lon|floatformat:2}} W 
{{row.distance|floatformat:1}} miles
{{row.bearing|floatformat:0}} deg
{{row.dt_heard|date:"D 
m/d H:i"}}


{% endfor %}

## METHOD 2 use @property in the model.py file 

# Put the data calculations in the model.py file

class TncData(models.Model):
callsign = models.TextField(max_length=20)
lat = models.FloatField(null=True, blank=True, default=None)
lon = models.FloatField(null=True, blank=True, default=None)
path = models.TextField(max_length=250)
message = models.TextField(max_length=250)
dt_heard = models.DateTimeField(auto_now_add=False)

def __str__(self):
return str(self.callsign)

## HAVERSINE FORMULA

@property
def calculate_distance(self):

# Variables

r = 6371 # radius earth km

lat_1 = radians(42.97)
lon_1 = radians(89.77)

lat_2 = radians(self.lat)
lon_2 = radians(self.lon)

dlat = lat_1 - lat_2
dlon = lon_1 - lon_2

a = sin(dlat / 2)**2 + cos(lat_1) * cos(lat_2) * sin(dlon / 2)**2

c = 2 * asin(sqrt(a))

distance_km = c*r
distance = distance_km * 0.621

return(distance)

# html template snippet

{% for row in packet_data %}


{{row.callsign}}
{{row.lat|floatformat:2}} N 
{{row.lon|floatformat:2}} W 
{{*row.calculate_distance|*floatformat:1}} 
miles
{{row.bearing|floatformat:0}} deg
{{row.dt_heard|date:"D 
m/d H:i"}}



{% endfor %}


On Wednesday, July 21, 2021 at 5:01:02 PM UTC-5 mab.mo...@gmail.com wrote:

> Thanks Derek. I'll give it a try
>
> On Wednesday, July 21, 2021 at 8:55:08 AM UTC-5 Derek wrote:
>
>> See:  
>> https://stackoverflow.com/questions/54412377/adding-values-to-django-queryset
>>  
>>
>> VIEW
>>
>> def position_view(request):
>>
>> packet_data = 
>> TncData.objects.exclude(lat=0).order_by('-dt_heard')[:100]
>> for packet in packet_data:
>> *# my complex math calculation*
>> *packet.distance = "# complex math formula using packet.lat and 
>> packet.lon"*
>>
>> return render(
>> request, 
>> 'main/position.html',
>> {'packet_data': packet_data, 'distance': distance})
>>
>> TEMPLATE
>>
>> *# suggest you use a better term than "field" => maybe "record"?*
>> {% for field in packet_data %}
>> 
>> {{field.callsign}}
>> {{field.lat|floatformat:2}} N 
>> {{field.lon|floatformat:2}} W 
>> 
>> *{{field.distance}}*
>>
>>
>> HTH!
>>
>> On Tuesday, 20 July 2021 at 22:25:04 UTC+2 mab.mo...@gmail.com wrote:
>>
>>> Hello,
>>>
>>> I am trying to perform a complex math calculation from a django query 
>>> set. How can I pass the results to the template for display? Annotations 
>>> won't work since the math is not a simple sum, diff or average. 
>>>
>>> MODEL
>>>
>>> class TncData(models.Model):
>>> callsign = models.TextField(max_length=20)
>>> lat = models.FloatField(null=True, blank=True, default=None)
>>> lon = models.FloatField(null=True, blank=True, default=None)
>>> path = models.TextField(max_length=250)
>>> message = models.TextField(max_length=250)
>>> dt_heard = models.DateTimeField(auto_now_add=False)
>>>
>>> def __str__(self):
>>> return str(self.callsign)
>>>
>>> VIEW
>>>
>>> def position_view(request):
>>>
>>> packet_data = 
>>> TncData.objects.exclude(lat=0).order_by('-dt_heard')[:100]
>>>
>>> *# my complex math calculation*
>>> * # need to do something like append distance to packet_data like 
>>> packet_data.distance*
>>>
>>> *distance = "# complex math formula using packet_data.lat and 
>>> packet_data.lon"*
>>>
>>> return render(request, 'main/position.html', 
>>> {'packet_data':packet_data,})
>>>
>>> TEMPLATE
>>>
>>> {% for field in packet_data %}
>>>
>>> 
>>> {{field.callsign}}
>>> {{field.lat|floatformat:2}} N 
>>> {{field.lon|floatformat:2}} W 
>>> 
>>> *{{packet_data.distance}}*
>>> {{field.dt_heard|date:"D m/d H:i"}}
>>> 
>>>
>>> {% endfor %}
>>>  
>>>
>>>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the w

Re: complex math calculation from query set # SOLVED

2021-07-24 Thread esteem learning center
If you want to do some basic django calculations, you can use
django-mathfilters is simple to use for add, subtract, mul, and div

On Sat, Jul 24, 2021, 3:33 PM mab.mo...@gmail.com 
wrote:

> Thank you Derek for the information. For those that are having similar
> problems this is the solution that worked for me based on Derek's lead
>
> ## METHOD 1 ##
>
> # view
>
> def position_view(request):
>
> packet_data =
> TncData.objects.exclude(lat=0).order_by('-dt_heard')[:100]
>
> # loop through query set and add new dictionary key-value pair
>
> for row in packet_data:
>
> row.distance = row.lat*row.lon # example only of calculation  only
> not real
>
> return render(request, 'main/position.html',
> {'packet_data':packet_data,})
>
> # html template snippet
>
> {% for row in packet_data %}
>
> 
> {{row.callsign}}
> {{row.lat|floatformat:2}} N
> {{row.lon|floatformat:2}} W 
>  style='text-align:right;'>{{row.distance|floatformat:1}} miles
>  style='text-align:right;'>{{row.bearing|floatformat:0}} deg
> {{row.dt_heard|date:"D
> m/d H:i"}}
> 
>
> {% endfor %}
>
> ## METHOD 2 use @property in the model.py file
>
> # Put the data calculations in the model.py file
>
> class TncData(models.Model):
> callsign = models.TextField(max_length=20)
> lat = models.FloatField(null=True, blank=True, default=None)
> lon = models.FloatField(null=True, blank=True, default=None)
> path = models.TextField(max_length=250)
> message = models.TextField(max_length=250)
> dt_heard = models.DateTimeField(auto_now_add=False)
>
> def __str__(self):
> return str(self.callsign)
>
> ## HAVERSINE FORMULA
>
> @property
> def calculate_distance(self):
>
> # Variables
>
> r = 6371 # radius earth km
>
> lat_1 = radians(42.97)
> lon_1 = radians(89.77)
>
> lat_2 = radians(self.lat)
> lon_2 = radians(self.lon)
>
> dlat = lat_1 - lat_2
> dlon = lon_1 - lon_2
>
> a = sin(dlat / 2)**2 + cos(lat_1) * cos(lat_2) * sin(dlon / 2)**2
>
> c = 2 * asin(sqrt(a))
>
> distance_km = c*r
> distance = distance_km * 0.621
>
> return(distance)
>
> # html template snippet
>
> {% for row in packet_data %}
>
> 
> {{row.callsign}}
> {{row.lat|floatformat:2}} N
> {{row.lon|floatformat:2}} W 
> {{
> *row.calculate_distance|*floatformat:1}} miles
>  style='text-align:right;'>{{row.bearing|floatformat:0}} deg
> {{row.dt_heard|date:"D
> m/d H:i"}}
> 
>
>
> {% endfor %}
>
>
> On Wednesday, July 21, 2021 at 5:01:02 PM UTC-5 mab.mo...@gmail.com wrote:
>
>> Thanks Derek. I'll give it a try
>>
>> On Wednesday, July 21, 2021 at 8:55:08 AM UTC-5 Derek wrote:
>>
>>> See:
>>> https://stackoverflow.com/questions/54412377/adding-values-to-django-queryset
>>>
>>>
>>> VIEW
>>>
>>> def position_view(request):
>>>
>>> packet_data =
>>> TncData.objects.exclude(lat=0).order_by('-dt_heard')[:100]
>>> for packet in packet_data:
>>> *# my complex math calculation*
>>> *packet.distance = "# complex math formula using packet.lat and
>>> packet.lon"*
>>>
>>> return render(
>>> request,
>>> 'main/position.html',
>>> {'packet_data': packet_data, 'distance': distance})
>>>
>>> TEMPLATE
>>>
>>> *# suggest you use a better term than "field" => maybe "record"?*
>>> {% for field in packet_data %}
>>> 
>>> {{field.callsign}}
>>> {{field.lat|floatformat:2}} N
>>> {{field.lon|floatformat:2}} W 
>>> 
>>> *{{field.distance}}*
>>>
>>>
>>> HTH!
>>>
>>> On Tuesday, 20 July 2021 at 22:25:04 UTC+2 mab.mo...@gmail.com wrote:
>>>
 Hello,

 I am trying to perform a complex math calculation from a django query
 set. How can I pass the results to the template for display? Annotations
 won't work since the math is not a simple sum, diff or average.

 MODEL

 class TncData(models.Model):
 callsign = models.TextField(max_length=20)
 lat = models.FloatField(null=True, blank=True, default=None)
 lon = models.FloatField(null=True, blank=True, default=None)
 path = models.TextField(max_length=250)
 message = models.TextField(max_length=250)
 dt_heard = models.DateTimeField(auto_now_add=False)

 def __str__(self):
 return str(self.callsign)

 VIEW

 def position_view(request):

 packet_data =
 TncData.objects.exclude(lat=0).order_by('-dt_heard')[:100]

 *# my complex math calculation*
 * # need to do something like append distance to packet_data like
 packet_data.distance*

 *distance = "# complex math formula using packet_data.lat and
 packet_data.lon"*

 return render(request, 'main/position.html',
 {'packet_data':packet_data,})

 TEMPLATE

 {% for field in packet_data %}

 
 {{field.callsign}}
>>>

Re: complex math calculation from query set # SOLVED

2021-07-24 Thread Franck Tchouanga
Guys I got a good place to manage all your django and good enough developer
and designers for your project just check out the below link to get it all

https://bit.ly/3eUJJcX

On Sat, Jul 24, 2021 at 7:36 PM esteem learning center <
ahmeddauda.dr...@gmail.com> wrote:

> If you want to do some basic django calculations, you can use
> django-mathfilters is simple to use for add, subtract, mul, and div
>
> On Sat, Jul 24, 2021, 3:33 PM mab.mo...@gmail.com 
> wrote:
>
>> Thank you Derek for the information. For those that are having similar
>> problems this is the solution that worked for me based on Derek's lead
>>
>> ## METHOD 1 ##
>>
>> # view
>>
>> def position_view(request):
>>
>> packet_data =
>> TncData.objects.exclude(lat=0).order_by('-dt_heard')[:100]
>>
>> # loop through query set and add new dictionary key-value pair
>>
>> for row in packet_data:
>>
>> row.distance = row.lat*row.lon # example only of calculation
>> only not real
>>
>> return render(request, 'main/position.html',
>> {'packet_data':packet_data,})
>>
>> # html template snippet
>>
>> {% for row in packet_data %}
>>
>> 
>> {{row.callsign}}
>> {{row.lat|floatformat:2}} N
>> {{row.lon|floatformat:2}} W 
>> > style='text-align:right;'>{{row.distance|floatformat:1}} miles
>> > style='text-align:right;'>{{row.bearing|floatformat:0}} deg
>> {{row.dt_heard|date:"D
>> m/d H:i"}}
>> 
>>
>> {% endfor %}
>>
>> ## METHOD 2 use @property in the model.py file
>>
>> # Put the data calculations in the model.py file
>>
>> class TncData(models.Model):
>> callsign = models.TextField(max_length=20)
>> lat = models.FloatField(null=True, blank=True, default=None)
>> lon = models.FloatField(null=True, blank=True, default=None)
>> path = models.TextField(max_length=250)
>> message = models.TextField(max_length=250)
>> dt_heard = models.DateTimeField(auto_now_add=False)
>>
>> def __str__(self):
>> return str(self.callsign)
>>
>> ## HAVERSINE FORMULA
>>
>> @property
>> def calculate_distance(self):
>>
>> # Variables
>>
>> r = 6371 # radius earth km
>>
>> lat_1 = radians(42.97)
>> lon_1 = radians(89.77)
>>
>> lat_2 = radians(self.lat)
>> lon_2 = radians(self.lon)
>>
>> dlat = lat_1 - lat_2
>> dlon = lon_1 - lon_2
>>
>> a = sin(dlat / 2)**2 + cos(lat_1) * cos(lat_2) * sin(dlon / 2)**2
>>
>> c = 2 * asin(sqrt(a))
>>
>> distance_km = c*r
>> distance = distance_km * 0.621
>>
>> return(distance)
>>
>> # html template snippet
>>
>> {% for row in packet_data %}
>>
>> 
>> {{row.callsign}}
>> {{row.lat|floatformat:2}} N
>> {{row.lon|floatformat:2}} W 
>> {{
>> *row.calculate_distance|*floatformat:1}} miles
>> > style='text-align:right;'>{{row.bearing|floatformat:0}} deg
>> {{row.dt_heard|date:"D
>> m/d H:i"}}
>> 
>>
>>
>> {% endfor %}
>>
>>
>> On Wednesday, July 21, 2021 at 5:01:02 PM UTC-5 mab.mo...@gmail.com
>> wrote:
>>
>>> Thanks Derek. I'll give it a try
>>>
>>> On Wednesday, July 21, 2021 at 8:55:08 AM UTC-5 Derek wrote:
>>>
 See:
 https://stackoverflow.com/questions/54412377/adding-values-to-django-queryset


 VIEW

 def position_view(request):

 packet_data =
 TncData.objects.exclude(lat=0).order_by('-dt_heard')[:100]
 for packet in packet_data:
 *# my complex math calculation*
 *packet.distance = "# complex math formula using packet.lat and
 packet.lon"*

 return render(
 request,
 'main/position.html',
 {'packet_data': packet_data, 'distance': distance})

 TEMPLATE

 *# suggest you use a better term than "field" => maybe "record"?*
 {% for field in packet_data %}
 
 {{field.callsign}}
 {{field.lat|floatformat:2}} N
 {{field.lon|floatformat:2}} W 
 
 *{{field.distance}}*


 HTH!

 On Tuesday, 20 July 2021 at 22:25:04 UTC+2 mab.mo...@gmail.com wrote:

> Hello,
>
> I am trying to perform a complex math calculation from a django query
> set. How can I pass the results to the template for display? Annotations
> won't work since the math is not a simple sum, diff or average.
>
> MODEL
>
> class TncData(models.Model):
> callsign = models.TextField(max_length=20)
> lat = models.FloatField(null=True, blank=True, default=None)
> lon = models.FloatField(null=True, blank=True, default=None)
> path = models.TextField(max_length=250)
> message = models.TextField(max_length=250)
> dt_heard = models.DateTimeField(auto_now_add=False)
>
> def __str__(self):
> return str(self.callsign)
>
> VIEW
>
> def position_view(request):
>
> packet_data =
> TncData.objects.exclude(lat=0).order_by('-dt_heard')[:10