|
| 1 | +package access.stats; |
| 2 | + |
| 3 | +import access.manage.Manage; |
| 4 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 5 | +import org.springframework.stereotype.Component; |
| 6 | +import org.springframework.util.StringUtils; |
| 7 | + |
| 8 | +import java.util.*; |
| 9 | +import java.util.stream.Collectors; |
| 10 | + |
| 11 | +import static access.manage.ManageData.getData; |
| 12 | + |
| 13 | +@SuppressWarnings("unchecked") |
| 14 | +@Component |
| 15 | +@ConditionalOnProperty( |
| 16 | + prefix = "statistics", |
| 17 | + name = "enabled", |
| 18 | + havingValue = "false" |
| 19 | +) |
| 20 | +public class StatisticsMock implements Statistics { |
| 21 | + |
| 22 | + private final Manage manage; |
| 23 | + |
| 24 | + public StatisticsMock(Manage manage) { |
| 25 | + this.manage = manage; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public List<Object> loginTimeFrame(long from, long to, String scale, String idpEntityId, String spEntityId) { |
| 30 | + long step = step(scale); |
| 31 | + List<Object> result = new ArrayList<>(); |
| 32 | + for (long i = from; i <= to; i += step) { |
| 33 | + Map<String, Object> point = new HashMap<>(); |
| 34 | + point.put("count_user_id", countValue(scale)); |
| 35 | + if (!"minute".equals(scale) && !"hour".equals(scale)) { |
| 36 | + point.put("distinct_count_user_id", countValue(scale)); |
| 37 | + } |
| 38 | + if (StringUtils.hasText(spEntityId)) { |
| 39 | + point.put("sp_entity_id", spEntityId); |
| 40 | + } |
| 41 | + point.put("idp_entity_id", idpEntityId); |
| 42 | + point.put("time", i * 1000); |
| 43 | + result.add(point); |
| 44 | + } |
| 45 | + return result; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public List<Object> loginAggregated(String period, String idpEntityId, String spEntityId) { |
| 50 | + Calendar today = Calendar.getInstance(); |
| 51 | + today.set(Calendar.YEAR, Integer.valueOf(period.substring(0, 4))); |
| 52 | + today.set(Calendar.HOUR_OF_DAY, 0); |
| 53 | + today.set(Calendar.MINUTE, 0); |
| 54 | + today.set(Calendar.DAY_OF_MONTH, 1); |
| 55 | + if (period.length() > 4) { |
| 56 | + switch (period.substring(4, 5).toUpperCase()) { |
| 57 | + case "Q": { |
| 58 | + today.set(Calendar.MONTH, ((Integer.valueOf(period.substring(5)) - 1) * 3)); |
| 59 | + } |
| 60 | + case "M": { |
| 61 | + today.set(Calendar.MONTH, Integer.valueOf(period.substring(5)) - 1); |
| 62 | + } |
| 63 | + case "W": { |
| 64 | + today.set(Calendar.WEEK_OF_YEAR, Integer.valueOf(period.substring(5))); |
| 65 | + } |
| 66 | + case "D": { |
| 67 | + today.set(Calendar.DAY_OF_YEAR, Integer.valueOf(period.substring(5))); |
| 68 | + } |
| 69 | + } |
| 70 | + } else { |
| 71 | + today.set(Calendar.MONTH, 0); |
| 72 | + } |
| 73 | + long date = today.getTimeInMillis() / 1000; |
| 74 | + Map<String, Object> identityProvider = manage.identityProviderByEntityID(idpEntityId); |
| 75 | + Map<String, Object> data = getData(identityProvider); |
| 76 | + List<String> entityIdentifiers = ((List<Map<String, String>>) data.getOrDefault("allowedEntities", List.of())) |
| 77 | + .stream() |
| 78 | + .map(sp -> (String) sp.get("name")) |
| 79 | + .toList(); |
| 80 | + |
| 81 | + |
| 82 | + List<Map<String, Object>> serviceProvider = manage.serviceProvidersByEntityID(entityIdentifiers); |
| 83 | + return serviceProvider.stream() |
| 84 | + .filter(sp -> !StringUtils.hasText(spEntityId) || spEntityId.equals(getData(sp).get("entityid"))) |
| 85 | + .map(sp -> { |
| 86 | + Map<String, Object> point = new HashMap<>(); |
| 87 | + point.put("count_user_id", countValue(periodToScale(period))); |
| 88 | + point.put("distinct_count_user_id", countValue(periodToScale(period)) / 2); |
| 89 | + point.put("sp_entity_id", getData(sp).get("entityid")); |
| 90 | + point.put("idp_entity_id", idpEntityId); |
| 91 | + point.put("time", date); |
| 92 | + return point; |
| 93 | + }).collect(Collectors.toList()); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public List<Object> uniqueLoginCount(long from, long to, String idpEntityId, String spEntityId) { |
| 98 | + List<Object> result = new ArrayList<>(); |
| 99 | + Map<String, Object> point = new HashMap<>(); |
| 100 | + point.put("count_user_id", countValue("year")); |
| 101 | + point.put("sp_entity_id", spEntityId); |
| 102 | + point.put("idp_entity_id", idpEntityId); |
| 103 | + point.put("time", from); |
| 104 | + result.add(point); |
| 105 | + return result; |
| 106 | + } |
| 107 | + |
| 108 | + private String periodToScale(String period) { |
| 109 | + if (period.length() == 4) { |
| 110 | + return "year"; |
| 111 | + } |
| 112 | + switch (period.substring(4, 5).toLowerCase()) { |
| 113 | + case "d": |
| 114 | + return "day"; |
| 115 | + case "w": |
| 116 | + return "week"; |
| 117 | + case "m": |
| 118 | + return "month"; |
| 119 | + case "q": |
| 120 | + return "quarter"; |
| 121 | + default: |
| 122 | + throw new IllegalArgumentException("Unknown period:" + period); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private long countValue(String scale) { |
| 127 | + double base = Math.floor(10000 * (Math.random() + 1)); |
| 128 | + return (long) base; |
| 129 | + } |
| 130 | + |
| 131 | + private long doSwitch(String scale, long base) { |
| 132 | + switch (scale) { |
| 133 | + case "minute": |
| 134 | + return base * 60; |
| 135 | + case "hour": |
| 136 | + return base * 60 * 60; |
| 137 | + case "day": |
| 138 | + return base * 24 * 60 * 60; |
| 139 | + case "week": |
| 140 | + return base * 7 * 24 * 60 * 60; |
| 141 | + case "month": |
| 142 | + return base * 30 * 24 * 60 * 60; |
| 143 | + case "quarter": |
| 144 | + return base * 90 * 24 * 60 * 60; |
| 145 | + case "year": |
| 146 | + return base * 365 * 24 * 60 * 60; |
| 147 | + default: |
| 148 | + throw new IllegalArgumentException("Unknown scale:" + scale); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private long step(String scale) { |
| 153 | + return doSwitch(scale, 1L); |
| 154 | + } |
| 155 | +} |
0 commit comments