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

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)

Related

adding metric pattern in cloudwatch dashboard json

so I have created custom dashboard in AWS cloudwatch using json
{
"height": 6,
"width": 6,
"y": 54,
"x": 0,
"type": "metric",
"properties": {
"view": "timeSeries",
"stacked": false,
"metrics": [
[ "service-es-dashboard","fact-bulk-loader","service", "es-dashboard" ]
],
"region": "us-east-1",
"title": "ES-documents-count"
}
}
and my metric array has different metric names like fact-bulk-loader-1, fact-bulk-loader-2
now i want to add all the metric name present in the metric array in my widget in dashboard how do i do that ?
i can add a metric name and see but i want the process to be dynamic as every week new metrics gets added into the mertic array
You can use a search expression.
The metrics part would look something like this:
"metrics": [
[ { "expression": "SEARCH('{service-es-dashboard,service} service=\"es-dashboard\" MetricName=\"fact-bulk-loader', 'Average', 300)", "id": "e2", "period": 300 } ]
]

Is it possible to extend graphql response other than just data for pagination?

In GraphQL response normally looks like followings.
{
"data": [{
"id": 1,
"username": "Jon Snow",
"email": "crow#northofthew.all",
"age": 20
}, {
"id": 2,
"username": "Tyrion Lannister",
"email": "drunk#i.mp",
"age": 34
}, {
"id": 3,
"username": "Sansa Stark",
"email": "redhead#why.me",
"age": 17
}]
}
Is it possible to add meta data to your response such as pagination like this.
{
"pagination": {
"total": 14,
"count": 230,
},
"data": [{
"id": 1,
"username": "Jon Snow",
"email": "crow#northofthew.all",
"age": 20
}, {
"id": 2,
"username": "Tyrion Lannister",
"email": "drunk#i.mp",
"age": 34
}]
}
I'm using express-graphql and currently put those pagination to custom response header, which is fine but it can be better. Since GraphQL response is already wrapped with "data", it is not very strange to add more "data" to its response.
Reenforcing what #CommonsWare already stated, according to the specification that would a be an invalid GraphQL response. Regarding pagination, Relay has its own pagination approach called connections, but indeed, several other approaches are possible and even more suitable in some situtations (connections aren't a silver bullet).
I want to augment what was already said by adding that the hierarchical nature of GraphQL incites related data to be at the same level. An example is worth a thousands words, so here it goes:
query Q {
pagination_info { # what is this info related to? completely unclear
total
count
}
user {
friends {
id
}
}
}
Instead...
query Q {
user {
friends {
pagination_info { # fairly obvious that this is related to friends
total
count
}
friend {
id
}
}
}
}

Instagram API and Pagination

I can't get all images using Instagram API, Pagination seems to be working somehow different and I can't understand it yet
I use request:
https://api.instagram.com/v1/users/self/media/recent?access_token=TOKEN
and can get first 20 photos:
...
{
"attribution": null,
"tags": [
"beautiful",
"instalife",
"picoftheday",
"beauty",
"instalike",
"gf",
"traveling",
"instatravel",
"vsco",
"tourism",
"\u0438\u0441\u043f\u0430\u043d\u0438\u044f",
"travelphoto",
"vscogood",
"instafollow",
"travel",
"\u0433\u0440\u0430\u043d\u0430\u0434\u0430",
"amazing",
"vscocam",
"followme",
"photooftheday"
],
"type": "image",
"location": null,
"comments": {
"count": 1
},
"filter": "Normal",
"created_time": "1442825564",
"link": "https:\/\/instagram.com\/p\/74vm3GOCEn\/",
"likes": {
"count": 18
},
"images": {
"low_resolution": {
"url": "https:\/\/scontent.cdninstagram.com\/hphotos-xap1\/t51.2885-15\/s320x320\/e15\/11934647_531283580370186_1131008999_n.jpg",
"width": 320,
"height": 320
},
"thumbnail": {
"url": "https:\/\/scontent.cdninstagram.com\/hphotos-xap1\/t51.2885-15\/s150x150\/e15\/11934647_531283580370186_1131008999_n.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "https:\/\/scontent.cdninstagram.com\/hphotos-xap1\/t51.2885-15\/e15\/11934647_531283580370186_1131008999_n.jpg",
"width": 612,
"height": 612
}
},
"users_in_photo": [
],
"caption": {
"created_time": "1442825564",
"text": "#\u0413\u0440\u0430\u043d\u0430\u0434\u0430 #\u0418\u0441\u043f\u0430\u043d\u0438\u044f #photooftheday #picoftheday #instalike #followme #vscogood #vscocam #vsco #instafollow #travel #traveling #instatravel #instalife #tourism #gf #beauty #beautiful #amazing #travelphoto",
"from": {
"username": "solotravel_me",
"profile_picture": "https:\/\/igcdn-photos-h-a.akamaihd.net\/hphotos-ak-xaf1\/t51.2885-19\/11282631_115839268762391_863189534_a.jpg",
"id": "736938591",
"full_name": "and"
},
"id": "1078821495951073761"
},
"user_has_liked": false,
"id": "1078821489441513767_736938591",
"user": {
"username": "solotravel_me",
"profile_picture": "https:\/\/igcdn-photos-h-a.akamaihd.net\/hphotos-ak-xaf1\/t51.2885-19\/11282631_115839268762391_863189534_a.jpg",
"id": "736938591",
"full_name": "and"
}
}
...
after that I'm trying use max_id parameter, but I'm not sure which ID I need to use
I tried id of the photo, photo_user id, I even tried timestamp (found this idea on some forum), but every time I receive only first 20 photos
example:
https://api.instagram.com/v1/users/self/media/recent?access_token=TOKEN&max_id=1078821495951073761
I was having the same problem with the empty pagination object while the account actually had more photos.
And just to make things clear here. This question is answered in the comments of the origial question.
There is nothing in the pagination object because the app is in sandbox mode and app's in sandbox mode never returns more than 20 posts(photos).
Use this API for getting all posts of the user:
https://i.instagram.com/api/v1/feed/user/{user_id}
For pagination, the parameter name is max_id
Pass max_id which you have returned in this API's response.
Hope this helps !!

Right way to use function field() in yii2

I am writing REST API for my project.
field() function in my Model:
public function fields()
{
$fields = parent::fields();
// remove fields that contain sensitive information
unset($fields['media'], $fields['content']);
return $fields;
}
I have a 2 action:
/api/resources - get all resources lists
This api return:
"items":
[
{
"ID": 1,
"category_id": 1,
"title": "tset",
"image": "1437537044.png",
"ins_date": "2015-07-22 00:00:00",
"count": 20
},
{
"ID": 2,
"category_id": 1,
"title": "tset",
"image": "1437537176.png",
"ins_date": "2015-07-22 00:00:00",
"count": 0
},
/api/resources/view?id=1 - get all information about resource ID=1
{
"ID": 1,
"category_id": 1,
"title": "tset",
"image": "1437537044.png",
"ins_date": "2015-07-22 00:00:00",
"count": 21
}
I get all resources, but I want to get all fields when I call actionView($id) like this:
{
"ID": 1,
"category_id": 1,
"title": "tset", // I want to get response with this
"content": "test", //and this fields
"media": "test",
"image": "1437537044.png",
"ins_date": "2015-07-22 00:00:00",
"count": 22
}
HOw can solve this problem? Help me?
use subclass of models with different fields()
use Yii::$app->controller->action->uniqueId
public function fields()
{
if(Yii::$app->controller->action->uniqueId == 'controller/action'){
return ['field_1','field_2','field_3','field_4'];
}else{
return ['field_1','field_3'];
}
}
Extended yii\rest\Serializer change init() and getRequestedFields()

Aggregations on most recent document in group using elasticsearch

Suppose there are several documents per person that contain values:
{
"name": "John",
"value": 1,
"timestamp": 2014-06-15
}
{
"name": "John",
"value": 2,
"timestamp": 2014-06-16
}
{
"name": "Sam",
"value": 2,
"timestamp": 2014-06-15
}
{
"name": "Sam",
"value": 3,
"timestamp": 2014-06-16
}
How do I get a list of the most recent documents for each person?
How do I get an average of the values for the list of the most recent documents for each person? Given the sample data, this would be 2.5, not 2.
Is there some combination of buckets and metrics that could achieve this result? Will I need to implement a custom aggregator as part of a plugin, or must this sort of computation be performed in memory?
If you only need to find the most recent persons try something like this:
"aggs": {
"personName": {
"terms": {
"field": "name",
"size": 5,
"order": {"timeCreated": "desc"}
},
"aggs": {
"timeCreated": {
"max": {"field": "timestamp"}
}
}
}
}
The second operation is just an aggregation, and to get the average of the value field you could try something like:
curl -XPOST "http://DOMAIN:9200/your/data/_search" -d'
{
"size": 0,
"aggregations": {
"the_name": {
"terms": {
"field": "name",
"order": {
"value_avg": "desc"
}
},
"aggregations": {
"value_avg": {
"avg": {
"field": "value"
}
}
}
}
}
}'
To achieve a solution for your first issue I would recommend you to order the response by date, and then in your project ignore a term when you have another with the same name (meaning filter the data after the response of ES)