Azure Cosmos DB aggregation over nested structure - azure

I have an azure cosmos DB with documents as below
{
"id": "id_1",
"location": "location_1",
"colorCounts": {
"red" : 1,
"blue": 0,
"yellow": 1
}
},
{
"id": "id_2",
"location": "location_1",
"colorCounts": {
"red" : 0,
"blue": 0,
"yellow": 0
}
}
and want make a query that groups the results by location while averaging all the values in colorCounts. My result would look like this:
{
"location": "location_1",
"colorCounts": {
"red" : 0.5,
"blue": 0,
"yellow": 0.5
}
}
When I try to average over colorCounts:
SELECT c.id, c.location, AVG(c.colorCounts) FROM c GROUP BY c.location
I do not get any color counts. I can average over single colors, but I do not know how to average over the nested object colorCounts.

Script:
select a.location,{"red" : a.red, "blue": a.blue
,"yellow": a.yellow} as colorCounts FROM
(SELECT c.location,avg(c.colorCounts.red) as red,
avg(c.colorCounts.blue) as blue,
avg(c.colorCounts.yellow) as yellow FROM c
GROUP by c.location)a
I tried to repro this with the same sample input and got the required output.
Output:
[
{
"location": "location_1",
"colorCounts": {
"red": 0.5,
"blue": 0,
"yellow": 0.5
}
}
]

Related

Distinct count of multiple fields values using mongodb aggregation

I'm trying to count distinct values of multiple fields By one MongoDB Aggregation query.
So here's my data:
{
"_id":ObjectID( "617b0dbacda6cbd1a0403f68")
"car_type": "suv",
"color": "red",
"num_doors": 4
},
{
"_id":ObjectID( "617b0dbacda6cbd1a04078df")
"car_type": " suv ",
"color": "blue",
"num_doors": 4
},
{
"_id":ObjectID( "617b0dbacda6cbd1a040ld45")
"car_type": "wagon",
"color": "red",
"num_doors": 4
},
{
"_id":ObjectID( "617b0dbacda6cbd1a0403dcd")
"car_type": "suv",
"color": "blue",
"num_doors": 4
},
{
"_id":ObjectID( "617b0dbacda6cbd1a0403879")
"car_type": " wagon ",
"color": "red",
"num_doors": 4
},
{
"_id":ObjectID( "617b0dbacda6cbd1a0405478")
"car_type": "wagon",
"color": "red",
"num_doors": 4
}
I want a distinct count of each color by car_type:
"car_type": "suv"
"red":2,
"blue":2
iwas able to distinct and cound all colors but i couldnt distinct them by car_type
Query
group specific first (cartype+color), to count the same colors
group less specific after (cartype), to get all colors/count for each car_type
project to fix structure and $arrayToObject to make the colors keys and the the count values
*query assumes that " wagon " was typing mistake(the extra spaces i mean), if your collection has those problems, use $trim to clear the database from those.
*query is updated to include the sum also, from the comment
Test code here
aggregate(
[{"$group":
{"_id": {"car_type": "$car_type", "color": "$color"},
"count": {"$sum": 1}}},
{"$group":
{"_id": "$_id.car_type",
"colors": {"$push": {"k": "$_id.color", "v": "$count"}}}},
{"$set": {"sum": {"$sum": "$colors.v"}}},
{"$project":
{"_id": 0,
"sum": 1,
"car_type": "$_id",
"colors": {"$arrayToObject": ["$colors"]}}},
{"$replaceRoot": {"newRoot": {"$mergeObjects": ["$colors", "$$ROOT"]}}},
{"$project": {"colors": 0}}])

Algolia: Filter if attribute exists

For the below data
[
{
"name": "iPhone XR Black 64GB",
"color": "red"
},
{
"name": "iPhone XS Gold 64GB",
"color": "blue"
},
{
"name": "Galaxy Note9 Ocean Blue 128GB",
},
{
"name": "G7 ThinQ™ Platinum Gray 64GB",
},
{
"name": "Moto E5 Play 16GB",
}
]
If I filter color:red it should return the records matching the following criterias.
If the color attribute exists it should be red.
If the color attribute doesn't exist.
Output would be
[
{
"name": "iPhone XR Black 64GB",
"color": "red"
},
{
"name": "Galaxy Note9 Ocean Blue 128GB",
},
{
"name": "G7 ThinQ™ Platinum Gray 64GB",
},
{
"name": "Moto E5 Play 16GB",
}
]
Click here for flow chart
Algolia is a search engine, meaning that it returns matching records as is. It doesn't perform any transformations, and you can't inject logic into the engine's behavior.
With your above dataset, if you run an empty search ('') with a filter "color:red", Algolia will only return the following record:
{
"name": "iPhone XR Black 64GB",
"color": "red"
}
If you want all records (in the limit of 1,000 records), you need to perform an empty query with no filters.

Circles of different radius based on other values in d3

I am trying to create nodes whose radius depend on the amount value in the link array. The data structure is as shown below:
{ "nodes": [{"id": "site01", "x": 317.5, "y": 282.5},
{"id": "site02", "x": 112, "y": 47},
{"id": "site03", "x": 69.5,"y": 287},
{"id": "site04", "x": 424.5, "y": 99.5},
],
"links": [
{"node01": "site01", "node02": "site04", "amount": 10},
{"node01": "site03", "node02": "site02", "amount": 120},
{"node01": "site01", "node02": "site03", "amount": 50},
{"node01": "site04", "node02": "site02", "amount": 80}]
}
The radius must be proportional to the total amount. For example, site01 must be of radius 60 while site02, 200. I've tried few codes but it doesn't seem to work.
var nodeRadius = {};
data.links.forEach(function (n){
nodeRadius[n.id] = n;
});
nodes.attr("r", function (d) {return nodeRadius[d.node01].amount;})
Please suggest a working code.

Change font for particular text via script in Photoshop

Looking for a script or action for changing font face for a particular word or text in a paragraph.
I have multiple .psd files (80+) where I need to change font for a specific text say "Hello" from Arial to Tahoma and also make it bold.
Really appreciate a help!
You could try "jam" framework. There is class for manipulating with text. http://www.tonton-pixel.com/JSON%20Action%20Manager/jsDoc/symbols/jamText.html
You are looking for textStyleRange.
var text = "Bonjour !";
var layerText =
{
"layerText":
{
"textKey": text,
"textClickPoint": { "horizontal": 50, "vertical": 95 },
"antiAlias": "antiAliasCrisp",
"textShape":
[
{ "textType": "point", "orientation": "horizontal" }
],
"textStyleRange":
[
{
"from": 0,
"to": text.length,
"textStyle":
{
"fontPostScriptName": "Myriad-Italic",
"size": 288,
"color": { "red": 144, "green": 0, "blue": 255 }
}
}
],
"paragraphStyleRange":
[
{
"from": 0,
"to": text.length,
"paragraphStyle": { "alignment": "center" }
}
]
},
"typeUnit": "pixelsUnit"
};
jamText.setLayerText (layerText);
You need:
- create loop which checks all layers
- read one text layer
- find on which character index substring starts, where is end
- apply jamText.setLayerText
- read next layer
It goes also without framework. The structure is analogous. The code will be uglier.

Amcharts: Column chart color

I followed this code http://jsfiddle.net/amcharts/w8Bcy/ to create my own column chart. I succed but now i want to improve that charts with other options.
Actual chart def is :
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"theme": "none",
"dataProvider": chartData,
"valueAxes": [{
"gridColor": "#FFFFFF",
"gridAlpha": 0.2,
"dashLength": 0
}],
"gridAboveGraphs": true,
"startDuration": 1,
"graphs": [{
"balloonText": "[[category]]: <b>[[value]]</b>",
"fillAlphas": 0.8,
"lineAlpha": 0.2,
"type": "column",
"valueField": "visits"
}],
"chartCursor": {
"categoryBalloonEnabled": false,
"cursorAlpha": 0,
"zoomable": false
},
"categoryField": "country",
"categoryAxis": {
"gridPosition": "start",
"gridAlpha": 0
},
"exportConfig": {
"menuTop": 0,
"menuItems": [{
"icon": '/lib/3/images/export.png',
"format": 'png'
}]
}
});
I have multiple column with some values, and i want the column to have a color based on their values. How i can change column color with value?
Example: Under x, column will be red .
Above x, column will be green .
I also want to trace a line with the reference value.
Thanks.
graph.colorField = "color"
and in data add field
"color" : "your color hex code "
its done check at http://jsfiddle.net/w8Bcy/68/ i have color all of them red , you can add color of your choice when your generating your data / json

Resources