-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathStatisticsController.cs
More file actions
223 lines (197 loc) · 9.38 KB
/
StatisticsController.cs
File metadata and controls
223 lines (197 loc) · 9.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using HwProj.APIGateway.API.ExportServices;
using HwProj.APIGateway.API.Models.Statistics;
using HwProj.APIGateway.API.TableGenerators;
using HwProj.AuthService.Client;
using HwProj.CoursesService.Client;
using HwProj.Models.AuthService.DTO;
using HwProj.Models.CoursesService.DTO;
using HwProj.Models.CoursesService.ViewModels;
using HwProj.Models.Roles;
using HwProj.Models.Result;
using HwProj.SolutionsService.Client;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace HwProj.APIGateway.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StatisticsController : AggregationController
{
private readonly ISolutionsServiceClient _solutionClient;
private readonly ICoursesServiceClient _coursesClient;
private readonly GoogleService _googleService;
public StatisticsController(
ISolutionsServiceClient solutionClient,
ICoursesServiceClient coursesServiceClient,
IAuthServiceClient authServiceClient,
GoogleService googleService)
: base(authServiceClient)
{
_solutionClient = solutionClient;
_coursesClient = coursesServiceClient;
_googleService = googleService;
}
[HttpGet("{courseId}/lecturers")]
[Authorize(Roles = Roles.LecturerRole)]
[ProducesResponseType(typeof(StatisticsLecturersModel[]), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetLecturersStatistics(long courseId)
{
var statistics = await _solutionClient.GetLecturersStatistics(courseId);
if (statistics == null)
return NotFound();
var lecturers = await AuthServiceClient.GetAccountsData(statistics.Select(s => s.LecturerId).ToArray());
var result = statistics.Zip(lecturers, (stat, lecturer) => new StatisticsLecturersModel
{
Lecturer = lecturer,
NumberOfCheckedSolutions = stat.NumberOfCheckedSolutions,
NumberOfCheckedUniqueSolutions = stat.NumberOfCheckedUniqueSolutions
}).ToArray();
return Ok(result);
}
[HttpGet("{courseId}")]
[ProducesResponseType(typeof(StatisticsCourseMatesModel[]), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetCourseStatistics(long courseId)
{
var result = await GetStatistics(courseId);
if (result == null)
{
return Forbid();
}
return Ok(result);
}
private async Task<IOrderedEnumerable<StatisticsCourseMatesModel>?> GetStatistics(long courseId)
{
var statistics = await _solutionClient.GetCourseStatistics(courseId, UserId);
if (statistics == null) return null;
var studentIds = statistics.Select(t => t.StudentId).ToArray();
var getStudentsTask = AuthServiceClient.GetAccountsData(studentIds);
// Получаем пары <студент, закрепленные преподаватели (те, которые его явно в фильтре выбрали)>
var mentorsToStudents = await _coursesClient.GetMentorsToAssignedStudents(courseId);
var studentsToMentors = await GetStudentsToMentorsDictionary(mentorsToStudents);
var result = statistics.Zip(
await getStudentsTask,
(stats, student) =>
{
studentsToMentors.TryGetValue(student.UserId, out var reviewers);
return new StatisticsCourseMatesModel()
{
Id = student.UserId,
Name = student.Name,
Surname = student.Surname,
Reviewers = reviewers ?? Array.Empty<AccountDataDto>(),
Homeworks = stats.Homeworks
};
}).OrderBy(t => t.Surname).ThenBy(t => t.Name);
return result;
}
[HttpGet("{courseId}/charts")]
[ProducesResponseType(typeof(AdvancedCourseStatisticsViewModel), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetChartStatistics(long courseId)
{
var course = await _coursesClient.GetCourseById(courseId);
if (course == null)
return Forbid();
var statistics = await _solutionClient.GetCourseStatistics(courseId, UserId);
var studentIds = statistics.Select(t => t.StudentId).ToArray();
var studentsData = await AuthServiceClient.GetAccountsData(studentIds);
var students = statistics.Zip(studentsData,
(stats, student) => new StatisticsCourseMatesModel
{
Id = student.UserId,
Name = student.Name,
Surname = student.Surname,
Homeworks = stats.Homeworks
}).OrderBy(t => t.Surname).ThenBy(t => t.Name);
var statisticsMeasure = await _solutionClient.GetBenchmarkStatistics(courseId);
var result = new AdvancedCourseStatisticsViewModel
{
Course = new CoursePreview
{
Id = course.Id,
Name = course.Name,
GroupName = course.GroupName,
},
StudentStatistics = students.ToArray(),
Homeworks = course.Homeworks,
AverageStudentSolutions = statisticsMeasure.AverageStudentSolutions,
BestStudentSolutions = statisticsMeasure.BestStudentSolutions
};
return Ok(result);
}
/// <summary>
/// Implements file download.
/// </summary>
/// <param name="courseId">The course Id the report is based on.</param>
/// <param name="sheetName">Name of the sheet on which the report will be generated.</param>
/// <returns>File download process.</returns>
[HttpGet("getFile")]
public async Task<IActionResult> GetFile(long courseId, string sheetName)
{
var course = await _coursesClient.GetCourseById(courseId);
var statistics = await GetStatistics(courseId);
if (statistics == null || course == null) return Forbid();
var statisticStream =
await ExcelGenerator.Generate(statistics.ToList(), course, sheetName).GetAsByteArrayAsync();
return new FileContentResult(statisticStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
[HttpGet("getSheetTitles")]
public async Task<Result<string[]>> GetSheetTitles(string sheetUrl)
=> await _googleService.GetSheetTitles(sheetUrl);
[HttpPost("processLink")]
public Result ProcessLink(string? sheetUrl)
{
if (sheetUrl == null) return Result.Failed("Некорректная ссылка");
if (GoogleService.ParseLink(sheetUrl).Succeeded) return Result.Success();
return Result.Failed("Некорректная ссылка");
}
/// <summary>
/// Implements sending a report to the Google Sheets.
/// </summary>
/// <param name="courseId">The course Id the report is based on.</param>
/// <param name="sheetUrl">Sheet Url parameter, required to make requests to the Google Sheets.</param>
/// <param name="sheetName">Sheet Name parameter, required to make requests to the Google Sheets.</param>
/// <returns>Operation status.</returns>
[HttpGet("exportToSheet")]
public async Task<Result> ExportToGoogleSheets(
long courseId, string sheetUrl, string sheetName)
{
var course = await _coursesClient.GetCourseById(courseId);
var statistics = await GetStatistics(courseId);
if (course == null || statistics == null) return Result.Failed("Ошибка при получении статистики");
var result = await _googleService.Export(course, statistics, sheetUrl, sheetName);
return result;
}
private async Task<Dictionary<string, AccountDataDto[]>> GetStudentsToMentorsDictionary(
MentorToAssignedStudentsDTO[] mentorsToStudents)
{
var mentorsIds = mentorsToStudents.Select(mts => mts.MentorId).ToArray();
var mentorsAccountData = await AuthServiceClient.GetAccountsData(mentorsIds);
var mentorIdToAccountData = mentorsAccountData
.ToDictionary(
accountData => accountData.UserId,
accountData => accountData
);
return mentorsToStudents
.SelectMany(m =>
m.SelectedStudentsIds.Select(studentId =>
new
{
StudentId = studentId,
Reviewer = mentorIdToAccountData[m.MentorId]
})
)
.GroupBy(sr => sr.StudentId)
.ToDictionary(
groups => groups.Key,
groups => groups.Select(sr => sr.Reviewer)
.Distinct()
.ToArray()
);
}
}
}