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.server.wm.flicker.rules 18 19 import android.device.collectors.BaseCollectionListener 20 import android.util.Log 21 import com.android.helpers.ICollectorHelper 22 23 class FlickerResultsCollector : BaseCollectionListener<Boolean>() { 24 private val collectionHelper = CollectionHelper() 25 private var criticalUserJourneyName: String = UNDEFINED_CUJ 26 27 init { 28 createHelperInstance(collectionHelper) 29 } 30 setCriticalUserJourneyNamenull31 fun setCriticalUserJourneyName(className: String?) { 32 this.criticalUserJourneyName = className ?: UNDEFINED_CUJ 33 } 34 postRunResultsnull35 fun postRunResults(results: Map<String, Int>) { 36 collectionHelper.postMetrics(assertionsToMetrics(results)) 37 } 38 getMetricsnull39 fun getMetrics(): Map<String, Int> { 40 return collectionHelper.metrics 41 } 42 43 /** 44 * Convert the assertions generated by the Flicker Service to specific metric key pairs that 45 * contain enough information to later further and analyze in dashboards. 46 */ assertionsToMetricsnull47 private fun assertionsToMetrics(assertions: Map<String, Int>): Map<String, Int> { 48 val processedAssertions: MutableMap<String, Int> = mutableMapOf() 49 50 for ((assertionName, result) in assertions) { 51 // Add information about the CUJ we are running the assertions on 52 processedAssertions["$FASS_METRICS_PREFIX::$criticalUserJourneyName::$assertionName"] = 53 result 54 } 55 56 return processedAssertions 57 } 58 59 class CollectionHelper : ICollectorHelper<Int> { 60 private var metrics: MutableMap<String, Int> = mutableMapOf() 61 postMetricsnull62 fun postMetrics(results: Map<String, Int>) { 63 for ((key, res) in results) { 64 require(res == 1 || res == 0) 65 // If a failure is posted for key then we fail 66 metrics[key] = (metrics[key] ?: 1) and res 67 } 68 } 69 70 /** Do nothing. */ startCollectingnull71 override fun startCollecting(): Boolean { 72 Log.i(LOG_TAG, "startCollecting") 73 return true 74 } 75 76 /** Do nothing. */ stopCollectingnull77 override fun stopCollecting(): Boolean { 78 Log.i(LOG_TAG, "stopCollecting") 79 return true 80 } 81 82 /** Collect the assertions metrics for Flicker as a Service. */ getMetricsnull83 override fun getMetrics(): Map<String, Int> { 84 Log.i(LOG_TAG, "getMetrics") 85 return metrics 86 } 87 88 companion object { 89 private val LOG_TAG = this::class.java.simpleName 90 } 91 } 92 93 companion object { 94 // Unique prefix to add to all fass metrics to identify them 95 private const val FASS_METRICS_PREFIX = "FASS" 96 private const val UNDEFINED_CUJ = "UndefinedCUJ" 97 } 98 } 99