How to limit the number of ids selected in JSON extractor? - jmeter

I have been able to get the id of all the fields using the JSON extractor in Jmeter with the following query:
$..MetaInputList.*.DocumentElementMetaInputId
But I want to get the limited number of random ids, let's say 4, 0r 5 ids in documentElementMetaInputId.
The ids are selected as:
documentElementMetaInputId_1=1398
documentElementMetaInputId_10=1407
documentElementMetaInputId_11=1408
documentElementMetaInputId_12=1409
documentElementMetaInputId_13=1410
documentElementMetaInputId_14=1411
documentElementMetaInputId_15=1412
.....so on.
How can I do that using JSON extractor? further, I want to use that list of ids in another request.
{
"documentElementMetaInputIdList": [
//list of ids
]
}
Updated JSON structure.
{
"Sections": [{
"Row": "R0",
"Disable": true,
"Cols": [{
"Column": "c0",
"Section": [{
"Name": "Eclampsia - EmerCheck",
"SectionElement": [{
"DocumentElementId": 47267
}]
}, {
"Name": "Eclampsia - EmerCheck",
"SectionElement": [{
"DocumentElementId": 47268
}]
}, {
"Name": "Eclampsia - EmerCheck",
"SectionElement": [{
"DocumentElementId": 47271,
"MetaInputList": [{
"DocumentElementMetaInputId": 3588
}, {
"DocumentElementMetaInputId": 3589
}, {
"DocumentElementMetaInputId": 3590
}, {
"DocumentElementMetaInputId": 3591
}, {
"DocumentElementMetaInputId": 3592
}, {
"DocumentElementMetaInputId": 3593
}, {
"DocumentElementMetaInputId": 3594
}, {
"DocumentElementMetaInputId": 3595
}, {
"DocumentElementMetaInputId": 3596
}, {
"DocumentElementMetaInputId": 3606
}]
}]
}, {
"Name": "Eclampsia - EmerCheck",
"SectionElement": [{
"DocumentElementId": 47272,
"MetaInputList": [{
"DocumentElementMetaInputId": 3607
}, {
"DocumentElementMetaInputId": 3608
}]
}]
}]
}]
}],
"metaInformation": []
}

Add JSR223 PostProcessor after your JSON Extractor
Put the following code into "Script" area:
def list = new HashSet<>()
1.upto(4, {
while (list.size() != 4) {
list.add(vars.get('documentElementMetaInputId_' + org.apache.commons.lang3.RandomUtils.nextInt(1, vars.get('documentElementMetaInputId_MatchNr') as int + 1)))
}
})
def payload = [documentElementMetaInputIdList: list.collect { entry -> entry as int }]
vars.put('payload', new groovy.json.JsonBuilder(payload).toPrettyString())
Use ${payload} JMeter Variable where you need the generated list of random IDs
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It

Another solution which could go with the JSON Extractor.
Add following into a JSR223 Post Processor just below the JSON Extractor to limit the IDs.
def lstIds = []
int idsToIncludeCount = 50
int idCount = vars.get("DocumentElementMetaInputId_matchNr").toInteger()
log.info("Total IDs count : ${idCount}")
if (idCount >0) {
for (int i=1 ; i <= idCount; i++ ) {
String IdReference = "DocumentElementMetaInputId_" + i
String currentId = vars.get(IdReference)
lstIds.add(centreId)
log.info("Current centre is ${currentId}")
}
lstIds = lstIds[0.. idsToIncludeCount]
}
Groovy is used for slicing the elements (IDs) from a list.
Assign the IDs into a list
Slice the list to limit the IDs

Solution 1
You can use JSON JMESPath Extractor with slicing feature to limit the result.
Your JMESPath expressions
$..MetaInputList.*.DocumentElementMetaInputId | [0:5]

Another Solution with the For Each controller.
The solution will work with your JSON Extractor result, DocumentElementMetaInputId
Add a For Each controller to iterate over the required number of IDs. You can set the starting and end indexes.
You can use ${DocumentElementMetaInputIdOut} within the child controllers .

Related

How to update a List of Maps in DynamoDB table via boto3?

is there any way to update/change value of a attribute as map nested in a list. My table looks something like this
{
"MainId": {
"S": "8981d9d0-373d-4f30-8084-cc4e18f9855f"
},
"date": {
"S": "8/11/2020"
},
"user_id": {
"S": "xyz#gmail.com"
},
"role": {
"S": "normal"
},
"finale": {
"L": [
{
"M": {
"Hatch_type": {
"S": "P7684"
}
I would like to change the update the value of 'Hatch Type' map in the 'finale' list. Please note that the provided table is not complete.
My current code looks like
import json
import boto3
from datetime import datetime
#TABLE_NAME = "table1"
def lambda_handler(event, context):
dynamodb = boto3.resource('dynamodb', region_name="us-east-2")
client = boto3.client('dynamodb', region_name="us-east-2")
TABLE_NAME = dynamodb.Table('table1')
response = TABLE_NAME.update_item(
Key={
'MainId':'8981d9d0-373d-4f30-8084-cc4e18f9855f'
}
UpdateExpression='SET #finale[0].#Hatch_type = :newvalue",
ExpressionAttributeNames = {
??
},
ExpressionAttributeValues = {
???
},
ReturnValues="UPDATED_NEW"
)
I'm stuck at this point and not sure how to proceed further. Please help.
ExpressionAttributeNames = {
':newvalue': "P7686 //whatever you want to update it io"
},
ExpressionAttributeValues = {
'#Hatch_type': " Hatch_type"
},

How do i connect two types in GraphQL?

So I have two types in GraphQL:
article.mdx
---
title: "Post n.1"
category: "Category"
---
Post Content
categories.json
[
{
"name": "Category",
"description": "This is a description",
"order": 1
}
]
I want to query my post type in order to have this kind of result:
{
"node": {
"title": Post n.1
"category": {
"name": "Category",
"description": "This is a description",
"order": 1
}
}
}
How can i do this? I'm currently using GatsbyJS! Thanks.
its pretty easy as you know you should use gatsby-transformer-remark to read md files , so for the json files you should use gatsby-transformer-json , add it in the gatsby-config.js file under plugins. then you need to query your data , unfortunatly i realy dont think you can combile two files to get data as you ask , but you can try this
first in gatsby-node.js file you need to reference the variables you gonna use for filter the query data , pass those fields in to the context
exports.createPages = async function({ actions, graphql }) {
const { data } = await graphql(`
query {
allMarkdownRemark {
edges {
node {
fields {
slug
}
}
}
}
}
`)
data.allMarkdownRemark.edges.forEach(edge => {
const slug = edge.node.fields.slug
actions.createPage({
path: slug,
component: require.resolve(`./src/templates/article.js`),
context: { category: category},
})
})
}
then in your page query you can accesss the filterd query by this read more in Creating Pages from Data Pro grammatically
export const pageQuery = graphql`
query MyQuery($category: String!) {
allMarkdownRemark(filter: {frontmatter: {category: {eq: $category}}}) {
edges {
node {
frontmatter {
title
}
}
}
}
allDataJson(filter: {name: {eq: $category}}) {
edges {
node {
nodes {
name,
description,
order
}
}
}
}
}`
then access you can access the your data by const {allMarkdownRemark , allDataJson} = data
then combine those two data as you prefer
const item = {node : { title: allMarkdownRemark.edges.node[0].frontmatter }};
item.node.category = allDataJson.edges.node[0].nodes
note this was assuming that edges.node is an array so we need to exact the 1st element of your data by node[0] , please check whether this method is working .
and the structure for the json data was
{ "nodes": [ {
"name": "Category",
"description": "This is a description",
"order": 1
}
]
}

loopback REST API filter by nested data

I would like to filter from REST API by nested data. For example this object:
[
{
"name": "Handmade Soft Fish",
"tags": "Rubber, Rubber, Salad",
"categories": [
{
"name": "women",
"id": 2,
"parent_id": 0,
"permalink": "/women"
},
{
"name": "kids",
"id": 3,
"parent_id": 0,
"permalink": "/kids"
}
]
},
{
"name": "Tasty Rubber Soap",
"tags": "Granite, Granite, Chair",
"categories": [
{
"name": "kids",
"id": 3,
"parent_id": 0,
"permalink": "/kids"
}
]
}
]
is comming by GET /api/products?filter[include]=categories
and i would like to get only products which has category name "women". How do this?
LoopBack does not support filters based on related models.
This is a limitation that we have never had bandwidth to solve, unfortunately :(
For more details, see the discussion and linked issues here:
Filter on level 2 properties: https://github.com/strongloop/loopback/issues/517
Filter by properties of related models (use SQL JOIN in queries): https://github.com/strongloop/loopback/issues/683
Maybe you want to get this data by the Category REST API. For example:
GET /api/categories?filter[include]=products&filter[where][name]=woman
The result will be a category object with all products related. To this, will be necessary declare this relation on the models.
Try like this.It has worked for me.
const filter = {
where: {
'categories.name': {
inq: ['women']**strong text**
}
}
};
Pass this filter to request as path parameters and the request would be like bellow
GET /api/categoriesfilter=%7B%22where%22:%7B%categories.name%22:%7B%22inq%22:%5B%women%22%5D%7D%7D%7D
Can you share how it looks like without filter[include]=categorie, please ?
[edit]
after a few questions in comment, I'd build a remote method : in common/models/myModel.js (inside the function) :
function getItems(filter, categorieIds = []) {
return new Promise((resolve, reject) => {
let newInclude;
if (filter.hasOwnProperty(include)){
if (Array.isArray(filter.include)) {
newInclude = [].concat(filter.include, "categories")
}else{
if (filter.include.length > 0) {
newInclude = [].concat(filter.include, "categories");
}else{
newInclude = "categories";
}
}
}else{
newInclude = "categories";
}
myModel.find(Object.assign({}, filter, {include: newInclude}))
.then(data => {
if (data.length <= 0) return resolve(data);
if (categoriesIds.length <= 0) return resolve(data);
// there goes your specific filter on categories
const tmp = data.filter(
item => item.categories.findIndex(
categorie => categorieIds.indexOf(categorie.id) > -1
) > -1
);
return resolve(tmp);
})
}
}
myModel.remoteMethod('getItems', {
accepts: [{
arg: "filter",
type: "object",
required: true
}, {
arg: "categorieIds",
type: "array",
required: true
}],
returns: {arg: 'getItems', type: 'array'}
});
I hope it answers your question...

Rethinkdb insert query results into a table

I'm trying to insert the results of a query from one table into another table. However, when I attempt to run the query I am receiving an error.
{
"deleted": 0 ,
"errors": 1 ,
"first_error": "Expected type OBJECT but found ARRAY." ,
"inserted": 0 ,
"replaced": 0 ,
"skipped": 0 ,
"unchanged": 0
}
Here is the the insert and query:
r.db('test').table('destination').insert(
r.db('test').table('source').map(function(doc) {
var result = doc('result');
return result('section_list').concatMap(function(section) {
return section('section_content').map(function(item) {
return {
"code": item("code"),
"name": item("name"),
"foo": result("foo"),
"bar": result("bar"),
"baz": section("baz"),
"average": item("average"),
"lowerBound": item("from"),
"upperBound": item("to")
};
});
});
});
);
Is there a special syntax for this, or do I have to retrieve the results and then run a separate insert?
The problem is that your inner query is returning a stream of arrays. You can't insert arrays into a table (only objects), so the query fails. If you change the outermost map into a concatMap it should work.
The problem here was that the result was a sequence of an array of objects. i.e
[ [ { a:1, b:2 }, { a:1, b:2 } ], [ { a:2, b:3 } ] ]
Therefore, I had to change the outer map call to a concatMap call. The query then becomes:
r.db('test').table('destination').insert(
r.db('test').table('source').concatMap(function(doc) {
var result = doc('result');
return result('section_list').concatMap(function(section) {
return section('section_content').map(function(item) {
return {
"code": item("code"),
"name": item("name"),
"foo": result("foo"),
"bar": result("bar"),
"baz": section("baz"),
"average": item("average"),
"lowerBound": item("from"),
"upperBound": item("to")
};
)});
});
});
}
Thanks goes to #AtnNn on the #rethinkdb freenode for pointing me in the right direction.

How to create *static* property in can.Model

I need somehow to store metadata in the can.Model
I use findAll method and receive such JSON:
{
"metadata": {
"color": "red"
},
"data": [
{ "id": 1, "description": "Do the dishes." },
{ "id": 2, "description": "Mow the lawn." },
{ "id": 3, "description": "Finish the laundry." }
]
}
I can work with data like can.Model.List, but I need metadata like a static property or something.
You can use can.Model.parseModels to adjust your response JSON before it's turned into a can.Model.List.
parseModels: function(response, xhr) {
var data = response.data;
var metadata = response.metadata;
var properties;
if(data && data.length && metadata) {
properties = Object.getOwnPropertyNames(metadata);
can.each(data, function(datum) {
can.each(properties, function(property) {
datum[property] = metadata[property];
});
});
}
return response;
}
Here's a functional example in JS Bin: http://jsbin.com/qoxuju/1/edit?js,console

Resources