Skip to content

Commit 1b14999

Browse files
committed
Merge develop into main
2 parents e53179e + d2f4b44 commit 1b14999

4 files changed

Lines changed: 66 additions & 68 deletions

File tree

‎wise-api/src/main/java/com/wisemapping/rest/UserController.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.wisemapping.service.*;
3030
import com.wisemapping.validator.Messages;
3131
import com.wisemapping.validator.UserValidator;
32+
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
3233
import org.slf4j.LoggerFactory;
3334
import org.slf4j.Logger;
3435
import org.jetbrains.annotations.NotNull;
@@ -121,6 +122,7 @@ public void registerUser(@RequestBody RestUserRegistration registration,
121122

122123
}
123124

125+
@RateLimiter(name = "passwordResetLimiter")
124126
@RequestMapping(method = RequestMethod.PUT, value = "/resetPassword", produces = {"application/json"})
125127
@ResponseStatus(value = HttpStatus.OK)
126128
public RestResetPasswordResponse resetPassword(@RequestParam String email) throws InvalidAuthSchemaException, EmailNotExistsException {

‎wise-api/src/main/java/com/wisemapping/service/MetricsService.java‎

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.slf4j.Logger;
2929
import org.slf4j.LoggerFactory;
3030
import org.springframework.beans.factory.annotation.Autowired;
31+
import org.springframework.beans.factory.annotation.Value;
3132
import org.springframework.stereotype.Service;
3233

3334
/**
@@ -49,6 +50,9 @@ public class MetricsService {
4950
@Autowired
5051
private MeterRegistry meterRegistry;
5152

53+
@Value("${app.telemetry.enabled:false}")
54+
private boolean telemetryEnabled;
55+
5256
// Metric names as constants to avoid typos
5357
private static final String USER_LOGINS = "wisemapping.api.user.auth";
5458
private static final String USER_LOGOUTS = "wisemapping.api.user.logouts";
@@ -74,6 +78,7 @@ public class MetricsService {
7478
* @param authType The authentication type used (e.g., "database", "oauth", "google")
7579
*/
7680
public void trackUserLogin(@NotNull Account user, @NotNull String authType) {
81+
if (!telemetryEnabled) return;
7782
try {
7883
Counter.builder(USER_LOGINS)
7984
.description("Total number of user logins")
@@ -94,6 +99,7 @@ public void trackUserLogin(@NotNull Account user, @NotNull String authType) {
9499
* @param logoutType The logout type (e.g., "manual", "session_expired", "admin")
95100
*/
96101
public void trackUserLogout(@NotNull Account user, @NotNull String logoutType) {
102+
if (!telemetryEnabled) return;
97103
try {
98104
Counter.builder(USER_LOGOUTS)
99105
.description("Total number of user logouts")
@@ -113,13 +119,13 @@ public void trackUserLogout(@NotNull Account user, @NotNull String logoutType) {
113119
* @param user The newly registered user
114120
*/
115121
public void trackUserRegistration(@NotNull Account user) {
122+
if (!telemetryEnabled) return;
116123
try {
117124
String authType = user.getAuthenticationType() != null ?
118125
String.valueOf(user.getAuthenticationType().getCode()) : "unknown";
119126

120127
Counter.builder(USER_REGISTRATIONS)
121128
.description("Total number of user registrations")
122-
.tag("email_provider", emailProvider)
123129
.tag("auth_type", authType)
124130
.register(meterRegistry)
125131
.increment();
@@ -135,6 +141,7 @@ public void trackUserRegistration(@NotNull Account user) {
135141
* @param user The user whose account was activated
136142
*/
137143
public void trackUserActivation(@NotNull Account user) {
144+
if (!telemetryEnabled) return;
138145
try {
139146
String authType = user.getAuthenticationType() != null ?
140147
String.valueOf(user.getAuthenticationType().getCode()) : "unknown";
@@ -158,6 +165,7 @@ public void trackUserActivation(@NotNull Account user) {
158165
* @param creationType The type of creation (e.g., "new", "duplicate", "tutorial", "import")
159166
*/
160167
public void trackMindmapCreation(@NotNull Mindmap mindmap, @NotNull Account user, @NotNull String creationType) {
168+
if (!telemetryEnabled) return;
161169
try {
162170
String visibility = mindmap.isPublic() ? "public" : "private";
163171

@@ -180,26 +188,29 @@ public void trackMindmapCreation(@NotNull Mindmap mindmap, @NotNull Account user
180188
* @param user The user who was suspended
181189
* @param reason The suspension reason
182190
*/
183-
public void trackUserSuspension(@NotNull Account user, @NotNull String reason) {
184-
Counter.builder(USER_SUSPENSIONS)
185-
.description("Total number of users suspended")
186-
.tag("reason", reason.toLowerCase())
187-
.tag("user_type", String.valueOf(user.getAuthenticationType().getCode()))
188-
.register(meterRegistry)
189-
.increment();
190-
191-
logger.debug("Tracked user suspension metric");
192-
} catch (Exception e) {
193-
logger.warn("Failed to track user suspension metric: {}", e.getMessage());
194-
}
195-
}
191+
public void trackUserSuspension(@NotNull Account user, @NotNull String reason) {
192+
if (!telemetryEnabled) return;
193+
try {
194+
Counter.builder(USER_SUSPENSIONS)
195+
.description("Total number of users suspended")
196+
.tag("reason", reason.toLowerCase())
197+
.tag("user_type", String.valueOf(user.getAuthenticationType().getCode()))
198+
.register(meterRegistry)
199+
.increment();
200+
201+
logger.debug("Tracked user suspension metric");
202+
} catch (Exception e) {
203+
logger.warn("Failed to track user suspension metric: {}", e.getMessage());
204+
}
205+
}
196206

197207
/**
198208
* Track when a mindmap is made public
199209
* @param mindmap The mindmap that was made public
200210
* @param user The user who made it public
201211
*/
202212
public void trackMindmapMadePublic(@NotNull Mindmap mindmap, @NotNull Account user) {
213+
if (!telemetryEnabled) return;
203214
try {
204215
Counter.builder(MINDMAPS_MADE_PUBLIC)
205216
.description("Total number of mindmaps made public")
@@ -221,6 +232,7 @@ public void trackMindmapMadePublic(@NotNull Mindmap mindmap, @NotNull Account us
221232
* @param sharedBy The user who shared the mindmap
222233
*/
223234
public void trackMindmapShared(@NotNull Mindmap mindmap, @NotNull String role, @NotNull Account sharedBy) {
235+
if (!telemetryEnabled) return;
224236
try {
225237
Counter.builder(MINDMAPS_SHARED)
226238
.description("Total number of mindmaps shared with collaborators")
@@ -243,6 +255,7 @@ public void trackMindmapShared(@NotNull Mindmap mindmap, @NotNull String role, @
243255
* @param context The context where spam analysis was performed (e.g., "creation", "update", "batch_scan")
244256
*/
245257
public void trackSpamAnalysis(@NotNull Mindmap mindmap, @NotNull SpamDetectionResult spamResult, @NotNull String context) {
258+
if (!telemetryEnabled) return;
246259
try {
247260
String isSpam = spamResult.isSpam() ? "yes" : "no";
248261
String spamType = spamResult.isSpam() && spamResult.getStrategyType() != null ?
@@ -271,6 +284,7 @@ public void trackSpamAnalysis(@NotNull Mindmap mindmap, @NotNull SpamDetectionRe
271284
* @param context The context where spam was detected (e.g., "creation", "update", "batch_scan")
272285
*/
273286
public void trackSpamDetection(@NotNull Mindmap mindmap, @NotNull SpamDetectionResult spamResult, @NotNull String context) {
287+
if (!telemetryEnabled) return;
274288
try {
275289
if (spamResult.isSpam()) {
276290
Counter.builder(SPAM_DETECTED)
@@ -295,6 +309,7 @@ public void trackSpamDetection(@NotNull Mindmap mindmap, @NotNull SpamDetectionR
295309
* @param action The action that was prevented (e.g., "publish", "share")
296310
*/
297311
public void trackSpamPrevention(@NotNull Mindmap mindmap, @NotNull String action) {
312+
if (!telemetryEnabled) return;
298313
try {
299314
Counter.builder(SPAM_PREVENTED)
300315
.description("Total number of actions prevented due to spam detection")
@@ -315,6 +330,7 @@ public void trackSpamPrevention(@NotNull Mindmap mindmap, @NotNull String action
315330
* @param suspended Total number of users suspended in the batch
316331
*/
317332
public void trackInactiveUserProcessing(int processed, int suspended) {
333+
if (!telemetryEnabled) return;
318334
try {
319335
Counter.builder(INACTIVE_USERS_PROCESSED)
320336
.description("Total number of inactive users processed")
@@ -339,6 +355,7 @@ public void trackInactiveUserProcessing(int processed, int suspended) {
339355
* @param suspended Number of users suspended in this batch
340356
*/
341357
public void trackInactiveUserBatchSuspension(int suspended) {
358+
if (!telemetryEnabled) return;
342359
try {
343360
if (suspended > 0) {
344361
Counter.builder(INACTIVE_USERS_BATCH_SUSPENDED)
@@ -358,6 +375,7 @@ public void trackInactiveUserBatchSuspension(int suspended) {
358375
* @param candidates Number of users identified as suspension candidates in dry run
359376
*/
360377
public void trackInactiveUserDryRunCandidates(int candidates) {
378+
if (!telemetryEnabled) return;
361379
try {
362380
if (candidates > 0) {
363381
Counter.builder(INACTIVE_USERS_DRY_RUN_CANDIDATES)
@@ -378,6 +396,7 @@ public void trackInactiveUserDryRunCandidates(int candidates) {
378396
* @param mindmapsMigrated Total number of mindmaps migrated to inactive table
379397
*/
380398
public void trackInactiveMindmapMigration(int usersProcessed, int mindmapsMigrated) {
399+
if (!telemetryEnabled) return;
381400
try {
382401
Counter.builder(INACTIVE_MINDMAPS_USERS_PROCESSED)
383402
.description("Total number of inactive users processed for mindmap migration")

‎wise-api/src/main/resources/application.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,17 @@ resilience4j:
186186
limitForPeriod: 20
187187
limitRefreshPeriod: 1s
188188
timeoutDuration: 0
189+
passwordResetLimiter:
190+
limitForPeriod: 50 # max 50 password reset requests per window
191+
limitRefreshPeriod: 1m # 1-minute sliding window
192+
timeoutDuration: 0 # reject immediately (no queuing) when limit exceeded
189193

190194
#
191195
# Application Config related options:
192196
#
193197
app:
198+
telemetry:
199+
enabled: false
194200
monitoring:
195201
performance:
196202
log-mindmap-listing: true

‎wise-api/src/test/java/com/wisemapping/service/TelemetryMetricsTest.java‎

Lines changed: 25 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,13 @@ void setUp() {
6161
java.lang.reflect.Field field = MetricsService.class.getDeclaredField("meterRegistry");
6262
field.setAccessible(true);
6363
field.set(metricsService, meterRegistry);
64+
65+
// Inject telemetryEnabled=true
66+
java.lang.reflect.Field telemetryField = MetricsService.class.getDeclaredField("telemetryEnabled");
67+
telemetryField.setAccessible(true);
68+
telemetryField.set(metricsService, true);
6469
} catch (Exception e) {
65-
fail("Failed to inject meterRegistry: " + e.getMessage());
70+
fail("Failed to inject fields: " + e.getMessage());
6671
}
6772
}
6873

@@ -142,66 +147,47 @@ void testMeterRegistryConfiguration() {
142147

143148
@Test
144149
void testUserRegistrationMetrics() {
145-
// Test Gmail registration
146-
Account gmailUser = createTestUser("test@gmail.com", AuthenticationType.DATABASE);
147-
metricsService.trackUserRegistration(gmailUser, "gmail");
150+
// Test registration
151+
Account user1 = createTestUser("test@gmail.com", AuthenticationType.DATABASE);
152+
metricsService.trackUserRegistration(user1);
148153

149154
// Test other email registration
150-
Account otherUser = createTestUser("test@company.com", AuthenticationType.DATABASE);
151-
metricsService.trackUserRegistration(otherUser, "other");
155+
Account user2 = createTestUser("test@company.com", AuthenticationType.DATABASE);
156+
metricsService.trackUserRegistration(user2);
152157

153158
// Test Google OAuth registration
154159
Account googleUser = createTestUser("test@gmail.com", AuthenticationType.GOOGLE_OAUTH2);
155-
metricsService.trackUserRegistration(googleUser, "gmail");
160+
metricsService.trackUserRegistration(googleUser);
156161

157162
// Verify metrics were recorded
158-
Double gmailRegistrations = meterRegistry.find("wisemapping.api.user.registrations")
159-
.tag("email_provider", "gmail")
160-
.tag("auth_type", "D")
161-
.counter().count();
162-
163-
Double otherRegistrations = meterRegistry.find("wisemapping.api.user.registrations")
164-
.tag("email_provider", "other")
163+
Double databaseRegistrations = meterRegistry.find("wisemapping.api.user.registrations")
165164
.tag("auth_type", "D")
166165
.counter().count();
167166

168167
Double googleRegistrations = meterRegistry.find("wisemapping.api.user.registrations")
169-
.tag("email_provider", "gmail")
170168
.tag("auth_type", "G")
171169
.counter().count();
172170

173-
assertEquals(1.0, gmailRegistrations);
174-
assertEquals(1.0, otherRegistrations);
171+
assertEquals(2.0, databaseRegistrations);
175172
assertEquals(1.0, googleRegistrations);
176173
}
177174

178175
@Test
179176
void testUserActivationMetrics() {
180-
// Test Gmail activation
181-
Account gmailUser = createTestUser("test@gmail.com", AuthenticationType.DATABASE);
182-
metricsService.trackUserActivation(gmailUser);
177+
// Test activation
178+
Account user1 = createTestUser("test@gmail.com", AuthenticationType.DATABASE);
179+
metricsService.trackUserActivation(user1);
183180

184181
// Test other email activation
185-
Account otherUser = createTestUser("test@company.com", AuthenticationType.DATABASE);
186-
metricsService.trackUserActivation(otherUser);
187-
188-
// Test multiple activations for the same email provider
189-
Account anotherGmailUser = createTestUser("another@gmail.com", AuthenticationType.DATABASE);
190-
metricsService.trackUserActivation(anotherGmailUser);
182+
Account user2 = createTestUser("test@company.com", AuthenticationType.DATABASE);
183+
metricsService.trackUserActivation(user2);
191184

192185
// Verify metrics were recorded
193-
Double gmailActivations = meterRegistry.find("wisemapping.api.user.activations")
194-
.tag("email_provider", "gmail")
195-
.tag("auth_type", "D")
196-
.counter().count();
197-
198-
Double otherActivations = meterRegistry.find("wisemapping.api.user.activations")
199-
.tag("email_provider", "other")
186+
Double databaseActivations = meterRegistry.find("wisemapping.api.user.activations")
200187
.tag("auth_type", "D")
201188
.counter().count();
202189

203-
assertEquals(2.0, gmailActivations);
204-
assertEquals(1.0, otherActivations);
190+
assertEquals(2.0, databaseActivations);
205191
}
206192

207193
@Test
@@ -285,19 +271,17 @@ void testMindmapSharedMetrics() {
285271
Account sharer = createTestUser("sharer@example.com", AuthenticationType.DATABASE);
286272
Mindmap mindmap = createTestMindmap();
287273

288-
// Test sharing with different roles and email providers
289-
metricsService.trackMindmapShared(mindmap, "collaborator@gmail.com", "VIEWER", sharer);
290-
metricsService.trackMindmapShared(mindmap, "editor@company.com", "EDITOR", sharer);
274+
// Test sharing with different roles
275+
metricsService.trackMindmapShared(mindmap, "VIEWER", sharer);
276+
metricsService.trackMindmapShared(mindmap, "EDITOR", sharer);
291277

292278
// Verify metrics
293279
Double viewerShares = meterRegistry.find("wisemapping.api.mindmaps.shared")
294280
.tag("role", "viewer")
295-
.tag("collaborator_email_provider", "gmail")
296281
.counter().count();
297282

298283
Double editorShares = meterRegistry.find("wisemapping.api.mindmaps.shared")
299284
.tag("role", "editor")
300-
.tag("collaborator_email_provider", "other")
301285
.counter().count();
302286

303287
assertEquals(1.0, viewerShares);
@@ -370,20 +354,7 @@ void testSpamPreventionMetrics() {
370354
assertEquals(1.0, prevention);
371355
}
372356

373-
@Test
374-
void testEmailProviderExtraction() {
375-
// Test various email providers
376-
assertEquals("gmail", metricsService.extractEmailProvider("test@gmail.com"));
377-
assertEquals("gmail", metricsService.extractEmailProvider("test@googlemail.com"));
378-
assertEquals("yahoo", metricsService.extractEmailProvider("test@yahoo.com"));
379-
assertEquals("microsoft", metricsService.extractEmailProvider("test@outlook.com"));
380-
assertEquals("microsoft", metricsService.extractEmailProvider("test@hotmail.com"));
381-
assertEquals("apple", metricsService.extractEmailProvider("test@icloud.com"));
382-
assertEquals("education", metricsService.extractEmailProvider("test@university.edu"));
383-
assertEquals("government", metricsService.extractEmailProvider("test@agency.gov"));
384-
assertEquals("other", metricsService.extractEmailProvider("test@company.com"));
385-
assertEquals("other", metricsService.extractEmailProvider("invalid-email"));
386-
}
357+
387358

388359
private Account createTestUser(String email, AuthenticationType authType) {
389360
Account user = new Account();

0 commit comments

Comments
 (0)