how to perform Rank query in Django - sql

I am stuck at this point and this query seems a bit tricky to me
so basically i am getting data from devices in periodic intervals 'lets say 3 hours'
But every device is sending data twice on every interval(3 hours), i am required to extract device data that is send last(in intervals)
"date": "2021-11-28",
"time": "12 : 35",
"meter": "70F8E7FFFE10C495",
"dr": 3,
"date_received": "2021-11-28T18:05:20.473430+05:30",
},
{
"id": 2,
"date": "2021-11-28",
"time": "12 : 37",
"meter": "70F8E7FFFE10C459",
"date_received": "2021-11-28T18:07:09.980403+05:30",
},
{
"id": 3,
"date": "2021-11-28",
"time": "12 : 37",
"meter": "70F8E7FFFE10C459", <---- this occurrence only
"dr": 3,
"date_received": "2021-11-28T18:07:11.533388+05:30",
},
{
"id": 4,
"date": "2021-11-28",
"time": "12 : 50",
"meter": "70F8E7FFFE10C463",
"date_received": "2021-11-28T18:20:44.197284+05:30",
},
{
"id": 5,
"date": "2021-11-28",
"time": "12 : 56",
"meter": "70F8E7FFFE10C47D",
"date_received": "2021-11-28T18:26:43.221316+05:30",
},
{
"id": 6,
"date": "2021-11-28",
"time": "12 : 56",
"meter": "70F8E7FFFE10C47D", <---- only want to get this occurrence in similar manner
"date_received": "2021-11-28T18:26:44.925292+05:30",
},
So if device : 70F8E7FFFE10C459 sends data twice i want to retrieve only last sent data , and this needs obj in queryset , i was told about RANK concept don't understand how to apply that here
In general i just want to know the query to apply, raw query will also work
class MeterData(models.Model):
meter = models.ForeignKey('dashboard.Meter',on_delete=models.SET_NULL,null=True)
customer = models.ForeignKey('Customer',on_delete=models.SET_NULL,null=True,blank=True)
battery = models.CharField(max_length=40,null=True,blank=True)
crc_status = models.CharField(max_length=43,null=True,blank=True)
Pulse_count = models.IntegerField(null=True,blank=False)
status_data = models.CharField(max_length=50,blank=True,null=True)
status_magnet_temper = models.BooleanField(default=False)
meter_reading = models.CharField(max_length=50,null=True,blank=True)
status_switch_temper = models.BooleanField(default=False)
status_voltage_level = models.CharField(max_length=10,null=True,blank=True)
uplink_type = models.CharField(max_length=50,null=True,blank=True)
confirmed_uplink = models.BooleanField(max_length=50,null=True,blank=True)
tx_info_frequency = models.CharField(max_length=50,null=True,blank=True)
dr = models.IntegerField(null=True,blank=True)
date_received = models.DateTimeField(auto_now_add=True,null=True)

It doesn't work on Sqlite
I think the following query is the correct answer.
MeterData.objects.filter(put_your_filters_here).order_by('-date_received').distinct('customer_id')
For SQLite DB use the below query:
from django.db.models import Subquery, OuterRef
distinct_subquery = Subquery(MeterData.objects.filter(put_your_filters_here, customer_id=OuterRef('customer_id')).order_by('-date_received').values('id')[:1])
MeterData.objects.filter(put_your_filters_here, id__in=distinct_subquery).order_by('-date_received')
if it doesn't work let me know.

Related

Adobe Analytics - how to get report API 2.0 to get multi-level breakdown data using Java

I need help in getting adobe-analytics data when multiple IDs are passed for multi-level breakdown using API 2.0.
I am getting first level data for V124 ----
"metricContainer": {
"metrics": [
{
"columnId": "0",
"id": "metrics/event113",
"sort": "desc"
}
]
},
"dimension": "variables/evar124",
but i want to use IDs returned in above call response to in second level breakdown of v124 to get v125 as---
"metricContainer": {
"metrics": [
{
"columnId": "0",
"id": "metrics/event113",
"sort": "desc",
"filters": [
"0"
]
}
],
"metricFilters": [
{
"id": "0",
"type": "breakdown",
"dimension": "variables/evar124",
"itemId": "2629267831"
},
{
"id": "1",
"type": "breakdown",
"dimension": "variables/evar124",
"itemId": "2629267832"
}
]
},
"dimension": "variables/evar125",
This always returns data only for 2629267831 ID and not 2629267832.
I want to get data for thousands of IDs (returned from first API call) in a single API call. What am i doing wrong?

Querying using query parameters in Postman

As a beginner to Postman, I am trying to use Query Parameters to search via filtering by keys. COnsider the following content of a certain endpoint.
[
{
"id": 1,
"name": "The Russian",
"type": "fiction",
"available": true
},
{
"id": 2,
"name": "Just as I Am",
"type": "non-fiction",
"available": false
}
]
1st scenario :
Doing a GET on the endpoint with the syntax {{baseURL}}/books?type=fiction, I get
[
{
"id": 1,
"name": "The Russian",
"type": "fiction",
"available": true
}
]
which is correct.
2nd scenario :
Doing a GET on the endpoint with the syntax {{baseURL}}/books?id=1, I get
[
{
"id": 1,
"name": "The Russian",
"type": "fiction",
"available": true
},
{
"id": 2,
"name": "Just as I Am",
"type": "non-fiction",
"available": false
}
]
which is not filtering by id = 1. It display id = 2 item as well. I was expecting it to show item base on id = 1 only.
AM I missing anything in understanding on how to use the query parameters ?

Get the value from the response based on a condition and store it to a variable

I would like to get the value from the response based on a condition and store it to a variable.
In the below JSON, I would like to store the value when the name matches to something I prefer. Is there a way to achieve this using Karate API?
{
"results": [
{
"name": "Sample1",
"email": "sample1#text.com",
"id": "U-123"
},
{
"name": "Sample2",
"email": "sample2#text.com",
"id": "U-456"
},
{
"name": "Sample3",
"email": "sample3#text.com",
"id": "U-789"
}
]
}
So after reading the comment, my interpretation is to "find" the id where name is Sample2. Easy, just use a filter() operation, refer the docs: https://github.com/karatelabs/karate#jsonpath-filters
Instead of using a filter, I'm using the JS Array find() method as a very concise example below:
* def response =
"""
{ "results": [
{ "name": "Sample1", "email": "sample1#text.com", "id": "U-123" },
{ "name": "Sample2", "email": "sample2#text.com", "id": "U-456" },
{ "name": "Sample3", "email": "sample3#text.com", "id": "U-789" }
]
}
"""
* def id = response.results.find(x => x.name == 'Sample2').id
* match id == 'U-456'
Take some time to understand how it works. Talk to someone who knows JS if needed.

how to convert the query result data to json format in kusto query language

I'm working on a logic app which will create a bug I have to display description in the work item like below:
{
"Description":
{
"Title":"",
"Validation step":"",
"OperationActivityId:":"",
"Environment detailsLink":"",
"Please click here":"",
"ErrorMessage":""
}
}
I'm Querying all above mentioned fields as result of query from Kusto(KQL) and getting all the required fields but I don't know how to convert it to make it Json.
Can anyone help me on this..?
Thanks in advance.
Here you go:
PackedRecordlet MyTable = datatable(Category:string, Value:long) [
"A", 1,
"A", 3,
"A", 5,
"B", 1,
"B", 2
];
MyTable
| extend PackedRecord = pack_all()
| summarize Result = make_list(PackedRecord)
This will output everything in a nice json:
[
{
"Category": "A",
"Value": 1
},
{
"Category": "A",
"Value": 3
},
{
"Category": "A",
"Value": 5
},
{
"Category": "B",
"Value": 1
},
{
"Category": "B",
"Value": 2
}
]
Explanation
pack_all() puts all the data from the columns of a record into a JSON
summarize make_list(...) takes all these JSONs, and creates a list (array) from them

Select game scores with a player's context from Postgres

I am developing a web app with rails and postgres as a shooting range game backend. It has an endpoint to save players scores, which writes the scores to DB and returns the first three places and the players' ranks with some context around it: a place before and a place after. For example if a player has rank 23, the method should return 1st, 2nd, 3rd places, the player rank itself and also two records with ranks 22 and 24 besides it. I don't store the rank in the DB it is calculated dynamically using following sql query:
SELECT RANK() OVER(ORDER BY score DESC) AS place, name, score
FROM scores
WHERE game_id=? AND rules_version=? AND game_mode=?
LIMIT 10
POST request with following data:
{
"players": [
{ "name": "Jack", "score": 100, "local_id": 1, "stats": {}},
{ "name": "Anna", "score": 200, "local_id": 2, "stats": {}}
]
}
should return following result set:
{
"scores": [
{
"name": "Piter",
"place": 1,
"score": 800
},
{
"name": "Lisa",
"place": 2,
"score": 700
},
{
"name": "Philip",
"place": 3,
"score": 600
},
{
"name": "Max",
"place": 25,
"score": 204
},
{
"name": "Anna",
"place": 26,
"score": 200,
"local_id": 2
},
{
"name": "Ashley",
"place": 27,
"score": 193
}
{
"name": "Norman",
"place": 36,
"score": 103
},
{
"name": "Jack",
"place": 37,
"score": 100,
"local_id": 1
},
{
"name": "Chris",
"place": 38,
"score": 95
}
]
}
Every field except local_id and as I said place is stored in DB. I can't figure out the right sql query to do such select. Please help.