1/ Change all "name" to id in statsD.
2/ Handle Subscription for alert.
3/ Support no_report_metric

Bug: 69522276
Test: all statsd unit tests passed.
Change-Id: I851b235f2d149b8602b0cad632d5bf541962f40a
diff --git a/cmds/statsd/src/config/ConfigKey.cpp b/cmds/statsd/src/config/ConfigKey.cpp
index a365dc0..d791f86 100644
--- a/cmds/statsd/src/config/ConfigKey.cpp
+++ b/cmds/statsd/src/config/ConfigKey.cpp
@@ -27,10 +27,10 @@
 ConfigKey::ConfigKey() {
 }
 
-ConfigKey::ConfigKey(const ConfigKey& that) : mName(that.mName), mUid(that.mUid) {
+ConfigKey::ConfigKey(const ConfigKey& that) : mId(that.mId), mUid(that.mUid) {
 }
 
-ConfigKey::ConfigKey(int uid, const string& name) : mName(name), mUid(uid) {
+ConfigKey::ConfigKey(int uid, const int64_t& id) : mId(id), mUid(uid) {
 }
 
 ConfigKey::~ConfigKey() {
@@ -38,10 +38,21 @@
 
 string ConfigKey::ToString() const {
     ostringstream out;
-    out << '(' << mUid << ',' << mName << ')';
+    out << '(' << mUid << ',' << mId << ')';
     return out.str();
 }
 
+
+int64_t StrToInt64(const string& str) {
+    char* endp;
+    int64_t value;
+    value = strtoll(str.c_str(), &endp, 0);
+    if (endp == str.c_str() || *endp != '\0') {
+        value = 0;
+    }
+    return value;
+}
+
 }  // namespace statsd
 }  // namespace os
 }  // namespace android
diff --git a/cmds/statsd/src/config/ConfigKey.h b/cmds/statsd/src/config/ConfigKey.h
index 3489c43..3ad0eed 100644
--- a/cmds/statsd/src/config/ConfigKey.h
+++ b/cmds/statsd/src/config/ConfigKey.h
@@ -37,14 +37,14 @@
 public:
     ConfigKey();
     explicit ConfigKey(const ConfigKey& that);
-    ConfigKey(int uid, const string& name);
+    ConfigKey(int uid, const int64_t& id);
     ~ConfigKey();
 
     inline int GetUid() const {
         return mUid;
     }
-    inline const string& GetName() const {
-        return mName;
+    inline const int64_t& GetId() const {
+        return mId;
     }
 
     inline bool operator<(const ConfigKey& that) const {
@@ -54,17 +54,17 @@
         if (mUid > that.mUid) {
             return false;
         }
-        return mName < that.mName;
+        return mId < that.mId;
     };
 
     inline bool operator==(const ConfigKey& that) const {
-        return mUid == that.mUid && mName == that.mName;
+        return mUid == that.mUid && mId == that.mId;
     };
 
     string ToString() const;
 
 private:
-    string mName;
+    int64_t mId;
     int mUid;
 };
 
@@ -72,6 +72,8 @@
     return os << config.ToString();
 }
 
+int64_t StrToInt64(const string& str);
+
 }  // namespace statsd
 }  // namespace os
 }  // namespace android
@@ -87,7 +89,7 @@
 template <>
 struct hash<ConfigKey> {
     std::size_t operator()(const ConfigKey& key) const {
-        return (7 * key.GetUid()) ^ ((hash<string>()(key.GetName())));
+        return (7 * key.GetUid()) ^ ((hash<long long>()(key.GetId())));
     }
 };
 
diff --git a/cmds/statsd/src/config/ConfigManager.cpp b/cmds/statsd/src/config/ConfigManager.cpp
index addc111..5cf8b9b 100644
--- a/cmds/statsd/src/config/ConfigManager.cpp
+++ b/cmds/statsd/src/config/ConfigManager.cpp
@@ -103,7 +103,7 @@
 }
 
 void ConfigManager::remove_saved_configs(const ConfigKey& key) {
-    string prefix = StringPrintf("%d-%s", key.GetUid(), key.GetName().c_str());
+    string prefix = StringPrintf("%d-%lld", key.GetUid(), (long long)key.GetId());
     StorageManager::deletePrefixedFiles(STATS_SERVICE_DIR, prefix.c_str());
 }
 
@@ -173,7 +173,7 @@
     fprintf(out, "CONFIGURATIONS (%d)\n", (int)mConfigs.size());
     fprintf(out, "     uid name\n");
     for (const auto& key : mConfigs) {
-        fprintf(out, "  %6d %s\n", key.GetUid(), key.GetName().c_str());
+        fprintf(out, "  %6d %lld\n", key.GetUid(), (long long)key.GetId());
         auto receiverIt = mConfigReceivers.find(key);
         if (receiverIt != mConfigReceivers.end()) {
             fprintf(out, "    -> received by %s, %s\n", receiverIt->second.first.c_str(),
@@ -189,8 +189,8 @@
     remove_saved_configs(key);
 
     // Then we save the latest config.
-    string file_name = StringPrintf("%s/%d-%s-%ld", STATS_SERVICE_DIR, key.GetUid(),
-                                    key.GetName().c_str(), time(nullptr));
+    string file_name = StringPrintf("%s/%d-%lld-%ld", STATS_SERVICE_DIR, key.GetUid(),
+                                    (long long)key.GetId(), time(nullptr));
     const int numBytes = config.ByteSize();
     vector<uint8_t> buffer(numBytes);
     config.SerializeToArray(&buffer[0], numBytes);
@@ -200,7 +200,7 @@
 StatsdConfig build_fake_config() {
     // HACK: Hard code a test metric for counting screen on events...
     StatsdConfig config;
-    config.set_name("CONFIG_12345");
+    config.set_id(12345);
 
     int WAKE_LOCK_TAG_ID = 1111;  // put a fake id here to make testing easier.
     int WAKE_LOCK_UID_KEY_ID = 1;
@@ -232,14 +232,14 @@
 
     // Count Screen ON events.
     CountMetric* metric = config.add_count_metric();
-    metric->set_name("METRIC_1");
-    metric->set_what("SCREEN_TURNED_ON");
+    metric->set_id(1);  // METRIC_1
+    metric->set_what(102);  //  "SCREEN_TURNED_ON"
     metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
 
     // Anomaly threshold for screen-on count.
     // TODO(b/70627390): Uncomment once the bug is fixed.
     /*Alert* alert = config.add_alert();
-    alert->set_name("ALERT_1");
+    alert->set_id("ALERT_1");
     alert->set_metric_name("METRIC_1");
     alert->set_number_of_buckets(6);
     alert->set_trigger_if_sum_gt(10);
@@ -256,8 +256,8 @@
 
     // Count process state changes, slice by uid.
     metric = config.add_count_metric();
-    metric->set_name("METRIC_2");
-    metric->set_what("PROCESS_STATE_CHANGE");
+    metric->set_id(2);  // "METRIC_2"
+    metric->set_what(104);
     metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
     FieldMatcher* dimensions = metric->mutable_dimensions();
     dimensions->set_field(UID_PROCESS_STATE_TAG_ID);
@@ -267,7 +267,7 @@
     // TODO(b/70627390): Uncomment once the bug is fixed.
     /*
     alert = config.add_alert();
-    alert->set_name("ALERT_2");
+    alert->set_id("ALERT_2");
     alert->set_metric_name("METRIC_2");
     alert->set_number_of_buckets(4);
     alert->set_trigger_if_sum_gt(30);
@@ -278,28 +278,28 @@
 
     // Count process state changes, slice by uid, while SCREEN_IS_OFF
     metric = config.add_count_metric();
-    metric->set_name("METRIC_3");
-    metric->set_what("PROCESS_STATE_CHANGE");
+    metric->set_id(3);
+    metric->set_what(104);
     metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
 
     dimensions = metric->mutable_dimensions();
     dimensions->set_field(UID_PROCESS_STATE_TAG_ID);
     dimensions->add_child()->set_field(UID_PROCESS_STATE_UID_KEY);
-    metric->set_condition("SCREEN_IS_OFF");
+    metric->set_condition(202);
 
     // Count wake lock, slice by uid, while SCREEN_IS_ON and app in background
     metric = config.add_count_metric();
-    metric->set_name("METRIC_4");
-    metric->set_what("APP_GET_WL");
+    metric->set_id(4);
+    metric->set_what(107);
     metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
     dimensions = metric->mutable_dimensions();
     dimensions->set_field(WAKE_LOCK_TAG_ID);
     dimensions->add_child()->set_field(WAKE_LOCK_UID_KEY_ID);
 
 
-    metric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
+    metric->set_condition(204);
     MetricConditionLink* link = metric->add_links();
-    link->set_condition("APP_IS_BACKGROUND");
+    link->set_condition(203);
     link->mutable_dimensions_in_what()->set_field(WAKE_LOCK_TAG_ID);
     link->mutable_dimensions_in_what()->add_child()->set_field(WAKE_LOCK_UID_KEY_ID);
     link->mutable_dimensions_in_condition()->set_field(APP_USAGE_TAG_ID);
@@ -307,16 +307,16 @@
 
     // Duration of an app holding any wl, while screen on and app in background, slice by uid
     DurationMetric* durationMetric = config.add_duration_metric();
-    durationMetric->set_name("METRIC_5");
+    durationMetric->set_id(5);
     durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
     durationMetric->set_aggregation_type(DurationMetric_AggregationType_SUM);
     dimensions = durationMetric->mutable_dimensions();
     dimensions->set_field(WAKE_LOCK_TAG_ID);
     dimensions->add_child()->set_field(WAKE_LOCK_UID_KEY_ID);
-    durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
-    durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
+    durationMetric->set_what(205);
+    durationMetric->set_condition(204);
     link = durationMetric->add_links();
-    link->set_condition("APP_IS_BACKGROUND");
+    link->set_condition(203);
     link->mutable_dimensions_in_what()->set_field(WAKE_LOCK_TAG_ID);
     link->mutable_dimensions_in_what()->add_child()->set_field(WAKE_LOCK_UID_KEY_ID);
     link->mutable_dimensions_in_condition()->set_field(APP_USAGE_TAG_ID);
@@ -324,16 +324,16 @@
 
     // max Duration of an app holding any wl, while screen on and app in background, slice by uid
     durationMetric = config.add_duration_metric();
-    durationMetric->set_name("METRIC_6");
+    durationMetric->set_id(6);
     durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
     durationMetric->set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE);
     dimensions = durationMetric->mutable_dimensions();
     dimensions->set_field(WAKE_LOCK_TAG_ID);
     dimensions->add_child()->set_field(WAKE_LOCK_UID_KEY_ID);
-    durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
-    durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
+    durationMetric->set_what(205);
+    durationMetric->set_condition(204);
     link = durationMetric->add_links();
-    link->set_condition("APP_IS_BACKGROUND");
+    link->set_condition(203);
     link->mutable_dimensions_in_what()->set_field(WAKE_LOCK_TAG_ID);
     link->mutable_dimensions_in_what()->add_child()->set_field(WAKE_LOCK_UID_KEY_ID);
     link->mutable_dimensions_in_condition()->set_field(APP_USAGE_TAG_ID);
@@ -341,13 +341,13 @@
 
     // Duration of an app holding any wl, while screen on and app in background
     durationMetric = config.add_duration_metric();
-    durationMetric->set_name("METRIC_7");
+    durationMetric->set_id(7);
     durationMetric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
     durationMetric->set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE);
-    durationMetric->set_what("WL_HELD_PER_APP_PER_NAME");
-    durationMetric->set_condition("APP_IS_BACKGROUND_AND_SCREEN_ON");
+    durationMetric->set_what(205);
+    durationMetric->set_condition(204);
     link = durationMetric->add_links();
-    link->set_condition("APP_IS_BACKGROUND");
+    link->set_condition(203);
     link->mutable_dimensions_in_what()->set_field(WAKE_LOCK_TAG_ID);
     link->mutable_dimensions_in_what()->add_child()->set_field(WAKE_LOCK_UID_KEY_ID);
     link->mutable_dimensions_in_condition()->set_field(APP_USAGE_TAG_ID);
@@ -356,17 +356,17 @@
 
     // Duration of screen on time.
     durationMetric = config.add_duration_metric();
-    durationMetric->set_name("METRIC_8");
+    durationMetric->set_id(8);
     durationMetric->mutable_bucket()->set_bucket_size_millis(10 * 1000L);
     durationMetric->set_aggregation_type(DurationMetric_AggregationType_SUM);
-    durationMetric->set_what("SCREEN_IS_ON");
+    durationMetric->set_what(201);
 
     // Anomaly threshold for background count.
     // TODO(b/70627390): Uncomment once the bug is fixed.
     /*
     alert = config.add_alert();
-    alert->set_name("ALERT_8");
-    alert->set_metric_name("METRIC_8");
+    alert->set_id(308);
+    alert->set_metric_id(8);
     alert->set_number_of_buckets(4);
     alert->set_trigger_if_sum_gt(2000000000); // 2 seconds
     alert->set_refractory_period_secs(120);
@@ -375,10 +375,10 @@
 
     // Value metric to count KERNEL_WAKELOCK when screen turned on
     ValueMetric* valueMetric = config.add_value_metric();
-    valueMetric->set_name("METRIC_6");
-    valueMetric->set_what("KERNEL_WAKELOCK");
+    valueMetric->set_id(11);
+    valueMetric->set_what(109);
     valueMetric->set_value_field(KERNEL_WAKELOCK_COUNT_KEY);
-    valueMetric->set_condition("SCREEN_IS_ON");
+    valueMetric->set_condition(201);
     dimensions = valueMetric->mutable_dimensions();
     dimensions->set_field(KERNEL_WAKELOCK_TAG_ID);
     dimensions->add_child()->set_field(KERNEL_WAKELOCK_NAME_KEY);
@@ -387,13 +387,13 @@
 
     // Add an EventMetric to log process state change events.
     EventMetric* eventMetric = config.add_event_metric();
-    eventMetric->set_name("METRIC_9");
-    eventMetric->set_what("SCREEN_TURNED_ON");
+    eventMetric->set_id(9);
+    eventMetric->set_what(102); // "SCREEN_TURNED_ON"
 
     // Add an GaugeMetric.
     GaugeMetric* gaugeMetric = config.add_gauge_metric();
-    gaugeMetric->set_name("METRIC_10");
-    gaugeMetric->set_what("DEVICE_TEMPERATURE");
+    gaugeMetric->set_id(10);
+    gaugeMetric->set_what(101);
     auto gaugeFieldMatcher = gaugeMetric->mutable_gauge_fields_filter()->mutable_fields();
     gaugeFieldMatcher->set_field(DEVICE_TEMPERATURE_TAG_ID);
     gaugeFieldMatcher->add_child()->set_field(DEVICE_TEMPERATURE_KEY);
@@ -401,12 +401,12 @@
 
     // Event matchers.
     AtomMatcher* temperatureAtomMatcher = config.add_atom_matcher();
-    temperatureAtomMatcher->set_name("DEVICE_TEMPERATURE");
+    temperatureAtomMatcher->set_id(101);  // "DEVICE_TEMPERATURE"
     temperatureAtomMatcher->mutable_simple_atom_matcher()->set_atom_id(
         DEVICE_TEMPERATURE_TAG_ID);
 
     AtomMatcher* eventMatcher = config.add_atom_matcher();
-    eventMatcher->set_name("SCREEN_TURNED_ON");
+    eventMatcher->set_id(102);  // "SCREEN_TURNED_ON"
     SimpleAtomMatcher* simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
     simpleAtomMatcher->set_atom_id(SCREEN_EVENT_TAG_ID);
     FieldValueMatcher* fieldValueMatcher = simpleAtomMatcher->add_field_value_matcher();
@@ -414,7 +414,7 @@
     fieldValueMatcher->set_eq_int(SCREEN_EVENT_ON_VALUE);
 
     eventMatcher = config.add_atom_matcher();
-    eventMatcher->set_name("SCREEN_TURNED_OFF");
+    eventMatcher->set_id(103);  // "SCREEN_TURNED_OFF"
     simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
     simpleAtomMatcher->set_atom_id(SCREEN_EVENT_TAG_ID);
     fieldValueMatcher = simpleAtomMatcher->add_field_value_matcher();
@@ -422,12 +422,12 @@
     fieldValueMatcher->set_eq_int(SCREEN_EVENT_OFF_VALUE);
 
     eventMatcher = config.add_atom_matcher();
-    eventMatcher->set_name("PROCESS_STATE_CHANGE");
+    eventMatcher->set_id(104);  // "PROCESS_STATE_CHANGE"
     simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
     simpleAtomMatcher->set_atom_id(UID_PROCESS_STATE_TAG_ID);
 
     eventMatcher = config.add_atom_matcher();
-    eventMatcher->set_name("APP_GOES_BACKGROUND");
+    eventMatcher->set_id(105);  // "APP_GOES_BACKGROUND"
     simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
     simpleAtomMatcher->set_atom_id(APP_USAGE_TAG_ID);
     fieldValueMatcher = simpleAtomMatcher->add_field_value_matcher();
@@ -435,7 +435,7 @@
     fieldValueMatcher->set_eq_int(APP_USAGE_BACKGROUND);
 
     eventMatcher = config.add_atom_matcher();
-    eventMatcher->set_name("APP_GOES_FOREGROUND");
+    eventMatcher->set_id(106);  // "APP_GOES_FOREGROUND"
     simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
     simpleAtomMatcher->set_atom_id(APP_USAGE_TAG_ID);
     fieldValueMatcher = simpleAtomMatcher->add_field_value_matcher();
@@ -443,7 +443,7 @@
     fieldValueMatcher->set_eq_int(APP_USAGE_FOREGROUND);
 
     eventMatcher = config.add_atom_matcher();
-    eventMatcher->set_name("APP_GET_WL");
+    eventMatcher->set_id(107);  // "APP_GET_WL"
     simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
     simpleAtomMatcher->set_atom_id(WAKE_LOCK_TAG_ID);
     fieldValueMatcher = simpleAtomMatcher->add_field_value_matcher();
@@ -451,7 +451,7 @@
     fieldValueMatcher->set_eq_int(WAKE_LOCK_ACQUIRE_VALUE);
 
     eventMatcher = config.add_atom_matcher();
-    eventMatcher->set_name("APP_RELEASE_WL");
+    eventMatcher->set_id(108);  //"APP_RELEASE_WL"
     simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
     simpleAtomMatcher->set_atom_id(WAKE_LOCK_TAG_ID);
     fieldValueMatcher = simpleAtomMatcher->add_field_value_matcher();
@@ -460,47 +460,47 @@
 
     // pulled events
     eventMatcher = config.add_atom_matcher();
-    eventMatcher->set_name("KERNEL_WAKELOCK");
+    eventMatcher->set_id(109);  // "KERNEL_WAKELOCK"
     simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
     simpleAtomMatcher->set_atom_id(KERNEL_WAKELOCK_TAG_ID);
 
     // Predicates.............
     Predicate* predicate = config.add_predicate();
-    predicate->set_name("SCREEN_IS_ON");
+    predicate->set_id(201);  // "SCREEN_IS_ON"
     SimplePredicate* simplePredicate = predicate->mutable_simple_predicate();
-    simplePredicate->set_start("SCREEN_TURNED_ON");
-    simplePredicate->set_stop("SCREEN_TURNED_OFF");
+    simplePredicate->set_start(102);  // "SCREEN_TURNED_ON"
+    simplePredicate->set_stop(103);
     simplePredicate->set_count_nesting(false);
 
     predicate = config.add_predicate();
-    predicate->set_name("SCREEN_IS_OFF");
+    predicate->set_id(202);  // "SCREEN_IS_OFF"
     simplePredicate = predicate->mutable_simple_predicate();
-    simplePredicate->set_start("SCREEN_TURNED_OFF");
-    simplePredicate->set_stop("SCREEN_TURNED_ON");
+    simplePredicate->set_start(103);
+    simplePredicate->set_stop(102);  // "SCREEN_TURNED_ON"
     simplePredicate->set_count_nesting(false);
 
     predicate = config.add_predicate();
-    predicate->set_name("APP_IS_BACKGROUND");
+    predicate->set_id(203);  // "APP_IS_BACKGROUND"
     simplePredicate = predicate->mutable_simple_predicate();
-    simplePredicate->set_start("APP_GOES_BACKGROUND");
-    simplePredicate->set_stop("APP_GOES_FOREGROUND");
+    simplePredicate->set_start(105);
+    simplePredicate->set_stop(106);
     FieldMatcher* predicate_dimension1 = simplePredicate->mutable_dimensions();
     predicate_dimension1->set_field(APP_USAGE_TAG_ID);
     predicate_dimension1->add_child()->set_field(APP_USAGE_UID_KEY_ID);
     simplePredicate->set_count_nesting(false);
 
     predicate = config.add_predicate();
-    predicate->set_name("APP_IS_BACKGROUND_AND_SCREEN_ON");
+    predicate->set_id(204);  // "APP_IS_BACKGROUND_AND_SCREEN_ON"
     Predicate_Combination* combination_predicate = predicate->mutable_combination();
     combination_predicate->set_operation(LogicalOperation::AND);
-    combination_predicate->add_predicate("APP_IS_BACKGROUND");
-    combination_predicate->add_predicate("SCREEN_IS_ON");
+    combination_predicate->add_predicate(203);
+    combination_predicate->add_predicate(201);
 
     predicate = config.add_predicate();
-    predicate->set_name("WL_HELD_PER_APP_PER_NAME");
+    predicate->set_id(205);  // "WL_HELD_PER_APP_PER_NAME"
     simplePredicate = predicate->mutable_simple_predicate();
-    simplePredicate->set_start("APP_GET_WL");
-    simplePredicate->set_stop("APP_RELEASE_WL");
+    simplePredicate->set_start(107);
+    simplePredicate->set_stop(108);
     FieldMatcher* predicate_dimension = simplePredicate->mutable_dimensions();
     predicate_dimension1->set_field(WAKE_LOCK_TAG_ID);
     predicate_dimension->add_child()->set_field(WAKE_LOCK_UID_KEY_ID);
@@ -508,10 +508,10 @@
     simplePredicate->set_count_nesting(true);
 
     predicate = config.add_predicate();
-    predicate->set_name("WL_HELD_PER_APP");
+    predicate->set_id(206);  // "WL_HELD_PER_APP"
     simplePredicate = predicate->mutable_simple_predicate();
-    simplePredicate->set_start("APP_GET_WL");
-    simplePredicate->set_stop("APP_RELEASE_WL");
+    simplePredicate->set_start(107);
+    simplePredicate->set_stop(108);
     simplePredicate->set_initial_value(SimplePredicate_InitialValue_FALSE);
     predicate_dimension = simplePredicate->mutable_dimensions();
     predicate_dimension->set_field(WAKE_LOCK_TAG_ID);