1/*
2 * Copyright (C) 2017 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 */
16package com.android.statsd.loadtest;
17
18import android.content.Context;
19import android.util.Log;
20import com.android.os.StatsLog.ConfigMetricsReport;
21import com.android.os.StatsLog.EventMetricData;
22import com.android.os.StatsLog.StatsLogReport;
23import com.android.internal.os.StatsdConfigProto.TimeUnit;
24import java.util.List;
25import java.util.regex.Matcher;
26import java.util.regex.Pattern;
27
28/**
29 * Checks the correctness of the stats.
30 */
31public class ValidationRecorder extends PerfDataRecorder {
32    private static final String TAG = "loadtest.ValidationRecorder";
33
34    private final LoadtestActivity mLoadtestActivity;
35
36    public ValidationRecorder(LoadtestActivity loadtestActivity, boolean placebo, int replication,
37        TimeUnit bucket, long periodSecs, int burst,  boolean includeCountMetric,
38        boolean includeDurationMetric, boolean includeEventMetric,  boolean includeValueMetric,
39        boolean includeGaugeMetric) {
40      super(placebo, replication, bucket, periodSecs, burst, includeCountMetric,
41          includeDurationMetric, includeEventMetric, includeValueMetric, includeGaugeMetric);
42        mLoadtestActivity = loadtestActivity;
43    }
44
45    @Override
46    public void startRecording(Context context) {
47        // Nothing to do.
48    }
49
50    @Override
51    public void onAlarm(Context context) {
52        validateData();
53    }
54
55    @Override
56    public void stopRecording(Context context) {
57        validateData();
58    }
59
60    private void validateData() {
61        // The code below is commented out because it calls getData, which has the side-effect
62        // of clearing statsd's data buffer.
63        /*
64        List<ConfigMetricsReport> reports = mLoadtestActivity.getData();
65        if (reports != null) {
66            Log.d(TAG, "GOT DATA");
67            for (ConfigMetricsReport report : reports) {
68                for (StatsLogReport logReport : report.getMetricsList()) {
69                    if (!logReport.hasMetricId()) {
70                        Log.e(TAG, "Metric missing name.");
71                    }
72                }
73            }
74        }
75        */
76    }
77
78    private void validateEventBatteryLevelChanges(StatsLogReport logReport) {
79        Log.d(TAG, "Validating " + logReport.getMetricId());
80        if (logReport.hasEventMetrics()) {
81            Log.d(TAG, "Num events captured: " + logReport.getEventMetrics().getDataCount());
82            for (EventMetricData data : logReport.getEventMetrics().getDataList()) {
83                Log.d(TAG, "  Event : " + data.getAtom());
84            }
85        } else {
86            Log.d(TAG, "Metric is invalid");
87        }
88    }
89
90    private void validateEventBatteryLevelChangesWhileScreenIsOn(StatsLogReport logReport) {
91        Log.d(TAG, "Validating " + logReport.getMetricId());
92    }
93}
94