Skip to content

Commit

Permalink
[backend] add cache status
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyCloarec committed Nov 29, 2024
1 parent e894f0b commit 6840f87
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8138,6 +8138,7 @@ type Query {
supportPackages(first: Int, after: ID, orderBy: SupportPackageOrdering, orderMode: OrderingMode, filters: FilterGroup, search: String): SupportPackageConnection
exclusionList(id: String!): ExclusionList
exclusionLists(first: Int, after: ID, orderBy: ExclusionListOrdering, orderMode: OrderingMode, filters: FilterGroup, search: String): ExclusionListConnection
exclusionListCacheStatus: ExclusionListCacheStatus
draftWorkspace(id: String!): DraftWorkspace
draftWorkspaces(first: Int, after: ID, orderBy: DraftWorkspacesOrdering, orderMode: OrderingMode, filters: FilterGroup, search: String): DraftWorkspaceConnection
draftWorkspaceEntities(draftId: String!, types: [String], first: Int, after: ID, orderBy: StixCoreObjectsOrdering, orderMode: OrderingMode, filters: FilterGroup, search: String): StixCoreObjectConnection
Expand Down Expand Up @@ -12327,6 +12328,12 @@ type ExclusionListEdge {
node: ExclusionList!
}

type ExclusionListCacheStatus {
refreshAskDate: String!
cacheDate: String!
allNodeCacheUpdated: Boolean!
}

enum ExclusionListOrdering {
name
created_at
Expand Down
19 changes: 19 additions & 0 deletions opencti-platform/opencti-graphql/src/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7502,6 +7502,13 @@ export type ExclusionList = BasicObject & InternalObject & {
standard_id: Scalars['String']['output'];
};

export type ExclusionListCacheStatus = {
__typename?: 'ExclusionListCacheStatus';
allNodeCacheUpdated: Scalars['Boolean']['output'];
cacheDate: Scalars['String']['output'];
refreshAskDate: Scalars['String']['output'];
};

export type ExclusionListConnection = {
__typename?: 'ExclusionListConnection';
edges?: Maybe<Array<ExclusionListEdge>>;
Expand Down Expand Up @@ -19333,6 +19340,7 @@ export type Query = {
event?: Maybe<Event>;
events?: Maybe<EventConnection>;
exclusionList?: Maybe<ExclusionList>;
exclusionListCacheStatus?: Maybe<ExclusionListCacheStatus>;
exclusionLists?: Maybe<ExclusionListConnection>;
externalReference?: Maybe<ExternalReference>;
externalReferences?: Maybe<ExternalReferenceConnection>;
Expand Down Expand Up @@ -30961,6 +30969,7 @@ export type ResolversTypes = ResolversObject<{
EventEdge: ResolverTypeWrapper<Omit<EventEdge, 'node'> & { node: ResolversTypes['Event'] }>;
EventsOrdering: EventsOrdering;
ExclusionList: ResolverTypeWrapper<BasicStoreEntityExclusionList>;
ExclusionListCacheStatus: ResolverTypeWrapper<ExclusionListCacheStatus>;
ExclusionListConnection: ResolverTypeWrapper<Omit<ExclusionListConnection, 'edges'> & { edges?: Maybe<Array<ResolversTypes['ExclusionListEdge']>> }>;
ExclusionListContentAddInput: ExclusionListContentAddInput;
ExclusionListEdge: ResolverTypeWrapper<Omit<ExclusionListEdge, 'node'> & { node: ResolversTypes['ExclusionList'] }>;
Expand Down Expand Up @@ -31779,6 +31788,7 @@ export type ResolversParentTypes = ResolversObject<{
EventConnection: Omit<EventConnection, 'edges'> & { edges?: Maybe<Array<Maybe<ResolversParentTypes['EventEdge']>>> };
EventEdge: Omit<EventEdge, 'node'> & { node: ResolversParentTypes['Event'] };
ExclusionList: BasicStoreEntityExclusionList;
ExclusionListCacheStatus: ExclusionListCacheStatus;
ExclusionListConnection: Omit<ExclusionListConnection, 'edges'> & { edges?: Maybe<Array<ResolversParentTypes['ExclusionListEdge']>> };
ExclusionListContentAddInput: ExclusionListContentAddInput;
ExclusionListEdge: Omit<ExclusionListEdge, 'node'> & { node: ResolversParentTypes['ExclusionList'] };
Expand Down Expand Up @@ -34783,6 +34793,13 @@ export type ExclusionListResolvers<ContextType = any, ParentType extends Resolve
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
}>;

export type ExclusionListCacheStatusResolvers<ContextType = any, ParentType extends ResolversParentTypes['ExclusionListCacheStatus'] = ResolversParentTypes['ExclusionListCacheStatus']> = ResolversObject<{
allNodeCacheUpdated?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType>;
cacheDate?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
refreshAskDate?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
}>;

export type ExclusionListConnectionResolvers<ContextType = any, ParentType extends ResolversParentTypes['ExclusionListConnection'] = ResolversParentTypes['ExclusionListConnection']> = ResolversObject<{
edges?: Resolver<Maybe<Array<ResolversTypes['ExclusionListEdge']>>, ParentType, ContextType>;
pageInfo?: Resolver<ResolversTypes['PageInfo'], ParentType, ContextType>;
Expand Down Expand Up @@ -38393,6 +38410,7 @@ export type QueryResolvers<ContextType = any, ParentType extends ResolversParent
event?: Resolver<Maybe<ResolversTypes['Event']>, ParentType, ContextType, RequireFields<QueryEventArgs, 'id'>>;
events?: Resolver<Maybe<ResolversTypes['EventConnection']>, ParentType, ContextType, Partial<QueryEventsArgs>>;
exclusionList?: Resolver<Maybe<ResolversTypes['ExclusionList']>, ParentType, ContextType, RequireFields<QueryExclusionListArgs, 'id'>>;
exclusionListCacheStatus?: Resolver<Maybe<ResolversTypes['ExclusionListCacheStatus']>, ParentType, ContextType>;
exclusionLists?: Resolver<Maybe<ResolversTypes['ExclusionListConnection']>, ParentType, ContextType, Partial<QueryExclusionListsArgs>>;
externalReference?: Resolver<Maybe<ResolversTypes['ExternalReference']>, ParentType, ContextType, RequireFields<QueryExternalReferenceArgs, 'id'>>;
externalReferences?: Resolver<Maybe<ResolversTypes['ExternalReferenceConnection']>, ParentType, ContextType, Partial<QueryExternalReferencesArgs>>;
Expand Down Expand Up @@ -41735,6 +41753,7 @@ export type Resolvers<ContextType = any> = ResolversObject<{
EventConnection?: EventConnectionResolvers<ContextType>;
EventEdge?: EventEdgeResolvers<ContextType>;
ExclusionList?: ExclusionListResolvers<ContextType>;
ExclusionListCacheStatus?: ExclusionListCacheStatusResolvers<ContextType>;
ExclusionListConnection?: ExclusionListConnectionResolvers<ContextType>;
ExclusionListEdge?: ExclusionListEdgeResolvers<ContextType>;
ExternalReference?: ExternalReferenceResolvers<ContextType>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { listEntitiesPaginated, storeLoadById } from '../../database/middleware-
import type { AuthContext, AuthUser } from '../../types/user';
import { type BasicStoreEntityExclusionList, ENTITY_TYPE_EXCLUSION_LIST, type StoreEntityExclusionList } from './exclusionList-types';
import { type ExclusionListContentAddInput, type ExclusionListFileAddInput, type MutationExclusionListFieldPatchArgs, type QueryExclusionListsArgs } from '../../generated/graphql';
import { notify, redisUpdateExclusionListStatus } from '../../database/redis';
import { getClusterInstances, notify, redisGetExclusionListStatus, redisUpdateExclusionListStatus } from '../../database/redis';
import { FunctionalError } from '../../config/errors';
import { updateAttribute } from '../../database/middleware';
import { publishUserAction } from '../../listener/UserActionListener';
Expand All @@ -25,6 +25,21 @@ export const findAll = (context: AuthContext, user: AuthUser, args: QueryExclusi
return listEntitiesPaginated<BasicStoreEntityExclusionList>(context, user, [ENTITY_TYPE_EXCLUSION_LIST], args);
};

export const getCacheStatus = async () => {
const redisCacheStatus = await redisGetExclusionListStatus();
const refreshAskDate = redisCacheStatus.last_refresh_ask_date ?? '';
const cacheDate = redisCacheStatus.last_cache_date ?? '';
const clusterConfig = await getClusterInstances();
const allNodeIds = clusterConfig.map((c) => c.platform_id.split(':')[2]);
let allNodeCacheUpdated = refreshAskDate === cacheDate;
for (let i = 0; i < allNodeIds.length; i += 1) {
const nodeId = allNodeIds[i];
allNodeCacheUpdated = allNodeCacheUpdated && refreshAskDate === redisCacheStatus[nodeId];
}

Check warning on line 38 in opencti-platform/opencti-graphql/src/modules/exclusionList/exclusionList-domain.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/modules/exclusionList/exclusionList-domain.ts#L29-L38

Added lines #L29 - L38 were not covered by tests

return { refreshAskDate, cacheDate, allNodeCacheUpdated };
};

Check warning on line 41 in opencti-platform/opencti-graphql/src/modules/exclusionList/exclusionList-domain.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/modules/exclusionList/exclusionList-domain.ts#L40-L41

Added lines #L40 - L41 were not covered by tests

const refreshExclusionListStatus = async () => {
await redisUpdateExclusionListStatus({ last_refresh_ask_date: (new Date()).toString() });
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Resolvers } from '../../generated/graphql';
import { findById, findAll, addExclusionListContent, addExclusionListFile, deleteExclusionList, fieldPatchExclusionList } from './exclusionList-domain';
import { findById, findAll, addExclusionListContent, addExclusionListFile, deleteExclusionList, fieldPatchExclusionList, getCacheStatus } from './exclusionList-domain';

const exclusionListResolver: Resolvers = {
Query: {
exclusionList: (_, { id }, context) => findById(context, context.user, id),
exclusionLists: (_, args, context) => findAll(context, context.user, args),
exclusionListCacheStatus: () => getCacheStatus(),
},
Mutation: {
exclusionListContentAdd: (_, { input }, context) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type ExclusionListEdge {
}

type ExclusionListCacheStatus {
refreshAskDate: DateTime!
cacheDate: DateTime!
refreshAskDate: String!
cacheDate: String!
allNodeCacheUpdated: Boolean!
}

Expand Down Expand Up @@ -58,7 +58,7 @@ type Query {
filters: FilterGroup
search: String
): ExclusionListConnection @auth(for: [SETTINGS_SETCUSTOMIZATION])
exclusionListCacheStatus():
exclusionListCacheStatus: ExclusionListCacheStatus @auth(for: [SETTINGS_SETCUSTOMIZATION])
}

type Mutation {
Expand Down

0 comments on commit 6840f87

Please sign in to comment.