Rui Qiu | a341898 | 2021-08-20 11:21:12 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package com.android.car.telemetry; |
| 18 | |
| 19 | import android.util.Slog; |
| 20 | |
| 21 | import com.android.car.CarLog; |
| 22 | import com.android.internal.annotations.VisibleForTesting; |
| 23 | |
| 24 | import java.io.File; |
| 25 | import java.io.IOException; |
| 26 | import java.nio.file.Files; |
| 27 | import java.util.ArrayList; |
| 28 | import 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 | */ |
| 35 | class 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 | } |