-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathLetterSoundLearningEventsCsvExportController.java
More file actions
89 lines (76 loc) · 3.02 KB
/
LetterSoundLearningEventsCsvExportController.java
File metadata and controls
89 lines (76 loc) · 3.02 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
package ai.elimu.web.analytics.students;
import ai.elimu.dao.LetterSoundLearningEventDao;
import ai.elimu.dao.StudentDao;
import ai.elimu.entity.analytics.LetterSoundLearningEvent;
import ai.elimu.entity.analytics.students.Student;
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.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}/letter-sound-learning-events.csv")
@RequiredArgsConstructor
@Slf4j
public class LetterSoundLearningEventsCsvExportController {
private final StudentDao studentDao;
private final LetterSoundLearningEventDao letterSoundLearningEventDao;
@GetMapping
public void handleRequest(
@PathVariable Long studentId,
HttpServletResponse response,
OutputStream outputStream
) throws IOException {
log.info("handleRequest");
Student student = studentDao.read(studentId);
log.info("student.getAndroidId(): " + student.getAndroidId());
List<LetterSoundLearningEvent> letterSoundLearningEvents = letterSoundLearningEventDao.readAll(student.getAndroidId());
log.info("letterSoundLearningEvents.size(): " + letterSoundLearningEvents.size());
CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
.setHeader(
"id",
"timestamp",
"package_name",
// "letter_sound_letters",
// "letter_sound_sounds",
"letter_sound_id",
"additional_data"
)
.build();
StringWriter stringWriter = new StringWriter();
CSVPrinter csvPrinter = new CSVPrinter(stringWriter, csvFormat);
for (LetterSoundLearningEvent letterSoundLearningEvent : letterSoundLearningEvents) {
log.info("letterSoundLearningEvent.getId(): " + letterSoundLearningEvent.getId());
csvPrinter.printRecord(
letterSoundLearningEvent.getId(),
letterSoundLearningEvent.getTimestamp().getTimeInMillis() / 1_000,
letterSoundLearningEvent.getPackageName(),
// letterSoundLearningEvent.getLetterSoundLetters(),
// letterSoundLearningEvent.getLetterSoundSounds(),
letterSoundLearningEvent.getLetterSoundId(),
letterSoundLearningEvent.getAdditionalData()
);
}
csvPrinter.flush();
csvPrinter.close();
String csvFileContent = stringWriter.toString();
response.setContentType("text/csv");
byte[] bytes = csvFileContent.getBytes();
response.setContentLength(bytes.length);
try {
outputStream.write(bytes);
outputStream.flush();
outputStream.close();
} catch (IOException ex) {
log.error(ex.getMessage());
}
}
}