blob: 76fab2681634a93b356acabcb7c617d23bfb0f51 [file]
#include "tlbmc/collector/sensor_collector_aggregator.h"
#include <algorithm>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "absl/status/status.h"
#include "tlbmc/collector/monitoring_change_base.h"
#include "tlbmc/collector/sensor_collector.h"
#include "sensor.pb.h"
#include "tlbmc/sensors/sensor.h"
#include "tlbmc/sensors/shared_mem_based_sensor.h"
namespace milotic_tlbmc {
namespace {
// A MonitoringChangeBase implementation for SensorCollectorAggregator.
// This wraps around the MonitoringChangeBase from BulkSensorCollectorBase and
// SensorCollector.
class AggregatorMonitoringChange : public MonitoringChangeBase {
public:
AggregatorMonitoringChange(
std::unique_ptr<MonitoringChangeBase> sensor_change,
std::unique_ptr<MonitoringChangeBase> bulk_change)
: sensor_change_(std::move(sensor_change)),
bulk_change_(std::move(bulk_change)) {}
bool AddSensor(std::string_view sensor_key, Mutation mutation) & override {
bool handled =
bulk_change_ && bulk_change_->AddSensor(sensor_key, mutation);
if (!handled && sensor_change_) {
handled = sensor_change_->AddSensor(sensor_key, mutation);
}
return handled;
}
void Apply() && override {
if (sensor_change_) {
std::move(*sensor_change_).Apply();
}
if (bulk_change_) {
std::move(*bulk_change_).Apply();
}
}
private:
std::unique_ptr<MonitoringChangeBase> sensor_change_;
std::unique_ptr<MonitoringChangeBase> bulk_change_;
};
} // namespace
std::vector<std::string> SensorCollectorAggregator::GetAllSensorKeysByConfigKey(
const std::string& board_config_key) const {
std::vector<std::string> keys;
sensor_collector_->GetAllSensorKeysByConfigKey(board_config_key, keys);
if (bulk_sensor_collector_) {
bulk_sensor_collector_->GetAllSensorKeysByConfigKey(board_config_key, keys);
}
std::sort(keys.begin(), keys.end());
return keys;
}
std::vector<std::shared_ptr<const Sensor>>
SensorCollectorAggregator::GetAllSensors() const {
std::vector<std::shared_ptr<const Sensor>> sensors;
sensor_collector_->GetAllSensors(sensors);
if (bulk_sensor_collector_) {
bulk_sensor_collector_->GetAllSensors(sensors);
}
std::sort(sensors.begin(), sensors.end(),
[](const std::shared_ptr<const Sensor>& a,
const std::shared_ptr<const Sensor>& b) {
return a->GetKey() < b->GetKey();
});
return sensors;
}
std::vector<std::string>
SensorCollectorAggregator::GetAllSharedMemorySensorKeys() const {
// No shared memory sensors in bulk_sensor_collector_
std::vector<std::string> all_shm_keys;
std::vector<std::shared_ptr<const Sensor>> sensors;
sensor_collector_->GetAllSensors(sensors);
for (const std::shared_ptr<const Sensor>& sensor : sensors) {
if (!sensor) {
continue;
}
const std::string sensor_key = sensor->GetKey();
if (SharedMemBasedSensor::IsSharedMemSensorKey(sensor_key)) {
all_shm_keys.push_back(sensor_key);
}
}
return all_shm_keys;
}
std::shared_ptr<const Sensor> SensorCollectorAggregator::GetSensorBySensorKey(
const std::string& sensor_key) const {
std::shared_ptr<const Sensor> sensor = nullptr;
if (bulk_sensor_collector_) {
sensor = bulk_sensor_collector_->GetSensorBySensorKey(sensor_key);
}
if (!sensor) {
sensor = sensor_collector_->GetSensorBySensorKey(sensor_key);
}
return sensor;
}
std::shared_ptr<const Sensor>
SensorCollectorAggregator::GetSensorByConfigKeyAndSensorKey(
const std::string& board_config_key, const std::string& sensor_key) const {
std::shared_ptr<const Sensor> sensor =
sensor_collector_->GetSensorByConfigKeyAndSensorKey(board_config_key,
sensor_key);
if (!sensor && bulk_sensor_collector_) {
sensor = bulk_sensor_collector_->GetSensorByConfigKeyAndSensorKey(
board_config_key, sensor_key);
}
return sensor;
}
absl::Status SensorCollectorAggregator::WriteToSensor(
const std::string& sensor_key, const SensorValue& value) {
// No write support in bulk_sensor_collector_
return sensor_collector_->WriteToSensor(sensor_key, value);
}
absl::Status SensorCollectorAggregator::SetDevpathForSensor(
std::string_view sensor_key, std::string_view devpath) {
absl::Status status =
sensor_collector_->SetDevpathForSensor(sensor_key, devpath);
if (status.ok() || !absl::IsNotFound(status)) {
return status;
}
if (bulk_sensor_collector_ != nullptr) {
return bulk_sensor_collector_->SetDevpathForSensor(sensor_key, devpath);
}
return status;
}
void SensorCollectorAggregator::StartSensorCollection() {
sensor_collector_->StartCollection();
if (bulk_sensor_collector_) {
bulk_sensor_collector_->StartSensorCollection();
}
}
void SensorCollectorAggregator::HandleNewlyFoundFru(
const std::string& config_key) {
sensor_collector_->ReinitializeAndScheduleAllSensorsForConfigKey(config_key);
if (bulk_sensor_collector_) {
bulk_sensor_collector_->HandleNewlyFoundFru(config_key);
}
}
std::unique_ptr<MonitoringChangeBase>
SensorCollectorAggregator::CreateMonitoringChange() {
std::unique_ptr<MonitoringChangeBase> bulk_change = nullptr;
if (bulk_sensor_collector_) {
bulk_change = bulk_sensor_collector_->CreateMonitoringChange();
}
return std::make_unique<AggregatorMonitoringChange>(
sensor_collector_->CreateMonitoringChange(), std::move(bulk_change));
}
} // namespace milotic_tlbmc