skip failed to parse rules and show the timestamp in the UI

This commit is contained in:
Hongbo Wu
2024-03-17 12:38:23 +08:00
parent 2fc6fd6e93
commit 9af4235233
10 changed files with 65 additions and 7 deletions

View File

@ -59,4 +59,7 @@ export class Rule {
@UpdateDateColumn({ default: () => 'CURRENT_TIMESTAMP' })
updatedAt!: Date
@Column('timestamptz')
failedAt?: Date
}

View File

@ -2434,6 +2434,7 @@ export type Rule = {
createdAt: Scalars['Date'];
enabled: Scalars['Boolean'];
eventTypes: Array<RuleEventType>;
failedAt?: Maybe<Scalars['Date']>;
filter: Scalars['String'];
id: Scalars['ID'];
name: Scalars['String'];
@ -6322,6 +6323,7 @@ export type RuleResolvers<ContextType = ResolverContext, ParentType extends Reso
createdAt?: Resolver<ResolversTypes['Date'], ParentType, ContextType>;
enabled?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType>;
eventTypes?: Resolver<Array<ResolversTypes['RuleEventType']>, ParentType, ContextType>;
failedAt?: Resolver<Maybe<ResolversTypes['Date']>, ParentType, ContextType>;
filter?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
id?: Resolver<ResolversTypes['ID'], ParentType, ContextType>;
name?: Resolver<ResolversTypes['String'], ParentType, ContextType>;

View File

@ -1825,6 +1825,7 @@ type Rule {
createdAt: Date!
enabled: Boolean!
eventTypes: [RuleEventType!]!
failedAt: Date
filter: String!
id: ID!
name: String!

View File

@ -8,7 +8,7 @@ import {
softDeleteLibraryItem,
updateLibraryItem,
} from '../services/library_item'
import { findEnabledRules } from '../services/rules'
import { findEnabledRules, markRuleAsFailed } from '../services/rules'
import { sendPushNotifications } from '../services/user'
import { logger } from '../utils/logger'
@ -112,14 +112,27 @@ const triggerActions = async (
query: `(${rule.filter}) AND includes:${itemId}`,
}
const libraryItems = await searchLibraryItems(searchArgs, userId)
if (libraryItems.count === 0) {
logger.info(`No pages found for rule ${rule.id}`)
let libraryItem: LibraryItem
try {
const { libraryItems, count } = await searchLibraryItems(
searchArgs,
userId
)
if (count === 0) {
logger.info(`No pages found for rule ${rule.id}`)
continue
}
libraryItem = libraryItems[0]
} catch (error) {
// failed to search for library items, mark rule as failed
logger.error('Error parsing filter in rules', error)
await markRuleAsFailed(rule.id, userId)
continue
}
const libraryItem = libraryItems.libraryItems[0]
for (const action of rule.actions) {
const actionFunc = getRuleAction(action.type)
const actionObj: RuleActionObj = {

View File

@ -2152,6 +2152,7 @@ const schema = gql`
createdAt: Date!
updatedAt: Date
eventTypes: [RuleEventType!]!
failedAt: Date
}
type RuleAction {

View File

@ -1,4 +1,4 @@
import { ArrayContains, ILike } from 'typeorm'
import { ArrayContains, ILike, IsNull, Not } from 'typeorm'
import { Rule, RuleAction, RuleEventType } from '../entity/rule'
import { authTrx, getRepository } from '../repository'
@ -62,5 +62,17 @@ export const findEnabledRules = async (
user: { id: userId },
enabled: true,
eventTypes: ArrayContains([eventType]),
failedAt: IsNull(), // only rules that have not failed
})
}
export const markRuleAsFailed = async (id: string, userId: string) => {
return authTrx(
(t) =>
t.getRepository(Rule).update(id, {
failedAt: new Date(),
}),
undefined,
userId
)
}

View File

@ -0,0 +1,9 @@
-- Type: DO
-- Name: add_failed_at_to_rule
-- Description: Add failed_at column to rules table
BEGIN;
ALTER TABLE omnivore.rules ADD COLUMN failed_at timestamptz;
COMMIT;

View File

@ -0,0 +1,9 @@
-- Type: UNDO
-- Name: add_failed_at_to_rule
-- Description: Add failed_at column to rules table
BEGIN;
ALTER TABLE omnivore.rules DROP COLUMN failed_at;
COMMIT;

View File

@ -29,6 +29,7 @@ export interface Rule {
createdAt: Date
updatedAt: Date
eventTypes: RuleEventType[]
failedAt?: Date
}
interface RulesQueryResponse {
@ -62,6 +63,7 @@ export function useGetRulesQuery(): RulesQueryResponse {
createdAt
updatedAt
eventTypes
failedAt
}
}
... on RulesError {

View File

@ -235,6 +235,7 @@ export default function Rules(): JSX.Element {
filter: rule.filter,
actions: rule.actions,
eventTypes: rule.eventTypes,
failedAt: rule.failedAt,
}
})
}, [rules])
@ -318,6 +319,11 @@ export default function Rules(): JSX.Element {
</>
),
},
{
title: 'Failed At',
dataIndex: 'failedAt',
key: 'failedAt',
},
{
title: '',
key: 'tools',