xref: /aosp_15_r20/external/toolchain-utils/update_telemetry_defaults.py (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright 2020 The ChromiumOS Authors
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Script to maintain the Telemetry benchmark default results file.
8
9This script allows the user to see and update the set of default
10results to be used in generating reports from running the Telemetry
11benchmarks.
12"""
13
14
15__author__ = "[email protected] (Caroline Tice)"
16
17import json
18import os
19import sys
20
21from cros_utils import misc
22
23
24Defaults = {}
25
26
27class TelemetryDefaults(object):
28    """Class for handling telemetry default return result fields."""
29
30    DEFAULTS_FILE_NAME = "crosperf/default-telemetry-results.json"
31
32    def __init__(self):
33        # Get the Crosperf directory; that is where the defaults
34        # file should be.
35        dirname, __ = misc.GetRoot(__file__)
36        fullname = os.path.join(dirname, self.DEFAULTS_FILE_NAME)
37        self._filename = fullname
38        self._defaults = {}
39
40    def ReadDefaultsFile(self):
41        if os.path.exists(self._filename):
42            with open(self._filename, "r", encoding="utf-8") as fp:
43                self._defaults = json.load(fp)
44
45    def WriteDefaultsFile(self):
46        with open(self._filename, "w", encoding="utf-8") as fp:
47            json.dump(self._defaults, fp, indent=2)
48
49    def ListCurrentDefaults(self, benchmark="all"):
50        # Show user current defaults. By default, show all.  The user
51        # can specify the name of a particular benchmark to see only that
52        # benchmark's default values.
53        if len(self._defaults) == 0:
54            print("The benchmark default results are currently empty.")
55        if benchmark == "all":
56            for b in self._defaults.keys():
57                results = self._defaults[b]
58                out_str = b + " : "
59                for r in results:
60                    out_str += r + " "
61                print(out_str)
62        elif benchmark in self._defaults:
63            results = self._defaults[benchmark]
64            out_str = benchmark + " : "
65            for r in results:
66                out_str += r + " "
67            print(out_str)
68        else:
69            print("Error:  Unrecognized benchmark '%s'" % benchmark)
70
71    def AddDefault(self, benchmark, result):
72        if benchmark in self._defaults:
73            resultList = self._defaults[benchmark]
74        else:
75            resultList = []
76        resultList.append(result)
77        self._defaults[benchmark] = resultList
78        print("Updated results set for '%s': " % benchmark)
79        print("%s : %s" % (benchmark, repr(self._defaults[benchmark])))
80
81    def RemoveDefault(self, benchmark, result):
82        if benchmark in self._defaults:
83            resultList = self._defaults[benchmark]
84            if result in resultList:
85                resultList.remove(result)
86                print("Updated results set for '%s': " % benchmark)
87                print("%s : %s" % (benchmark, repr(self._defaults[benchmark])))
88            else:
89                print(
90                    "'%s' is not in '%s's default results list."
91                    % (result, benchmark)
92                )
93        else:
94            print("Cannot find benchmark named '%s'" % benchmark)
95
96    def GetDefault(self):
97        return self._defaults
98
99    def RemoveBenchmark(self, benchmark):
100        if benchmark in self._defaults:
101            del self._defaults[benchmark]
102            print("Deleted benchmark '%s' from list of benchmarks." % benchmark)
103        else:
104            print("Cannot find benchmark named '%s'" % benchmark)
105
106    def RenameBenchmark(self, old_name, new_name):
107        if old_name in self._defaults:
108            resultsList = self._defaults[old_name]
109            del self._defaults[old_name]
110            self._defaults[new_name] = resultsList
111            print("Renamed '%s' to '%s'." % (old_name, new_name))
112        else:
113            print("Cannot find benchmark named '%s'" % old_name)
114
115    def UsageError(self, user_input):
116        # Print error message, then show options
117        print("Error:Invalid user input: '%s'" % user_input)
118        self.ShowOptions()
119
120    def ShowOptions(self):
121        print(
122            """
123Below are the valid user options and their arguments, and an explanation
124of what each option does.  You may either print out the full name of the
125option, or you may use the first letter of the option.  Case (upper or
126lower) does not matter, for the command (case of the result name DOES matter):
127
128    (L)ist                           - List all current defaults
129    (L)ist <benchmark>               - List current defaults for benchmark
130    (H)elp                           - Show this information.
131    (A)dd <benchmark> <result>       - Add a default result for a particular
132                                       benchmark (appends to benchmark's list
133                                       of results, if list already exists)
134    (D)elete <benchmark> <result>    - Delete a default result for a
135                                       particular benchmark
136    (R)emove <benchmark>             - Remove an entire benchmark (and its
137                                       results)
138    (M)ove <old-benchmark> <new-benchmark>    - Rename a benchmark
139    (Q)uit                           - Exit this program, saving changes.
140    (T)erminate                      - Exit this program; abandon changes.
141
142"""
143        )
144
145    def GetUserInput(self):
146        # Prompt user
147        print("Enter option> ")
148        # Process user input
149        inp = sys.stdin.readline()
150        inp = inp[:-1]
151        # inp = inp.lower()
152        words = inp.split(" ")
153        option = words[0]
154        option = option.lower()
155        if option in ("h", "help"):
156            self.ShowOptions()
157        elif option in ("l", "list"):
158            if len(words) == 1:
159                self.ListCurrentDefaults()
160            else:
161                self.ListCurrentDefaults(benchmark=words[1])
162        elif option in ("a", "add"):
163            if len(words) < 3:
164                self.UsageError(inp)
165            else:
166                benchmark = words[1]
167                resultList = words[2:]
168                for r in resultList:
169                    self.AddDefault(benchmark, r)
170        elif option in ("d", "delete"):
171            if len(words) != 3:
172                self.UsageError(inp)
173            else:
174                benchmark = words[1]
175                result = words[2]
176                self.RemoveDefault(benchmark, result)
177        elif option in ("r", "remove"):
178            if len(words) != 2:
179                self.UsageError(inp)
180            else:
181                benchmark = words[1]
182                self.RemoveBenchmark(benchmark)
183        elif option in ("m", "move"):
184            if len(words) != 3:
185                self.UsageError(inp)
186            else:
187                old_name = words[1]
188                new_name = words[2]
189                self.RenameBenchmark(old_name, new_name)
190        elif option in ("q", "quit"):
191            self.WriteDefaultsFile()
192
193        return option in ("q", "quit", "t", "terminate")
194
195
196def Main():
197    defaults = TelemetryDefaults()
198    defaults.ReadDefaultsFile()
199    defaults.ShowOptions()
200    done = defaults.GetUserInput()
201    while not done:
202        done = defaults.GetUserInput()
203    return 0
204
205
206if __name__ == "__main__":
207    retval = Main()
208    sys.exit(retval)
209