|
| 1 | +package stats; |
| 2 | + |
| 3 | +import java.time.Duration; |
| 4 | +import java.time.Instant; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | +import models.EnrichedFileSegment; |
| 8 | + |
| 9 | +public class UserProjectRollingStat implements IStat { |
| 10 | + public static final String[] PRIMITIVE_COLUMNS = { "user_id", "project_path", "window_type", }; |
| 11 | + public static final String[] JSONB_COLUMNS = { "lang_durations", "machine_durations", "editor_durations", |
| 12 | + "activity_durations", "files_durations" }; |
| 13 | + public static final String[] CONFLICT_KEYS = { "user_id", "project_path", "window_type" }; |
| 14 | + private final int user_id; |
| 15 | + private final String project_path; |
| 16 | + private long total_duration; |
| 17 | + private String window_type; |
| 18 | + |
| 19 | + private final HashMap<String, Long> machine_durations = new HashMap<>(); |
| 20 | + private final HashMap<String, Long> language_durations = new HashMap<>(); |
| 21 | + private final HashMap<String, Long> editor_durations = new HashMap<>(); |
| 22 | + private final HashMap<String, Long> project_durations = new HashMap<>(); |
| 23 | + private final HashMap<String, Long> activity_durations = new HashMap<>(); |
| 24 | + private final HashMap<String, Long> files_durations = new HashMap<>(); |
| 25 | + |
| 26 | + public UserProjectRollingStat(int user_id, String project_path) { |
| 27 | + this.user_id = user_id; |
| 28 | + this.project_path = project_path; |
| 29 | + this.total_duration = 0; |
| 30 | + } |
| 31 | + |
| 32 | + public void setWindowType(String window_type) { |
| 33 | + this.window_type = window_type; |
| 34 | + } |
| 35 | + |
| 36 | + public String getWindowType() { |
| 37 | + return this.window_type; |
| 38 | + } |
| 39 | + |
| 40 | + public int getUserId() { |
| 41 | + return user_id; |
| 42 | + } |
| 43 | + |
| 44 | + public String getProjectPath() { |
| 45 | + return project_path; |
| 46 | + } |
| 47 | + |
| 48 | + public long getTotalDuration() { |
| 49 | + return total_duration; |
| 50 | + } |
| 51 | + |
| 52 | + public HashMap<String, Long> getMachineDurations() { |
| 53 | + return machine_durations; |
| 54 | + } |
| 55 | + |
| 56 | + public HashMap<String, Long> getLangDurations() { |
| 57 | + return language_durations; |
| 58 | + } |
| 59 | + |
| 60 | + public HashMap<String, Long> getEditorDurations() { |
| 61 | + return editor_durations; |
| 62 | + } |
| 63 | + |
| 64 | + public HashMap<String, Long> getProjectDurations() { |
| 65 | + return project_durations; |
| 66 | + } |
| 67 | + |
| 68 | + public HashMap<String, Long> getActivityDurations() { |
| 69 | + return activity_durations; |
| 70 | + } |
| 71 | + |
| 72 | + public HashMap<String, Long> getFilesDurations() { |
| 73 | + return files_durations; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Map<String, Object> asRecord() { |
| 78 | + Map<String, Object> map = new HashMap<>(); |
| 79 | + map.put("user_id", this.user_id); |
| 80 | + map.put("project_path", this.project_path); |
| 81 | + map.put("window_type", this.window_type); |
| 82 | + map.put("lang_durations", this.language_durations); |
| 83 | + map.put("machine_durations", this.machine_durations); |
| 84 | + map.put("editor_durations", this.editor_durations); |
| 85 | + map.put("project_durations", this.project_durations); |
| 86 | + map.put("activity_durations", this.activity_durations); |
| 87 | + map.put("file_durations", this.files_durations); |
| 88 | + return map; |
| 89 | + } |
| 90 | + |
| 91 | + public void add(EnrichedFileSegment seg) { |
| 92 | + long duration = Duration.between(Instant.parse(seg.getStart_time()), Instant.parse(seg.getEnd_time())) |
| 93 | + .toMillis(); |
| 94 | + |
| 95 | + this.total_duration += duration; |
| 96 | + |
| 97 | + if (seg.getLang() != null) |
| 98 | + language_durations.merge(seg.getLang(), duration, Long::sum); |
| 99 | + |
| 100 | + if (seg.getEditor() != null) |
| 101 | + editor_durations.merge(seg.getEditor(), duration, Long::sum); |
| 102 | + |
| 103 | + if (seg.getMachine_name() != null) |
| 104 | + machine_durations.merge(seg.getMachine_name(), duration, Long::sum); |
| 105 | + |
| 106 | + if (seg.getProject_name() != null) |
| 107 | + project_durations.merge(seg.getProject_name(), duration, Long::sum); |
| 108 | + |
| 109 | + if (seg.getSegment_type() != null) |
| 110 | + activity_durations.merge(seg.getSegment_type(), duration, Long::sum); |
| 111 | + |
| 112 | + if (seg.getFile_path() != null) |
| 113 | + files_durations.merge(seg.getFile_path(), duration, Long::sum); |
| 114 | + } |
| 115 | + |
| 116 | + public void postProcess(WindowContext ctx) { |
| 117 | + setWindowType(ctx.getWindowType()); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public String toString() { |
| 122 | + StringBuilder sb = new StringBuilder(); |
| 123 | + sb.append("UserProjectStat{user_id=").append(user_id).append(", project_path=").append(project_path) |
| 124 | + .append(", total_duration=").append(total_duration).append(" ms"); |
| 125 | + |
| 126 | + appendCategory(sb, "files", files_durations); |
| 127 | + appendCategory(sb, "languages", language_durations); |
| 128 | + appendCategory(sb, "machines", machine_durations); |
| 129 | + appendCategory(sb, "projects", project_durations); |
| 130 | + appendCategory(sb, "activities", activity_durations); |
| 131 | + appendCategory(sb, "editor", editor_durations); |
| 132 | + |
| 133 | + sb.append("}"); |
| 134 | + return sb.toString(); |
| 135 | + } |
| 136 | + |
| 137 | + private void appendCategory(StringBuilder sb, String name, HashMap<String, Long> durations) { |
| 138 | + if (!durations.isEmpty()) { |
| 139 | + sb.append(", ").append(name).append("={"); |
| 140 | + for (Map.Entry<String, Long> entry : durations.entrySet()) { |
| 141 | + double percent = (total_duration > 0) ? (entry.getValue() * 100.0 / total_duration) : 0.0; |
| 142 | + sb.append(entry.getKey()).append(": ").append(String.format("%.2f%%", percent)).append(" (") |
| 143 | + .append(entry.getValue()).append(" ms), "); |
| 144 | + } |
| 145 | + sb.setLength(sb.length() - 2); |
| 146 | + sb.append("}"); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments