From e1314c815d87d26cdabd1302a83fc30ab613c42b Mon Sep 17 00:00:00 2001 From: Martin Sottnik Date: Wed, 17 Jul 2024 08:30:25 +0200 Subject: [PATCH] FR-239 stream filtering (#447) * stream filtering added * streamName filter query added --------- Co-authored-by: plehocky <117287338+plehocky@users.noreply.github.com> --- src/config.ts | 6 ++--- .../performance-monitoring-graphql.ts | 3 +++ src/helpers/stream-helpers.ts | 4 +++ src/schema/api.graphql | 2 ++ src/schema/nexus-typegen.ts | 2 ++ src/schema/stream.ts | 27 +++++++++++++++---- 6 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/config.ts b/src/config.ts index 6ff2da48..bc77eb80 100644 --- a/src/config.ts +++ b/src/config.ts @@ -33,7 +33,7 @@ type TopologyConfigEnabled = { topologyEnabled: true; topologyDiscoveryURL: string; topologyDiscoveryGraphqlURL: string; - performanceMonitoringGraphqlURL: string; + performanceMonitoringGraphqlURL: string | null; }; type TopologyConfigDisabled = { @@ -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.', ); } diff --git a/src/external-api/performance-monitoring-graphql.ts b/src/external-api/performance-monitoring-graphql.ts index eb04b0c7..f48cbecb 100644 --- a/src/external-api/performance-monitoring-graphql.ts +++ b/src/external-api/performance-monitoring-graphql.ts @@ -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 { diff --git a/src/helpers/stream-helpers.ts b/src/helpers/stream-helpers.ts index 8d9b30fb..2501b842 100644 --- a/src/helpers/stream-helpers.ts +++ b/src/helpers/stream-helpers.ts @@ -51,3 +51,7 @@ export function getMountParamsForStream(mountParameters: JsonValue, streamParame }, }; } + +export function getStreamNameQuery(streamName?: string | null): Record | undefined { + return streamName ? { contains: streamName, mode: 'insensitive' } : undefined; +} diff --git a/src/schema/api.graphql b/src/schema/api.graphql index c537db2c..66010d52 100644 --- a/src/schema/api.graphql +++ b/src/schema/api.graphql @@ -340,6 +340,8 @@ input FilterLabelsInput { } input FilterStreamsInput { + deviceName: String + labels: [String!] streamName: String } diff --git a/src/schema/nexus-typegen.ts b/src/schema/nexus-typegen.ts index 853ccea3..dc3c26d1 100644 --- a/src/schema/nexus-typegen.ts +++ b/src/schema/nexus-typegen.ts @@ -124,6 +124,8 @@ export interface NexusGenInputs { }; FilterStreamsInput: { // input type + deviceName?: string | null; // String + labels?: string[] | null; // [String!] streamName?: string | null; // String }; FilterTopologyInput: { diff --git a/src/schema/stream.ts b/src/schema/stream.ts index 6ed8ce54..cf701b0f 100644 --- a/src/schema/stream.ts +++ b/src/schema/stream.ts @@ -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'; @@ -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({ @@ -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, );