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

Commit e8989ae1 authored by Andy Hung's avatar Andy Hung
Browse files

MediaMetrics: Unify Elem of TimeMachine with Elem of Item

Allows simplified management of item data storage.

Test: atest mediametrics_tests
Bug: 138583596
Change-Id: Iaefc53564f1863b77e5e1d3b1be798b4c8ef0175
parent 370e9407
Loading
Loading
Loading
Loading
+17 −13
Original line number Diff line number Diff line
@@ -35,6 +35,14 @@ inline std::ostream & operator<< (std::ostream& s,
    return s;
}

// define a way of printing a std::pair.
template <typename T, typename U>
std::ostream & operator<< (std::ostream& s,
                           const std::pair<T, U>& v) {
    s << "{ " << v.first << ", " << v.second << " }";
    return s;
}

// define a way of printing a variant
// see https://en.cppreference.com/w/cpp/utility/variant/visit
template <typename T0, typename ... Ts>
@@ -53,10 +61,12 @@ std::ostream & operator<< (std::ostream& s,
 * The TimeMachine is NOT thread safe.
 */
class TimeMachine {

    using Elem = std::variant<std::monostate, int32_t, int64_t, double, std::string>;
public:
    using Elem = Item::Prop::Elem;  // use the Item property element.
    using PropertyHistory = std::multimap<int64_t /* time */, Elem>;

private:

    // KeyHistory contains no lock.
    // Access is through the TimeMachine, and a hash-striped lock is used
    // before calling into KeyHistory.
@@ -102,7 +112,8 @@ class TimeMachine {

        void putProp(
                const std::string &name, const mediametrics::Item::Prop &prop, int64_t time = 0) {
            prop.visit([&](auto value) { putValue(name, value, time); });
            //alternatively: prop.visit([&](auto value) { putValue(name, value, time); });
            putValue(name, prop.get(), time);
        }

        template <typename T>
@@ -119,13 +130,6 @@ class TimeMachine {
            }
        }

        // Explicitly ignore rate properties - we don't expose them for now.
        void putValue(
                      const std::string &property __unused,
                      std::pair<int64_t, int64_t>& e __unused,
                      int64_t time __unused) {
        }

        std::pair<std::string, int32_t> dump(int32_t lines, int64_t time) const {
            std::stringstream ss;
            int32_t ll = lines;
@@ -358,10 +362,10 @@ public:

        std::stringstream ss;
        int32_t ll = lines;
        for (const auto &keyPair : mHistory) {
            std::lock_guard lock(getLockForKey(keyPair.first));
        for (const auto &[lkey, lhist] : mHistory) {
            std::lock_guard lock(getLockForKey(lkey));
            if (lines <= 0) break;
            auto [s, l] = keyPair.second->dump(ll, time);
            auto [s, l] = lhist->dump(ll, time);
            ss << s;
            ll -= l;
        }