Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 182673ec authored by Bertrand Simonnet's avatar Bertrand Simonnet Committed by Gerrit Code Review
Browse files

Merge "metrics: Cleanup the system profile setter."

parents 38f55079 52e1d55f
Loading
Loading
Loading
Loading
+21 −113
Original line number Original line Diff line number Diff line
@@ -16,7 +16,6 @@
#include "persistent_integer.h"
#include "persistent_integer.h"
#include "uploader/metrics_log_base.h"
#include "uploader/metrics_log_base.h"
#include "uploader/proto/chrome_user_metrics_extension.pb.h"
#include "uploader/proto/chrome_user_metrics_extension.pb.h"
#include "vboot/crossystem.h"


namespace {
namespace {


@@ -63,43 +62,24 @@ bool SystemProfileCache::Initialize() {
  CHECK(!initialized_)
  CHECK(!initialized_)
      << "this should be called only once in the metrics_daemon lifetime.";
      << "this should be called only once in the metrics_daemon lifetime.";


  std::string chromeos_version;
  std::string channel;
  std::string board;
  if (!base::SysInfo::GetLsbReleaseValue("BRILLO_CHANNEL", &channel) ||
  std::string build_type;
      !base::SysInfo::GetLsbReleaseValue("BRILLO_VERSION", &profile_.version) ||
  if (!base::SysInfo::GetLsbReleaseValue("CHROMEOS_RELEASE_NAME",
      !base::SysInfo::GetLsbReleaseValue("BRILLO_BUILD_TARGET_ID",
                                         &profile_.os_name) ||
                                         &profile_.build_target_id)) {
      !base::SysInfo::GetLsbReleaseValue("CHROMEOS_RELEASE_VERSION",
    LOG(ERROR) << "Could not initialize system profile.";
                                         &profile_.os_version) ||
      !base::SysInfo::GetLsbReleaseValue("CHROMEOS_RELEASE_BOARD", &board) ||
      !base::SysInfo::GetLsbReleaseValue("CHROMEOS_RELEASE_BUILD_TYPE",
                                         &build_type) ||
      !GetChromeOSVersion(&chromeos_version) ||
      !GetHardwareId(&profile_.hardware_class)) {
    DLOG(ERROR) << "failing to initialize profile cache";
    return false;
    return false;
  }
  }


  std::string channel_string;
  base::SysInfo::GetLsbReleaseValue("CHROMEOS_RELEASE_TRACK", &channel_string);
  profile_.channel = ProtoChannelFromString(channel_string);

  profile_.app_version = chromeos_version + " (" + build_type + ")" +
      ChannelToString(profile_.channel) + " " + board;

  // If the product id is not defined, use the default one from the protobuf.
  profile_.product_id = metrics::ChromeUserMetricsExtension::CHROME;
  if (GetProductId(&profile_.product_id)) {
    DLOG(INFO) << "Set the product id to " << profile_.product_id;
  }

  profile_.client_id =
  profile_.client_id =
      testing_ ? "client_id_test" : GetPersistentGUID(kPersistentGUIDFile);
      testing_ ? "client_id_test" :
      GetPersistentGUID(metrics::kMetricsGUIDFilePath);
  profile_.hardware_class = "unknown";
  profile_.channel = ProtoChannelFromString(channel);


  // Increment the session_id everytime we initialize this. If metrics_daemon
  // Increment the session_id everytime we initialize this. If metrics_daemon
  // does not crash, this should correspond to the number of reboots of the
  // does not crash, this should correspond to the number of reboots of the
  // system.
  // system.
  // TODO(bsimonnet): Change this to map to the number of time system-services
  // is started.
  session_id_->Add(1);
  session_id_->Add(1);
  profile_.session_id = static_cast<int32_t>(session_id_->Get());
  profile_.session_id = static_cast<int32_t>(session_id_->Get());


@@ -123,21 +103,21 @@ void SystemProfileCache::Populate(
  metrics_proto->set_session_id(profile_.session_id);
  metrics_proto->set_session_id(profile_.session_id);


  // Sets the product id.
  // Sets the product id.
  metrics_proto->set_product(profile_.product_id);
  metrics_proto->set_product(9);


  metrics::SystemProfileProto* profile_proto =
  metrics::SystemProfileProto* profile_proto =
      metrics_proto->mutable_system_profile();
      metrics_proto->mutable_system_profile();
  profile_proto->mutable_hardware()->set_hardware_class(
  profile_proto->mutable_hardware()->set_hardware_class(
      profile_.hardware_class);
      profile_.hardware_class);
  profile_proto->set_app_version(profile_.app_version);
  profile_proto->set_app_version(profile_.version);
  profile_proto->set_channel(profile_.channel);
  profile_proto->set_channel(profile_.channel);

  metrics::SystemProfileProto_BrilloDeviceData* device_data =
  metrics::SystemProfileProto_OS* os = profile_proto->mutable_os();
      profile_proto->mutable_brillo();
  os->set_name(profile_.os_name);
  device_data->set_build_target_id(profile_.build_target_id);
  os->set_version(profile_.os_version);
}
}


std::string SystemProfileCache::GetPersistentGUID(const std::string& filename) {
std::string SystemProfileCache::GetPersistentGUID(
    const std::string& filename) {
  std::string guid;
  std::string guid;
  base::FilePath filepath(filename);
  base::FilePath filepath(filename);
  if (!base::ReadFileToString(filepath, &guid)) {
  if (!base::ReadFileToString(filepath, &guid)) {
@@ -149,87 +129,15 @@ std::string SystemProfileCache::GetPersistentGUID(const std::string& filename) {
  return guid;
  return guid;
}
}


bool SystemProfileCache::GetChromeOSVersion(std::string* version) {
  if (testing_) {
    *version = "0.0.0.0";
    return true;
  }

  std::string milestone, build, branch, patch;
  unsigned tmp;
  if (base::SysInfo::GetLsbReleaseValue("CHROMEOS_RELEASE_CHROME_MILESTONE",
                                        &milestone) &&
      base::SysInfo::GetLsbReleaseValue("CHROMEOS_RELEASE_BUILD_NUMBER",
                                        &build) &&
      base::SysInfo::GetLsbReleaseValue("CHROMEOS_RELEASE_BRANCH_NUMBER",
                                        &branch) &&
      base::SysInfo::GetLsbReleaseValue("CHROMEOS_RELEASE_PATCH_NUMBER",
                                        &patch)) {
    // Convert to uint to ensure those fields are positive numbers.
    if (base::StringToUint(milestone, &tmp) &&
        base::StringToUint(build, &tmp) &&
        base::StringToUint(branch, &tmp) &&
        base::StringToUint(patch, &tmp)) {
      std::vector<std::string> parts = {milestone, build, branch, patch};
      *version = JoinString(parts, '.');
      return true;
    }
    DLOG(INFO) << "The milestone, build, branch or patch is not a positive "
               << "number.";
    return false;
  }
  DLOG(INFO) << "Field missing from /etc/lsb-release";
  return false;
}

bool SystemProfileCache::GetHardwareId(std::string* hwid) {
  CHECK(hwid);

  if (testing_) {
    // if we are in test mode, we do not call crossystem directly.
    DLOG(INFO) << "skipping hardware id";
    *hwid = "";
    return true;
  }

  char buffer[128];
  if (buffer != VbGetSystemPropertyString("hwid", buffer, sizeof(buffer))) {
    LOG(ERROR) << "error getting hwid";
    return false;
  }

  *hwid = std::string(buffer);
  return true;
}

bool SystemProfileCache::GetProductId(int* product_id) const {
  chromeos::OsReleaseReader reader;
  if (testing_) {
    base::FilePath root(config_root_);
    reader.LoadTestingOnly(root);
  } else {
    reader.Load();
  }

  std::string id;
  if (reader.GetString(kProductIdFieldName, &id)) {
    CHECK(base::StringToInt(id, product_id)) << "Failed to convert product_id "
                                             << id << " to int.";
    return true;
  }
  return false;
}

metrics::SystemProfileProto_Channel SystemProfileCache::ProtoChannelFromString(
metrics::SystemProfileProto_Channel SystemProfileCache::ProtoChannelFromString(
    const std::string& channel) {
    const std::string& channel) {

  if (channel == "stable") {
  if (channel == "stable-channel") {
    return metrics::SystemProfileProto::CHANNEL_STABLE;
    return metrics::SystemProfileProto::CHANNEL_STABLE;
  } else if (channel == "dev-channel") {
  } else if (channel == "dev") {
    return metrics::SystemProfileProto::CHANNEL_DEV;
    return metrics::SystemProfileProto::CHANNEL_DEV;
  } else if (channel == "beta-channel") {
  } else if (channel == "beta") {
    return metrics::SystemProfileProto::CHANNEL_BETA;
    return metrics::SystemProfileProto::CHANNEL_BETA;
  } else if (channel == "canary-channel") {
  } else if (channel == "canary") {
    return metrics::SystemProfileProto::CHANNEL_CANARY;
    return metrics::SystemProfileProto::CHANNEL_CANARY;
  }
  }


+6 −23
Original line number Original line Diff line number Diff line
@@ -21,14 +21,12 @@ class ChromeUserMetricsExtension;
}
}


struct SystemProfile {
struct SystemProfile {
  std::string os_name;
  std::string version;
  std::string os_version;
  metrics::SystemProfileProto::Channel channel;
  std::string app_version;
  std::string hardware_class;
  std::string hardware_class;
  std::string client_id;
  std::string client_id;
  int32_t session_id;
  int session_id;
  int32_t product_id;
  metrics::SystemProfileProto::Channel channel;
  std::string build_target_id;
};
};


// Retrieves general system informations needed by the protobuf for context and
// Retrieves general system informations needed by the protobuf for context and
@@ -42,9 +40,9 @@ class SystemProfileCache : public SystemProfileSetter {
  SystemProfileCache(bool testing, const std::string& config_root);
  SystemProfileCache(bool testing, const std::string& config_root);


  // Populates the ProfileSystem protobuf with system information.
  // Populates the ProfileSystem protobuf with system information.
  void Populate(metrics::ChromeUserMetricsExtension* profile_proto) override;
  void Populate(metrics::ChromeUserMetricsExtension* metrics_proto) override;


  // Converts a string representation of the channel (|channel|-channel) to a
  // Converts a string representation of the channel to a
  // SystemProfileProto_Channel
  // SystemProfileProto_Channel
  static metrics::SystemProfileProto_Channel ProtoChannelFromString(
  static metrics::SystemProfileProto_Channel ProtoChannelFromString(
      const std::string& channel);
      const std::string& channel);
@@ -65,21 +63,6 @@ class SystemProfileCache : public SystemProfileSetter {
  // Initializes |profile_| only if it has not been yet initialized.
  // Initializes |profile_| only if it has not been yet initialized.
  bool InitializeOrCheck();
  bool InitializeOrCheck();


  // Gets the hardware ID using crossystem
  bool GetHardwareId(std::string* hwid);

  // Gets the product ID from the GOOGLE_METRICS_PRODUCT_ID field.
  bool GetProductId(int* product_id) const;

  // Generate the formatted chromeos version from the fields in
  // /etc/lsb-release. The format is A.B.C.D where A, B, C and D are positive
  // integer representing:
  // * the chrome milestone
  // * the build number
  // * the branch number
  // * the patch number
  bool GetChromeOSVersion(std::string* version);

  bool initialized_;
  bool initialized_;
  bool testing_;
  bool testing_;
  std::string config_root_;
  std::string config_root_;
+1 −1
Original line number Original line Diff line number Diff line
@@ -21,7 +21,7 @@
#include "serialization/serialization_utils.h"
#include "serialization/serialization_utils.h"
#include "uploader/metrics_log.h"
#include "uploader/metrics_log.h"
#include "uploader/sender_http.h"
#include "uploader/sender_http.h"
#include "uploader/system_profile_cache.h"
#include "uploader/system_profile_setter.h"


const int UploadService::kMaxFailedUpload = 10;
const int UploadService::kMaxFailedUpload = 10;