NGRX entity adapter - how to update/upsert many nested properties - ngrx

I am trying to update many entities using adapter.updateMany().
I have Groups entity that looks like this (There are multiple groups with multiple children):
{
"id": 1,
"children": [
{
"id": 222,
"width": null,
"height": null,
...multipleOtherProperties
}
]
}
I am trying to update child width and height inside the group with:
const groups = [
{
"id": 0,
"children": [
{
"id": 222,
"groupId": 0,
"width": 400,
"height": 368.28125
}
]
}
]
But when I use:
on(GroupsActions.updateGroups, (state, { groups }) =>
adapter.updateMany(groups, state)
)
Nothing is happening and when I change updateMany to upsertMany
on(GroupsActions.updateGroups, (state, { groups }) =>
adapter.upsertMany(groups, state)
)
Children are replaced and I am losing all the properties which are not part of the update.
How to update the properties of children without removing all other properties?
Any help would be appreciated. Thank you

Related

How to create widget in a single AWS Cloud Watch dashboard for a list of instances

Terraform (aws provider) beginner here. I am trying to create aws-cloudwatch widget that graphs CPU Utilization for 3 instances. However, I only get one graph generated for the last instance in the list instead three graphs on one widget! How can I fix this please or get this How can I get this code to create CPUUtilisation graph for three instances on the same widget? Thank you
variable "targets" {
default = ["i-086e06769b6c67665","i-0477b6f25ad155290","i-0e6320273511d17dc"]
}
resource "aws_cloudwatch_dashboard" "ec2" {
dashboard_name = "ec2-test-dashboard"
count = length(var.targets)
dashboard_body = <<EOF
{
"widgets": [
{
"type": "metric",
"x": 0,
"y": 0,
"width": 17,
"height": 8,
"properties": {
"metrics": [
["AWS/EC2","CPUUtilization","InstanceId","${(var.targets[count.index])}"]
],
"title": "EC2 dashboard: CPUUtilization",
"stat": "Average",
"period": 300,
"stacked": false,
"view": "timeSeries",
"region": "eu-west-1",
"annotations": {
"horizontal": [
{
"label": "critical range",
"value": 10
}
]
},
"legend": {
"position": "right"
}
}
}
]
}
EOF
}

How do I change the output of the serialization from my entity?

$normalizer = new ObjectNormalizer();
// $normalizer->setCircularReferenceLimit(2);
$normalizer->setCircularReferenceHandler(function ($object) {
return $object->getId();
});
$normalizer->setIgnoredAttributes(array('users', '__initializer__', '__cloner__', 'authkey', '__isInitialized__', "products"));
$serializer = new Serializer(array($normalizer), array(new JsonEncoder()));
return new JsonResponse($serializer->serialize($product, 'json'));
Thats the result:
{
"id": 128,
"name": "product",
"price": 12,
"category": {
"id": 58,
"name": "category",
"company": {
"id": 1,
"name": "foo",
"tables": []
}
},
"description": "this is a product",
"company": {
"id": 1,
"name": "foo",
"tables": []
}
}
but how can i get the following result:
{
"id": 128,
"name": "product",
"price": 12,
"category": {
"name": "category"
},
"description": "this is a product",
"company": {
"id": 1,
"name": "foo",
"tables": []
}
}
How can I variate between the values?
for example I want here:
"category": {
"name": "category" },
later the id as well.
Take a look at the serializer component Attributes Groups. I believe this is what you are looking for.
You can select which entity properties should be included in each particular response.
Just simply set group in an entity
/**
* #Groups({"group1", "group2"})
*/
public $foo;
And then specify group or an array of groups for serialization
$serializer = new Serializer(array($normalizer));
$data = $serializer->normalize($obj, null, array('groups' => 'group1'));
Sometimes just groups is not enough, that is when you can use Doctrine hydrator. This is a bit more complex solution, but this gives you much more possibilities.
Normally you would use Attributes Groups when you need to make a simple selection - retrieve one particular entity or a collection of entities with some joined entities (use groups for joins as well). Doctrine hydrator is used when you need an aggregated selection, for example retrieve data from table that does not has specified relation, or add combined data along with regular data.

API Platform filter entity data

I just start to use Api platform and immediately stuck with problem how to filter data.
I have entity User and i want to filter data that are present in response ( JSON API format)
{
"links": {
"self": "/api/users"
},
"meta": {
"totalItems": 2,
"itemsPerPage": 30,
"currentPage": 1
},
"data": [
{
"id": "/api/users/1",
"type": "User",
"attributes": {
"_id": 1,
"username": "jonhdoe",
"isActive": true,
"address": null
}
},
{
"id": "/api/users/3",
"type": "User",
"attributes": {
"_id": 3,
"username": "test",
"isActive": true,
"address": null
}
}
]
}
so I want to remove e.g. User with id 3, but not use filters sent via request. I just want to set filter that will be always run when someone go to /api/users.
I look to api-platform extensions but this will be applied on each request e.g. /api/trucks. So at end I just want to get something like
{
"links": {
"self": "/api/users"
},
"meta": {
"totalItems": 1,
"itemsPerPage": 30,
"currentPage": 1
},
"data": [
{
"id": "/api/users/1",
"type": "User",
"attributes": {
"_id": 1,
"username": "jonhdoe",
"isActive": true,
"address": null
}
}
]
}
As you pointed out, extensions are the way to go.
The applyToCollection method gets a $resourceClass parameter containing the current resource class.
So you can apply the WHERE clause only for a specific class in this method like:
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
{
if (User::class === $resourceClass) {
$queryBuilder->doSomething();
}
// Do nothing for other classes
}

Recursion closure in groovy json [duplicate]

This question already has answers here:
Groovy :: Map Find Recursive
(1 answer)
Recursively extracting JSON field values in Groovy
(1 answer)
Closed 5 years ago.
I am trying to fetch all the values from a json whose key name is "label" and want to store in a list.
My problem is, the position of label key is not fixed.Sometimes it comes under parent node sometimes in child and sometime under child to child.We can use recursive closure in groovy but i don't know how?
Json::
[
{
{
"id": "2",
"label": "NameWhatever"
},
{
"id": "123",
"name": "Some Parent Element",
"children": [{
"id": "123123",
"label": "NameWhatever"
},
{
"id": "123123123",
"name": "Element with Additional Children",
"children": [{
"id": "123123123",
"label": "WhateverChildName"
},
{
"id": "12112",
"name": "Element with Additional Children",
"children": [{
"id": "123123123",
"label": "WhateverChildName"
},
{
"id": "12112",
"name": "Element with Additional Children",
"children": [{
"id": "12318123",
"label": "WhateverChildName"
},
{
"id": "12112",
"label": "NameToMap"
}
]
}
]
}
]
}
]
}
]
Based on similar question
import groovy.json.JsonSlurper
def mapOrCollection (def it) {
it instanceof Map || it instanceof Collection
}
def findDeep(def tree, String key, def collector) {
switch (tree) {
case Map: return tree.each { k, v ->
mapOrCollection(v)
? findDeep(v, key, collector)
: k == key
? collector.add(v)
: null
}
case Collection: return tree.each { e ->
mapOrCollection(e)
? findDeep(e, key, collector)
: null
}
default: return null
}
}
def collector = []
def found = findDeep(new JsonSlurper().parseText(json), 'label', collector)
println collector
Assuming variable json contains the given json input from question, prints:
[NameWhatever, NameWhatever, WhateverChildName, WhateverChildName, WhateverChildName, NameToMap]

How to get the Bounding box values from a GeoJSON file?

I have of GeoJSON file. But problem is, I do not know the projection of this file. Now, I want to overlay on my base map(OSM). How should I do it? This GeoJSON file contains features around Stockholm area. How can i set the bounding box for this? I tried with couple of openlayers example from hosting sites. But, those do not work for me. I got one example which is very similar with what i want, from (http://dev.openlayers.org/releases/OpenLayers-2.11/examples/all-overlays.html). I checked the source code he used bounding box. But problem is, if I remove bounding box this example does not work(I tied with given data with this). I do not know, is there anything wrong with my json file? Bellow, I have given some sample data. Is the format OK? As I told, I tried with different way. Some time i read the json file just fine from script. But, It can not show the featured overlay ed map. Bellow, I gave sample code, that shows how I tried.
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "FNODE_": 387603, "TNODE_": 387603, "LPOLY_": 0, "RPOLY_": 0, "LENGTH": 89.206900, "ROADS_SWER": 519006, "ROADS_SW_1": 519006, "REF": "", "TYPE": "residential", "ONEWAY": 8224, "BRIDGE": 8224, "MAXSPEED": 0, "LENGTH_M": 89.500000, "ID": 0, "dist_fnode": 13655.200000, "dist_tnode": 13655.200000 }, "geometry": { "type": "LineString", "coordinates": [ [ 676868.875000, 6569872.000000 ], [ 676882.437500, 6569861.000000 ], [ 676894.062500, 6569851.500000 ], [ 676894.500000, 6569844.500000 ], [ 676891.812500, 6569840.500000 ], [ 676887.312500, 6569841.000000 ], [ 676882.187500, 6569843.000000 ], [ 676875.250000, 6569850.000000 ], [ 676868.125000, 6569858.500000 ], [ 676866.500000, 6569867.000000 ], [ 676868.875000, 6569872.000000 ] ] } }
,
{ "type": "Feature", "properties": { "FNODE_": 387723, "TNODE_": 387724, "LPOLY_": 0, "RPOLY_": 0, "LENGTH": 9.581310, "ROADS_SWER": 519163, "ROADS_SW_1": 519163, "REF": "", "TYPE": "service", "ONEWAY": 8224, "BRIDGE": 8224, "MAXSPEED": 0, "LENGTH_M": 9.500000, "ID": 1, "dist_fnode": 13705.100000, "dist_tnode": 13695.600000 }, "geometry": { "type": "LineString", "coordinates": [ [ 677125.375000, 6569906.500000 ], [ 677133.437500, 6569911.500000 ] ] } }
,
..
..
]
}
Code Sample----
//OSM Layer-----------------------------------------------
var layer = new OpenLayers.Layer.OSM( "Simple OSM Map");
//GeoJSON Layer-------------------------------------------
var vector_format = new OpenLayers.Format.GeoJSON({});
var vector_protocol = new OpenLayers.Protocol.HTTP({
url: 'ml/roads.json',
format: vector_format
});
var vector_strategies = [new OpenLayers.Strategy.Fixed()];
var vector_layer = new OpenLayers.Layer.Vector('More Advanced Vector Layer',{
protocol: vector_protocol,
strategies: vector_strategies,
isBaseLayer: false
});
var options = {
layers: [layer, vector_layer]
};
var map = new OpenLayers.Map("map", options);
//Projection-----------------------------------------------
map.setCenter(
new OpenLayers.LonLat(18.068611, 59.329444).transform(
new OpenLayers.Projection("EPSG:4326"),
map.getProjectionObject()
), 10
);
Thanks, in advance.
vector_layer.events.on({
loadend: function() {
map.zoomToExtent(vector_layer.getDataExtent());
}
});
I still think you should set your projection in the map constructor…

Resources