Skip to content
This repository was archived by the owner on Nov 20, 2023. It is now read-only.

Commit 45a7918

Browse files
committed
saving events
1 parent 6777ed2 commit 45a7918

2 files changed

Lines changed: 93 additions & 1 deletion

File tree

src/main/java/com/moesif/api/controllers/APIController.java

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package com.moesif.api.controllers;
77

88
import java.util.*;
9+
import java.util.logging.Logger;
910

1011
import com.fasterxml.jackson.core.JsonProcessingException;
1112

@@ -19,6 +20,9 @@
1920
import com.moesif.api.controllers.syncwrapper.APICallBackCatcher;
2021

2122
public class APIController extends BaseController implements IAPIController {
23+
private static final Logger logger = Logger.getLogger(APIController.class.toString());
24+
private static final boolean debug = false; // TODO
25+
2226
//private static variables for the singleton pattern
2327
private static Object syncObject = new Object();
2428
private static APIController instance = null;
@@ -461,6 +465,87 @@ public void run() {
461465
//execute async using thread pool
462466
APIHelper.getScheduler().execute(_responseTask);
463467
}
468+
469+
public AppConfigModel getCachedAndLoadAppConfig() {
470+
// TODO: download new one if necessary
471+
return getDefaultAppConfig();
472+
}
473+
474+
public AppConfigModel getDefaultAppConfig() {
475+
return new AppConfigBuilder()
476+
.sampleRate(100)
477+
.etag("default")
478+
.build();
479+
}
480+
481+
private EventModel buildEvent(
482+
EventRequestModel eventRequestModel,
483+
EventResponseModel eventResponseModel,
484+
String userId,
485+
String sessionToken,
486+
String tags,
487+
Object metadata) {
488+
EventBuilder eb = new EventBuilder();
489+
eb.request(eventRequestModel);
490+
eb.response(eventResponseModel);
491+
if (userId != null) {
492+
eb.userId(userId);
493+
}
494+
if (sessionToken != null) {
495+
eb.sessionToken(sessionToken);
496+
}
497+
if (tags != null) {
498+
eb.tags(tags);
499+
}
500+
501+
if (metadata != null) {
502+
eb.metadata(metadata);
503+
}
504+
505+
EventModel event = eb.build();
506+
507+
// actually send the event here.
508+
APICallBack<Object> callBack = new APICallBack<Object>() {
509+
public void onSuccess(HttpContext context, Object response) {
510+
if (debug) {
511+
logger.info("send to Moesif success");
512+
}
513+
}
514+
515+
public void onFailure(HttpContext context, Throwable error) {
516+
if (debug) {
517+
logger.info("send to Moesif error ");
518+
logger.info( error.toString());
519+
}
520+
}
521+
};
522+
523+
return event;
524+
}
525+
526+
public boolean trySendEvent(EventModel event) {
527+
AppConfigModel appConfig = getCachedAndLoadAppConfig();
528+
529+
boolean willSend = appConfig.getSampleRate() >= Math.random() * 100;
530+
531+
if (willSend) {
532+
Map<String, String> eventApiResponse = null;
533+
534+
try {
535+
eventApiResponse = createEvent(event);
536+
} catch (Throwable e) {
537+
if (debug) {
538+
logger.warning("Send to Moesif failed " + e.toString());
539+
}
540+
}
541+
542+
if (eventApiResponse != null) {
543+
544+
}
545+
}
546+
547+
return willSend;
548+
}
464549

465550
/**
466551
* Update a Single Company
@@ -588,7 +673,6 @@ public void run() {
588673
APIHelper.getScheduler().execute(_responseTask);
589674
}
590675

591-
592676
private APICallBack<HttpResponse> createHttpResponseCallback(final APICallBack<Object> callBack) {
593677

594678
return new APICallBack<HttpResponse>() {

src/main/java/com/moesif/api/models/AppConfigBuilder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,12 @@ public AppConfigBuilder etag(String etag) {
2929
AppConfigModel.setEtag(etag);
3030
return this;
3131
}
32+
33+
/**
34+
* Build the instance with the given values
35+
* @return The built UserModel
36+
*/
37+
public AppConfigModel build() {
38+
return AppConfigModel;
39+
}
3240
}

0 commit comments

Comments
 (0)