Skip to content

Commit 20aa0d2

Browse files
author
Ivan Efremov
committed
Issue 6841: Drop unused P3A values from local state.
1 parent 31be1fc commit 20aa0d2

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

components/p3a/brave_p3a_log_store.cc

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ void RecordP3A(uint64_t answers_count) {
3434

3535
} // namespace
3636

37-
BraveP3ALogStore::BraveP3ALogStore(LogSerializer* serializer,
37+
BraveP3ALogStore::BraveP3ALogStore(Delegate* delegate,
3838
PrefService* local_state)
39-
: serializer_(serializer), local_state_(local_state) {
40-
DCHECK(serializer_);
39+
: delegate_(delegate), local_state_(local_state) {
40+
DCHECK(delegate_);
4141
DCHECK(local_state);
4242
}
4343

@@ -123,7 +123,8 @@ void BraveP3ALogStore::StageNextLog() {
123123
DCHECK(!log_.find(staged_entry_key_)->second.sent);
124124

125125
uint64_t staged_entry_value = log_[staged_entry_key_].value;
126-
staged_log_ = serializer_->Serialize(staged_entry_key_, staged_entry_value);
126+
staged_log_ = delegate_->Serialize(staged_entry_key_, staged_entry_value);
127+
127128
VLOG(2) << "BraveP3ALogStore::StageNextLog: staged " << staged_entry_key_;
128129
}
129130

@@ -161,10 +162,17 @@ void BraveP3ALogStore::LoadPersistedUnsentLogs() {
161162
DCHECK(log_.empty());
162163
DCHECK(unsent_entries_.empty());
163164

164-
const base::DictionaryValue* list = local_state_->GetDictionary(kPrefName);
165+
DictionaryPrefUpdate update(local_state_, kPrefName);
166+
base::DictionaryValue* list = update.Get();
165167
for (auto dict_item : list->DictItems()) {
166168
LogEntry entry;
167-
std::string name = dict_item.first;
169+
const std::string name = dict_item.first;
170+
// Check if the metric is obsolete.
171+
if (!delegate_->IsActualMetric(name)) {
172+
// Drop it from the local state.
173+
list->RemoveKey(name);
174+
continue;
175+
}
168176
const base::Value& dict = dict_item.second;
169177
// Value.
170178
if (const base::Value* v =

components/p3a/brave_p3a_log_store.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@ namespace brave {
2525
// for now persisted entries never expire.
2626
class BraveP3ALogStore : public metrics::LogStore {
2727
public:
28-
// Prepares a string representaion of an entry.
29-
class LogSerializer {
28+
class Delegate {
3029
public:
30+
// Prepares a string representaion of an entry.
3131
virtual std::string Serialize(base::StringPiece histogram_name,
3232
uint64_t value) const = 0;
33-
virtual ~LogSerializer() {}
33+
// Returns false if the metric is obsolete and should be cleaned up.
34+
virtual bool IsActualMetric(base::StringPiece histogram_name) const = 0;
35+
virtual ~Delegate() {}
3436
};
3537

36-
explicit BraveP3ALogStore(LogSerializer* serializer,
38+
explicit BraveP3ALogStore(Delegate* delegate,
3739
PrefService* local_state);
3840

3941
// TODO(iefremov): Make parent destructor virtual?
@@ -78,7 +80,7 @@ class BraveP3ALogStore : public metrics::LogStore {
7880
base::Time sent_timestamp; // At the moment only for debugging purposes.
7981
};
8082

81-
const LogSerializer* const serializer_ = nullptr; // Weak.
83+
const Delegate* const delegate_ = nullptr; // Weak.
8284
PrefService* const local_state_ = nullptr;
8385

8486
// TODO(iefremov): Try to replace with base::StringPiece?

components/p3a/brave_p3a_service.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ std::string BraveP3AService::Serialize(base::StringPiece histogram_name,
207207
return message.SerializeAsString();
208208
}
209209

210+
bool
211+
BraveP3AService::IsActualMetric(base::StringPiece histogram_name) const {
212+
static const base::NoDestructor<base::flat_set<base::StringPiece>>
213+
metric_names {std::begin(kCollectedHistograms),
214+
std::end(kCollectedHistograms)};
215+
return metric_names->contains(histogram_name);
216+
}
217+
210218
void BraveP3AService::MaybeOverrideSettingsFromCommandLine() {
211219
base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
212220

components/p3a/brave_p3a_service.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class BraveP3AUploader;
3030
// on any thread.
3131
// TODO(iefremov): It should be possible to get rid of refcounted here.
3232
class BraveP3AService : public base::RefCountedThreadSafe<BraveP3AService>,
33-
public BraveP3ALogStore::LogSerializer {
33+
public BraveP3ALogStore::Delegate {
3434
public:
3535
explicit BraveP3AService(PrefService* local_state);
3636

@@ -43,10 +43,12 @@ class BraveP3AService : public base::RefCountedThreadSafe<BraveP3AService>,
4343
// Needs a living browser process to complete the initialization.
4444
void Init();
4545

46-
// BraveP3ALogStore::LogSerializer
46+
// BraveP3ALogStore::Delegate
4747
std::string Serialize(base::StringPiece histogram_name,
4848
uint64_t value) const override;
4949

50+
bool IsActualMetric(base::StringPiece histogram_name) const override;
51+
5052
private:
5153
friend class base::RefCountedThreadSafe<BraveP3AService>;
5254
~BraveP3AService() override;

0 commit comments

Comments
 (0)