1 /*
2  * Copyright (C) 2019 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 package com.android.tradefed.clearcut;
17 
18 import com.android.asuite.clearcut.Common;
19 import com.android.asuite.clearcut.Common.UserType;
20 import com.android.asuite.clearcut.ExternalUserLog.AtestLogEventExternal;
21 import com.android.asuite.clearcut.ExternalUserLog.AtestLogEventExternal.AtestExitEvent;
22 import com.android.asuite.clearcut.ExternalUserLog.AtestLogEventExternal.AtestStartEvent;
23 import com.android.asuite.clearcut.ExternalUserLog.AtestLogEventExternal.RunTestsFinishEvent;
24 import com.android.asuite.clearcut.ExternalUserLog.AtestLogEventExternal.RunnerFinishEvent;
25 import com.android.asuite.clearcut.InternalUserLog.AtestLogEventInternal;
26 
27 import com.google.protobuf.ByteString;
28 
29 import java.time.Duration;
30 
31 /** Utility to help populate the event protos */
32 public class ClearcutEventHelper {
33 
34     private static final String TOOL_NAME = "Tradefed";
35 
36     /**
37      * Create the start event for Tradefed.
38      *
39      * @param userKey The unique id representing the user
40      * @param runId The current id for the session.
41      * @param userType The type of the user: internal or external.
42      * @param subToolName The name of test suite tool.
43      * @return a ByteString representation of the even proto.
44      */
createStartEvent( String userKey, String runId, UserType userType, String subToolName)45     public static ByteString createStartEvent(
46             String userKey, String runId, UserType userType, String subToolName) {
47         if (UserType.GOOGLE.equals(userType)) {
48             AtestLogEventInternal.Builder builder =
49                     createBaseInternalEventBuilder(userKey, runId, userType, subToolName);
50             AtestLogEventInternal.AtestStartEvent.Builder startEventBuilder =
51                     AtestLogEventInternal.AtestStartEvent.newBuilder();
52             builder.setAtestStartEvent(startEventBuilder.build());
53             return builder.build().toByteString();
54         }
55 
56         AtestLogEventExternal.Builder builder =
57                 createBaseExternalEventBuilder(userKey, runId, userType, subToolName);
58         AtestStartEvent.Builder startBuilder = AtestStartEvent.newBuilder();
59         builder.setAtestStartEvent(startBuilder.build());
60         return builder.build().toByteString();
61     }
62 
63     /**
64      * Create the end event for Tradefed.
65      *
66      * @param userKey The unique id representing the user
67      * @param runId The current id for the session.
68      * @param userType The type of the user: internal or external.
69      * @param subToolName The name of test suite tool.
70      * @param sessionDuration The duration of the complete session.
71      * @return a ByteString representation of the even proto.
72      */
createFinishedEvent( String userKey, String runId, UserType userType, String subToolName, Duration sessionDuration)73     public static ByteString createFinishedEvent(
74             String userKey,
75             String runId,
76             UserType userType,
77             String subToolName,
78             Duration sessionDuration) {
79         if (UserType.GOOGLE.equals(userType)) {
80             AtestLogEventInternal.Builder builder =
81                     createBaseInternalEventBuilder(userKey, runId, userType, subToolName);
82             AtestLogEventInternal.AtestExitEvent.Builder exitEventBuilder =
83                     AtestLogEventInternal.AtestExitEvent.newBuilder();
84             Common.Duration duration =
85                     Common.Duration.newBuilder()
86                             .setSeconds(sessionDuration.getSeconds())
87                             .setNanos(sessionDuration.getNano())
88                             .build();
89             exitEventBuilder.setDuration(duration);
90             builder.setAtestExitEvent(exitEventBuilder.build());
91             return builder.build().toByteString();
92         }
93 
94         AtestLogEventExternal.Builder builder =
95                 createBaseExternalEventBuilder(userKey, runId, userType, subToolName);
96         AtestLogEventExternal.AtestExitEvent.Builder startBuilder = AtestExitEvent.newBuilder();
97         builder.setAtestExitEvent(startBuilder.build());
98         return builder.build().toByteString();
99     }
100 
101     /**
102      * Create the start invocation event for Tradefed.
103      *
104      * @param userKey The unique id representing the user
105      * @param runId The current id for the session.
106      * @param userType The type of the user: internal or external.
107      * @param subToolName The name of test suite tool.
108      * @return a ByteString representation of the even proto.
109      */
createRunStartEvent( String userKey, String runId, UserType userType, String subToolName)110     public static ByteString createRunStartEvent(
111             String userKey, String runId, UserType userType, String subToolName) {
112         if (UserType.GOOGLE.equals(userType)) {
113             AtestLogEventInternal.Builder builder =
114                     createBaseInternalEventBuilder(userKey, runId, userType, subToolName);
115             AtestLogEventInternal.RunnerFinishEvent.Builder startRunEventBuilder =
116                     AtestLogEventInternal.RunnerFinishEvent.newBuilder();
117             builder.setRunnerFinishEvent(startRunEventBuilder.build());
118             return builder.build().toByteString();
119         }
120 
121         AtestLogEventExternal.Builder builder =
122                 createBaseExternalEventBuilder(userKey, runId, userType, subToolName);
123         RunnerFinishEvent.Builder startBuilder = RunnerFinishEvent.newBuilder();
124         builder.setRunnerFinishEvent(startBuilder.build());
125         return builder.build().toByteString();
126     }
127 
128     /**
129      * Create the run test finished event for Tradefed.
130      *
131      * @param userKey The unique id representing the user
132      * @param runId The current id for the session.
133      * @param userType The type of the user: internal or external.
134      * @param subToolName The name of test suite tool.
135      * @param testDuration the duration of the test session.
136      * @return a ByteString representation of the even proto.
137      */
creatRunTestFinished( String userKey, String runId, UserType userType, String subToolName, Duration testDuration)138     public static ByteString creatRunTestFinished(
139             String userKey,
140             String runId,
141             UserType userType,
142             String subToolName,
143             Duration testDuration) {
144         if (UserType.GOOGLE.equals(userType)) {
145             AtestLogEventInternal.Builder builder =
146                     createBaseInternalEventBuilder(userKey, runId, userType, subToolName);
147             AtestLogEventInternal.RunTestsFinishEvent.Builder runTestsFinished =
148                     AtestLogEventInternal.RunTestsFinishEvent.newBuilder();
149             Common.Duration duration =
150                     Common.Duration.newBuilder()
151                             .setSeconds(testDuration.getSeconds())
152                             .setNanos(testDuration.getNano())
153                             .build();
154             runTestsFinished.setDuration(duration);
155             builder.setRunTestsFinishEvent(runTestsFinished.build());
156             return builder.build().toByteString();
157         }
158 
159         AtestLogEventExternal.Builder builder =
160                 createBaseExternalEventBuilder(userKey, runId, userType, subToolName);
161         RunTestsFinishEvent.Builder startBuilder = RunTestsFinishEvent.newBuilder();
162         builder.setRunTestsFinishEvent(startBuilder.build());
163         return builder.build().toByteString();
164     }
165 
166     /**
167      * Create the basic event builder with all the common informations.
168      *
169      * @param userKey The unique id representing the user
170      * @param runId The current id for the session.
171      * @param userType The type of the user: internal or external.
172      * @param subToolName The name of test suite tool.
173      * @return a builder for the event.
174      */
createBaseExternalEventBuilder( String userKey, String runId, UserType userType, String subToolName)175     private static AtestLogEventExternal.Builder createBaseExternalEventBuilder(
176             String userKey, String runId, UserType userType, String subToolName) {
177         AtestLogEventExternal.Builder builder = AtestLogEventExternal.newBuilder();
178         builder.setUserKey(userKey);
179         builder.setRunId(runId);
180         builder.setUserType(userType);
181         builder.setToolName(TOOL_NAME);
182         builder.setSubToolName(subToolName);
183         return builder;
184     }
185 
186     /**
187      * Create the basic event builder with all the common informations.
188      *
189      * @param userKey The unique id representing the user
190      * @param runId The current id for the session.
191      * @param userType The type of the user: internal or external.
192      * @param subToolName The name of test suite tool.
193      * @return a builder for the event.
194      */
createBaseInternalEventBuilder( String userKey, String runId, UserType userType, String subToolName)195     private static AtestLogEventInternal.Builder createBaseInternalEventBuilder(
196             String userKey, String runId, UserType userType, String subToolName) {
197         AtestLogEventInternal.Builder builder = AtestLogEventInternal.newBuilder();
198         builder.setUserKey(userKey);
199         builder.setRunId(runId);
200         builder.setUserType(userType);
201         builder.setToolName(TOOL_NAME);
202         builder.setSubToolName(subToolName);
203         return builder;
204     }
205 }
206