-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathLetterSoundUsageCountScheduler.java
More file actions
49 lines (39 loc) · 1.39 KB
/
LetterSoundUsageCountScheduler.java
File metadata and controls
49 lines (39 loc) · 1.39 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
package ai.elimu.tasks;
import ai.elimu.dao.LetterSoundDao;
import ai.elimu.dao.WordDao;
import ai.elimu.entity.content.LetterSound;
import ai.elimu.entity.content.Word;
import java.util.HashMap;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
/**
* Iterates all words and calculates the frequency of each letter-sound.
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class LetterSoundUsageCountScheduler {
private final WordDao wordDao;
private final LetterSoundDao letterSoundDao;
@Scheduled(cron = "00 10 06 * * *") // At 06:10 every day
public synchronized void execute() {
log.info("execute");
// <ID, frequency>
Map<Long, Integer> frequencyMap = new HashMap<>();
// Calculate the frequency of each letter-sound
for (Word word : wordDao.readAll()) {
for (LetterSound letterSound : word.getLetterSounds()) {
frequencyMap.put(letterSound.getId(), frequencyMap.getOrDefault(letterSound.getId(), 0) + 1);
}
}
// Update the values previously stored in the database
for (LetterSound letterSound : letterSoundDao.readAll()) {
letterSound.setUsageCount(frequencyMap.getOrDefault(letterSound.getId(), 0));
letterSoundDao.update(letterSound);
}
log.info("execute complete");
}
}