Skip to content

Commit

Permalink
FR-239 stream filtering (#447)
Browse files Browse the repository at this point in the history
* stream filtering added

* streamName filter query added

---------

Co-authored-by: plehocky <[email protected]>
  • Loading branch information
soson and plehocky authored Jul 17, 2024
1 parent 8cd726a commit e1314c8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type TopologyConfigEnabled = {
topologyEnabled: true;
topologyDiscoveryURL: string;
topologyDiscoveryGraphqlURL: string;
performanceMonitoringGraphqlURL: string;
performanceMonitoringGraphqlURL: string | null;
};

type TopologyConfigDisabled = {
Expand Down Expand Up @@ -66,9 +66,9 @@ function getTopologyConfig(): TopologyConfig {
};
}

if (!topologyDiscoveryURL || !topologyDiscoveryGraphqlURL || !performanceMonitoringGraphqlURL) {
if (!topologyDiscoveryURL || !topologyDiscoveryGraphqlURL) {
throw new Error(
'Not all mandatory topology discovery url (TOPOLOGY_DISCOVERY_API_URL, TOPOLOGY_DISCOVERY_GRAPHQL_API_URL, PERFORMANCE_MONITORING_GRAPHQL_API_URL) were found.',
'Not all mandatory topology discovery url (TOPOLOGY_DISCOVERY_API_URL, TOPOLOGY_DISCOVERY_GRAPHQL_API_URL) were found.',
);
}

Expand Down
3 changes: 3 additions & 0 deletions src/external-api/performance-monitoring-graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ function getPerformanceMonitoringAPI() {
return undefined;
}
}
if (!config.performanceMonitoringGraphqlURL) {
return undefined;
}
const client = new GraphQLClient(config.performanceMonitoringGraphqlURL);

async function getDeviceCpuUsage(deviceName: string): Promise<CurrentCpuUsageQuery> {
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/stream-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ export function getMountParamsForStream(mountParameters: JsonValue, streamParame
},
};
}

export function getStreamNameQuery(streamName?: string | null): Record<string, unknown> | undefined {
return streamName ? { contains: streamName, mode: 'insensitive' } : undefined;
}
2 changes: 2 additions & 0 deletions src/schema/api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ input FilterLabelsInput {
}

input FilterStreamsInput {
deviceName: String
labels: [String!]
streamName: String
}

Expand Down
2 changes: 2 additions & 0 deletions src/schema/nexus-typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ export interface NexusGenInputs {
};
FilterStreamsInput: {
// input type
deviceName?: string | null; // String
labels?: string[] | null; // [String!]
streamName?: string | null; // String
};
FilterTopologyInput: {
Expand Down
27 changes: 22 additions & 5 deletions src/schema/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
installDeviceCache,
uninstallDeviceCache,
} from '../external-api/uniconfig-cache';
import { getMountParamsForStream, getUniconfigStreamName } from '../helpers/stream-helpers';
import { getMountParamsForStream, getStreamNameQuery, getUniconfigStreamName } from '../helpers/stream-helpers';
import config from '../config';
import { Blueprint } from './blueprint';

Expand Down Expand Up @@ -92,7 +92,9 @@ export const StreamConnection = objectType({
export const FilterStreamsInput = inputObjectType({
name: 'FilterStreamsInput',
definition: (t) => {
t.string('streamName', 'deviceName');
t.list.nonNull.string('labels');
t.string('deviceName');
t.string('streamName');
},
});
export const SortStreamBy = enumType({
Expand All @@ -118,11 +120,26 @@ export const StreamQuery = extendType({
},
resolve: async (_, args, { prisma, tenantId }) => {
const { filter, orderBy } = args;
const filterQuery = getFilterQuery({ deviceName: filter?.streamName });
const labels = filter?.labels ?? [];
const dbLabels = await prisma.label.findMany({ where: { name: { in: labels } } });
const labelIds = dbLabels.map((l) => l.id);
const filterQuery = getFilterQuery({ deviceName: filter?.deviceName, labelIds });
const orderingArgs = getOrderingQuery(orderBy);
const baseArgs = { where: { tenantId, ...filterQuery } };
const baseArgs = { where: { tenantId } };
const result = await findManyCursorConnection(
(paginationArgs) => prisma.stream.findMany({ ...baseArgs, ...orderingArgs, ...paginationArgs }),
(paginationArgs) =>
prisma.stream.findMany({
...baseArgs,
...orderingArgs,
...paginationArgs,
include: {
device: true,
},
where: {
streamName: getStreamNameQuery(filter?.streamName),
device: filterQuery,
},
}),
() => prisma.device.count(baseArgs),
args,
);
Expand Down

0 comments on commit e1314c8

Please sign in to comment.