Add State to statsd config language

Introduce State, StateMapping, StateGroup, and MetricStateLink into the
statsd config language and add a slice_by_state field in CountMetrics
for singular and aggregate state slicing.

Bug: 136566566
Test: bit statsd_test:*
Change-Id: Icbb50f25d1c9f5bec0a2f07e6b977820ade289e6
diff --git a/bin/src/stats_log.proto b/bin/src/stats_log.proto
index d9c04f2..e45e24f 100644
--- a/bin/src/stats_log.proto
+++ b/bin/src/stats_log.proto
@@ -41,6 +41,15 @@
   repeated DimensionsValue dimensions_value = 1;
 }
 
+message StateValue {
+  optional int32 atom_id = 1;
+
+  oneof contents {
+    int64 group_id = 2;
+    int32 value = 3;
+  }
+}
+
 message EventMetricData {
   optional int64 elapsed_timestamp_nanos = 1;
 
@@ -66,12 +75,14 @@
 message CountMetricData {
   optional DimensionsValue dimensions_in_what = 1;
 
-  optional DimensionsValue dimensions_in_condition = 2 [deprecated = true];
+  repeated StateValue slice_by_state = 6;
 
   repeated CountBucketInfo bucket_info = 3;
 
   repeated DimensionsValue dimension_leaf_values_in_what = 4;
 
+  optional DimensionsValue dimensions_in_condition = 2 [deprecated = true];
+
   repeated DimensionsValue dimension_leaf_values_in_condition = 5 [deprecated = true];
 }
 
diff --git a/bin/src/statsd_config.proto b/bin/src/statsd_config.proto
index 1b7f398..c107397 100644
--- a/bin/src/statsd_config.proto
+++ b/bin/src/statsd_config.proto
@@ -150,6 +150,24 @@
   }
 }
 
+message StateMap {
+  message StateGroup {
+    optional int64 group_id = 1;
+
+    repeated int32 value = 2;
+  }
+
+  repeated StateGroup group = 1;
+}
+
+message State {
+  optional int64 id = 1;
+
+  optional int32 atom_id = 2;
+
+  optional StateMap map = 3;
+}
+
 message MetricConditionLink {
   optional int64 condition = 1;
 
@@ -158,6 +176,14 @@
   optional FieldMatcher fields_in_condition = 3;
 }
 
+message MetricStateLink {
+  optional int64 state = 1;
+
+  optional FieldMatcher fields_in_what = 2;
+
+  optional FieldMatcher fields_in_state = 3;
+}
+
 message FieldFilter {
   optional bool include_all = 1 [default = false];
   optional FieldMatcher fields = 2;
@@ -182,11 +208,15 @@
 
   optional FieldMatcher dimensions_in_what = 4;
 
-  optional FieldMatcher dimensions_in_condition = 7 [deprecated = true];
+  repeated int64 slice_by_state = 8;
 
   optional TimeUnit bucket = 5;
 
   repeated MetricConditionLink links = 6;
+
+  repeated MetricStateLink state_link = 9;
+
+  optional FieldMatcher dimensions_in_condition = 7 [deprecated = true];
 }
 
 message DurationMetric {
@@ -438,6 +468,8 @@
 
   optional bool persist_locally = 20 [default = false];
 
+  repeated State state = 21;
+
   // Field number 1000 is reserved for later use.
   reserved 1000;
 }