|
6 | 6 | package com.moesif.api.controllers; |
7 | 7 |
|
8 | 8 | import java.util.*; |
| 9 | +import java.util.logging.Logger; |
9 | 10 |
|
10 | 11 | import com.fasterxml.jackson.core.JsonProcessingException; |
11 | 12 |
|
|
19 | 20 | import com.moesif.api.controllers.syncwrapper.APICallBackCatcher; |
20 | 21 |
|
21 | 22 | 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 | + |
22 | 26 | //private static variables for the singleton pattern |
23 | 27 | private static Object syncObject = new Object(); |
24 | 28 | private static APIController instance = null; |
@@ -461,6 +465,87 @@ public void run() { |
461 | 465 | //execute async using thread pool |
462 | 466 | APIHelper.getScheduler().execute(_responseTask); |
463 | 467 | } |
| 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 | + } |
464 | 549 |
|
465 | 550 | /** |
466 | 551 | * Update a Single Company |
@@ -588,7 +673,6 @@ public void run() { |
588 | 673 | APIHelper.getScheduler().execute(_responseTask); |
589 | 674 | } |
590 | 675 |
|
591 | | - |
592 | 676 | private APICallBack<HttpResponse> createHttpResponseCallback(final APICallBack<Object> callBack) { |
593 | 677 |
|
594 | 678 | return new APICallBack<HttpResponse>() { |
|
0 commit comments