Merge pull request #446 from automatisch/feature/show-flow-count-with-connections

feat: Expose flow count with connections of getApp query
This commit is contained in:
Ömer Faruk Aydın
2022-08-13 11:59:30 +03:00
committed by GitHub
7 changed files with 34 additions and 11 deletions

View File

@@ -1,4 +1,8 @@
import process from 'process'; 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 knex from 'knex';
import type { Knex } from 'knex'; import type { Knex } from 'knex';
import knexConfig from '../../knexfile'; import knexConfig from '../../knexfile';
@@ -8,10 +12,12 @@ export const client: Knex = knex(knexConfig);
const CONNECTION_REFUSED = 'ECONNREFUSED'; const CONNECTION_REFUSED = 'ECONNREFUSED';
client.raw('SELECT 1') client.raw('SELECT 1').catch((err) => {
.catch((err) => { if (err.code === CONNECTION_REFUSED) {
if (err.code === CONNECTION_REFUSED) { logger.error(
logger.error('Make sure you have installed PostgreSQL and it is running.', err); 'Make sure you have installed PostgreSQL and it is running.',
process.exit(); err
} );
}); process.exit();
}
});

View File

@@ -11,10 +11,14 @@ const getApp = async (_parent: unknown, params: Params, context: Context) => {
if (context.currentUser) { if (context.currentUser) {
const connections = await context.currentUser const connections = await context.currentUser
.$relatedQuery('connections') .$relatedQuery('connections')
.select('connections.*')
.fullOuterJoinRelated('steps')
.where({ .where({
key: params.key, 'connections.key': params.key,
draft: false, 'connections.draft': false,
}) })
.countDistinct('steps.flow_id as flowCount')
.groupBy('connections.id')
.orderBy('created_at', 'desc'); .orderBy('created_at', 'desc');
return { return {

View File

@@ -161,6 +161,7 @@ type Connection {
verified: Boolean verified: Boolean
app: App app: App
createdAt: String createdAt: String
flowCount: Int
} }
type ConnectionData { type ConnectionData {

View File

@@ -3,6 +3,7 @@ import type { RelationMappings } from 'objection';
import { AES, enc } from 'crypto-js'; import { AES, enc } from 'crypto-js';
import Base from './base'; import Base from './base';
import User from './user'; import User from './user';
import Step from './step';
import appConfig from '../config/app'; import appConfig from '../config/app';
import { IJSONObject } from '@automatisch/types'; import { IJSONObject } from '@automatisch/types';
import Telemetry from '../helpers/telemetry'; import Telemetry from '../helpers/telemetry';
@@ -16,6 +17,7 @@ class Connection extends Base {
verified = false; verified = false;
draft: boolean; draft: boolean;
count?: number; count?: number;
flowCount?: number;
static tableName = 'connections'; static tableName = 'connections';
@@ -43,6 +45,14 @@ class Connection extends Base {
to: 'users.id', to: 'users.id',
}, },
}, },
steps: {
relation: Base.HasManyRelation,
modelClass: Step,
join: {
from: 'connections.id',
to: 'steps.connection_id',
},
},
}); });
encryptData(): void { encryptData(): void {

View File

@@ -14,6 +14,7 @@ export interface IConnection {
userId: string; userId: string;
verified: boolean; verified: boolean;
count: number; count: number;
flowCount: number;
createdAt: string; createdAt: string;
} }

View File

@@ -42,7 +42,7 @@ function AppConnectionRow(props: AppConnectionRowProps): React.ReactElement {
const [deleteConnection] = useMutation(DELETE_CONNECTION); const [deleteConnection] = useMutation(DELETE_CONNECTION);
const formatMessage = useFormatMessage(); const formatMessage = useFormatMessage();
const { id, key, formattedData, verified, createdAt } = props.connection; const { id, key, formattedData, verified, createdAt, flowCount } = props.connection;
const contextButtonRef = React.useRef<SVGSVGElement | null>(null); const contextButtonRef = React.useRef<SVGSVGElement | null>(null);
const [anchorEl, setAnchorEl] = React.useState<SVGSVGElement | null>(null); const [anchorEl, setAnchorEl] = React.useState<SVGSVGElement | null>(null);
@@ -121,7 +121,7 @@ function AppConnectionRow(props: AppConnectionRowProps): React.ReactElement {
<Box sx={{ px: 2 }}> <Box sx={{ px: 2 }}>
<Typography variant="caption" color="textSecondary" sx={{ display: ['none', 'inline-block'] }}> <Typography variant="caption" color="textSecondary" sx={{ display: ['none', 'inline-block'] }}>
{formatMessage('connection.flowCount', { count: countTranslation(0) })} {formatMessage('connection.flowCount', { count: countTranslation(flowCount) })}
</Typography> </Typography>
</Box> </Box>

View File

@@ -8,6 +8,7 @@ export const GET_APP_CONNECTIONS = gql`
id id
key key
verified verified
flowCount
formattedData { formattedData {
screenName screenName
} }