SQL Query to Node Red Line Chart - sql

I am attempting to query data out of SQL Server and display it as a line chart in Node Red.
My Data from SQL looks like
1556029184000 0.0675168918918922
1556029139000 0.0675515463917528
1556029079000 0.0679347826086958
1556029019000 0.0674082568807342
1556028959000 0.0674431818181822
1556028898000 0.0675537634408605
1556028838000 0.0673611111111115
1556028779000 0.0675917431192663
1556028719000 0.06744212962963
1556028659000 0.0673148148148151
Left column is a timestamp converted to Epoch and right column is the value to plot.
Node red debug shows this:
[{"x":"1556029788000","y":0.06772222222222232},
{"x":"1556029738000","y":0.06855053191489367},
{"x":"1556029678000","y":0.06858333333333343},
{"x":"1556029619000","y":0.06751146788990835},
{"x":"1556029559000","y":0.06805180180180205},
{"x":"1556029499000","y":2.714885321100926},
{"x":"1556029439000","y":11.43350290697674},
{"x":"1556029378000","y":6.6709253246753235},
{"x":"1556029319000","y":0.06748842592592619},
{"x":"1556029259000","y":0.06760714285714318}]
Nothing is displayed in the chart. All help is appreciated.

The node-red-dashboard sidebar help has a link to the details the format for passing data to the chart node.
What you currently have is a msg.payload that contains an array of objects with x & y values. These need to be moved to the msg.payload.data field as described:
[{
"series": ["A", "B", "C"],
"data": [
[{ "x": 1504029632890, "y": 5 },
{ "x": 1504029636001, "y": 4 },
{ "x": 1504029638656, "y": 2 }
],
[{ "x": 1504029633514, "y": 6 },
{ "x": 1504029636622, "y": 7 },
{ "x": 1504029639539, "y": 6 }
],
[{ "x": 1504029634400, "y": 7 },
{ "x": 1504029637959, "y": 7 },
{ "x": 1504029640317, "y": 7 }
]
],
"labels": [""]
}]

Related

JSON SQL column in azure data factory

I have a JSON type SQL column in SQL table as below example. I want the below code to be converted into separate columns such as drugs as table name and other attribute as column name, how can I use adf or any other means please guide. The below code is a single column in a table called report where I need to convert this into separate columns .
{
"drugs": {
"Codeine": {
"bin": "Y",
"name": "Codeine",
"icons": [
93,
103
],
"drug_id": 36,
"pathway": {
"code": "prodrug",
"text": "is **inactive**, its metabolites are active."
},
"targets": [],
"rxnorm_id": "2670",
"priclasses": [
"Analgesic/Anesthesiology"
],
"references": [
1,
16,
17,
100
],
"subclasses": [
"Analgesic agent",
"Antitussive agent",
"Opioid agonist",
"Phenanthrene "
],
"metabolizers": [
"CYP2D6"
],
"phenotype_ids": {
"metabolic": "5"
},
"relevant_genes": [
"CYP2D6"
],
"dosing_guidelines": [
{
"text": "Reduced morphine formation. Use label recommended age- or weight-specific dosing. If no response, consider alternative analgesics such as morphine or a non-opioid.",
"source": "CPIC",
"guidelines_id": 1
},
{
"text": "Analgesia: select alternative drug (e.g., acetaminophen, NSAID, morphine-not tramadol or oxycodone) or be alert to symptoms of insufficient pain relief.",
"source": "DPWG",
"guidelines_id": 362
}
],
"drug_report_notes": [
{
"text": "Predicted codeine metabolism is reduced.",
"icons_id": 58,
"sort_key": 58,
"references_id": null
},
{
"text": "Genotype suggests a possible decrease in exposure to the active metabolite(s) of codeine.",
"icons_id": 93,
"sort_key": 56,
"references_id": null
},
{
"text": "Professional guidelines exist for the use of codeine in patients with this genotype and/or phenotype.",
"icons_id": 103,
"sort_key": 50,
"references_id": null
}
]
}
Since this json is already in a SQL column, you don't need ADF to break it down to parts. You can use JSON functions in SQL server to do that.
example of few first columns:
declare #json varchar(max) = '{
"drugs": {
"Codeine": {
"bin": "Y",
"name": "Codeine",
"icons": [
93,
103
],
"drug_id": 36,
"pathway": {
"code": "prodrug",
"text": "is **inactive**, its metabolites are active."
},
"targets": [],
"rxnorm_id": "2670",
"priclasses": [
"Analgesic/Anesthesiology"
],
"references": [
1,
16,
17,
100
],
"subclasses": [
"Analgesic agent",
"Antitussive agent",
"Opioid agonist",
"Phenanthrene "
],
"metabolizers": [
"CYP2D6"
],
"phenotype_ids": {
"metabolic": "5"
},
"relevant_genes": [
"CYP2D6"
],
"dosing_guidelines": [
{
"text": "Reduced morphine formation. Use label recommended age- or weight-specific dosing. If no response, consider alternative analgesics such as morphine or a non-opioid.",
"source": "CPIC",
"guidelines_id": 1
},
{
"text": "Analgesia: select alternative drug (e.g., acetaminophen, NSAID, morphine-not tramadol or oxycodone) or be alert to symptoms of insufficient pain relief.",
"source": "DPWG",
"guidelines_id": 362
}
],
"drug_report_notes": [
{
"text": "Predicted codeine metabolism is reduced.",
"icons_id": 58,
"sort_key": 58,
"references_id": null
},
{
"text": "Genotype suggests a possible decrease in exposure to the active metabolite(s) of codeine.",
"icons_id": 93,
"sort_key": 56,
"references_id": null
},
{
"text": "Professional guidelines exist for the use of codeine in patients with this genotype and/or phenotype.",
"icons_id": 103,
"sort_key": 50,
"references_id": null
}
]
}
}
}
select JSON_VALUE(JSON_QUERY(#json,'$.drugs.Codeine'),'$.bin') as bin,
JSON_VALUE(JSON_QUERY(#json,'$.drugs.Codeine'),'$.name') as name,
JSON_VALUE(JSON_QUERY(#json,'$.drugs.Codeine'),'$.drug_id') as drug_id,
JSON_VALUE(JSON_QUERY(#json,'$.drugs.Codeine'),'$.icons[0]') as icon_1
'
You need to decide how to handle arrays, such as icons, where there are multiple values inside the same element.
References:
JSON_QUERY function
JSON_VALUE function

Ramda - How to sort array with nested object with string to number conversion

I am trying to sort the array which looks like this:
const testArray = [
{
"id": "1",
"nodeLayout": {
"x": "12.0",
"y": "1.0",
"width": 200,
"height": 87
}
},
{
"id": "2",
"nodeLayout": {
"x": "1.0",
"y": "1.0",
"width": 200,
"height": 87
}
},
{
"id": "3",
"nodeLayout": {
"x": "0.0",
"y": "1.0",
"width": 200,
"height": 87
}
}
]
I was trying to sort it with this:
R.pipe(
R.pluck('nodeLayout'),
R.map(R.pipe(R.props(['x']), R.join(','))),
R.sortWith([R.ascend(parseFloat)])
)(testArray)
Which is working fine but I am getting only x values sorted and I am not able to fit this sort into this one:
R.pipe(
R.filter(
R.allPass([
R.pathEq(['nodeLayout', 'y'], '1.0'),
R.propEq('group', 4)
]
))
// I tried to add it here: R.sortBy(R.path(['nodeLayout', 'x'])) but I need to parse string first and I have no idea how to combine those
)(testArray)
To sum up, I am trying to get the whole object with all properties sorted.
Any help would be appreciated. Thank you
If you combine your two approaches, you should be able to achieve what you're wanting.
The main thing being that you can R.pipe the call to parseFloat after obtaining the value you'd like to sort with R.path. This piped function can be provided as the argument to R.sortBy.
R.pipe(
R.filter(
R.allPass([
R.pathEq(['nodeLayout', 'y'], '1.0'),
R.propEq('group', 4)
]
)),
R.sortBy(R.pipe(R.path(['nodeLayout', 'x']), parseFloat))
)(testArray)

How to filter Cosmos DB data based on value of an element in an array of values Using SQL API

I have a cosmosDB collection with below Data in it.
I have to find out the data only for EVENT named ABC and its value using SQL query.
[
{
"ID": "01XXXXX",
"EVENTS": [
{
"Name": "ABC",
"Value": 0
},
{
"Name": "XYZ",
"Value": 4
},
{
"Name": "PQR",
"Value": 5
}
]
},
{
"ID": "02XXXXX",
"EVENTS": [
{
"Name": "ABC",
"Value": 1
},
{
"Name": "XYZ",
"Value": 2
},
{
"Name": "PQR",
"Value": 3
}
]
}
]
I have tried the below code but it is not working since EVENT is an array.
SELECT * FROM c where c.EVENTS.Name = 'ABC'
Is there any way to find filter out the data only with Event Name as ABC using SQL?
Try using join
SELECT c FROM c
join l in c.EVENTS
where l.Name = 'ABC'

How to visualize array of objects in Kibana?

I have a few documents of the below format.
Document1:
{
"_index": "myIndex",
"preferenceCount": [
{
"name": "apple",
"count": 1
},
{
"name": "mango",
"count": 1
},
{
"name": "apple",
"count": 1
}
]
}
Document2:
{
"_index": "myIndex",
"preferenceCount": [
{
"name": "mango",
"count": 1
},
{
"name": "mango",
"count": 1
},
{
"name": "orange",
"count": 1
}
]
}
I want to visualise this data aggregated in such a way that I get the below graph (sorry for not uploading picture)
apple: 2 (sum of count for name = apple across documents in time range)
mango: 3
orange: 1
I tried
sum(preferenceCount.count) groupBy (preferenceCount.name.keyword)
But that sums count across documents and displays below graph
apple: 3
mango: 6
orange: 3
Please let me know how might I achieve this.
Thanks!
I don't know kibana but in Vega-Lite, you can extract data from a property:
{
"data": {
"property": "preferenceCount",
"type": "json",
...
}
}

vega stacked bar syntax for "y2" field in properties

Can someone explain to me the following line in the vincent docs for a stacked bar:
https://github.com/wrobstory/vincent/blob/master/examples/stacked_bar_examples.py
y2=ValueRef(field='y2', scale='y')
I don't see any field called "y2" in the data set so I am confused as to where it is coming from
The y2 field is generated by the Vega stack transform (code here).
In Vega, The rect mark can be defined by y+y2 or y+height. See Marks#Shared Visual Properties in Vega's docs:
For marks involving Cartesian extents (e.g., rect marks), the
horizontal dimensions are determined by (in order of precedence) the x
and x2 properties, the x and width properties, and the x2 and width
properties. If all three of x, x2 and width are specified, the width
value is ignored. The y, y2 and height properties are treated
similarly.
Check out the stacked bar demo in the Vega Live Editor, which includes:
...
"marks": [
{
"type": "rect",
"properties": {
"enter": {
"x": {"scale": "x", "field": "data.x"},
"width": {"scale": "x", "band": true, "offset": -1},
"y": {"scale": "y", "field": "y"},
"y2": {"scale": "y", "field": "y2"},
"fill": {"scale": "color", "field": "data.c"}
},
"update": {
"fillOpacity": {"value": 1}
},
"hover": {
"fillOpacity": {"value": 0.5}
}
}
}
]
...
Try changing y2 with height in the live editor.