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.car.telemetry.MetricsConfigKey;
20 import android.os.PersistableBundle;
21 
22 import com.android.car.telemetry.TelemetryProto;
23 
24 /**
25  * A wrapper class containing all the necessary information to invoke the ScriptExecutor API. It
26  * is enqueued into the priority queue where it pends execution by {@link DataBroker}.
27  * It implements the {@link Comparable} interface so it has a natural ordering by priority and
28  * creation timestamp in the priority queue.
29  * The object can be accessed from any thread. See {@link DataSubscriber} for thread-safety.
30  */
31 public class ScriptExecutionTask implements Comparable<ScriptExecutionTask> {
32     private final long mTimestampMillis;
33     private final DataSubscriber mSubscriber;
34     private final PersistableBundle mData;
35     private final boolean mIsLargeData;
36 
ScriptExecutionTask(DataSubscriber subscriber, PersistableBundle data, long elapsedRealtimeMillis, boolean isLargeData)37     ScriptExecutionTask(DataSubscriber subscriber, PersistableBundle data,
38             long elapsedRealtimeMillis, boolean isLargeData) {
39         mTimestampMillis = elapsedRealtimeMillis;
40         mSubscriber = subscriber;
41         mData = data;
42         mIsLargeData = isLargeData;
43     }
44 
45     /** Returns the priority of the task. */
getPriority()46     public int getPriority() {
47         return mSubscriber.getPriority();
48     }
49 
50     /** Returns the creation timestamp of the task. */
getCreationTimestampMillis()51     public long getCreationTimestampMillis() {
52         return mTimestampMillis;
53     }
54 
getMetricsConfig()55     public TelemetryProto.MetricsConfig getMetricsConfig() {
56         return mSubscriber.getMetricsConfig();
57     }
58 
getHandlerName()59     public String getHandlerName() {
60         return mSubscriber.getHandlerName();
61     }
62 
getData()63     public PersistableBundle getData() {
64         return mData;
65     }
66 
67     /**
68      * Indicates whether the task is associated with MetricsConfig specified by its key.
69      */
isAssociatedWithMetricsConfig(MetricsConfigKey key)70     public boolean isAssociatedWithMetricsConfig(MetricsConfigKey key) {
71         return mSubscriber.getMetricsConfig().getName().equals(key.getName())
72                 && mSubscriber.getMetricsConfig().getVersion() == key.getVersion();
73     }
74 
75     /**
76      * Returns the script input data size in bytes.
77      */
isLargeData()78     public boolean isLargeData() {
79         return mIsLargeData;
80     }
81 
82     @Override
compareTo(ScriptExecutionTask other)83     public int compareTo(ScriptExecutionTask other) {
84         if (getPriority() < other.getPriority()) {
85             return -1;
86         } else if (getPriority() > other.getPriority()) {
87             return 1;
88         }
89         // if equal priority, compare creation timestamps
90         if (getCreationTimestampMillis() < other.getCreationTimestampMillis()) {
91             return -1;
92         }
93         return 1;
94     }
95 }
96