Skip to content

Commit 0b7da03

Browse files
committed
Fix a bunch of lints
1 parent 067af68 commit 0b7da03

File tree

7 files changed

+128
-91
lines changed

7 files changed

+128
-91
lines changed

.clang-tidy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
22
CheckOptions:
33
- { key: readability-identifier-length.IgnoredParameterNames, value: 'i|j|k|c|os|it' }
4+
- { key: readability-identifier-length.IgnoredVariableNames, value: 'ec' }
45
- { key: readability-identifier-length.IgnoredLoopCounterNames, value: 'i|j|k|c|os|it' }

libs/common/include/events/detail/asio_event_processor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class AsioEventProcessor : public IEventProcessor {
4343

4444
boost::asio::any_io_executor io_;
4545
Outbox outbox_;
46-
Summarizer summary_state_;
46+
Summarizer summarizer_;
4747

4848
std::chrono::milliseconds flush_interval_;
4949
boost::asio::steady_timer timer_;

libs/common/include/events/detail/summarizer.hpp

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,54 @@
99

1010
namespace launchdarkly::events::detail {
1111

12+
/**
13+
* Summarizer is responsible for accepting FeatureEventParams (the context
14+
* related to a feature evaluation) and outputting summary events (which
15+
* essentially condenses the various evaluation results into a single
16+
* structure).
17+
*/
1218
class Summarizer {
1319
public:
14-
using Date = std::chrono::system_clock::time_point;
20+
using Time = std::chrono::system_clock::time_point;
1521
using FlagKey = std::string;
1622

17-
explicit Summarizer(Date start);
23+
/**
24+
* Construct a Summarizer starting at the given time.
25+
* @param start Start time of the summary.
26+
*/
27+
explicit Summarizer(Time start_time);
28+
29+
/**
30+
* Construct a Summarizer at time zero.
31+
*/
1832
Summarizer();
19-
void update(client::FeatureEventParams const& event);
2033

21-
bool empty() const;
34+
/**
35+
* Updates the summary with a feature event.
36+
* @param event Feature event.
37+
*/
38+
void Update(client::FeatureEventParams const& event);
39+
40+
/**
41+
* Returns true if the summary is empty.
42+
*/
43+
bool Empty() const;
2244

23-
Date start_time() const;
45+
/**
46+
* Returns the summary's start time as given in the constructor.
47+
*/
48+
[[nodiscard]] Time start_time() const;
2449

2550
struct VariationSummary {
26-
std::size_t count;
27-
Value value;
51+
public:
2852
explicit VariationSummary(Value value);
2953
void Increment();
54+
std::size_t count() const;
55+
Value const& value() const;
56+
57+
private:
58+
std::size_t count_;
59+
Value value_;
3060
};
3161

3262
struct VariationKey {
@@ -58,19 +88,19 @@ class Summarizer {
5888
Summarizer::VariationKey::Hash>
5989
counters;
6090

61-
State(Value defaultVal);
91+
explicit State(Value defaultVal);
6292
};
6393

64-
std::unordered_map<FlagKey, State> const& features() const;
94+
[[nodiscard]] std::unordered_map<FlagKey, State> const& features() const;
6595

6696
private:
67-
Date start_time_;
97+
Time start_time_;
6898
std::unordered_map<FlagKey, State> features_;
6999
};
70100

71101
struct Summary {
72102
Summarizer const& summarizer;
73-
Summarizer::Date end_time;
103+
Summarizer::Time end_time;
74104
};
75105

76106
} // namespace launchdarkly::events::detail

libs/common/src/events/asio_event_processor.cpp

Lines changed: 61 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ AsioEventProcessor::AsioEventProcessor(
2323
Logger& logger)
2424
: io_(boost::asio::make_strand(io)),
2525
outbox_(config.capacity()),
26-
summary_state_(std::chrono::system_clock::now()),
26+
summarizer_(std::chrono::system_clock::now()),
2727
flush_interval_(config.flush_interval()),
2828
timer_(io_),
2929
host_(endpoints.events_base_url()), // TODO: parse and use host
@@ -68,14 +68,14 @@ void AsioEventProcessor::AsyncSend(InputEvent input_event) {
6868
if (!InboxIncrement()) {
6969
return;
7070
}
71-
boost::asio::post(io_, [this, e = std::move(input_event)]() mutable {
71+
boost::asio::post(io_, [this, event = std::move(input_event)]() mutable {
7272
InboxDecrement();
73-
HandleSend(std::move(e));
73+
HandleSend(std::move(event));
7474
});
7575
}
7676

77-
void AsioEventProcessor::HandleSend(InputEvent e) {
78-
std::vector<OutputEvent> output_events = Process(std::move(e));
77+
void AsioEventProcessor::HandleSend(InputEvent event) {
78+
std::vector<OutputEvent> output_events = Process(std::move(event));
7979

8080
bool inserted = outbox_.PushDiscardingOverflow(std::move(output_events));
8181
if (!inserted && !full_outbox_encountered_) {
@@ -93,7 +93,7 @@ void AsioEventProcessor::Flush(FlushTrigger flush_type) {
9393
LD_LOG(logger_, LogLevel::kDebug)
9494
<< "event-processor: nothing to flush";
9595
}
96-
summary_state_ = Summarizer(std::chrono::system_clock::now());
96+
summarizer_ = Summarizer(std::chrono::system_clock::now());
9797
if (flush_type == FlushTrigger::Automatic) {
9898
ScheduleFlush();
9999
}
@@ -132,14 +132,16 @@ AsioEventProcessor::MakeRequest() {
132132
return std::nullopt;
133133
}
134134

135-
boost::json::value summary_event;
136-
if (!summary_state_.empty()) {
137-
summary_event = boost::json::value_from(
138-
Summary{summary_state_, std::chrono::system_clock::now()});
135+
auto events = boost::json::value_from(outbox_.Consume());
136+
137+
if (!summarizer_.Empty()) {
138+
events.as_array().push_back(boost::json::value_from(
139+
Summary{summarizer_, std::chrono::system_clock::now()}));
139140
}
140141

141142
LD_LOG(logger_, LogLevel::kDebug)
142143
<< "event-processor: generating http request";
144+
143145
RequestType req;
144146

145147
req.set(http::field::host, host_);
@@ -150,11 +152,6 @@ AsioEventProcessor::MakeRequest() {
150152
req.set(kPayloadIdHeader, boost::lexical_cast<std::string>(uuids_()));
151153
req.target(host_ + path_);
152154

153-
auto events = boost::json::value_from(outbox_.Consume());
154-
if (!summary_event.is_null()) {
155-
events.as_array().push_back(summary_event);
156-
}
157-
158155
req.body() = boost::json::serialize(events);
159156
req.prepare_payload();
160157
return req;
@@ -169,55 +166,57 @@ struct overloaded : Ts... {
169166
template <class... Ts>
170167
overloaded(Ts...) -> overloaded<Ts...>;
171168

172-
std::vector<OutputEvent> AsioEventProcessor::Process(InputEvent event) {
169+
std::vector<OutputEvent> AsioEventProcessor::Process(InputEvent input_event) {
173170
std::vector<OutputEvent> out;
174171
std::visit(
175-
overloaded{
176-
[&](client::FeatureEventParams&& e) {
177-
summary_state_.update(e);
178-
179-
if (!e.eval_result.track_events()) {
180-
return;
181-
}
182-
std::optional<Reason> reason;
183-
184-
// TODO(cwaldren): should also add the reason if the variation
185-
// method was VariationDetail().
186-
if (e.eval_result.track_reason()) {
187-
reason = e.eval_result.detail().reason();
188-
}
189-
190-
client::FeatureEventBase b = {
191-
e.creation_date, std::move(e.key), e.eval_result.version(),
192-
e.eval_result.detail().variation_index(),
193-
e.eval_result.detail().value(), reason,
194-
// TODO(cwaldren): change to actual default; figure out
195-
// where this should be plumbed through.
196-
Value::null()};
197-
198-
auto debug_until_date = e.eval_result.debug_events_until_date();
199-
bool emit_debug_event =
200-
debug_until_date &&
201-
debug_until_date.value() > std::chrono::system_clock::now();
202-
203-
if (emit_debug_event) {
204-
out.emplace_back(
205-
client::DebugEvent{b, filter_.filter(e.context)});
206-
}
207-
// TODO(cwaldren): see about not copying the keys / having the
208-
// getter return a value.
209-
out.emplace_back(client::FeatureEvent{
210-
std::move(b), e.context.kinds_to_keys()});
211-
},
212-
[&](client::IdentifyEventParams&& e) {
213-
// Contexts should already have been checked for
214-
// validity by this point.
215-
assert(e.context.valid());
216-
out.emplace_back(client::IdentifyEvent{
217-
e.creation_date, filter_.filter(e.context)});
218-
},
219-
[&](TrackEventParams&& e) { out.emplace_back(std::move(e)); }},
220-
std::move(event));
172+
overloaded{[&](client::FeatureEventParams&& event) {
173+
summarizer_.Update(event);
174+
175+
if (!event.eval_result.track_events()) {
176+
return;
177+
}
178+
std::optional<Reason> reason;
179+
180+
// TODO(cwaldren): should also add the reason if the
181+
// variation method was VariationDetail().
182+
if (event.eval_result.track_reason()) {
183+
reason = event.eval_result.detail().reason();
184+
}
185+
186+
client::FeatureEventBase base = {
187+
event.creation_date, std::move(event.key),
188+
event.eval_result.version(),
189+
event.eval_result.detail().variation_index(),
190+
event.eval_result.detail().value(), reason,
191+
// TODO(cwaldren): change to actual default; figure
192+
// out where this should be plumbed through.
193+
Value::null()};
194+
195+
auto debug_until_date =
196+
event.eval_result.debug_events_until_date();
197+
bool emit_debug_event =
198+
debug_until_date &&
199+
debug_until_date.value() >
200+
std::chrono::system_clock::now();
201+
202+
if (emit_debug_event) {
203+
out.emplace_back(client::DebugEvent{
204+
base, filter_.filter(event.context)});
205+
}
206+
out.emplace_back(client::FeatureEvent{
207+
std::move(base), event.context.kinds_to_keys()});
208+
},
209+
[&](client::IdentifyEventParams&& event) {
210+
// Contexts should already have been checked for
211+
// validity by this point.
212+
assert(event.context.valid());
213+
out.emplace_back(client::IdentifyEvent{
214+
event.creation_date, filter_.filter(event.context)});
215+
},
216+
[&](TrackEventParams&& event) {
217+
out.emplace_back(std::move(event));
218+
}},
219+
std::move(input_event));
221220

222221
return out;
223222
}

libs/common/src/events/summarizer.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Summarizer::Summarizer(std::chrono::system_clock::time_point start)
77

88
Summarizer::Summarizer() : start_time_(), features_() {}
99

10-
bool Summarizer::empty() const {
10+
bool Summarizer::Empty() const {
1111
return features_.empty();
1212
}
1313

@@ -24,12 +24,9 @@ static bool FlagNotFound(client::FeatureEventParams const& event) {
2424
return false;
2525
}
2626

27-
void Summarizer::update(client::FeatureEventParams const& event) {
27+
void Summarizer::Update(client::FeatureEventParams const& event) {
2828
auto const& kinds = event.context.kinds();
2929

30-
// TODO(cwaldren): Value::null() should be replaced with the default value
31-
// from the evaluation.
32-
3330
auto feature_state_iterator =
3431
features_.try_emplace(event.key, event.default_).first;
3532

@@ -57,9 +54,11 @@ void Summarizer::update(client::FeatureEventParams const& event) {
5754

5855
summary_counter->second.Increment();
5956
}
60-
Summarizer::Date Summarizer::start_time() const {
57+
58+
Summarizer::Time Summarizer::start_time() const {
6159
return start_time_;
6260
}
61+
6362
Summarizer::VariationKey::VariationKey(Version version,
6463
std::optional<VariationIndex> variation)
6564
: version(version), variation(variation) {}
@@ -68,13 +67,21 @@ Summarizer::VariationKey::VariationKey()
6867
: version(std::nullopt), variation(std::nullopt) {}
6968

7069
Summarizer::VariationSummary::VariationSummary(Value value)
71-
: count(0), value(std::move(value)) {}
70+
: count_(0), value_(std::move(value)) {}
7271

7372
void Summarizer::VariationSummary::Increment() {
7473
// todo(cwaldren): prevent overflow?
75-
count++;
74+
count_++;
75+
}
76+
77+
Value const& Summarizer::VariationSummary::value() const {
78+
return value_;
79+
}
80+
81+
std::size_t Summarizer::VariationSummary::count() const {
82+
return count_;
7683
}
7784

78-
Summarizer::State::State(Value defaultVal)
79-
: default_(std::move(defaultVal)), context_kinds() {}
85+
Summarizer::State::State(Value default_value)
86+
: default_(std::move(default_value)), context_kinds() {}
8087
} // namespace launchdarkly::events::detail

libs/common/src/serialization/events/json_events.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ void tag_invoke(boost::json::value_from_tag const&,
124124
if (kvp.first.variation) {
125125
counter.emplace("variation", *kvp.first.variation);
126126
}
127-
counter.emplace("value", boost::json::value_from(kvp.second.value));
128-
counter.emplace("count", kvp.second.count);
127+
counter.emplace("value", boost::json::value_from(kvp.second.value()));
128+
counter.emplace("count", kvp.second.count());
129129
counters.push_back(std::move(counter));
130130
}
131131
obj.emplace("counters", std::move(counters));

libs/common/tests/event_processor_test.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static std::chrono::system_clock::time_point ZeroTime() {
1818

1919
TEST(SummarizerTests, IsEmptyOnConstruction) {
2020
Summarizer summarizer;
21-
ASSERT_TRUE(summarizer.empty());
21+
ASSERT_TRUE(summarizer.Empty());
2222
}
2323

2424
TEST(SummarizerTests, DefaultConstructionUsesZeroStartTime) {
@@ -60,7 +60,7 @@ TEST(SummarizerTests, SummaryCounterUpdates) {
6060

6161
auto const num_events = 10;
6262
for (size_t i = 0; i < num_events; i++) {
63-
summarizer.update(event);
63+
summarizer.Update(event);
6464
}
6565

6666
auto const& features = summarizer.features();
@@ -71,8 +71,8 @@ TEST(SummarizerTests, SummaryCounterUpdates) {
7171
Summarizer::VariationKey(feature_version, feature_variation));
7272
ASSERT_TRUE(counter != cat_food->second.counters.end());
7373

74-
ASSERT_EQ(counter->second.value.as_double(), feature_value.as_double());
75-
ASSERT_EQ(counter->second.count, num_events);
74+
ASSERT_EQ(counter->second.value().as_double(), feature_value.as_double());
75+
ASSERT_EQ(counter->second.count(), num_events);
7676
}
7777

7878
TEST(SummarizerTests, JsonSerialization) {
@@ -103,7 +103,7 @@ TEST(SummarizerTests, JsonSerialization) {
103103

104104
auto const num_events = 10;
105105
for (size_t i = 0; i < num_events; i++) {
106-
summarizer.update(event);
106+
summarizer.Update(event);
107107
}
108108

109109
auto json = boost::json::value_from(Summary{summarizer, ZeroTime()});

0 commit comments

Comments
 (0)