Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "cartographer/sensor/internal/trajectory_collator.h"
00018
00019 #include "absl/strings/str_cat.h"
00020
00021 namespace cartographer {
00022 namespace sensor {
00023
00024 metrics::Family<metrics::Counter>*
00025 TrajectoryCollator::collator_metrics_family_ =
00026 metrics::Family<metrics::Counter>::Null();
00027
00028 void TrajectoryCollator::AddTrajectory(
00029 const int trajectory_id,
00030 const absl::flat_hash_set<std::string>& expected_sensor_ids,
00031 const Callback& callback) {
00032 CHECK_EQ(trajectory_to_queue_.count(trajectory_id), 0);
00033 for (const auto& sensor_id : expected_sensor_ids) {
00034 const auto queue_key = QueueKey{trajectory_id, sensor_id};
00035 trajectory_to_queue_[trajectory_id].AddQueue(
00036 queue_key, [callback, sensor_id](std::unique_ptr<Data> data) {
00037 callback(sensor_id, std::move(data));
00038 });
00039 trajectory_to_queue_keys_[trajectory_id].push_back(queue_key);
00040 }
00041 }
00042
00043 void TrajectoryCollator::FinishTrajectory(const int trajectory_id) {
00044 for (const auto& queue_key : trajectory_to_queue_keys_[trajectory_id]) {
00045 trajectory_to_queue_.at(trajectory_id).MarkQueueAsFinished(queue_key);
00046 }
00047 }
00048
00049 void TrajectoryCollator::AddSensorData(const int trajectory_id,
00050 std::unique_ptr<Data> data) {
00051 QueueKey queue_key{trajectory_id, data->GetSensorId()};
00052 auto* metric = GetOrCreateSensorMetric(data->GetSensorId(), trajectory_id);
00053 metric->Increment();
00054 trajectory_to_queue_.at(trajectory_id)
00055 .Add(std::move(queue_key), std::move(data));
00056 }
00057
00058 void TrajectoryCollator::Flush() {
00059 for (auto& it : trajectory_to_queue_) {
00060 it.second.Flush();
00061 }
00062 }
00063
00064 absl::optional<int> TrajectoryCollator::GetBlockingTrajectoryId() const {
00065 return absl::optional<int>();
00066 }
00067
00068 void TrajectoryCollator::RegisterMetrics(
00069 metrics::FamilyFactory* family_factory) {
00070 collator_metrics_family_ = family_factory->NewCounterFamily(
00071 "collator_input_total", "Sensor data received");
00072 }
00073
00074 metrics::Counter* TrajectoryCollator::GetOrCreateSensorMetric(
00075 const std::string& sensor_id, int trajectory_id) {
00076 const std::string trajectory_id_str = absl::StrCat(trajectory_id);
00077 const std::string map_key = absl::StrCat(sensor_id, "/", trajectory_id_str);
00078
00079 auto metrics_map_itr = metrics_map_.find(map_key);
00080 if (metrics_map_itr != metrics_map_.end()) {
00081 return metrics_map_itr->second;
00082 }
00083
00084 LOG(INFO) << "Create metrics handler for key: " << map_key;
00085 auto new_counter = collator_metrics_family_->Add(
00086 {{"sensor_id", sensor_id}, {"trajectory_id", trajectory_id_str}});
00087
00088 metrics_map_[map_key] = new_counter;
00089 return new_counter;
00090 }
00091
00092 }
00093 }