Home | History | Annotate | Download | only in entity
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you
      5  * may not use this file except in compliance with the License. You may
      6  * 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
     13  * implied. See the License for the specific language governing
     14  * permissions and limitations under the License.
     15  */
     16 
     17 package com.android.vts.entity;
     18 
     19 import com.android.vts.proto.VtsReportMessage.VtsProfilingRegressionMode;
     20 import com.android.vts.proto.VtsReportMessage.VtsProfilingType;
     21 import com.google.appengine.api.datastore.Entity;
     22 import com.google.appengine.api.datastore.Key;
     23 import com.google.appengine.api.datastore.KeyFactory;
     24 import java.util.logging.Level;
     25 import java.util.logging.Logger;
     26 
     27 /** Entity describing a profiling point. */
     28 public class ProfilingPointEntity implements DashboardEntity {
     29     protected static final Logger logger = Logger.getLogger(ProfilingPointEntity.class.getName());
     30     protected static final String DELIMITER = "#";
     31 
     32     public static final String KIND = "ProfilingPoint";
     33 
     34     // Property keys
     35     public static final String TEST_NAME = "testName";
     36     public static final String PROFILING_POINT_NAME = "profilingPointName";
     37     public static final String TYPE = "type";
     38     public static final String REGRESSION_MODE = "regressionMode";
     39     public static final String X_LABEL = "xLabel";
     40     public static final String Y_LABEL = "yLabel";
     41 
     42     public final Key key;
     43 
     44     public final String testName;
     45     public final String profilingPointName;
     46     public final VtsProfilingType type;
     47     public final VtsProfilingRegressionMode regressionMode;
     48     public final String xLabel;
     49     public final String yLabel;
     50 
     51     /**
     52      * Create a ProfilingPointEntity object.
     53      *
     54      * @param testName The name of test containing the profiling point.
     55      * @param profilingPointName The name of the profiling point.
     56      * @param type The (number) type of the profiling point data.
     57      * @param regressionMode The (number) mode to use for detecting regression.
     58      * @param xLabel The x axis label.
     59      * @param yLabel The y axis label.
     60      */
     61     public ProfilingPointEntity(
     62             String testName,
     63             String profilingPointName,
     64             int type,
     65             int regressionMode,
     66             String xLabel,
     67             String yLabel) {
     68         this.key = createKey(testName, profilingPointName);
     69         this.testName = testName;
     70         this.profilingPointName = profilingPointName;
     71         this.type = VtsProfilingType.valueOf(type);
     72         this.regressionMode = VtsProfilingRegressionMode.valueOf(regressionMode);
     73         this.xLabel = xLabel;
     74         this.yLabel = yLabel;
     75     }
     76 
     77     /**
     78      * Create a key for a ProfilingPointEntity.
     79      *
     80      * @param testName The name of test containing the profiling point.
     81      * @param profilingPointName The name of the profiling point.
     82      * @return a Key object for the ProfilingEntity in the database.
     83      */
     84     public static Key createKey(String testName, String profilingPointName) {
     85         return KeyFactory.createKey(KIND, testName + DELIMITER + profilingPointName);
     86     }
     87 
     88     @Override
     89     public Entity toEntity() {
     90         Entity profilingPoint = new Entity(key);
     91         profilingPoint.setIndexedProperty(TEST_NAME, this.testName);
     92         profilingPoint.setIndexedProperty(PROFILING_POINT_NAME, this.profilingPointName);
     93         profilingPoint.setUnindexedProperty(TYPE, this.type.getNumber());
     94         profilingPoint.setUnindexedProperty(REGRESSION_MODE, this.regressionMode.getNumber());
     95         profilingPoint.setUnindexedProperty(X_LABEL, this.xLabel);
     96         profilingPoint.setUnindexedProperty(Y_LABEL, this.yLabel);
     97 
     98         return profilingPoint;
     99     }
    100 
    101     /**
    102      * Convert an Entity object to a ProfilingPointEntity.
    103      *
    104      * @param e The entity to process.
    105      * @return ProfilingPointEntity object with the properties from e, or null if incompatible.
    106      */
    107     @SuppressWarnings("unchecked")
    108     public static ProfilingPointEntity fromEntity(Entity e) {
    109         if (!e.getKind().equals(KIND)
    110                 || e.getKey().getName() == null
    111                 || !e.hasProperty(TEST_NAME)
    112                 || !e.hasProperty(PROFILING_POINT_NAME)
    113                 || !e.hasProperty(TYPE)
    114                 || !e.hasProperty(REGRESSION_MODE)
    115                 || !e.hasProperty(X_LABEL)
    116                 || !e.hasProperty(Y_LABEL)) {
    117             logger.log(
    118                     Level.WARNING, "Missing profiling point attributes in entity: " + e.toString());
    119             return null;
    120         }
    121         try {
    122             String testName = (String) e.getProperty(TEST_NAME);
    123             String profilingPointName = (String) e.getProperty(PROFILING_POINT_NAME);
    124             int type = (int) (long) e.getProperty(TYPE);
    125             int regressionMode = (int) (long) e.getProperty(REGRESSION_MODE);
    126             String xLabel = (String) e.getProperty(X_LABEL);
    127             String yLabel = (String) e.getProperty(Y_LABEL);
    128 
    129             return new ProfilingPointEntity(
    130                     testName, profilingPointName, type, regressionMode, xLabel, yLabel);
    131         } catch (ClassCastException exception) {
    132             // Invalid cast
    133             logger.log(Level.WARNING, "Error parsing profiling point entity.", exception);
    134         }
    135         return null;
    136     }
    137 }
    138