2021-08-20 10:58:07 -07:00
|
|
|
package controllers;
|
2021-05-11 15:41:42 -07:00
|
|
|
|
2023-12-06 11:02:42 +05:30
|
|
|
import static auth.AuthUtils.ACTOR;
|
|
|
|
|
2022-10-06 18:56:32 -07:00
|
|
|
import auth.Authenticator;
|
|
|
|
import client.AuthServiceClient;
|
2023-12-06 11:02:42 +05:30
|
|
|
import client.KafkaTrackingProducer;
|
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;
|
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 {
|
|
|
|
|
2023-12-06 11:02:42 +05:30
|
|
|
private final Logger _logger = LoggerFactory.getLogger(TrackingController.class.getName());
|
2021-06-25 10:56:45 -07:00
|
|
|
|
2023-12-06 11:02:42 +05:30
|
|
|
private final String _topic;
|
2021-05-11 15:41:42 -07:00
|
|
|
|
2023-12-06 11:02:42 +05:30
|
|
|
@Inject KafkaTrackingProducer _producer;
|
2022-12-26 10:09:08 -06:00
|
|
|
|
2023-12-06 11:02:42 +05:30
|
|
|
@Inject AuthServiceClient _authClient;
|
2022-10-06 18:56:32 -07:00
|
|
|
|
2023-12-06 11:02:42 +05:30
|
|
|
@Inject
|
|
|
|
public TrackingController(@Nonnull Config config) {
|
|
|
|
_topic = config.getString("analytics.tracking.topic");
|
|
|
|
}
|
2021-05-11 15:41:42 -07:00
|
|
|
|
2023-12-06 11:02:42 +05:30
|
|
|
@Security.Authenticated(Authenticator.class)
|
|
|
|
@Nonnull
|
|
|
|
public Result track(Http.Request request) throws Exception {
|
|
|
|
if (!_producer.isEnabled()) {
|
|
|
|
// If tracking is disabled, simply return a 200.
|
|
|
|
return status(200);
|
|
|
|
}
|
2021-05-11 15:41:42 -07:00
|
|
|
|
2023-12-06 11:02:42 +05:30
|
|
|
JsonNode event;
|
|
|
|
try {
|
|
|
|
event = request.body().asJson();
|
|
|
|
} catch (Exception e) {
|
|
|
|
return badRequest();
|
|
|
|
}
|
|
|
|
final String actor = request.session().data().get(ACTOR);
|
|
|
|
try {
|
|
|
|
_logger.debug(
|
|
|
|
String.format("Emitting product analytics event. actor: %s, event: %s", actor, event));
|
|
|
|
final ProducerRecord<String, String> record =
|
|
|
|
new ProducerRecord<>(_topic, actor, event.toString());
|
|
|
|
_producer.send(record);
|
|
|
|
_authClient.track(event.toString());
|
|
|
|
return ok();
|
|
|
|
} catch (Exception e) {
|
|
|
|
_logger.error(
|
|
|
|
String.format(
|
|
|
|
"Failed to emit product analytics event. actor: %s, event: %s", actor, event));
|
|
|
|
return internalServerError(e.getMessage());
|
2021-05-11 15:41:42 -07:00
|
|
|
}
|
2023-12-06 11:02:42 +05:30
|
|
|
}
|
2021-05-11 15:41:42 -07:00
|
|
|
}
|