-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathVideoLearningEventsCsvExportController.java
More file actions
99 lines (88 loc) · 3.67 KB
/
VideoLearningEventsCsvExportController.java
File metadata and controls
99 lines (88 loc) · 3.67 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
package ai.elimu.web.analytics.students;
import ai.elimu.dao.StudentDao;
import ai.elimu.dao.VideoLearningEventDao;
import ai.elimu.entity.analytics.VideoLearningEvent;
import ai.elimu.entity.analytics.students.Student;
import ai.elimu.util.AnalyticsHelper;
import ai.elimu.util.DiscordHelper;
import ai.elimu.util.DiscordHelper.Channel;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/analytics/students/{studentId}/video-learning-events.csv")
@RequiredArgsConstructor
@Slf4j
public class VideoLearningEventsCsvExportController {
private final StudentDao studentDao;
private final VideoLearningEventDao videoLearningEventDao;
@GetMapping
public void handleRequest(
@PathVariable Long studentId,
HttpServletResponse response,
OutputStream outputStream
) throws IOException {
log.info("handleRequest");
try {
Student student = studentDao.read(studentId);
log.info("student.getAndroidId(): " + student.getAndroidId());
List<VideoLearningEvent> videoLearningEvents = videoLearningEventDao.readAll(student.getAndroidId());
log.info("videoLearningEvents.size(): " + videoLearningEvents.size());
for (VideoLearningEvent videoLearningEvent : videoLearningEvents) {
videoLearningEvent.setAndroidId(AnalyticsHelper.redactAndroidId(videoLearningEvent.getAndroidId()));
}
CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
.setHeader(
"id",
"timestamp",
"package_name",
"learning_event_type",
"additional_data",
"research_experiment",
"experiment_group",
"video_title",
"video_id"
).build();
StringWriter stringWriter = new StringWriter();
CSVPrinter csvPrinter = new CSVPrinter(stringWriter, csvFormat);
for (VideoLearningEvent videoLearningEvent : videoLearningEvents) {
log.info("videoLearningEvent.getId(): " + videoLearningEvent.getId());
csvPrinter.printRecord(
videoLearningEvent.getId(),
videoLearningEvent.getTimestamp().getTimeInMillis() / 1_000,
videoLearningEvent.getPackageName(),
videoLearningEvent.getLearningEventType(),
videoLearningEvent.getAdditionalData(),
videoLearningEvent.getResearchExperiment().ordinal(),
videoLearningEvent.getExperimentGroup().ordinal(),
videoLearningEvent.getVideoTitle(),
videoLearningEvent.getVideoId()
);
}
csvPrinter.flush();
csvPrinter.close();
String csvFileContent = stringWriter.toString();
response.setContentType("text/csv");
byte[] bytes = csvFileContent.getBytes();
response.setContentLength(bytes.length);
outputStream.write(bytes);
outputStream.flush();
outputStream.close();
} catch (Exception ex) {
log.error(ex.getMessage());
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
DiscordHelper.postToChannel(Channel.ANALYTICS, "Error during CSV export of video learning events: `" + ex.getClass() + ": " + ex.getMessage() + "`");
}
}
}