Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: allowed mongoQueries.resources filter for the cluster endpoints #711

Merged
merged 3 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions app/apollo/resolvers/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ const clusterResolvers = {
Query: {
clusterByClusterId: async (
parent,
{ orgId, clusterId, resourceLimit, groupLimit },
args,
context,
fullQuery
) => {
var { orgId, clusterId } = args;
const queryFields = GraphqlFields(fullQuery);
const queryName = 'clusterByClusterId';
const { models, me, req_id, logger } = context;
Expand Down Expand Up @@ -107,17 +108,18 @@ const clusterResolvers = {
cluster.registration.url = url;
}

await applyQueryFieldsToClusters([cluster], queryFields, { orgId, resourceLimit, groupLimit }, context);
await applyQueryFieldsToClusters([cluster], queryFields, args, context);

return cluster;
}, // end cluster by _id

clusterByName: async (
parent,
{ orgId, clusterName, resourceLimit },
args,
context,
fullQuery
) => {
var { orgId, clusterName } = args;
const queryFields = GraphqlFields(fullQuery);
const queryName = 'clusterByName';
const { models, me, req_id, logger } = context;
Expand Down Expand Up @@ -150,7 +152,7 @@ const clusterResolvers = {
cluster.registration.url = url;
}

await applyQueryFieldsToClusters([cluster], queryFields, { resourceLimit }, context);
await applyQueryFieldsToClusters([cluster], queryFields, args, context);

return cluster;
}, // end clusterByClusterName
Expand All @@ -163,10 +165,11 @@ const clusterResolvers = {
// older than.
clustersByOrgId: async (
parent,
{ orgId, limit, startingAfter, resourceLimit, groupLimit },
args,
context,
fullQuery
) => {
var { orgId, limit, startingAfter } = args;
const queryFields = GraphqlFields(fullQuery);
const queryName = 'clustersByOrgId';
const { models, me, req_id, logger } = context;
Expand All @@ -178,18 +181,19 @@ const clusterResolvers = {
const searchFilter = { org_id: orgId, ...conditions };
const clusters = await commonClusterSearch(models, searchFilter, { limit, startingAfter });

await applyQueryFieldsToClusters(clusters, queryFields, { orgId, resourceLimit, groupLimit }, context);
await applyQueryFieldsToClusters(clusters, queryFields, args, context);

return clusters;
}, // end clustersByOrgId

// Find all the clusters that have not been updated in the last day
inactiveClusters: async (
parent,
{ orgId, limit, resourceLimit, groupLimit },
args,
context,
fullQuery
) => {
var { orgId, limit } = args;
const queryFields = GraphqlFields(fullQuery);
const queryName = 'inactiveClusters';
const { models, me, req_id, logger } = context;
Expand All @@ -205,17 +209,18 @@ const clusterResolvers = {
};
const clusters = await commonClusterSearch(models, searchFilter, { limit });

await applyQueryFieldsToClusters(clusters, queryFields, { orgId, resourceLimit, groupLimit }, context);
await applyQueryFieldsToClusters(clusters, queryFields, args, context);

return clusters;
}, // end inactiveClusters

clusterSearch: async (
parent,
{ orgId, filter, limit, skip, resourceLimit, groupLimit, mongoQuery },
args,
context,
fullQuery
) => {
var { orgId, filter, limit, skip, mongoQuery } = args;
const queryFields = GraphqlFields(fullQuery);
const queryName = 'clusterSearch';
const { models, me, req_id, logger } = context;
Expand Down Expand Up @@ -251,7 +256,7 @@ const clusterResolvers = {

const clusters = await commonClusterSearch(models, searchFilter, { limit, skip });

await applyQueryFieldsToClusters(clusters, queryFields, { orgId, resourceLimit, groupLimit }, context);
await applyQueryFieldsToClusters(clusters, queryFields, args, context);

return clusters;
}, // end clusterSearch
Expand Down
15 changes: 6 additions & 9 deletions app/apollo/schema/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

const { gql } = require('apollo-server-express');
var { globalGraphqlInputs } = require('./common');

const clusterSchema = gql`
type Comment {
Expand Down Expand Up @@ -81,8 +82,7 @@ const clusterSchema = gql`
clusterByClusterId(
orgId: String!, @sv
clusterId: String! @sv
resourceLimit: Int = 500
groupLimit: Int = 16
${globalGraphqlInputs}
): Cluster

"""
Expand All @@ -91,7 +91,7 @@ const clusterSchema = gql`
clusterByName(
orgId: String!, @sv
clusterName: String! @sv
resourceLimit: Int = 500
${globalGraphqlInputs}
): Cluster

"""
Expand All @@ -103,8 +103,7 @@ const clusterSchema = gql`
limit: Int = 50
"**startingAfter**: For pagination. Specify the **id** of the document you want results older than."
startingAfter: String @sv
resourceLimit: Int = 500
groupLimit: Int = 16
${globalGraphqlInputs}
): [Cluster]!

"""
Expand All @@ -121,8 +120,7 @@ const clusterSchema = gql`
"**limit**: Number of docs to return. default 50, 0 means return all"
limit: Int = 50
skip: Int = 0
resourceLimit: Int = 500
groupLimit: Int = 16
${globalGraphqlInputs}
): [Cluster]!

"""
Expand All @@ -133,8 +131,7 @@ const clusterSchema = gql`
orgId: String! @sv
"**limit**: Number of docs to return. default 50, 0 means return all"
limit: Int = 50
resourceLimit: Int = 500
groupLimit: Int = 16
${globalGraphqlInputs}
): [Cluster]

"""
Expand Down
11 changes: 11 additions & 0 deletions app/apollo/schema/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var globalGraphqlInputs = `
mongoQueries: MongoQueries

resourceLimit: Int = 500
groupLimit: Int = 16

`;

module.exports = {
globalGraphqlInputs,
};
11 changes: 11 additions & 0 deletions app/apollo/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ const linkSchema = gql`
field: String!
desc: Boolean = false
}

input MongoQueries {
resources: JSON

#we'll enable these ones later
# clusters: JSON
# channels: JSON
# groups: JSON
# orgs: JSON
# subscriptions: JSON
}
`;

const schemas = [ linkSchema,
Expand Down
40 changes: 32 additions & 8 deletions app/apollo/utils/applyQueryFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,24 @@ var _ = require('lodash');
const { getGroupConditions, filterChannelsToAllowed, filterSubscriptionsToAllowed } = require('../resolvers/common');
const { ACTIONS, TYPES, CLUSTER_REG_STATES, CLUSTER_STATUS } = require('../models/const');


var loadResourcesWithSearchAndArgs = async({ search, args, context })=>{
var { models } = context;
var resourceLimit = args.resourceLimit || 500;
return await models.Resource.find({
$and: [
search,
_.get(args, 'mongoQueries.resources', {}),
],
}).limit(resourceLimit).lean({virtuals: true});
};


const applyQueryFieldsToClusters = async(clusters, queryFields={}, args, context)=>{
var { models } = context;
var { orgId, resourceLimit, groupLimit } = args;
var { orgId, groupLimit } = args;

const clusterIds = _.map(clusters, 'cluster_id');
resourceLimit = resourceLimit || 500;
const now = new Date();

_.each(clusters, (cluster)=>{
Expand All @@ -40,7 +52,11 @@ const applyQueryFieldsToClusters = async(clusters, queryFields={}, args, context
}
});
if(queryFields.resources) {
const resources = await models.Resource.find({ cluster_id: { $in: clusterIds } }).limit(resourceLimit).lean({virtuals: true});
var resources = await loadResourcesWithSearchAndArgs({
search: { cluster_id: { $in: clusterIds } },
args,
context,
});
await applyQueryFieldsToResources(resources, queryFields.resources, args, context);

const resourcesByClusterId = _.groupBy(resources, 'cluster_id');
Expand Down Expand Up @@ -209,7 +225,11 @@ const applyQueryFieldsToSubscriptions = async(subs, queryFields={}, args, contex
});
}
if(queryFields.resources){
var resources = await models.Resource.find({ org_id: orgId, 'searchableData.subscription_id' : { $in: subUuids } }).lean({ virtuals: true });
var resources = await loadResourcesWithSearchAndArgs({
search: { org_id: orgId, 'searchableData.subscription_id' : { $in: subUuids } },
args,
context,
});
await applyQueryFieldsToResources(resources, queryFields.resources, args, context);

var resourcesBySubUuid = _.groupBy(resources, 'searchableData.subscription_id');
Expand All @@ -230,10 +250,14 @@ const applyQueryFieldsToSubscriptions = async(subs, queryFields={}, args, contex
});
}
if(queryFields.remoteResources || queryFields.rolloutStatus){
var remoteResources = await models.Resource.find({
org_id: orgId,
'searchableData.annotations["deploy_razee_io_clustersubscription"]': { $in: subUuids },
deleted: false,
var remoteResources = await loadResourcesWithSearchAndArgs({
search: {
org_id: orgId,
'searchableData.annotations["deploy_razee_io_clustersubscription"]': { $in: subUuids },
deleted: false,
},
args,
context,
});
await applyQueryFieldsToResources(remoteResources, queryFields.remoteResources, args, context);

Expand Down