From bb37299c5bb2b803eb08578edecfda6bf9b80a2d Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Sat, 13 Aug 2022 01:50:03 +0300 Subject: [PATCH 1/2] feat: Expose flow count with connections of getApp query --- packages/backend/src/config/database.ts | 20 ++++++++++++------- .../backend/src/graphql/queries/get-app.ts | 8 ++++++-- packages/backend/src/graphql/schema.graphql | 1 + packages/backend/src/models/connection.ts | 10 ++++++++++ 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/packages/backend/src/config/database.ts b/packages/backend/src/config/database.ts index 559d5e0c..4f8c061b 100644 --- a/packages/backend/src/config/database.ts +++ b/packages/backend/src/config/database.ts @@ -1,4 +1,8 @@ import process from 'process'; +// The following two lines are required to get count values as number. +// More info: https://github.com/knex/knex/issues/387#issuecomment-51554522 +import pg from 'pg'; +pg.types.setTypeParser(20, 'text', parseInt); import knex from 'knex'; import type { Knex } from 'knex'; import knexConfig from '../../knexfile'; @@ -8,10 +12,12 @@ export const client: Knex = knex(knexConfig); const CONNECTION_REFUSED = 'ECONNREFUSED'; -client.raw('SELECT 1') - .catch((err) => { - if (err.code === CONNECTION_REFUSED) { - logger.error('Make sure you have installed PostgreSQL and it is running.', err); - process.exit(); - } - }); +client.raw('SELECT 1').catch((err) => { + if (err.code === CONNECTION_REFUSED) { + logger.error( + 'Make sure you have installed PostgreSQL and it is running.', + err + ); + process.exit(); + } +}); diff --git a/packages/backend/src/graphql/queries/get-app.ts b/packages/backend/src/graphql/queries/get-app.ts index e459319c..32479096 100644 --- a/packages/backend/src/graphql/queries/get-app.ts +++ b/packages/backend/src/graphql/queries/get-app.ts @@ -11,10 +11,14 @@ const getApp = async (_parent: unknown, params: Params, context: Context) => { if (context.currentUser) { const connections = await context.currentUser .$relatedQuery('connections') + .select('connections.*') + .fullOuterJoinRelated('steps') .where({ - key: params.key, - draft: false, + 'connections.key': params.key, + 'connections.draft': false, }) + .countDistinct('steps.flow_id as flowCount') + .groupBy('connections.id') .orderBy('created_at', 'desc'); return { diff --git a/packages/backend/src/graphql/schema.graphql b/packages/backend/src/graphql/schema.graphql index 40ba0ceb..4ec57ecc 100644 --- a/packages/backend/src/graphql/schema.graphql +++ b/packages/backend/src/graphql/schema.graphql @@ -161,6 +161,7 @@ type Connection { verified: Boolean app: App createdAt: String + flowCount: Int } type ConnectionData { diff --git a/packages/backend/src/models/connection.ts b/packages/backend/src/models/connection.ts index c7af9dd6..5a1460eb 100644 --- a/packages/backend/src/models/connection.ts +++ b/packages/backend/src/models/connection.ts @@ -3,6 +3,7 @@ import type { RelationMappings } from 'objection'; import { AES, enc } from 'crypto-js'; import Base from './base'; import User from './user'; +import Step from './step'; import appConfig from '../config/app'; import { IJSONObject } from '@automatisch/types'; import Telemetry from '../helpers/telemetry'; @@ -16,6 +17,7 @@ class Connection extends Base { verified = false; draft: boolean; count?: number; + flowCount?: number; static tableName = 'connections'; @@ -43,6 +45,14 @@ class Connection extends Base { to: 'users.id', }, }, + steps: { + relation: Base.HasManyRelation, + modelClass: Step, + join: { + from: 'connections.id', + to: 'steps.connection_id', + }, + }, }); encryptData(): void { From c1b637b2842c29f32bb2c30f05185a4e3019ee12 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Sat, 13 Aug 2022 01:39:41 +0200 Subject: [PATCH 2/2] feat: display flow count on per connection --- packages/types/index.d.ts | 1 + packages/web/src/components/AppConnectionRow/index.tsx | 4 ++-- packages/web/src/graphql/queries/get-app-connections.ts | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/types/index.d.ts b/packages/types/index.d.ts index b4f39bd7..a2a067a4 100644 --- a/packages/types/index.d.ts +++ b/packages/types/index.d.ts @@ -14,6 +14,7 @@ export interface IConnection { userId: string; verified: boolean; count: number; + flowCount: number; createdAt: string; } diff --git a/packages/web/src/components/AppConnectionRow/index.tsx b/packages/web/src/components/AppConnectionRow/index.tsx index d7d8d08c..b85a2328 100644 --- a/packages/web/src/components/AppConnectionRow/index.tsx +++ b/packages/web/src/components/AppConnectionRow/index.tsx @@ -42,7 +42,7 @@ function AppConnectionRow(props: AppConnectionRowProps): React.ReactElement { const [deleteConnection] = useMutation(DELETE_CONNECTION); const formatMessage = useFormatMessage(); - const { id, key, formattedData, verified, createdAt } = props.connection; + const { id, key, formattedData, verified, createdAt, flowCount } = props.connection; const contextButtonRef = React.useRef(null); const [anchorEl, setAnchorEl] = React.useState(null); @@ -121,7 +121,7 @@ function AppConnectionRow(props: AppConnectionRowProps): React.ReactElement { - {formatMessage('connection.flowCount', { count: countTranslation(0) })} + {formatMessage('connection.flowCount', { count: countTranslation(flowCount) })} diff --git a/packages/web/src/graphql/queries/get-app-connections.ts b/packages/web/src/graphql/queries/get-app-connections.ts index e8a972fe..1151d3a6 100644 --- a/packages/web/src/graphql/queries/get-app-connections.ts +++ b/packages/web/src/graphql/queries/get-app-connections.ts @@ -8,6 +8,7 @@ export const GET_APP_CONNECTIONS = gql` id key verified + flowCount formattedData { screenName }