| 
									
										
										
										
											2021-08-20 10:58:07 -07:00
										 |  |  | package controllers;
 | 
					
						
							| 
									
										
										
										
											2021-05-11 15:41:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-06 18:56:32 -07:00
										 |  |  | import auth.Authenticator;
 | 
					
						
							|  |  |  | import client.AuthServiceClient;
 | 
					
						
							| 
									
										
										
										
											2021-05-11 15:41:42 -07:00
										 |  |  | import com.fasterxml.jackson.databind.JsonNode;
 | 
					
						
							|  |  |  | import com.typesafe.config.Config;
 | 
					
						
							| 
									
										
										
										
											2022-10-06 18:56:32 -07:00
										 |  |  | import javax.annotation.Nonnull;
 | 
					
						
							|  |  |  | import javax.inject.Inject;
 | 
					
						
							| 
									
										
										
										
											2022-12-26 10:09:08 -06:00
										 |  |  | import javax.inject.Singleton;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-11 15:41:42 -07:00
										 |  |  | import org.apache.kafka.clients.producer.ProducerRecord;
 | 
					
						
							| 
									
										
										
										
											2021-06-25 10:56:45 -07:00
										 |  |  | import org.slf4j.Logger;
 | 
					
						
							|  |  |  | import org.slf4j.LoggerFactory;
 | 
					
						
							| 
									
										
										
										
											2021-05-11 15:41:42 -07:00
										 |  |  | import play.mvc.Controller;
 | 
					
						
							| 
									
										
										
										
											2022-10-06 18:56:32 -07:00
										 |  |  | import play.mvc.Http;
 | 
					
						
							| 
									
										
										
										
											2021-05-11 15:41:42 -07:00
										 |  |  | import play.mvc.Result;
 | 
					
						
							|  |  |  | import play.mvc.Security;
 | 
					
						
							| 
									
										
										
										
											2022-12-26 10:09:08 -06:00
										 |  |  | import client.KafkaTrackingProducer;
 | 
					
						
							| 
									
										
										
										
											2021-07-08 15:11:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-26 10:09:08 -06:00
										 |  |  | import static auth.AuthUtils.ACTOR;
 | 
					
						
							| 
									
										
										
										
											2021-05-11 15:41:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 10:58:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | // TODO: Migrate this to metadata-service.
 | 
					
						
							| 
									
										
										
										
											2022-12-26 10:09:08 -06:00
										 |  |  | @Singleton
 | 
					
						
							| 
									
										
										
										
											2021-05-11 15:41:42 -07:00
										 |  |  | public class TrackingController extends Controller {
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-25 10:56:45 -07:00
										 |  |  |     private final Logger _logger = LoggerFactory.getLogger(TrackingController.class.getName());
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-11 15:41:42 -07:00
										 |  |  |     private final String _topic;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-26 10:09:08 -06:00
										 |  |  |     @Inject
 | 
					
						
							|  |  |  |     KafkaTrackingProducer _producer;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-06 18:56:32 -07:00
										 |  |  |     @Inject
 | 
					
						
							|  |  |  |     AuthServiceClient _authClient;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-11 15:41:42 -07:00
										 |  |  |     @Inject
 | 
					
						
							|  |  |  |     public TrackingController(@Nonnull Config config) {
 | 
					
						
							| 
									
										
										
										
											2022-12-26 10:09:08 -06:00
										 |  |  |         _topic = config.getString("analytics.tracking.topic");
 | 
					
						
							| 
									
										
										
										
											2021-05-11 15:41:42 -07:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @Security.Authenticated(Authenticator.class)
 | 
					
						
							|  |  |  |     @Nonnull
 | 
					
						
							| 
									
										
										
										
											2022-10-06 18:56:32 -07:00
										 |  |  |     public Result track(Http.Request request) throws Exception {
 | 
					
						
							| 
									
										
										
										
											2022-12-26 10:09:08 -06:00
										 |  |  |         if (!_producer.isEnabled()) {
 | 
					
						
							| 
									
										
										
										
											2021-05-11 15:41:42 -07:00
										 |  |  |             // If tracking is disabled, simply return a 200.
 | 
					
						
							|  |  |  |             return status(200);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         JsonNode event;
 | 
					
						
							|  |  |  |         try {
 | 
					
						
							| 
									
										
										
										
											2022-10-06 18:56:32 -07:00
										 |  |  |             event = request.body().asJson();
 | 
					
						
							| 
									
										
										
										
											2021-05-11 15:41:42 -07:00
										 |  |  |         } catch (Exception e) {
 | 
					
						
							|  |  |  |             return badRequest();
 | 
					
						
							|  |  |  |         }
 | 
					
						
							| 
									
										
										
										
											2022-12-08 20:27:51 -06:00
										 |  |  |         final String actor = request.session().data().get(ACTOR);
 | 
					
						
							| 
									
										
										
										
											2021-05-11 15:41:42 -07:00
										 |  |  |         try {
 | 
					
						
							| 
									
										
										
										
											2021-06-25 10:56:45 -07:00
										 |  |  |             _logger.debug(String.format("Emitting product analytics event. actor: %s, event: %s", actor, event));
 | 
					
						
							| 
									
										
										
										
											2021-05-11 15:41:42 -07:00
										 |  |  |             final ProducerRecord<String, String> record = new ProducerRecord<>(
 | 
					
						
							| 
									
										
										
										
											2022-10-06 18:56:32 -07:00
										 |  |  |                 _topic,
 | 
					
						
							|  |  |  |                 actor,
 | 
					
						
							|  |  |  |                 event.toString());
 | 
					
						
							|  |  |  |             _producer.send(record);
 | 
					
						
							|  |  |  |             _authClient.track(event.toString());
 | 
					
						
							|  |  |  |             return ok();
 | 
					
						
							| 
									
										
										
										
											2022-05-10 18:15:53 -05:00
										 |  |  |         } catch (Exception e) {
 | 
					
						
							| 
									
										
										
										
											2021-06-25 10:56:45 -07:00
										 |  |  |             _logger.error(String.format("Failed to emit product analytics event. actor: %s, event: %s", actor, event));
 | 
					
						
							| 
									
										
										
										
											2021-05-11 15:41:42 -07:00
										 |  |  |             return internalServerError(e.getMessage());
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | }
 |