Skip to content

Commit

Permalink
[backend/frontend] Empower the injects timeline with new interactions (
Browse files Browse the repository at this point in the history
  • Loading branch information
savacano28 authored Jun 18, 2024
1 parent ae5f5ce commit 41915fb
Show file tree
Hide file tree
Showing 19 changed files with 289 additions and 272 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public List<ComcheckStatus> comcheckStatuses(@PathVariable String exercise, @Pat
@PreAuthorize("isExerciseObserver(#exerciseId)")
public Iterable<TeamSimple> getExerciseTeams(@PathVariable String exerciseId) {
return TeamHelper.rawTeamToSimplerTeam(teamRepository.rawTeamByExerciseId(exerciseId),
injectExpectationRepository,communicationRepository, exerciseTeamUserRepository, scenarioRepository);
injectExpectationRepository, injectRepository, communicationRepository, exerciseTeamUserRepository, scenarioRepository);
}

@Transactional(rollbackOn = Exception.class)
Expand Down
27 changes: 24 additions & 3 deletions openbas-api/src/main/java/io/openbas/rest/helper/TeamHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.openbas.database.repository.CommunicationRepository;
import io.openbas.database.repository.ExerciseTeamUserRepository;
import io.openbas.database.repository.InjectExpectationRepository;
import io.openbas.database.repository.InjectRepository;
import io.openbas.database.repository.ScenarioRepository;

import java.util.List;
Expand All @@ -17,6 +18,7 @@ public class TeamHelper {

public static List<TeamSimple> rawTeamToSimplerTeam(List<RawTeam> teams,
InjectExpectationRepository injectExpectationRepository,
InjectRepository injectRepository,
CommunicationRepository communicationRepository,
ExerciseTeamUserRepository exerciseTeamUserRepository,
ScenarioRepository scenarioRepository) {
Expand Down Expand Up @@ -109,11 +111,30 @@ public static List<TeamSimple> rawTeamToSimplerTeam(List<RawTeam> teams,
}

// We set the injects linked to the scenarios
teamSimple.setScenariosInjects(rawTeam.getTeam_scenarios().stream().flatMap(
teamSimple.setScenariosInjects(
getInjectTeamsIds(teamSimple.getId(),
rawTeam.getTeam_scenarios().stream().flatMap(
scenario -> mapInjectsByScenarioIds.get(scenario).stream()
).collect(Collectors.toSet()));
).collect(Collectors.toSet()),
injectRepository)
);

// We set the injects linked to the exercises
teamSimple.setExercisesInjects(
getInjectTeamsIds(teamSimple.getId(),
rawTeam.getTeam_exercise_injects(),
injectRepository)
);

return teamSimple;
return teamSimple;
}).collect(Collectors.toList());
}

private static Set<String> getInjectTeamsIds(final String teamId, Set<String> injectIds, final InjectRepository injectRepository) {
Set<RawInject> rawInjectTeams = injectRepository.findRawInjectTeams(injectIds, teamId);
return rawInjectTeams.stream()
.map(RawInject::getInject_id)
.collect(Collectors.toSet());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class InjectOutput {
@JsonProperty("inject_type")
public String injectType;

@JsonProperty("inject_teams")
private Set<String> teams;

public InjectOutput(
String id,
String title,
Expand Down Expand Up @@ -81,5 +84,6 @@ public InjectOutput(
assetGroups != null ? new HashSet<>(Arrays.asList(assetGroups)) : new HashSet<>()
);
this.injectType = injectType;
this.teams = teams != null ? new HashSet<>(Arrays.asList(teams)) : new HashSet<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class ScenarioApi {
private TeamRepository teamRepository;
private UserRepository userRepository;
private InjectExpectationRepository injectExpectationRepository;
private InjectRepository injectRepository;
private CommunicationRepository communicationRepository;
private ExerciseTeamUserRepository exerciseTeamUserRepository;
private ScenarioRepository scenarioRepository;
Expand All @@ -65,6 +66,11 @@ public void setInjectExpectationRepository(InjectExpectationRepository injectExp
this.injectExpectationRepository = injectExpectationRepository;
}

@Autowired
public void setInjectRepository(InjectRepository injectRepository) {
this.injectRepository = injectRepository;
}

@Autowired
public void setCommunicationRepository(CommunicationRepository communicationRepository) {
this.communicationRepository = communicationRepository;
Expand Down Expand Up @@ -190,7 +196,7 @@ public Iterable<Team> addScenarioTeams(
@PreAuthorize("isScenarioObserver(#scenarioId)")
public Iterable<TeamSimple> scenarioTeams(@PathVariable @NotBlank final String scenarioId) {
return TeamHelper.rawTeamToSimplerTeam(teamRepository.rawTeamByScenarioId(scenarioId),
injectExpectationRepository,communicationRepository, exerciseTeamUserRepository, scenarioRepository);
injectExpectationRepository, injectRepository, communicationRepository, exerciseTeamUserRepository, scenarioRepository);
}

@Transactional(rollbackOn = Exception.class)
Expand Down
8 changes: 7 additions & 1 deletion openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class TeamApi extends RestBehavior {
private TeamRepository teamRepository;
private CommunicationRepository communicationRepository;
private InjectExpectationRepository injectExpectationRepository;
private InjectRepository injectRepository;
private UserRepository userRepository;
private OrganizationRepository organizationRepository;
private TagRepository tagRepository;
Expand Down Expand Up @@ -85,6 +86,11 @@ public void setInjectExpectationRepository(InjectExpectationRepository injectExp
this.injectExpectationRepository = injectExpectationRepository;
}

@Autowired
public void setInjectRepository(InjectRepository injectRepository) {
this.injectRepository = injectRepository;
}

@Autowired
public void setCommunicationRepository(CommunicationRepository communicationRepository) {
this.communicationRepository = communicationRepository;
Expand Down Expand Up @@ -118,7 +124,7 @@ public Iterable<TeamSimple> getTeams() {
teams = teamRepository.rawTeamsAccessibleFromOrganization(organizationIds);
}

return TeamHelper.rawTeamToSimplerTeam(teams, injectExpectationRepository, communicationRepository,
return TeamHelper.rawTeamToSimplerTeam(teams, injectExpectationRepository, injectRepository, communicationRepository,
exerciseTeamUserRepository, scenarioRepository);
}

Expand Down
10 changes: 0 additions & 10 deletions openbas-front/src/actions/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,6 @@ export const storeHelper = (state) => ({
getExerciseCommunications: (id) => entities('communications', state).filter(
(i) => i.communication_exercise === id,
),
getExerciseTechnicalInjectsWithNoTeam: (id) => {
return getInjectsWithParsedInjectorContractContent(
entities('injects', state),
).filter((i) => !(i.inject_injector_contract?.injector_contract_content_parsed.fields.filter((f) => f.name === 'teams').length > 0) && i.inject_exercise === id);
},
getExerciseObjectives: (id) => entities('objectives', state).filter((o) => o.objective_exercise === id),
getExerciseLogs: (id) => entities('logs', state).filter((l) => l.log_exercise === id),
getExerciseLessonsCategories: (id) => entities('lessonscategorys', state).filter(
Expand Down Expand Up @@ -487,11 +482,6 @@ export const storeHelper = (state) => ({
getScenarioArticles: (id) => entities('articles', state).filter((i) => i.article_scenario === id),
getScenarioChallenges: (id) => entities('challenges', state).filter((c) => c.challenge_scenarios.includes(id)),
getScenarioInjects: (id) => getInjectsWithParsedInjectorContractContent(entities('injects', state).filter((i) => i.inject_scenario === id)),
getScenarioTechnicalInjectsWithNoTeam: (id) => {
return getInjectsWithParsedInjectorContractContent(
entities('injects', state),
).filter((i) => !(i.inject_injector_contract?.injector_contract_content_parsed.fields.filter((f) => f.name === 'teams').length > 0) && i.inject_scenario === id);
},
getTeamScenarioInjects: (id) => entities('injects', state).filter((i) => (entity(id, 'teams', state) || {}).team_scenario_injects?.includes(
i.inject_id,
)),
Expand Down
3 changes: 2 additions & 1 deletion openbas-front/src/actions/injects/Inject.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export type InjectInput = {
inject_depends_duration_seconds: number;
};

export type InjectStore = Omit<Inject, 'inject_tags' | 'inject_content' | 'inject_injector_contract'> & {
export type InjectStore = Omit<Inject, 'inject_tags' | 'inject_content' | 'inject_injector_contract' | 'inject_teams'> & {
inject_tags: string[] | undefined;
inject_teams: string[] | undefined;
inject_content: { expectationScore: number, challenges: string[] | undefined }
inject_injector_contract: {
// as we don't know the type of the content of a contract we need to put any here
Expand Down
2 changes: 0 additions & 2 deletions openbas-front/src/actions/injects/inject-helper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ export interface InjectHelper {

getExerciseInjects: (exerciseId: Exercise['exercise_id']) => Inject[];
getExerciseInjectExpectations: (scenarioId: Scenario['scenario_id']) => InjectExpectation[];
getExerciseTechnicalInjectsWithNoTeam: (exerciseId: Exercise['exercise_id']) => Inject[];
getTeamExerciseInjects: (teamId: Team['team_id']) => Inject[];

getScenarioInjects: (scenarioId: Scenario['scenario_id']) => Inject[];
getScenarioTechnicalInjectsWithNoTeam: (scenarioId: Scenario['scenario_id']) => Inject[];
getTeamScenarioInjects: (teamId: Team['team_id']) => Inject[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const Articles: FunctionComponent<Props> = ({ articles }) => {
}));
const sortedArticles: FullArticleStore[] = R.filter(
(n: FullArticleStore) => channels.length === 0
|| channels.map((o) => o.id).includes(n.article_fullchannel.channel_id ?? ''),
|| channels.map((o) => o.id).includes(n.article_fullchannel?.channel_id ?? ''),
filtering.filterAndSort(fullArticles),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const iconSelector = (type, variant, fontSize, done, disabled) => {
style={{
marginTop: variant === 'list' ? 5 : 0,
padding: variant === 'timeline' ? 1 : 0,
cursor: 'pointer',
width: fontSize === 'small' || variant === 'inline' ? 20 : 24,
height: fontSize === 'small' || variant === 'inline' ? 20 : 24,
borderRadius: 4,
Expand All @@ -34,11 +35,11 @@ const iconSelector = (type, variant, fontSize, done, disabled) => {
};

const InjectIcon = (props) => {
const { type, size, variant, tooltip, done, disabled } = props;
const { type, size, variant, tooltip, done, disabled, onClick } = props;
const fontSize = size || 'medium';
if (tooltip) {
return (
<CustomTooltip title={tooltip}>
<CustomTooltip title={tooltip} onClick={onClick}>
{iconSelector(type, variant, fontSize, done, disabled)}
</CustomTooltip>
);
Expand All @@ -49,10 +50,11 @@ const InjectIcon = (props) => {
InjectIcon.propTypes = {
type: PropTypes.string,
size: PropTypes.string,
tooltip: PropTypes.string,
tooltip: PropTypes.node,
variant: PropTypes.string,
done: PropTypes.bool,
disabled: PropTypes.bool,
onClick: PropTypes.func,
};

export default InjectIcon;
Loading

0 comments on commit 41915fb

Please sign in to comment.