• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 android.tools.device.traces.io
18 
19 import android.tools.common.Logger
20 import android.tools.common.Scenario
21 import android.tools.common.ScenarioBuilder
22 import android.tools.common.Tag
23 import android.tools.common.Timestamp
24 import android.tools.common.Timestamps
25 import android.tools.common.io.FLICKER_IO_TAG
26 import android.tools.common.io.ResultArtifactDescriptor
27 import android.tools.common.io.RunStatus
28 import android.tools.common.io.TraceType
29 import android.tools.common.io.TransitionTimeRange
30 import java.io.File
31 
32 /** Helper class to create run result artifact files */
33 open class ResultWriter {
34     protected var scenario: Scenario = ScenarioBuilder().createEmptyScenario()
35     private var runStatus: RunStatus = RunStatus.UNDEFINED
36     private val files = mutableMapOf<ResultArtifactDescriptor, File>()
37     private var transitionStartTime = Timestamps.min()
38     private var transitionEndTime = Timestamps.max()
39     private var executionError: Throwable? = null
40     private var outputDir: File? = null
41 
42     /** Sets the artifact scenario to [_scenario] */
<lambda>null43     fun forScenario(_scenario: Scenario) = apply { scenario = _scenario }
44 
45     /** Sets the artifact transition start time to [time] */
<lambda>null46     fun setTransitionStartTime(time: Timestamp) = apply { transitionStartTime = time }
47 
48     /** Sets the artifact transition end time to [time] */
<lambda>null49     fun setTransitionEndTime(time: Timestamp) = apply { transitionEndTime = time }
50 
51     /** Sets the artifact status as successfully executed transition ([RunStatus.RUN_EXECUTED]) */
<lambda>null52     fun setRunComplete() = apply { runStatus = RunStatus.RUN_EXECUTED }
53 
54     /** Sets the dir where the artifact file will be stored to [dir] */
<lambda>null55     fun withOutputDir(dir: File) = apply { outputDir = dir }
56 
57     /**
58      * Sets the artifact status as failed executed transition ([RunStatus.RUN_FAILED])
59      *
60      * @param error that caused the transition to fail
61      */
<lambda>null62     fun setRunFailed(error: Throwable) = apply {
63         runStatus = RunStatus.RUN_FAILED
64         executionError = error
65     }
66 
67     /**
68      * Adds [artifact] to the result artifact
69      *
70      * @param traceType used when adding [artifact] to the result artifact
71      * @param tag used when adding [artifact] to the result artifact
72      */
<lambda>null73     fun addTraceResult(traceType: TraceType, artifact: File, tag: String = Tag.ALL) = apply {
74         Logger.d(
75             FLICKER_IO_TAG,
76             "Add trace result file=$artifact type=$traceType tag=$tag scenario=$scenario"
77         )
78         val fileDescriptor = ResultArtifactDescriptor(traceType, tag)
79         files[fileDescriptor] = artifact
80     }
81 
82     /** @return writes the result artifact to disk and returns it */
writenull83     open fun write(): IResultData {
84         return Logger.withTracing("write") {
85             val outputDir = outputDir
86             requireNotNull(outputDir) { "Output dir not configured" }
87             require(!scenario.isEmpty) { "Scenario shouldn't be empty" }
88 
89             if (runStatus == RunStatus.UNDEFINED) {
90                 Logger.w(FLICKER_IO_TAG, "Writing result with $runStatus run status")
91             }
92 
93             val artifact =
94                 ArtifactBuilder()
95                     .withScenario(scenario)
96                     .withOutputDir(outputDir)
97                     .withStatus(runStatus)
98                     .withFiles(files)
99                     .build()
100             ResultData(
101                 artifact,
102                 TransitionTimeRange(transitionStartTime, transitionEndTime),
103                 executionError
104             )
105         }
106     }
107 }
108