Filtering and find unique value in Kibana(Elasticsearch) - elasticsearch

I use kibana.
I want to filter some value and find unique count.
ex)
{ user_id: 1, type: "customer", action: "xxx" }
{ user_id: 1, type: "customer", action: "xxx" }
{ user_id: 1, type: "seller", action: "xxx" } # user change type!!!
{ user_id: 2, type: "customer", action: "xxx" }
{ user_id: 2, type: "customer", action: "xxx" }
{ user_id: 3, type: "customer", action: "xxx" }
How can I count just customers.
I want to get
customer_count: 2
for example, in SQL:
SELECT COUNT(DISTINCT(user_id)) FROM users where user_id NOT IN (SELECT user_id FROM users WHERE type = "seller")
Is there any query for this situation?

Related

GraphQL: relationships in not normalizable

I am new to GraphQL not sure what I am doing wrong. Any help will be appreciated. I have included my GraphQL schema, data and query here. I am getting exception in query "Value to set in Organization.relationships in not normalizable: Value to set in Relationships.customers in not normalizable: should be an object or null or undefined".
My Schema:
type Query{
searchForOrganization: [Organization!]!
}
type Data{
id: ID
type: String
}
type Group{
data:[Data]
}
type User{
data:[Data]
}
type Customer{
data:[Data]
}
type Relationships{
groups: Group
users: User
customers: Customer
}
type Attributes{
name: String!
description: String
authenticationUrl: String
}
type Organization{
id: ID!
type: String!
attributes: Attributes!
relationships: Relationships!
}
Mock Data:
const mocks = {
Query: () => ({
searchForOrganization: () => [...new Array(9)],
}),
Organization: () => (
{
type: () => "organization",
id: () =>"Test002",
attributes: ()=>{
return{
authenticationUrl: "XXXX",
description: "Test Company",
"name": "Test Company"
};
},
relationships: () => {
return{
customers: () => {
return{
data: [{type: "customer", id: "Test002_Root_customers"},
{type: "customer", id: "Test003_Root_customers"}
]
};},
groups: () => {
return{
data: [{type: "group", id: "Test003_Root_groups"},
{type: "group", id: "Test002_Root_groups"}]
};},
users: () => {
return{
data: [{type: "user", id: "Test003_Root_users"},
{type: "user", id: "Test002_Root_users"}]
};}
};
}
}
),
};
My Query:
query SearchForOrganization {
searchForOrganization {
id
type
attributes {
name
description
authenticationUrl
}
relationships {
groups {
data {
id
type
}
}
users {
data {
id
type
}
}
customers {
data {
id
type
}
}
}
}
}
My Result:
{
"errors": [
{
"message": "Value to set in Organization.relationships in not normalizable: Value to set in Relationships.customers in not normalizable: should be an object or null or undefined. Received () => {\r\n return{\r\n data: [{type: \"customer\", id: \"Test002_Root_customers\"},\r\n {type: \"customer\", id: \"Test003_Root_customers\"}\r\n ]\r\n };}",
"locations": [
{
"line": 10,
"column": 5
}
],
"path": [
"searchForOrganization",
0,
"relationships"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"Error: Value to set in Organization.relationships in not normalizable: Value to set in Relationships.customers in not normalizable: should be an object or null or undefined. Received () => {\r",
" return{\r",
" data: [{type: \"customer\", id: \"Test002_Root_customers\"},\r",
" {type: \"customer\", id: \"Test003_Root_customers\"}\r",
" ]\r",
" };}",
Change in schema solved the issue
type TypeAndID{
id: ID
type: String
}
type DataGroup{
data:[TypeAndID]
}
type OrgRelationships{
groups: DataGroup
users: DataGroup
customers: DataGroup
}
type Attributes{
name: String!
description: String
authenticationUrl: String
}
type Organization{
id: ID!
type: String!
attributes: Attributes!
relationships: OrgRelationships!
}
type Data{
data: [Organization!]!
}
type Query{
getOrganizations: Data
}

how to get count of MongoDB documents in a given date range

The below mentioned is the Collection used:
[
{
_id: 1,
joinedAt:ISODate("2021-07-20T12:31:33.229+05:30"),
firstName: "John",
lastName: "King",
salary: 5000,
department: {
"name": "HR"
}
},
{
_id: 2,
joinedAt:ISODate("2021-08-20T12:31:33.229+05:30"),
firstName: "Sachin",
lastName: "T",
salary: 8000,
department: {
"name": "Marketing"
}
},
{
_id: 3,
joinedAt:ISODate("2021-06-20T12:31:33.229+05:30"),
firstName: "James",
lastName: "Bond",
salary: 7500,
department: {
"name": "Marketing"
}
},
{
_id: 4,
joinedAt:ISODate("2021-05-20T12:31:33.229+05:30"),
firstName: "Rosy",
lastName: "Brown",
salary: 5000,
department: {
"name": "HR"
}
},
{
_id: 5,
joinedAt:ISODate("2021-07-26T12:31:33.229+05:30"),
firstName: "Kapil",
lastName: "D",
salary: 4500,
department: {
"name": "HR"
}
},
{
_id: 6,
joinedAt:ISODate("2021-07-20T12:31:33.229+05:30"),
firstName: "Amitabh",
lastName: "B",
salary: 7000,
department: {
"name": "Marketing"
}
}
]
I used the following query:
db.collections.aggregate([
{
$match:
{ createdAt:
{
$gte: ISODate("2021-01-01T17:06:02.713+05:30"),
$lte: ISODate("2021-12-31T17:06:02.713+05:30")
}
}
},
{$sort: {createdAt: 1}},
{ $group:{ _id:{department:'$department.name'}, totalEmployees:
{$sum:1},firstEmployee: {$first: "$firstName"} }
}])
Using this query I can get the count of employees in each department and the first name of the employee who joined first in each department. I would also like to get the count of employees joined in each department in a particular month(eg: employees joined HR department in September). What are the changes to be made in this query?

I need return the user_roles in graphql query

i need return my user with your role, i'm using prisma.io and nestjs + graphql;
this is my return in graphl
{
"data": {
"login": {
"user": {
"secure_id": "1086dd35-d9ab-442a-8311-af4720ed3d9a",
"name": "Super Admin",
"email": "super#admin.com"
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InN1cGVyQGFkbWluLmNvbSIsInN1YiI6IjEwODZkZDM1LWQ5YWItNDQyYS04MzExLWFmNDcyMGVkM2Q5YSIsImlhdCI6MTY0OTY4NDg5NSwiZXhwIjoxNjQ5Njg4NDk1fQ.bwQRzcQ_gVR2wlVGPpKdSH1yZ6e6ipmENB3NqhKeN-8"
}
}
}
and this is the console.log of the user at moment of signin:
{
id: 5,
secure_id: '1086dd35-d9ab-442a-8311-af4720ed3d9a',
name: 'Super Admin',
email: 'super#admin.com',
password: '$2b$10$ioWYGeN.luOAzB9KdUjsp.qYPkgoqLtEqqcoN05dljgXQXPkX2N6W',
created_at: 2022-04-07T19:37:22.081Z,
updated_at: 2022-04-07T19:37:22.082Z,
UserRoles: [
{
user_id: 5,
roles_id: 2,
created_at: 2022-04-07T19:37:22.096Z,
updated_at: 2022-04-07T19:37:22.096Z
}
]
}
in the log i receive the UserRoles, but not in the signin.
this is the repository: https://github.com/paulozy/tgl-nest-and-graphql

GraphQL + Sequalize + existing database - "message": "parent.getPages is not a function" for one model not the other

GraphQL Query
Morning all,
I have a nice setup GraphQL -> Sequalize -> Existing DB (generated by a Laravel application). I've built this schema out:
type App {
id: ID!
userId: ID
user: User
pages: [Page]
experiments: [Experiment]
uri: String!
createdAt: String!
updatedAt: String!
}
type Page {
id: ID!
userId: ID
user: User
appId: ID!
app: App!
uri: String!
createdAt: String!
updatedAt: String!
}
type Experiment {
id: ID!
title: String!
appId: ID!
app: App!
createdAt: String!
updatedAt: String!
}
Based on the existing data. Querying an apps experiments works just great:
query {
app(id: 6) {
id
title
experiments {
id
}
}
}
{
"data": {
"app": {
"id": "6",
"title": "C-Map Embark: Boating",
"experiments": [
{
"id": "1"
}
]
}
}
}
But querying pages I get this:
query {
app(id: 6) {
id
title
pages {
id
}
}
}
{
"errors": [
{
"message": "parent.getPages is not a function",
"locations": [
{
"line": 5,
"column": 5
}
],
"path": [
"app",
"pages"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"TypeError: parent.getPages is not a function",
...
The db columns are the same, as are the resolvers:
/* jshint indent: 2 */
module.exports = function(sequelize, DataTypes) {
const Page = sequelize.define(
"page",
{
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
...
createdAt: {
type: DataTypes.DATE,
allowNull: true
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true
}
},
{
tableName: "pages",
underscored: true
}
);
Page.associate = models => {
Page.belongsTo(models.app);
};
return Page;
};
/* jshint indent: 2 */
module.exports = function(sequelize, DataTypes) {
const Experiment = sequelize.define(
"experiment",
{
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
...
createdAt: {
type: DataTypes.DATE,
allowNull: true
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true
}
},
{
tableName: "experiments",
underscored: true
}
);
Experiment.associate = models => {
Experiment.belongsTo(models.app);
};
return Experiment;
};
Have you come across this before?

How to merge array of hashes based on hash value but not merge values instead override

I have an array of hashes like this:
[
{ name: 'Pratha', email: 'c#f.com' },
{ name: 'John', email: 'j#g.com' },
{ name: 'Clark', email: 'x#z.com' },
]
And this is second group array of hashes:
[
{ name: 'AnotherNameSameEmail', email: 'c#f.com' },
{ name: 'JohnAnotherName', email: 'j#g.com' },
{ name: 'Mark', email: 'd#o.com' },
]
What I want is, merge these two arrays into one, merge based on :email and keep latest (or first) :name.
Expected Result (latest name overrided):
[
{ name: 'AnotherNameSameEmail', email: 'c#f.com' },
{ name: 'JohnAnotherName', email: 'j#g.com' },
{ name: 'Mark', email: 'd#o.com' },
{ name: 'Clark', email: 'x#z.com' },
]
or (first name preserved)
[
{ name: 'Pratha', email: 'c#f.com' },
{ name: 'John', email: 'j#g.com' },
{ name: 'Mark', email: 'd#o.com' },
{ name: 'Clark', email: 'x#z.com' },
]
So, basically, I want to group by :email, retain one :name, drop dupe emails.
The examples found on SO is creates an array of values for :name.
Ruby 2.6.3
Maybe you could just call Array#uniq with a block on email key of the concatenation (Array#+) of the two arrays:
(ary1 + ary2).uniq { |h| h[:email] }
a1 = [
{ name: 'Pratha', email: 'c#f.com' },
{ name: 'John', email: 'j#g.com' },
{ name: 'Clark', email: 'x#z.com' },
]
a2 = [
{ name: 'AnotherNameSameEmail', email: 'c#f.com' },
{ name: 'JohnAnotherName', email: 'j#g.com' },
{ name: 'Mark', email: 'd#o.com' },
]
Let's first keep the last:
(a1+a2).each_with_object({}) { |g,h| h.update(g[:email]=>g) }.values
#=> [{:name=>"AnotherNameSameEmail", :email=>"c#f.com"},
# {:name=>"JohnAnotherName", :email=>"j#g.com"},
# {:name=>"Clark", :email=>"x#z.com"},
# {:name=>"Mark", :email=>"d#o.com"}]
To keep the first, do the same with (a1+a2) replaced with (a2+a1), to obtain:
#=> [{:name=>"Pratha", :email=>"c#f.com"},
# {:name=>"John", :email=>"j#g.com"},
# {:name=>"Mark", :email=>"d#o.com"},
# {:name=>"Clark", :email=>"x#z.com"}]

Resources