Skip to content

Commit

Permalink
[backend] create publicStixCoreObjectsMultiTimeSeries api(#4903)
Browse files Browse the repository at this point in the history
  • Loading branch information
marieflorescontact committed Jan 15, 2024
1 parent 441f339 commit 007b2bc
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7075,6 +7075,7 @@ type Query {
stixCoreObjectsExportFiles(first: Int, exportContext: ExportContext!): FileConnection
stixCoreObjectsTimeSeries(authorId: String, field: String!, operation: StatsOperation!, startDate: DateTime!, endDate: DateTime, interval: String!, onlyInferred: Boolean, types: [String], filters: FilterGroup, search: String): [TimeSeries]
stixCoreObjectsMultiTimeSeries(operation: StatsOperation!, startDate: DateTime!, endDate: DateTime, interval: String!, onlyInferred: Boolean, timeSeriesParameters: [StixCoreObjectsTimeSeriesParameters]): [MultiTimeSeries]
publicStixCoreObjectsMultiTimeSeries(startDate: DateTime!, endDate: DateTime, interval: String!, onlyInferred: Boolean, dashboardId: String!, widgetId: String!): [MultiTimeSeries]
stixCoreObjectsNumber(types: [String], startDate: DateTime, endDate: DateTime, onlyInferred: Boolean, filters: FilterGroup, search: String): Number
stixCoreObjectsMultiNumber(startDate: DateTime, endDate: DateTime, onlyInferred: Boolean, numberParameters: [StixCoreObjectsNumberParameters]): [Number]
stixCoreObjectsDistribution(objectId: [String], relationship_type: [String], toTypes: [String], elementWithTargetTypes: [String], field: String!, startDate: DateTime, endDate: DateTime, dateAttribute: String, operation: StatsOperation!, limit: Int, order: String, types: [String], filters: FilterGroup, search: String): [Distribution]
Expand Down Expand Up @@ -10868,4 +10869,4 @@ input CsvMapperAddInput {
separator: String!
representations: String!
skipLineChar: String
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10937,6 +10937,14 @@ type Query {
onlyInferred: Boolean
timeSeriesParameters: [StixCoreObjectsTimeSeriesParameters]
): [MultiTimeSeries] @auth(for: [KNOWLEDGE, EXPLORE])
publicStixCoreObjectsMultiTimeSeries(
startDate: DateTime!
endDate: DateTime
interval: String!
onlyInferred: Boolean
dashboardId: String!
widgetId: String!
): [MultiTimeSeries]
stixCoreObjectsNumber(
types: [String]
startDate: DateTime
Expand Down
43 changes: 41 additions & 2 deletions opencti-platform/opencti-graphql/src/domain/stixCoreObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { internalLoadById, listEntities, storeLoadById, storeLoadByIds } from '../database/middleware-loader';
import { findAll as relationFindAll } from './stixCoreRelationship';
import { delEditContext, lockResource, notify, setEditContext, storeUpdateEvent } from '../database/redis';
import { BUS_TOPICS } from '../config/conf';
import { BUS_TOPICS, logApp } from '../config/conf';
import { FunctionalError, LockTimeoutError, TYPE_LOCK_ERROR, UnsupportedError } from '../config/errors';
import { isStixCoreObject, stixCoreObjectOptions } from '../schema/stixCoreObject';
import { findById as findStatusById } from './status';
Expand Down Expand Up @@ -56,7 +56,7 @@ import { findById as documentFindById } from '../modules/internal/document/docum
import { elCount, elUpdateElement } from '../database/engine';
import { generateStandardId, getInstanceIds } from '../schema/identifier';
import { askEntityExport, askListExport, exportTransformFilters } from './stix';
import { isEmptyField, isNotEmptyField, READ_ENTITIES_INDICES, READ_INDEX_INFERRED_ENTITIES } from '../database/utils';
import { fromBase64, isEmptyField, isNotEmptyField, READ_ENTITIES_INDICES, READ_INDEX_INFERRED_ENTITIES } from '../database/utils';
import { RELATION_RELATED_TO, STIX_CORE_RELATIONSHIPS } from '../schema/stixCoreRelationship';
import { ENTITY_TYPE_CONTAINER_CASE } from '../modules/case/case-types';
import { getEntitySettingFromCache } from '../modules/entitySetting/entitySetting-utils';
Expand All @@ -66,6 +66,8 @@ import { extractEntityRepresentativeName } from '../database/entity-representati
import { addFilter, extractFilterGroupValues } from '../utils/filtering/filtering-utils';
import { specialFilterKeysWhoseValueToResolve } from '../utils/filtering/filtering-constants';
import { schemaRelationsRefDefinition } from '../schema/schema-relationsRef';
import { ENTITY_TYPE_WORKSPACE } from '../modules/workspace/workspace-types';
import { findById as findUserById } from './user';

export const findAll = async (context, user, args) => {
let types = [];
Expand Down Expand Up @@ -260,6 +262,43 @@ export const stixCoreObjectsMultiTimeSeries = (context, user, args) => {
}));
};

export const publicStixCoreObjectsMultiTimeSeries = async (context, user, args) => {
// TODO : get User and Dashboard from cache
// TODO Get User from manifest => user should be added to manifest

// Get dashboard manifest
const dashboard = await storeLoadById(
context,
user,
args.dashboardId,
ENTITY_TYPE_WORKSPACE,
);

// Get widget query configuration
const parsedManifest = JSON.parse(fromBase64(dashboard.manifest) ?? '{}');
const { widgets } = parsedManifest;
const widgetConfigs = widgets[args.widgetId].dataSelection;
const timeSeriesParameters = [];
widgetConfigs.map((widgetConfig) => timeSeriesParameters.push({
field: widgetConfig.date_attribute,
filters: widgetConfig.filters,
types: [
'Stix-Core-Object' // ?? should be in the manifest
]
}));

const standardArgs = {
operation: 'count', // TODO 'count' is harcoded for now but should come from manifest
startDate: args.startDate,
endDate: args.endDate,
interval: args.interval,
timeSeriesParameters
};

// Use standard API
return stixCoreObjectsMultiTimeSeries(context, user, standardArgs);
};

export const stixCoreObjectsNumber = (context, user, args) => {
let types = [];
if (isNotEmptyField(args.types)) {
Expand Down
12 changes: 12 additions & 0 deletions opencti-platform/opencti-graphql/src/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17034,6 +17034,7 @@ export type Query = {
playbooks?: Maybe<PlaybookConnection>;
position?: Maybe<Position>;
positions?: Maybe<PositionConnection>;
publicStixCoreObjectsMultiTimeSeries?: Maybe<Array<Maybe<MultiTimeSeries>>>;
rabbitMQMetrics?: Maybe<RabbitMqMetrics>;
region?: Maybe<Region>;
regions?: Maybe<RegionConnection>;
Expand Down Expand Up @@ -18329,6 +18330,16 @@ export type QueryPositionsArgs = {
};


export type QueryPublicStixCoreObjectsMultiTimeSeriesArgs = {
dashboardId: Scalars['String']['input'];
endDate?: InputMaybe<Scalars['DateTime']['input']>;
interval: Scalars['String']['input'];
onlyInferred?: InputMaybe<Scalars['Boolean']['input']>;
startDate: Scalars['DateTime']['input'];
widgetId: Scalars['String']['input'];
};


export type QueryRabbitMqMetricsArgs = {
prefix?: InputMaybe<Scalars['String']['input']>;
};
Expand Down Expand Up @@ -33884,6 +33895,7 @@ export type QueryResolvers<ContextType = any, ParentType extends ResolversParent
playbooks?: Resolver<Maybe<ResolversTypes['PlaybookConnection']>, ParentType, ContextType, Partial<QueryPlaybooksArgs>>;
position?: Resolver<Maybe<ResolversTypes['Position']>, ParentType, ContextType, RequireFields<QueryPositionArgs, 'id'>>;
positions?: Resolver<Maybe<ResolversTypes['PositionConnection']>, ParentType, ContextType, Partial<QueryPositionsArgs>>;
publicStixCoreObjectsMultiTimeSeries?: Resolver<Maybe<Array<Maybe<ResolversTypes['MultiTimeSeries']>>>, ParentType, ContextType, RequireFields<QueryPublicStixCoreObjectsMultiTimeSeriesArgs, 'dashboardId' | 'interval' | 'startDate' | 'widgetId'>>;
rabbitMQMetrics?: Resolver<Maybe<ResolversTypes['RabbitMQMetrics']>, ParentType, ContextType, Partial<QueryRabbitMqMetricsArgs>>;
region?: Resolver<Maybe<ResolversTypes['Region']>, ParentType, ContextType, RequireFields<QueryRegionArgs, 'id'>>;
regions?: Resolver<Maybe<ResolversTypes['RegionConnection']>, ParentType, ContextType, Partial<QueryRegionsArgs>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
findAll,
findById,
findFiltersRepresentatives,
publicStixCoreObjectsMultiTimeSeries,
stixCoreObjectAddRelation,
stixCoreObjectAddRelations,
stixCoreObjectCleanContext,
Expand All @@ -34,7 +35,7 @@ import {
stixCoreObjectsNumber,
stixCoreObjectsTimeSeries,
stixCoreObjectsTimeSeriesByAuthor,
stixCoreRelationships,
stixCoreRelationships
} from '../domain/stixCoreObject';
import { fetchEditContext, pubSubAsyncIterator } from '../database/redis';
import { batchLoader, distributionRelations, stixLoadByIdStringify } from '../database/middleware';
Expand Down Expand Up @@ -74,6 +75,7 @@ const stixCoreObjectResolvers = {
return stixCoreObjectsTimeSeries(context, context.user, args);
},
stixCoreObjectsMultiTimeSeries: (_, args, context) => stixCoreObjectsMultiTimeSeries(context, context.user, args),
publicStixCoreObjectsMultiTimeSeries: (_, args, context) => publicStixCoreObjectsMultiTimeSeries(context, context.user, args),
stixCoreObjectsNumber: (_, args, context) => stixCoreObjectsNumber(context, context.user, args),
stixCoreObjectsMultiNumber: (_, args, context) => stixCoreObjectsMultiNumber(context, context.user, args),
stixCoreObjectsDistribution: (_, args, context) => {
Expand Down

0 comments on commit 007b2bc

Please sign in to comment.