Skip to content

Commit

Permalink
[backend] Works batching
Browse files Browse the repository at this point in the history
  • Loading branch information
Kedae authored Aug 14, 2024
1 parent 744e738 commit 62fb3f0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
30 changes: 28 additions & 2 deletions opencti-platform/opencti-graphql/src/domain/file.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import * as R from 'ramda';
import { Readable } from 'stream';
import { SEMATTRS_DB_NAME, SEMATTRS_DB_OPERATION } from '@opentelemetry/semantic-conventions';
import { logApp } from '../config/conf';
import { deleteFile, loadFile, uploadJobImport } from '../database/file-storage';
import { internalLoadById } from '../database/middleware-loader';
import { internalLoadById, listAllEntities } from '../database/middleware-loader';
import { buildContextDataForFile, completeContextDataForEntity, publishUserAction } from '../listener/UserActionListener';
import { stixCoreObjectImportDelete } from './stixCoreObject';
import { extractEntityRepresentativeName } from '../database/entity-representative';
import { allFilesMimeTypeDistribution, allRemainingFilesCount } from '../modules/internal/document/document-domain';
import { getManagerConfigurationFromCache } from '../modules/managerConfiguration/managerConfiguration-domain';
import { supportedMimeTypes } from '../modules/managerConfiguration/managerConfiguration-utils';
import { SYSTEM_USER } from '../utils/access';
import { isEmptyField, isNotEmptyField, READ_INDEX_FILES } from '../database/utils';
import { isEmptyField, isNotEmptyField, READ_INDEX_FILES, READ_INDEX_HISTORY } from '../database/utils';
import { getStats } from '../database/engine';
import { controlUserConfidenceAgainstElement } from '../utils/confidence-level';
import { uploadToStorage } from '../database/file-storage-helper';
import { extractContentFrom } from '../utils/fileToContent';
import { stixLoadById } from '../database/middleware';
import { getEntitiesMapFromCache } from '../database/cache';
import { ENTITY_TYPE_MARKING_DEFINITION } from '../schema/stixMetaObject';
import { FilterMode, OrderingMode } from '../generated/graphql';
import { telemetry } from '../config/tracing';
import { ENTITY_TYPE_WORK } from '../schema/internalObject';

export const buildOptionsFromFileManager = async (context) => {
let importPaths = ['import/'];
Expand Down Expand Up @@ -195,3 +199,25 @@ export const batchFileMarkingDefinitions = async (context, user, files) => {
])(markings);
});
};

export const batchFileWorks = async (context, user, files) => {
const getWorkForFileFn = async () => {
const filters = {
mode: FilterMode.And,
filters: [{ key: ['event_source_id'], values: files }],
filterGroups: [],
};
const items = await listAllEntities(context, user, [ENTITY_TYPE_WORK], {
indices: [READ_INDEX_HISTORY],
connectionFormat: false,
orderBy: 'timestamp',
orderMode: OrderingMode.Desc,
filters,
});
return files.map((fileId) => items.filter(({ event_source_id }) => event_source_id === fileId));
};
return telemetry(context, user, 'BATCH works for file', {
[SEMATTRS_DB_NAME]: 'file_domain',
[SEMATTRS_DB_OPERATION]: 'read',
}, getWorkForFileFn);
};
6 changes: 3 additions & 3 deletions opencti-platform/opencti-graphql/src/resolvers/file.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { loadFile } from '../database/file-storage';
import { askJobImport, batchFileMarkingDefinitions, deleteImport, filesMetrics, uploadImport, uploadPending } from '../domain/file';
import { worksForSource } from '../domain/work';
import { askJobImport, batchFileMarkingDefinitions, batchFileWorks, deleteImport, filesMetrics, uploadImport, uploadPending } from '../domain/file';
import { batchLoader } from '../database/middleware';
import { batchCreator } from '../domain/user';
import { batchStixDomainObjects } from '../domain/stixDomainObject';
Expand All @@ -9,6 +8,7 @@ import { paginatedForPathWithEnrichment } from '../modules/internal/document/doc
const creatorLoader = batchLoader(batchCreator);
const domainLoader = batchLoader(batchStixDomainObjects);
const markingDefinitionsLoader = batchLoader(batchFileMarkingDefinitions);
const worksLoader = batchLoader(batchFileWorks);

const fileResolvers = {
Query: {
Expand All @@ -23,7 +23,7 @@ const fileResolvers = {
},
File: {
objectMarking: (rel, _, context) => markingDefinitionsLoader.load(rel, context, context.user),
works: (file, _, context) => worksForSource(context, context.user, file.id),
works: (file, _, context) => worksLoader.load(file.id, context, context.user),
},
FileMetadata: {
entity: (metadata, _, context) => domainLoader.load(metadata.entity_id, context, context.user),
Expand Down

0 comments on commit 62fb3f0

Please sign in to comment.