blob: 10125d7519d4e6294a2c5065697c5c58099bd686 [file] [log] [blame]
Rui Qiua3418982021-08-20 11:21:12 -07001/*
2 * Copyright (C) 2021 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
17package com.android.car.telemetry;
18
19import android.util.Slog;
20
21import com.android.car.CarLog;
22import com.android.internal.annotations.VisibleForTesting;
23
24import java.io.File;
25import java.io.IOException;
26import java.nio.file.Files;
27import java.util.ArrayList;
28import java.util.List;
29
30/**
31 * This class is responsible for storing, retrieving, and deleting {@link
32 * TelemetryProto.MetricsConfig}. All of the methods are blocking so the class should only be
33 * accessed on the worker thread.
34 */
35class MetricsConfigStore {
36 @VisibleForTesting
37 static final String METRICS_CONFIG_DIR = "metrics_configs";
38
39 private final File mConfigDirectory;
40
41 MetricsConfigStore(File rootDirectory) {
42 mConfigDirectory = new File(rootDirectory, METRICS_CONFIG_DIR);
43 mConfigDirectory.mkdirs();
44 }
45
46 /**
47 * Returns all active {@link TelemetryProto.MetricsConfig} from disk.
48 */
49 List<TelemetryProto.MetricsConfig> getActiveMetricsConfigs() {
50 // TODO(b/197336485): Add expiration date check for MetricsConfig
51 List<TelemetryProto.MetricsConfig> activeConfigs = new ArrayList<>();
52 for (File file : mConfigDirectory.listFiles()) {
53 try {
54 byte[] serializedConfig = Files.readAllBytes(file.toPath());
55 activeConfigs.add(TelemetryProto.MetricsConfig.parseFrom(serializedConfig));
56 } catch (IOException e) {
57 // TODO(b/197336655): record failure
58 file.delete();
59 }
60 }
61 return activeConfigs;
62 }
63
64 /**
65 * Stores the MetricsConfig if it is valid.
66 *
67 * @param metricsConfig the config to be persisted to disk.
68 * @return true if the MetricsConfig should start receiving data, false otherwise.
69 */
70 boolean addMetricsConfig(TelemetryProto.MetricsConfig metricsConfig) {
71 // TODO(b/197336485): Check version and expiration date for MetricsConfig
72 try {
73 Files.write(
74 new File(mConfigDirectory, metricsConfig.getName()).toPath(),
75 metricsConfig.toByteArray());
76 } catch (IOException e) {
77 // TODO(b/197336655): record failure
78 Slog.w(CarLog.TAG_TELEMETRY, "Failed to write metrics config to disk", e);
79 }
80 return true;
81 }
82
83 /** Deletes the MetricsConfig from disk. Returns the success status. */
84 boolean deleteMetricsConfig(String metricsConfigName) {
85 return new File(mConfigDirectory, metricsConfigName).delete();
86 }
87}