1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50

ash / webui / metrics / structured_metrics_service_wrapper.cc [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/webui/metrics/structured_metrics_service_wrapper.h"

#include "base/system/sys_info.h"
#include "chromeos/crosapi/mojom/structured_metrics_service.mojom.h"
#include "components/metrics/structured/event.h"
#include "components/metrics/structured/structured_metrics_client.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver_set.h"

namespace ash {

StructuredMetricsServiceWrapper::StructuredMetricsServiceWrapper() = default;
StructuredMetricsServiceWrapper::~StructuredMetricsServiceWrapper() = default;

void StructuredMetricsServiceWrapper::BindReceiver(
    mojo::PendingReceiver<::crosapi::mojom::StructuredMetricsService>
        receiver) {
  receivers_.Add(this, std::move(receiver));
}

// static
base::TimeDelta StructuredMetricsServiceWrapper::ComputeEventUptime(
    base::TimeDelta system_uptime,
    base::TimeDelta system_timestamp,
    base::TimeDelta event_timestamp) {
  return system_uptime -
         std::max(system_timestamp - event_timestamp, base::Seconds(0));
}

void StructuredMetricsServiceWrapper::Record(
    std::vector<::metrics::structured::Event> events) {
  for (auto& event : events) {
    if (event.IsEventSequenceType()) {
      event.SetRecordedTimeSinceBoot(ComputeEventUptime(
          base::SysInfo::Uptime(),
          base::Time::NowFromSystemTime() - base::Time::UnixEpoch(),
          // if event does not have system uptime field populated, set it as the
          // current system timestamp.
          event.recorded_time_since_boot()));
    }
    ::metrics::structured::StructuredMetricsClient::Get()->Record(
        std::move(event));
  }
}

}  // namespace ash