1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define DEBUG false  // STOPSHIP if true
18 #include "Log.h"
19 
20 #include "external/Perfetto.h"
21 #include "subscriber/IncidentdReporter.h"
22 #include "subscriber/SubscriberReporter.h"
23 
24 namespace android {
25 namespace os {
26 namespace statsd {
27 
triggerSubscribers(int64_t ruleId,int64_t metricId,const MetricDimensionKey & dimensionKey,int64_t metricValue,const ConfigKey & configKey,const std::vector<Subscription> & subscriptions)28 void triggerSubscribers(int64_t ruleId, int64_t metricId, const MetricDimensionKey& dimensionKey,
29                         int64_t metricValue, const ConfigKey& configKey,
30                         const std::vector<Subscription>& subscriptions) {
31     VLOG("informSubscribers called.");
32     if (subscriptions.empty()) {
33         VLOG("No Subscriptions were associated.");
34         return;
35     }
36 
37     for (const Subscription& subscription : subscriptions) {
38         if (subscription.probability_of_informing() < 1
39                 && ((float)rand() / (float)RAND_MAX) >= subscription.probability_of_informing()) {
40             // Note that due to float imprecision, 0.0 and 1.0 might not truly mean never/always.
41             // The config writer was advised to use -0.1 and 1.1 for never/always.
42             ALOGI("Fate decided that a subscriber would not be informed.");
43             continue;
44         }
45         switch (subscription.subscriber_information_case()) {
46             case Subscription::SubscriberInformationCase::kIncidentdDetails:
47                 if (!GenerateIncidentReport(subscription.incidentd_details(), ruleId, metricId,
48                                             dimensionKey, metricValue, configKey)) {
49                     ALOGW("Failed to generate incident report.");
50                 }
51                 break;
52             case Subscription::SubscriberInformationCase::kPerfettoDetails:
53                 if (!CollectPerfettoTraceAndUploadToDropbox(subscription.perfetto_details(),
54                                                             subscription.id(), ruleId, configKey)) {
55                     ALOGW("Failed to generate perfetto traces.");
56                 }
57                 break;
58             case Subscription::SubscriberInformationCase::kBroadcastSubscriberDetails:
59                 SubscriberReporter::getInstance().alertBroadcastSubscriber(configKey, subscription,
60                                                                            dimensionKey);
61                 break;
62             default:
63                 break;
64         }
65     }
66 }
67 
68 }  // namespace statsd
69 }  // namespace os
70 }  // namespace android
71