How to cut and delete elements in an array with Logstash - arrays

I have Json logs in an array as follows:
e":[{"n":"3/0/1","st":"CONTENT","v":"Sensortag"},
{"n":"3/0/3","st":"CONTENT","v":"Contiki-develop-20150508-409-g2147b9e"},
{"n":"3/0/13","st":"CONTENT","v":"1970-01-09T21:02:18Z"},
{"n":"3301/0/5700","st":"CONTENT","v":"376.64"},
{"n":"3303/0/5700","st":"CONTENT","v":"22.843"},
{"n":"3304/0/5700","st":"CONTENT","v":"63.53"},
{"n":"3315/0/5700","st":"CONTENT","v":"1000.34"}]
I would like to delete the first 3 elements from the array and keep the 4 last ones using a filter.
I have this as my filter:
filter {
if ([type] == "testbed"){
if [MessageParserJson][e[{}] in [MessageParserJson]{
mutate {
remove_field => ["[MessageparserJson][e[{0}]]" , "[MessageparserJson][e[{1}]]" , "[MessageParserJson][e[{2}]]"]
add_field => { "[MessageParserJson][e[{3}]]" => "MessageParser" }
add_field => { "[MessageParserJson][e[{4}]]" => "MessageParser" }
add_field => { "[MessageParserJson][e[{5}]]" => "MessageParser" }
add_field => { "[MessageParserJson][e[{6}]]" => "MessageParser" }
}
}
drop {
remove_field => ["MessageParserJson"]
}
}
}
But Logstash puts itself in error

you could use the Ruby filter for that, to e.g. remove the first three elements this should work:
filter {
ruby {
code => "event['MessageParserJson'].slice!(0,3)"
}
}
Cheers

Related

how to get length of filtered array react js

I try to display the number of cards even after it's filtered, but with this code I have only the initial number.
thanks in adavance
<div>{cards.length} Results<div>
{
cards.filter(card => {
if (card.title.toLowerCase().includes(search.toLowerCase())) {
return card;
}
})
.map((card, index) => {
return <CardJob key={index.id}
/>
})
}
Just do your filter before render then you'll have access to the length:
const filteredCards = cards.filter(card => card.title.toLowerCase().includes(search.toLowerCase()));
Then :
filteredCards.map(...
Or:
console.log(filteredCards.length);

Find element and render on passing codition

I find names for some objects and have following source code:
const findAndRenderName = (projectId: number| undefined) => {
//i want to render here something when the condtion will pass
projectList?.map(project => projectId = project.id)
}
return (
<DetailsBox title={t('catalogPage.componentDetails.specs.used')}>
{
component?.projects.map(project => findAndRenderName(project.id))
}
</DetailsBox>
);
How to make kind of if from the map function, any idea?
I think what you're trying to accomplish is
projectList?.map(project => {
if(projectId === project.id){
// do something
}
})
of if you refer to the second map
component?.projects.map(project => {
if(findAndRenderName(project.id)){
// do something
}
})

ReactJS: Check if array contains value else append

I'm trying to check if a JSON response contains a value already inside an array and if it doesn't add it in. The problem I'm having is understanding how to approach this in reactjs. I'm checking before I append it but it doesn't want to work. I've tried passing in user object & user.id but these fail. The attempt below fails to compile but it should help understand what I'm trying to achieve.
Code:
componentWillMount() {
fetch('http://localhost:8090/v1/users')
.then(results => {
return results.json();
})
.then(data => {
data.map((user) => (
if(userList.hasOwnProperty(user.id)) {
userList.push({label: user.title, value: user.id})))
}
})
}
map return the resultant array, but you are not returning anything from it, you should instead use forEach Also you need to check if the userList array contains the id, for that you can use findIndex
What you need is
state = {
userList: [];
}
componentDidMount() {
fetch('http://localhost:8090/v1/users')
.then(results => {
return results.json();
})
.then(data => {
const newUserList = [...this.state.userList];
data.forEach((user) => { // use { here instead of
if(userList.findIndex(item => item.value === user.id) < 0) {
newData.push({label: user.title, value: user.id})
}
})
this.setState({userList: newUserList});
});
}
render() {
return (
{/* map over userList state and render it here */}
)
}
I'd recommend using reduce to turn the returned data into an array you'd like, then adding those values to your existing user list:
fetch('http://localhost:8090/v1/users')
.then(res => res.json())
.then(data => data.reduce((acc, user) => {
const idList = userList.map(user => user.id);
if (idList.indexOf(user.id) === -1) {
acc.push({label: user.title, value: user.id})
}
return acc;
},[]))
.then(newList => userList = [...userList, ...newList]);

Making one hash with array of Hashes

I have recursively put together an array of hashes for perl, which looks something like this :
[
{
'Default' => {
'Elect' => { 'P' => 1 }
}
},
{
'Default' => {
'Elect' => { 'A' => 1 }
}
},
{
'Default' => {
'Elect' => { 'M' => 1 }
}
},
{
'Default' => {
'Elect' => { 'I' => 1 }
}
},
{
'Default' => {
'Picker' => { 'L' => 1 }
}
},
]
My aim is to make this more condensed and look like a single hash, as compared to array of hashes. Is there anyway in which i can make this array of hashes look like a hash:
{
'Default' =>{
'Elect' =>{
'P' => 1,
'A' => 1,
'M' => 1,
'I' => 1,
},
'Picker' => {
'L' => 1
}
}
}
Well, here is a simple recursive procedure to merge two hash references:
sub merge {
my ($xs, $ys) = #_;
while (my ($k, $v) = each %$ys) {
if ('HASH' eq ref $v) {
merge($xs->{$k} //= {}, $v);
}
else {
$xs->{$k} = $v;
}
}
}
Then:
my $data = ...; # your input data structure
my $acc = {};
merge($acc, $_) for #$data;
which produces the result you desire in $acc.
There is also the Hash::Merge module, with that:
use Hash::Merge 'merge';
my $acc = {};
$acc = merge $acc, $_ for #$data;

Elegant way to build an inverted map for a certain key nested in hashrefs

I have the following hashref of hashrefs structure:
$hashref = {
user1 => {
key1 => "unique_value1",
...
key99 => "value1_99"
},
...
user26 => {
key1 => "unique_value2",
...
key99 => "value1_99"
},
user99 => {
key1 => "unique_value1",
...
key99 => "value99_99"
},
};
What I want out:
$hashref = {
"unique_value1" => ["user1","user99"],
"unique_value2" => ["user26"]
};
I've historically built the inverted map on create, but I'm getting lazy.
Any one line sugar I could use for this?
Thank you.
One way:
my $h;
push (#{$h->{$hashref->{$_}{key1}}}, $_) for keys %$hashref;
my %users_by_uval;
for my $user_id (%$users) {
push #{ $users_by_uval{ $users->{$user_id}{key1} } }, $user_id;
}

Resources