Search in sources :

Example 1 with WordLearningEvent

use of ai.elimu.model.analytics.WordLearningEvent in project webapp by elimu-ai.

the class WordLearningEventsRestController method handleUploadCsvRequest.

@RequestMapping(value = "/csv", method = RequestMethod.POST)
public String handleUploadCsvRequest(@RequestParam("file") MultipartFile multipartFile, HttpServletResponse response) {
    logger.info("handleUploadCsvRequest");
    String name = multipartFile.getName();
    logger.info("name: " + name);
    // Expected format: "7161a85a0e4751cd_word-learning-events_2020-04-23.csv"
    String originalFilename = multipartFile.getOriginalFilename();
    logger.info("originalFilename: " + originalFilename);
    // TODO: Send notification to the #📊-data-collection channel in Discord
    // Hide parts of the Android ID, e.g. "7161***51cd_word-learning-events_2020-04-23.csv"
    String anonymizedOriginalFilename = originalFilename.substring(0, 4) + "***" + originalFilename.substring(12);
    DiscordHelper.sendChannelMessage("Received dataset: `" + anonymizedOriginalFilename + "`", null, null, null, null);
    String androidIdExtractedFromFilename = AnalyticsHelper.extractAndroidIdFromCsvFilename(originalFilename);
    logger.info("androidIdExtractedFromFilename: \"" + androidIdExtractedFromFilename + "\"");
    String contentType = multipartFile.getContentType();
    logger.info("contentType: " + contentType);
    JSONObject jsonObject = new JSONObject();
    try {
        byte[] bytes = multipartFile.getBytes();
        logger.info("bytes.length: " + bytes.length);
        // Store a backup of the original CSV file on the filesystem (in case it will be needed for debugging)
        File elimuAiDir = new File(System.getProperty("user.home"), ".elimu-ai");
        File languageDir = new File(elimuAiDir, "lang-" + Language.valueOf(ConfigHelper.getProperty("content.language")));
        File analyticsDir = new File(languageDir, "analytics");
        File androidIdDir = new File(analyticsDir, "android-id_" + androidIdExtractedFromFilename);
        File wordLearningEventsDir = new File(androidIdDir, "word-learning-events");
        wordLearningEventsDir.mkdirs();
        File csvFile = new File(wordLearningEventsDir, originalFilename);
        logger.info("Storing CSV file at " + csvFile);
        multipartFile.transferTo(csvFile);
        // Iterate each row in the CSV file
        Path csvFilePath = Paths.get(csvFile.toURI());
        logger.info("csvFilePath: " + csvFilePath);
        Reader reader = Files.newBufferedReader(csvFilePath);
        CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader(// The Room database ID
        "id", "time", "android_id", "package_name", "word_id", "word_text", "learning_event_type").withSkipHeaderRecord();
        CSVParser csvParser = new CSVParser(reader, csvFormat);
        for (CSVRecord csvRecord : csvParser) {
            logger.info("csvRecord: " + csvRecord);
            // Convert from CSV to Java
            WordLearningEvent wordLearningEvent = new WordLearningEvent();
            long timeInMillis = Long.valueOf(csvRecord.get("time"));
            Calendar time = Calendar.getInstance();
            time.setTimeInMillis(timeInMillis);
            wordLearningEvent.setTime(time);
            String androidId = csvRecord.get("android_id");
            wordLearningEvent.setAndroidId(androidId);
            String packageName = csvRecord.get("package_name");
            wordLearningEvent.setPackageName(packageName);
            Application application = applicationDao.readByPackageName(packageName);
            logger.info("application: " + application);
            if (application == null) {
                // Return error message saying that the reporting Application has not yet been added
                logger.warn("An Application with package name " + packageName + " was not found");
                jsonObject.put("result", "error");
                jsonObject.put("errorMessage", "An Application with package name " + packageName + " was not found");
                response.setStatus(HttpStatus.UNPROCESSABLE_ENTITY.value());
                break;
            }
            wordLearningEvent.setApplication(application);
            Long wordId = Long.valueOf(csvRecord.get("word_id"));
            Word word = wordDao.read(wordId);
            logger.info("word: " + word);
            wordLearningEvent.setWord(word);
            if (word == null) {
                // Return error message saying that the Word ID was not found
                logger.warn("A Word with ID " + wordId + " was not found");
                jsonObject.put("result", "error");
                jsonObject.put("errorMessage", "A Word with ID " + wordId + " was not found");
                response.setStatus(HttpStatus.UNPROCESSABLE_ENTITY.value());
                break;
            }
            String wordText = csvRecord.get("word_text");
            wordLearningEvent.setWordText(wordText);
            LearningEventType learningEventType = LearningEventType.valueOf(csvRecord.get("learning_event_type"));
            wordLearningEvent.setLearningEventType(learningEventType);
            // Check if the event has already been stored in the database
            WordLearningEvent existingWordLearningEvent = wordLearningEventDao.read(time, androidId, application, word);
            logger.info("existingWordLearningEvent: " + existingWordLearningEvent);
            if (existingWordLearningEvent == null) {
                // Store the event in the database
                wordLearningEventDao.create(wordLearningEvent);
                logger.info("Stored WordLearningEvent in database with ID " + wordLearningEvent.getId());
                jsonObject.put("result", "success");
                jsonObject.put("successMessage", "The WordLearningEvent was stored in the database");
            } else {
                // Return error message saying that the event has already been uploaded
                logger.warn("The event has already been stored in the database");
                jsonObject.put("result", "error");
                jsonObject.put("errorMessage", "The event has already been stored in the database");
                response.setStatus(HttpStatus.CONFLICT.value());
            }
        }
    } catch (Exception ex) {
        logger.error(ex);
        jsonObject.put("result", "error");
        jsonObject.put("errorMessage", ex.getMessage());
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    }
    String jsonResponse = jsonObject.toString();
    logger.info("jsonResponse: " + jsonResponse);
    return jsonResponse;
}
Also used : Path(java.nio.file.Path) Word(ai.elimu.model.content.Word) Calendar(java.util.Calendar) Reader(java.io.Reader) WordLearningEvent(ai.elimu.model.analytics.WordLearningEvent) JSONObject(org.json.JSONObject) CSVParser(org.apache.commons.csv.CSVParser) LearningEventType(ai.elimu.model.v2.enums.analytics.LearningEventType) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) Application(ai.elimu.model.admin.Application) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Application (ai.elimu.model.admin.Application)1 WordLearningEvent (ai.elimu.model.analytics.WordLearningEvent)1 Word (ai.elimu.model.content.Word)1 LearningEventType (ai.elimu.model.v2.enums.analytics.LearningEventType)1 File (java.io.File)1 Reader (java.io.Reader)1 Path (java.nio.file.Path)1 Calendar (java.util.Calendar)1 CSVFormat (org.apache.commons.csv.CSVFormat)1 CSVParser (org.apache.commons.csv.CSVParser)1 CSVRecord (org.apache.commons.csv.CSVRecord)1 JSONObject (org.json.JSONObject)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1