• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.car.telemetry.databroker;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.car.telemetry.TelemetryProto;
22 import android.os.PersistableBundle;
23 
24 import java.util.List;
25 
26 /**
27  * A wrapper class containing all the necessary information to invoke the ScriptExecutor API. It
28  * is enqueued into the priority queue where it pends execution by {@link DataBroker}.
29  * It implements the {@link Comparable} interface so it has a natural ordering by priority and
30  * creation timestamp in the priority queue.
31  * The object can be accessed from any thread. See {@link DataSubscriber} for thread-safety.
32  */
33 public class ScriptExecutionTask implements Comparable<ScriptExecutionTask> {
34     private final int mPublisherType;
35     private final long mTimestampMillis;
36     private final DataSubscriber mSubscriber;
37 
38     private PersistableBundle mData = null;
39     private List<PersistableBundle> mBundleList = null;
40     private boolean mIsLargeData = false;
41 
ScriptExecutionTask( @onNull DataSubscriber subscriber, @NonNull PersistableBundle data, long elapsedRealtimeMillis, boolean isLargeData, int publisherType)42     ScriptExecutionTask(
43             @NonNull DataSubscriber subscriber,
44             @NonNull PersistableBundle data,
45             long elapsedRealtimeMillis,
46             boolean isLargeData,
47             int publisherType) {
48         mTimestampMillis = elapsedRealtimeMillis;
49         mSubscriber = subscriber;
50         mData = data;
51         mIsLargeData = isLargeData;
52         mPublisherType = publisherType;
53     }
54 
ScriptExecutionTask( @onNull DataSubscriber subscriber, @NonNull List<PersistableBundle> bundleList, long elapsedRealtimeMillis, int publisherType)55     ScriptExecutionTask(
56             @NonNull DataSubscriber subscriber,
57             @NonNull List<PersistableBundle> bundleList,
58             long elapsedRealtimeMillis,
59             int publisherType) {
60         mTimestampMillis = elapsedRealtimeMillis;
61         mSubscriber = subscriber;
62         mBundleList = bundleList;
63         mPublisherType = publisherType;
64     }
65 
getPublisherType()66     public int getPublisherType() {
67         return mPublisherType;
68     }
69 
70     /** Returns the priority of the task. */
getPriority()71     public int getPriority() {
72         return mSubscriber.getPriority();
73     }
74 
75     /** Returns the creation timestamp of the task. */
getCreationTimestampMillis()76     public long getCreationTimestampMillis() {
77         return mTimestampMillis;
78     }
79 
80     /** Returns the MetricsConfig associated with this task. */
81     @NonNull
getMetricsConfig()82     public TelemetryProto.MetricsConfig getMetricsConfig() {
83         return mSubscriber.getMetricsConfig();
84     }
85 
86     /** Returns the handler function name defined in MetricsConfig script. */
87     @NonNull
getHandlerName()88     public String getHandlerName() {
89         return mSubscriber.getHandlerName();
90     }
91 
92     /** Returns the data being sent to the subscriber. */
93     @Nullable
getData()94     public PersistableBundle getData() {
95         return mData;
96     }
97 
98     /** Returns the bundle list data being sent to subscriber. */
99     @Nullable
getBundleList()100     public List<PersistableBundle> getBundleList() {
101         return mBundleList;
102     }
103 
104     /**
105      * Indicates whether the task is associated with MetricsConfig specified by its name.
106      */
isAssociatedWithMetricsConfig(@onNull String metricsConfigName)107     public boolean isAssociatedWithMetricsConfig(@NonNull String metricsConfigName) {
108         return mSubscriber.getMetricsConfig().getName().equals(metricsConfigName);
109     }
110 
111     /**
112      * Returns the script input data size in bytes.
113      */
isLargeData()114     public boolean isLargeData() {
115         return mIsLargeData;
116     }
117 
118     /**
119      * Returns if the data is a list of bundles.
120      */
isBundleList()121     public boolean isBundleList() {
122         return mBundleList != null;
123     }
124 
125     /** Determines if the task is eligible to bypass script executor. */
bypassScriptExecutor()126     public boolean bypassScriptExecutor() {
127         return getHandlerName().isEmpty();
128     }
129 
130     @Override
compareTo(@onNull ScriptExecutionTask other)131     public int compareTo(@NonNull ScriptExecutionTask other) {
132         if (getPriority() < other.getPriority()) {
133             return -1;
134         } else if (getPriority() > other.getPriority()) {
135             return 1;
136         }
137         // if equal priority, compare creation timestamps
138         if (getCreationTimestampMillis() < other.getCreationTimestampMillis()) {
139             return -1;
140         }
141         return 1;
142     }
143 }
144