11package de .tum .cit .aet .dataProcessing .service ;
22
3+ import de .tum .cit .aet .analysis .dto .AuthorContributionDTO ;
34import de .tum .cit .aet .analysis .service .AnalysisService ;
45import de .tum .cit .aet .core .dto .ArtemisCredentials ;
56import de .tum .cit .aet .repositoryProcessing .domain .*;
@@ -36,6 +37,22 @@ public RequestService(RepositoryFetchingService repositoryFetchingService, Analy
3637 this .studentRepository = studentRepository ;
3738 }
3839
40+ /**
41+ * Fetches, analyzes, and saves repository data using the provided Artemis credentials.
42+ *
43+ * @param credentials The Artemis credentials
44+ */
45+ public void fetchAnalyzeAndSaveRepositories (ArtemisCredentials credentials ) {
46+ // Fetch and clone repositories
47+ List <TeamRepositoryDTO > repositories = fetchAndCloneRepositories (credentials );
48+
49+ // Analyze contributions
50+ Map <Long , AuthorContributionDTO > contributionData = getContributionData (repositories );
51+
52+ // Save results to the database
53+ saveResults (repositories , contributionData );
54+ }
55+
3956 /**
4057 * Fetches and clones all repositories from Artemis using dynamic credentials.
4158 *
@@ -53,42 +70,25 @@ public List<TeamRepositoryDTO> fetchAndCloneRepositories(ArtemisCredentials cred
5370 * @param repositories List of TeamRepositoryDTO to be analyzed
5471 * @return Map of Participant ID to an array of contribution metrics (e.g., lines added, lines deleted)
5572 */
56- public Map <Long , int [] > getContributionData (List <TeamRepositoryDTO > repositories ) {
73+ public Map <Long , AuthorContributionDTO > getContributionData (List <TeamRepositoryDTO > repositories ) {
5774 return analysisService .analyzeContributions (repositories );
5875 }
5976
60- /**
61- * Fetches, analyzes, and saves repository data using the provided Artemis credentials.
62- *
63- * @param credentials The Artemis credentials
64- */
65- public void fetchAnalyzeAndSaveRepositories (ArtemisCredentials credentials ) {
66- // Fetch and clone repositories
67- List <TeamRepositoryDTO > repositories = fetchAndCloneRepositories (credentials );
68-
69- // Analyze contributions
70- Map <Long , int []> contributionData = getContributionData (repositories );
71- for (int [] lines : contributionData .values ()) {
72- log .info ("Contribution Data - Added Lines: {}, Deleted Lines: {}" , lines [0 ], lines [1 ]);
73- }
74-
75- // Save results to the database
76- saveResults (repositories , contributionData );
77- }
78-
7977 /**
8078 * Saves the fetched repository information into the database.
8179 *
8280 * @param repositories List of TeamRepositoryDTO to be saved
8381 * @param contributionData Map of Participant ID to an array of contribution metrics (e.g., lines added, lines deleted)
8482 */
85- public void saveResults (List <TeamRepositoryDTO > repositories , Map <Long , int []> contributionData ) {
83+ public void saveResults (List <TeamRepositoryDTO > repositories , Map <Long , AuthorContributionDTO > contributionData ) {
84+ // Clear existing data in database tables. We assume a full refresh of all data is intended, effectively treating the run as idempotent
8685 teamRepositoryRepository .deleteAll ();
8786 studentRepository .deleteAll ();
8887 teamParticipationRepository .deleteAll ();
8988 tutorRepository .deleteAll ();
9089
9190 for (TeamRepositoryDTO repo : repositories ) {
91+ // Save tutor
9292 ParticipantDTO tut = repo .participation ().team ().owner ();
9393 Tutor tutor = null ;
9494 if (tut == null ) {
@@ -98,25 +98,35 @@ public void saveResults(List<TeamRepositoryDTO> repositories, Map<Long, int[]> c
9898 tutorRepository .save (tutor );
9999 }
100100
101+ // Save team participation
101102 ParticipationDTO participation = repo .participation ();
102103 TeamDTO team = participation .team ();
103104 TeamParticipation teamParticipation = new TeamParticipation (participation .id (), team .id (), tutor , team .name (), team .shortName (), participation .repositoryUri (), participation .submissionCount ());
104105 teamParticipationRepository .save (teamParticipation );
105106
107+ // Save students with contributions
106108 List <Student > students = new ArrayList <>();
107109 for (ParticipantDTO student : repo .participation ().team ().students ()) {
108- int [] lines = contributionData .get (student .id ());
109- if (lines == null ) {
110- lines = new int []{0 , 0 , 0 };
110+ AuthorContributionDTO contributionDTO = contributionData .get (student .id ());
111+
112+ // Handle the case where a student made no contributions (e.g., if they were registered but never committed)
113+ if (contributionDTO == null ) {
114+ contributionDTO = new AuthorContributionDTO (0 , 0 , 0 );
111115 }
112- students .add (new Student (student .id (), student .login (), student .name (), student .email (), teamParticipation , lines [2 ], lines [0 ], lines [1 ], lines [0 ] + lines [1 ]));
116+
117+ students .add (new Student (student .id (), student .login (), student .name (), student .email (), teamParticipation ,
118+ contributionDTO .commitCount (), contributionDTO .linesAdded (), contributionDTO .linesDeleted (),
119+ contributionDTO .linesAdded () + contributionDTO .linesDeleted ()));
113120 }
114121 studentRepository .saveAll (students );
115122
123+ // Save team repository
116124 TeamRepository teamRepo = new TeamRepository (teamParticipation , null , repo .localPath (), repo .isCloned (), repo .error ());
117125
126+ // Process VCS logs
118127 List <VCSLog > vcsLogs = repo .vcsLogs ().stream ().map (log -> new VCSLog (teamRepo , log .commitHash (), log .email ())).toList ();
119128
129+ // Save the TeamRepository (through cascade, VCSLogs will also be saved)
120130 teamRepo .setVcsLogs (vcsLogs );
121131 teamRepositoryRepository .save (teamRepo );
122132
@@ -125,17 +135,17 @@ public void saveResults(List<TeamRepositoryDTO> repositories, Map<Long, int[]> c
125135 }
126136
127137 /**
128- * Extracts all saved repository and contribution data from the database.
138+ * Retrieves all stored repository data from the database and assembles it into ClientResponseDTOs .
129139 *
130- * @return A list of ClientResponseDTOs containing all aggregated team and student data.
140+ * @return List of ClientResponseDTO containing the assembled data
131141 */
132142 public List <ClientResponseDTO > getAllRepositoryData () {
133143 log .info ("RequestService: Initiating data extraction from database" );
134144
135- // 1. Fetch all TeamParticipation records
145+ // Fetch all TeamParticipation records
136146 List <TeamParticipation > participations = teamParticipationRepository .findAll ();
137147
138- // 2. Map and assemble the data into ClientResponseDTOs
148+ // Map and assemble the data into ClientResponseDTOs
139149 List <ClientResponseDTO > responseDTOs = participations .stream ()
140150 .map (participation -> {
141151 List <Student > students = studentRepository .findAllByTeam (participation );
@@ -161,7 +171,7 @@ public List<ClientResponseDTO> getAllRepositoryData() {
161171 })
162172 .toList ();
163173
164- log .info ("RequestService: Extracted {} team participation records." , responseDTOs .size ());
174+ log .info ("RequestService: Extracted {} team participation records from the database ." , responseDTOs .size ());
165175 return responseDTOs ;
166176 }
167177}
0 commit comments