Skip to content

Commit

Permalink
[api/frontend] Fix update date, sorting person, relation attributed-t…
Browse files Browse the repository at this point in the history
…o and reindex (#258, #361, #363, #364)
  • Loading branch information
Samuel Hassine committed Dec 6, 2019
1 parent 76a22e6 commit fe502c6
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ import {
} from './ReportAddObjectRefsLines';
import StixRelationCreation from '../common/stix_relations/StixRelationCreation';
import StixDomainEntityEdition from '../common/stix_domain_entities/StixDomainEntityEdition';
import StixRelationEdition, {
stixRelationEditionDeleteMutation,
} from '../common/stix_relations/StixRelationEdition';
import StixRelationEdition from '../common/stix_relations/StixRelationEdition';

const styles = () => ({
container: {
Expand Down Expand Up @@ -450,19 +448,14 @@ class ReportKnowledgeGraphComponent extends Component {
fetchQuery(reportKnowledgeGraphCheckRelationQuery, {
id: node.extras.id,
}).then((data) => {
if (data.stixRelation.reports.edges.length === 1) {
commitMutation({
mutation: stixRelationEditionDeleteMutation,
variables: {
id: node.extras.id,
},
});
}
const relationIdToDelete = data.stixRelation.reports.edges.length === 1
? node.extras.id
: node.extras.relationId;
commitMutation({
mutation: reportMutationRelationDelete,
variables: {
id: this.props.report.id,
relationId: node.extras.relationId,
relationId: relationIdToDelete,
},
});
});
Expand Down Expand Up @@ -877,7 +870,4 @@ const ReportKnowledgeGraph = createFragmentContainer(
},
);

export default compose(
inject18n,
withStyles(styles),
)(ReportKnowledgeGraph);
export default compose(inject18n, withStyles(styles))(ReportKnowledgeGraph);
Original file line number Diff line number Diff line change
Expand Up @@ -873,8 +873,8 @@ enum UsersOrdering {
email
firstname
lastname
created_at
updated_at
created
modified
tags
markingDefinitions
}
Expand Down
2 changes: 1 addition & 1 deletion opencti-platform/opencti-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
]
},
"engines": {
"node": ">= 10.*"
"node": ">= 12.*"
},
"dependencies": {
"@babel/core": "^7.6.4",
Expand Down
13 changes: 11 additions & 2 deletions opencti-platform/opencti-graphql/src/database/grakn.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ const takeReadTx = async (retry = false) => {
return takeReadTx(true);
}
};
const executeRead = async executeFunction => {
export const executeRead = async executeFunction => {
const rTx = await takeReadTx();
try {
const result = await executeFunction(rTx);
Expand Down Expand Up @@ -936,6 +936,13 @@ export const updateAttribute = async (id, input, wTx) => {
const yearInput = { key: `${key}_year`, value: [yearValue] };
await updateAttribute(id, yearInput, wTx);
}
// Update modified / updated_at
if (currentInstanceData.parent_types.includes(TYPE_STIX_DOMAIN) && key !== 'modified' && key !== 'updated_at') {
const today = now();
await updateAttribute(id, { key: 'updated_at', value: [today] }, wTx);
await updateAttribute(id, { key: 'modified', value: [today] }, wTx);
}

// Update elasticsearch
const currentIndex = inferIndexFromConceptTypes(currentInstanceData.parent_types);
// eslint-disable-next-line no-nested-ternary
Expand All @@ -957,6 +964,7 @@ export const deleteEntityById = async id => {
const query = `match $x has internal_id_key "${eid}"; $z($x, $y); delete $z, $x;`;
logger.debug(`[GRAKN - infer: false] deleteEntityById > ${query}`);
await wTx.tx.query(query, { infer: false });
}).then(async () => {
// [ELASTIC] Delete entity and relations connected to
await elDeleteInstanceIds(append(eid, relationsIds));
return id;
Expand All @@ -974,11 +982,12 @@ export const deleteRelationById = async relationId => {
const query = `match $x has internal_id_key "${eid}"; $z($x, $y); delete $z, $x;`;
logger.debug(`[GRAKN - infer: false] deleteRelationById > ${query}`);
await wTx.tx.query(query, { infer: false });
}).then(async () => {
// [ELASTIC] Update - Delete the inner indexed relations in entities
await elRemoveRelationConnection(eid);
await elDeleteInstanceIds(append(eid, relationsIds));
return relationId;
});
return eid;
};

export const timeSeries = async (query, options) => {
Expand Down
4 changes: 2 additions & 2 deletions opencti-platform/opencti-graphql/src/database/graknRoles.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export const rolesMap = {
usage: 'to'
},
'attributed-to': {
origin: 'from',
attribution: 'to'
attribution: 'from',
origin: 'to'
},
mitigates: {
mitigation: 'from',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { logger } from '../config/conf';
import { executeRead } from '../database/grakn';

module.exports.up = async next => {
logger.info('[MIGRATION] reindex_elasticsearch_lowercase > Nothing to reindex');
await executeRead(rTx => {
rTx.tx.query(`match $x isa Settings;`);
});
logger.info(`[MIGRATION] reindex > Migration complete`);
next();
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { logger } from '../config/conf';
import { executeRead } from '../database/grakn';

module.exports.up = async next => {
logger.info('[MIGRATION] reindex_users > Nothing to reindex');
await executeRead(rTx => {
rTx.tx.query(`match $x isa Settings;`);
});
next();
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ import { logger } from '../config/conf';

module.exports.up = async next => {
logger.info(`[MIGRATION] reindex > Deleting potential orphan relations, this operation could take some time...`);
await executeWrite(wTx => {
wTx.tx.query(
`match $x isa related-to; $x($from, $to); $from has internal_id_key $fk; $to has internal_id_key $tk; $fk == $tk; delete $x;`
);
wTx.tx.query(`match $r isa owned_by; delete;`);
wTx.tx.query(`match $r isa stix_relation; not {$r ($x, $y) isa stix_relation;}; delete;`);
wTx.tx.query(`match $r isa stix_relation_embedded; not {$r ($x, $y) isa stix_relation_embedded;}; delete;`);
});
logger.info(`[MIGRATION] reindex > Delete related-to same origin/target`);
try {
await executeWrite(wTx => {
wTx.tx.query(
`match $x isa related-to; $x($from, $to); $from has internal_id_key $fk; $to has internal_id_key $tk; $fk == $tk; delete $x;`
);
wTx.tx.query(`match $r isa owned_by; delete;`);
});
logger.info(`[MIGRATION] reindex > Delete orphan stix_relation`);
await executeWrite(wTx => {
wTx.tx.query(`match $r isa stix_relation; not {$r ($x, $y) isa stix_relation;}; delete;`);
});
logger.info(`[MIGRATION] reindex > Delete orphan stix_relation_embedded`);
} catch (err) {
logger.info(`[MIGRATION] reindex > Error during deleting orphan relations (${err}), try to index...`);
}
try {
await write('undefine UsageIndicatesRule sub rule;');
} catch (err) {
Expand All @@ -24,4 +32,4 @@ module.exports.up = async next => {

module.exports.down = async next => {
next();
};
};

0 comments on commit fe502c6

Please sign in to comment.