aboutsummaryrefslogtreecommitdiff
path: root/src/common/telemetry.h
blob: 49186e848a6c13ca0ffe206af5357ae2b748838c (plain) (blame)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <chrono>
#include <map>
#include <memory>
#include <string>
#include "common/common_types.h"

namespace Common::Telemetry {

/// Field type, used for grouping fields together in the final submitted telemetry log
enum class FieldType : u8 {
    None = 0,     ///< No specified field group
    App,          ///< yuzu application fields (e.g. version, branch, etc.)
    Session,      ///< Emulated session fields (e.g. title ID, log, etc.)
    Performance,  ///< Emulated performance (e.g. fps, emulated CPU speed, etc.)
    UserFeedback, ///< User submitted feedback (e.g. star rating, user notes, etc.)
    UserConfig,   ///< User configuration fields (e.g. emulated CPU core, renderer, etc.)
    UserSystem,   ///< User system information (e.g. host CPU type, RAM, etc.)
};

struct VisitorInterface;

/**
 * Interface class for telemetry data fields.
 */
class FieldInterface : NonCopyable {
public:
    virtual ~FieldInterface() = default;

    /**
     * Accept method for the visitor pattern.
     * @param visitor Reference to the visitor that will visit this field.
     */
    virtual void Accept(VisitorInterface& visitor) const = 0;

    /**
     * Gets the name of this field.
     * @returns Name of this field as a string.
     */
    virtual const std::string& GetName() const = 0;
};

/**
 * Represents a telemetry data field, i.e. a unit of data that gets logged and submitted to our
 * telemetry web service.
 */
template <typename T>
class Field : public FieldInterface {
public:
    Field(FieldType type_, std::string name_, T value_)
        : name(std::move(name_)), type(type_), value(std::move(value_)) {}

    Field(const Field&) = default;
    Field& operator=(const Field&) = default;

    Field(Field&&) = default;
    Field& operator=(Field&& other) = default;

    void Accept(VisitorInterface& visitor) const override;

    [[nodiscard]] const std::string& GetName() const override {
        return name;
    }

    /**
     * Returns the type of the field.
     */
    [[nodiscard]] FieldType GetType() const {
        return type;
    }

    /**
     * Returns the value of the field.
     */
    [[nodiscard]] const T& GetValue() const {
        return value;
    }

    [[nodiscard]] bool operator==(const Field& other) const {
        return (type == other.type) && (name == other.name) && (value == other.value);
    }

    [[nodiscard]] bool operator!=(const Field& other) const {
        return !operator==(other);
    }

private:
    std::string name; ///< Field name, must be unique
    FieldType type{}; ///< Field type, used for grouping fields together
    T value;          ///< Field value
};

/**
 * Collection of data fields that have been logged.
 */
class FieldCollection final : NonCopyable {
public:
    FieldCollection() = default;

    /**
     * Accept method for the visitor pattern, visits each field in the collection.
     * @param visitor Reference to the visitor that will visit each field.
     */
    void Accept(VisitorInterface& visitor) const;

    /**
     * Creates a new field and adds it to the field collection.
     * @param type Type of the field to add.
     * @param name Name of the field to add.
     * @param value Value for the field to add.
     */
    template <typename T>
    void AddField(FieldType type, const char* name, T value) {
        return AddField(std::make_unique<Field<T>>(type, name, std::move(value)));
    }

    /**
     * Adds a new field to the field collection.
     * @param field Field to add to the field collection.
     */
    void AddField(std::unique_ptr<FieldInterface> field);

private:
    std::map<std::string, std::unique_ptr<FieldInterface>> fields;
};

/**
 * Telemetry fields visitor interface class. A backend to log to a web service should implement
 * this interface.
 */
struct VisitorInterface : NonCopyable {
    virtual ~VisitorInterface() = default;

    virtual void Visit(const Field<bool>& field) = 0;
    virtual void Visit(const Field<double>& field) = 0;
    virtual void Visit(const Field<float>& field) = 0;
    virtual void Visit(const Field<u8>& field) = 0;
    virtual void Visit(const Field<u16>& field) = 0;
    virtual void Visit(const Field<u32>& field) = 0;
    virtual void Visit(const Field<u64>& field) = 0;
    virtual void Visit(const Field<s8>& field) = 0;
    virtual void Visit(const Field<s16>& field) = 0;
    virtual void Visit(const Field<s32>& field) = 0;
    virtual void Visit(const Field<s64>& field) = 0;
    virtual void Visit(const Field<std::string>& field) = 0;
    virtual void Visit(const Field<const char*>& field) = 0;
    virtual void Visit(const Field<std::chrono::microseconds>& field) = 0;

    /// Completion method, called once all fields have been visited
    virtual void Complete() = 0;
    virtual bool SubmitTestcase() = 0;
};

/**
 * Empty implementation of VisitorInterface that drops all fields. Used when a functional
 * backend implementation is not available.
 */
struct NullVisitor : public VisitorInterface {
    ~NullVisitor() = default;

    void Visit(const Field<bool>& /*field*/) override {}
    void Visit(const Field<double>& /*field*/) override {}
    void Visit(const Field<float>& /*field*/) override {}
    void Visit(const Field<u8>& /*field*/) override {}
    void Visit(const Field<u16>& /*field*/) override {}
    void Visit(const Field<u32>& /*field*/) override {}
    void Visit(const Field<u64>& /*field*/) override {}
    void Visit(const Field<s8>& /*field*/) override {}
    void Visit(const Field<s16>& /*field*/) override {}
    void Visit(const Field<s32>& /*field*/) override {}
    void Visit(const Field<s64>& /*field*/) override {}
    void Visit(const Field<std::string>& /*field*/) override {}
    void Visit(const Field<const char*>& /*field*/) override {}
    void Visit(const Field<std::chrono::microseconds>& /*field*/) override {}

    void Complete() override {}
    bool SubmitTestcase() override {
        return false;
    }
};

/// Appends build-specific information to the given FieldCollection,
/// such as branch name, revision hash, etc.
void AppendBuildInfo(FieldCollection& fc);

/// Appends CPU-specific information to the given FieldCollection,
/// such as instruction set extensions, etc.
void AppendCPUInfo(FieldCollection& fc);

/// Appends OS-specific information to the given FieldCollection,
/// such as platform name, etc.
void AppendOSInfo(FieldCollection& fc);

} // namespace Common::Telemetry