1 /*
2  * Copyright (C) 2020 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 #include "FieldValue.h"
18 #include "metadata_util.h"
19 
20 namespace android {
21 namespace os {
22 namespace statsd {
23 
24 using google::protobuf::RepeatedPtrField;
25 
writeValueToProto(metadata::FieldValue * metadataFieldValue,const Value & value)26 void writeValueToProto(metadata::FieldValue* metadataFieldValue, const Value& value) {
27     std::string storage_value;
28     switch (value.getType()) {
29         case INT:
30             metadataFieldValue->set_value_int(value.int_value);
31             break;
32         case LONG:
33             metadataFieldValue->set_value_long(value.long_value);
34             break;
35         case FLOAT:
36             metadataFieldValue->set_value_float(value.float_value);
37             break;
38         case DOUBLE:
39             metadataFieldValue->set_value_double(value.double_value);
40             break;
41         case STRING:
42             metadataFieldValue->set_value_str(value.str_value.c_str());
43             break;
44         case STORAGE: // byte array
45             storage_value = ((char*) value.storage_value.data());
46             metadataFieldValue->set_value_storage(storage_value);
47             break;
48         default:
49             break;
50     }
51 }
52 
writeMetricDimensionKeyToMetadataDimensionKey(const MetricDimensionKey & metricKey,metadata::MetricDimensionKey * metadataMetricKey)53 void writeMetricDimensionKeyToMetadataDimensionKey(
54         const MetricDimensionKey& metricKey,
55         metadata::MetricDimensionKey* metadataMetricKey) {
56     for (const FieldValue& fieldValue : metricKey.getDimensionKeyInWhat().getValues()) {
57         metadata::FieldValue* metadataFieldValue = metadataMetricKey->add_dimension_key_in_what();
58         metadata::Field* metadataField = metadataFieldValue->mutable_field();
59         metadataField->set_tag(fieldValue.mField.getTag());
60         metadataField->set_field(fieldValue.mField.getField());
61         writeValueToProto(metadataFieldValue, fieldValue.mValue);
62     }
63 
64     for (const FieldValue& fieldValue : metricKey.getStateValuesKey().getValues()) {
65         metadata::FieldValue* metadataFieldValue = metadataMetricKey->add_state_values_key();
66         metadata::Field* metadataField = metadataFieldValue->mutable_field();
67         metadataField->set_tag(fieldValue.mField.getTag());
68         metadataField->set_field(fieldValue.mField.getField());
69         writeValueToProto(metadataFieldValue, fieldValue.mValue);
70     }
71 }
72 
writeFieldValuesFromMetadata(const RepeatedPtrField<metadata::FieldValue> & repeatedFieldValueList,std::vector<FieldValue> * fieldValues)73 void writeFieldValuesFromMetadata(
74         const RepeatedPtrField<metadata::FieldValue>& repeatedFieldValueList,
75         std::vector<FieldValue>* fieldValues) {
76     for (const metadata::FieldValue& metadataFieldValue : repeatedFieldValueList) {
77         Field field(metadataFieldValue.field().tag(), metadataFieldValue.field().field());
78         Value value;
79         switch (metadataFieldValue.value_case()) {
80             case metadata::FieldValue::ValueCase::kValueInt:
81                 value = Value(metadataFieldValue.value_int());
82                 break;
83             case metadata::FieldValue::ValueCase::kValueLong:
84                 value = Value(metadataFieldValue.value_long());
85                 break;
86             case metadata::FieldValue::ValueCase::kValueFloat:
87                 value = Value(metadataFieldValue.value_float());
88                 break;
89             case metadata::FieldValue::ValueCase::kValueDouble:
90                 value = Value(metadataFieldValue.value_double());
91                 break;
92             case metadata::FieldValue::ValueCase::kValueStr:
93                 value = Value(metadataFieldValue.value_str());
94                 break;
95             case metadata::FieldValue::ValueCase::kValueStorage:
96                 value = Value(metadataFieldValue.value_storage());
97                 break;
98             default:
99                 break;
100         }
101         FieldValue fieldValue(field, value);
102         fieldValues->emplace_back(field, value);
103     }
104 }
105 
loadMetricDimensionKeyFromProto(const metadata::MetricDimensionKey & metricDimensionKey)106 MetricDimensionKey loadMetricDimensionKeyFromProto(
107         const metadata::MetricDimensionKey& metricDimensionKey) {
108     std::vector<FieldValue> dimKeyInWhatFieldValues;
109     writeFieldValuesFromMetadata(metricDimensionKey.dimension_key_in_what(),
110             &dimKeyInWhatFieldValues);
111     std::vector<FieldValue> stateValuesFieldValues;
112     writeFieldValuesFromMetadata(metricDimensionKey.state_values_key(), &stateValuesFieldValues);
113 
114     HashableDimensionKey dimKeyInWhat(dimKeyInWhatFieldValues);
115     HashableDimensionKey stateValues(stateValuesFieldValues);
116     MetricDimensionKey metricKey(dimKeyInWhat, stateValues);
117     return metricKey;
118 }
119 
120 }  // namespace statsd
121 }  // namespace os
122 }  // namespace android