trajectory_collator.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2018 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
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 }  // namespace sensor
00093 }  // namespace cartographer


cartographer
Author(s): The Cartographer Authors
autogenerated on Thu May 9 2019 02:27:36