|
1 | 1 | package ai.elimu.rest.v2.analytics; |
2 | 2 |
|
3 | | -import ai.elimu.dao.ApplicationDao; |
4 | | -import ai.elimu.dao.WordDao; |
5 | 3 | import ai.elimu.model.v2.enums.Language; |
6 | 4 | import ai.elimu.util.AnalyticsHelper; |
7 | 5 | import ai.elimu.util.ConfigHelper; |
8 | | -import ai.elimu.util.DiscordHelper; |
9 | | -import jakarta.servlet.http.HttpServletResponse; |
10 | 6 | import java.io.File; |
11 | | -import java.io.Reader; |
12 | | -import java.nio.file.Files; |
13 | | -import java.nio.file.Path; |
14 | | -import java.nio.file.Paths; |
15 | | -import lombok.RequiredArgsConstructor; |
| 7 | +import jakarta.servlet.http.HttpServletResponse; |
16 | 8 | import lombok.extern.slf4j.Slf4j; |
17 | | -import org.apache.commons.csv.CSVFormat; |
18 | | -import org.apache.commons.csv.CSVParser; |
19 | | -import org.apache.commons.csv.CSVRecord; |
| 9 | + |
20 | 10 | import org.apache.commons.io.FileUtils; |
21 | 11 | import org.json.JSONObject; |
22 | 12 | import org.springframework.http.HttpStatus; |
|
27 | 17 | import org.springframework.web.bind.annotation.RestController; |
28 | 18 | import org.springframework.web.multipart.MultipartFile; |
29 | 19 |
|
| 20 | +/** |
| 21 | + * REST API endpoint for receiving word assessment events from the |
| 22 | + * <a href="https://github.com/elimu-ai/analytics">Analytics</a> application. |
| 23 | + */ |
30 | 24 | @RestController |
31 | | -@RequestMapping(value = "/rest/v2/analytics/word-assessment-events/csv", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) |
32 | | -@RequiredArgsConstructor |
| 25 | +@RequestMapping(value = "/rest/v2/analytics/word-assessment-events/csv", produces = MediaType.APPLICATION_JSON_VALUE) |
33 | 26 | @Slf4j |
34 | 27 | public class WordAssessmentEventsRestController { |
35 | | - |
36 | | -// @Autowired |
37 | | -// private WordAssessmentEventDao wordAssessmentEventDao; |
38 | | - |
39 | | - private final ApplicationDao applicationDao; |
40 | | - |
41 | | - private final WordDao wordDao; |
42 | | - |
43 | | - @PostMapping |
44 | | - public String handleUploadCsvRequest( |
45 | | - @RequestParam("file") MultipartFile multipartFile, |
46 | | - HttpServletResponse response |
47 | | - ) { |
48 | | - log.info("handleUploadCsvRequest"); |
49 | | - |
50 | | - String name = multipartFile.getName(); |
51 | | - log.info("name: " + name); |
52 | | - |
53 | | - // Expected format: "7161a85a0e4751cd_3001012_word-assessment-events_2020-04-23.csv" |
54 | | - String originalFilename = multipartFile.getOriginalFilename(); |
55 | | - log.info("originalFilename: " + originalFilename); |
56 | | - |
57 | | - // TODO: Send notification to the #📊-data-collection channel in Discord |
58 | | - // Hide parts of the Android ID, e.g. "7161***51cd_3001012_word-learning-events_2020-04-23.csv" |
59 | | - String anonymizedOriginalFilename = originalFilename.substring(0, 4) + "***" + originalFilename.substring(12); |
60 | | - DiscordHelper.sendChannelMessage("Received dataset: `" + anonymizedOriginalFilename + "`", null, null, null, null); |
61 | | - |
62 | | - String androidIdExtractedFromFilename = AnalyticsHelper.extractAndroidIdFromCsvFilename(originalFilename); |
63 | | - log.info("androidIdExtractedFromFilename: \"" + androidIdExtractedFromFilename + "\""); |
64 | | - |
65 | | - Integer versionCodeExtractedFromFilename = AnalyticsHelper.extractVersionCodeFromCsvFilename(originalFilename); |
66 | | - log.info("versionCodeExtractedFromFilename: " + versionCodeExtractedFromFilename); |
67 | | - |
68 | | - String contentType = multipartFile.getContentType(); |
69 | | - log.info("contentType: " + contentType); |
70 | | - |
71 | | - JSONObject jsonObject = new JSONObject(); |
72 | | - |
73 | | - try { |
74 | | - byte[] bytes = multipartFile.getBytes(); |
75 | | - log.info("bytes.length: " + bytes.length); |
76 | | - |
77 | | - // Store a backup of the original CSV file on the filesystem (in case it will be needed for debugging) |
78 | | - File elimuAiDir = new File(System.getProperty("user.home"), ".elimu-ai"); |
79 | | - File languageDir = new File(elimuAiDir, "lang-" + Language.valueOf(ConfigHelper.getProperty("content.language"))); |
80 | | - File analyticsDir = new File(languageDir, "analytics"); |
81 | | - File androidIdDir = new File(analyticsDir, "android-id-" + androidIdExtractedFromFilename); |
82 | | - File versionCodeDir = new File(androidIdDir, "version-code-" + versionCodeExtractedFromFilename); |
83 | | - File wordAssessmentEventsDir = new File(versionCodeDir, "word-assessment-events"); |
84 | | - wordAssessmentEventsDir.mkdirs(); |
85 | | - File csvFile = new File(wordAssessmentEventsDir, originalFilename); |
86 | | - log.info("Storing CSV file at " + csvFile); |
87 | | - FileUtils.writeByteArrayToFile(csvFile, bytes); |
88 | | - log.info("csvFile.exists(): " + csvFile.exists()); |
89 | | - |
90 | | - // Iterate each row in the CSV file |
91 | | - Path csvFilePath = Paths.get(csvFile.toURI()); |
92 | | - log.info("csvFilePath: " + csvFilePath); |
93 | | - Reader reader = Files.newBufferedReader(csvFilePath); |
94 | | - CSVFormat csvFormat = CSVFormat.DEFAULT |
95 | | - .withHeader( |
96 | | - "id", // The Room database ID |
97 | | - "time", |
98 | | - "android_id", |
99 | | - "package_name", |
100 | | - "word_id", |
101 | | - "word_text", |
102 | | - "mastery_score", |
103 | | - "time_spent_ms" |
104 | | - ) |
105 | | - .withSkipHeaderRecord(); |
106 | | - CSVParser csvParser = new CSVParser(reader, csvFormat); |
107 | | - for (CSVRecord csvRecord : csvParser) { |
108 | | - log.info("csvRecord: " + csvRecord); |
109 | | - |
110 | | - // Convert from CSV to Java |
111 | | - |
112 | | - // TODO |
113 | | - } |
114 | | - } catch (Exception ex) { |
115 | | - log.error(ex.getMessage()); |
116 | | - |
117 | | - jsonObject.put("result", "error"); |
118 | | - jsonObject.put("errorMessage", ex.getMessage()); |
119 | | - response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); |
| 28 | + |
| 29 | + @PostMapping |
| 30 | + public String handleUploadCsvRequest( |
| 31 | + @RequestParam("file") MultipartFile multipartFile, |
| 32 | + HttpServletResponse response |
| 33 | + ) { |
| 34 | + log.info("handleUploadCsvRequest"); |
| 35 | + |
| 36 | + JSONObject jsonResponseObject = new JSONObject(); |
| 37 | + try { |
| 38 | + String contentType = multipartFile.getContentType(); |
| 39 | + log.info("contentType: " + contentType); |
| 40 | + |
| 41 | + long size = multipartFile.getSize(); |
| 42 | + log.info("size: " + size); |
| 43 | + if (size == 0) { |
| 44 | + throw new IllegalArgumentException("Empty file"); |
| 45 | + } |
| 46 | + |
| 47 | + // Expected format: "7161a85a0e4751cd_3001012_word-assessment-events_2020-04-23.csv" |
| 48 | + String originalFilename = multipartFile.getOriginalFilename(); |
| 49 | + log.info("originalFilename: " + originalFilename); |
| 50 | + if (originalFilename.length() != "7161a85a0e4751cd_3001012_word-assessment-events_2020-04-23.csv".length()) { |
| 51 | + throw new IllegalArgumentException("Unexpected filename"); |
| 52 | + } |
| 53 | + |
| 54 | + String androidIdExtractedFromFilename = AnalyticsHelper.extractAndroidIdFromCsvFilename(originalFilename); |
| 55 | + log.info("androidIdExtractedFromFilename: \"" + androidIdExtractedFromFilename + "\""); |
| 56 | + |
| 57 | + Integer versionCodeExtractedFromFilename = AnalyticsHelper.extractVersionCodeFromCsvFilename(originalFilename); |
| 58 | + log.info("versionCodeExtractedFromFilename: " + versionCodeExtractedFromFilename); |
| 59 | + |
| 60 | + byte[] bytes = multipartFile.getBytes(); |
| 61 | + log.info("bytes.length: " + bytes.length); |
| 62 | + |
| 63 | + // Store the original CSV file on the filesystem |
| 64 | + File elimuAiDir = new File(System.getProperty("user.home"), ".elimu-ai"); |
| 65 | + File languageDir = new File(elimuAiDir, "lang-" + Language.valueOf(ConfigHelper.getProperty("content.language"))); |
| 66 | + File analyticsDir = new File(languageDir, "analytics"); |
| 67 | + File androidIdDir = new File(analyticsDir, "android-id-" + androidIdExtractedFromFilename); |
| 68 | + File versionCodeDir = new File(androidIdDir, "version-code-" + versionCodeExtractedFromFilename); |
| 69 | + File wordAssessmentEventsDir = new File(versionCodeDir, "word-assessment-events"); |
| 70 | + wordAssessmentEventsDir.mkdirs(); |
| 71 | + File csvFile = new File(wordAssessmentEventsDir, originalFilename); |
| 72 | + log.info("Storing CSV file at " + csvFile); |
| 73 | + FileUtils.writeByteArrayToFile(csvFile, bytes); |
| 74 | + log.info("csvFile.exists(): " + csvFile.exists()); |
| 75 | + |
| 76 | + jsonResponseObject.put("result", "success"); |
| 77 | + jsonResponseObject.put("successMessage", "The CSV file was uploaded"); |
| 78 | + response.setStatus(HttpStatus.OK.value()); |
| 79 | + } catch (Exception ex) { |
| 80 | + log.error(ex.getMessage()); |
| 81 | + |
| 82 | + jsonResponseObject.put("result", "error"); |
| 83 | + jsonResponseObject.put("errorMessage", ex.getMessage()); |
| 84 | + response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); |
| 85 | + } |
| 86 | + |
| 87 | + String jsonResponse = jsonResponseObject.toString(); |
| 88 | + log.info("jsonResponse: " + jsonResponse); |
| 89 | + return jsonResponse; |
120 | 90 | } |
121 | | - |
122 | | - String jsonResponse = jsonObject.toString(); |
123 | | - log.info("jsonResponse: " + jsonResponse); |
124 | | - return jsonResponse; |
125 | | - } |
126 | 91 | } |
0 commit comments