Просмотр исходного кода

web: use vehicle_id to refer to vehicles instead of just name

Shiv Tyagi 3 месяцев назад
Родитель
Сommit
5957bf7e13
3 измененных файлов с 43 добавлено и 36 удалено
  1. 27 20
      web/app.py
  2. 15 15
      web/static/js/add_build.js
  3. 1 1
      web/static/js/index.js

+ 27 - 20
web/app.py

@@ -142,7 +142,7 @@ def generate():
 
         vehicle = request.form['vehicle']
         version_info = versions_fetcher.get_version_info(
-            vehicle_name=vehicle,
+            vehicle_id=vehicle,
             remote=remote_name,
             commit_ref=commit_ref
         )
@@ -154,7 +154,7 @@ def generate():
         boards_at_commit = ap_src_metadata_fetcher.get_boards(
             remote=remote_name,
             commit_ref=commit_ref,
-            vehicle=vehicle,
+            vehicle_id=vehicle,
         )
         if board not in boards_at_commit:
             raise Exception("bad board")
@@ -176,7 +176,7 @@ def generate():
         )
 
         build_info = build_manager.BuildInfo(
-            vehicle=vehicle,
+            vehicle_id=vehicle,
             remote_info=remote_info,
             git_hash=git_hash,
             board=board,
@@ -231,20 +231,20 @@ def download_file(build_id, name):
     app.logger.info('Downloading %s/%s' % (path, name))
     return send_from_directory(path, name, as_attachment=False)
 
-@app.route("/boards_and_features/<string:vehicle_name>/<string:remote_name>/<string:commit_reference>", methods=['GET'])
-def boards_and_features(vehicle_name, remote_name, commit_reference):
+@app.route("/boards_and_features/<string:vehicle_id>/<string:remote_name>/<string:commit_reference>", methods=['GET'])
+def boards_and_features(vehicle_id, remote_name, commit_reference):
     commit_reference = base64.urlsafe_b64decode(commit_reference).decode()
 
-    if not versions_fetcher.is_version_listed(vehicle_name=vehicle_name, remote=remote_name, commit_ref=commit_reference):
+    if not versions_fetcher.is_version_listed(vehicle_id=vehicle_id, remote=remote_name, commit_ref=commit_reference):
         return "Bad request. Commit reference not allowed to build for the vehicle.", 400
 
-    app.logger.info('Board list and build options requested for %s %s %s' % (vehicle_name, remote_name, commit_reference))
+    app.logger.info('Board list and build options requested for %s %s %s' % (vehicle_id, remote_name, commit_reference))
     # getting board list for the branch
     with repo.get_checkout_lock():
         boards = ap_src_metadata_fetcher.get_boards(
             remote=remote_name,
             commit_ref=commit_reference,
-            vehicle=vehicle_name,
+            vehicle_id=vehicle_id,
         )
 
         options = ap_src_metadata_fetcher.get_build_options_at_commit(
@@ -279,10 +279,10 @@ def boards_and_features(vehicle_name, remote_name, commit_reference):
     # return jsonified result dict
     return jsonify(result)
 
-@app.route("/get_versions/<string:vehicle_name>", methods=['GET'])
-def get_versions(vehicle_name):
+@app.route("/get_versions/<string:vehicle_id>", methods=['GET'])
+def get_versions(vehicle_id):
     versions = list()
-    for version_info in versions_fetcher.get_versions_for_vehicle(vehicle_name=vehicle_name):
+    for version_info in versions_fetcher.get_versions_for_vehicle(vehicle_id=vehicle_id):
         if version_info.release_type == "latest":
             title = f"Latest ({version_info.remote})"
         else:
@@ -297,20 +297,27 @@ def get_versions(vehicle_name):
 
 @app.route("/get_vehicles")
 def get_vehicles():
-    return jsonify(vehicles_manager.get_all_vehicle_names_sorted())
-
-@app.route("/get_defaults/<string:vehicle_name>/<string:remote_name>/<string:commit_reference>/<string:board_name>", methods = ['GET'])
-def get_deafults(vehicle_name, remote_name, commit_reference, board_name):
-    # Heli is built on copter
-    if vehicle_name == "Heli":
-        vehicle_name = "Copter"
+    vehicles = [
+        {"id": vehicle.id, "name": vehicle.name}
+        for vehicle in vehicles_manager.get_all_vehicles()
+    ]
+    return jsonify(sorted(vehicles, key=lambda x: x['id']))
+
+@app.route("/get_defaults/<string:vehicle_id>/<string:remote_name>/<string:commit_reference>/<string:board_name>", methods = ['GET'])
+def get_deafults(vehicle_id, remote_name, commit_reference, board_name):
+    vehicle = vehicles_manager.get_vehicle_by_id(vehicle_id)
+    if vehicle is None:
+        return "Invalid vehicle ID", 400
+    
+    # Heli is built on copter boards with -heli suffix
+    if vehicle_id == "heli":
         board_name += "-heli"
 
     commit_reference = base64.urlsafe_b64decode(commit_reference).decode()
-    version_info = versions_fetcher.get_version_info(vehicle_name=vehicle_name, remote=remote_name, commit_ref=commit_reference)
+    version_info = versions_fetcher.get_version_info(vehicle_id=vehicle_id, remote=remote_name, commit_ref=commit_reference)
 
     if version_info is None:
-        return "Bad request. Commit reference %s is not allowed for builds for the %s for %s remote." % (commit_reference, vehicle_name, remote_name), 400
+        return "Bad request. Commit reference %s is not allowed for builds for the %s for %s remote." % (commit_reference, vehicle.name, remote_name), 400
 
     artifacts_dir = version_info.ap_build_artifacts_url
 

+ 15 - 15
web/static/js/add_build.js

@@ -315,7 +315,7 @@ function fetchVehicles() {
     sendAjaxRequestForJsonResponse(request_url)
         .then((json_response) => {
             let all_vehicles = json_response;
-            let new_vehicle = all_vehicles.find(vehicle => vehicle === "Copter") ? "Copter": all_vehicles[0];
+            let new_vehicle = all_vehicles.find(vehicle => vehicle.name === "Copter") ? "copter": all_vehicles[0].id;
             updateVehicles(all_vehicles, new_vehicle);
         })
         .catch((message) => {
@@ -328,20 +328,20 @@ function fetchVehicles() {
         });
 }
 
-function updateVehicles(all_vehicles, new_vehicle) {
+function updateVehicles(all_vehicles, new_vehicle_id) {
     let vehicle_element = document.getElementById('vehicle');
-    let old_vehicle = vehicle_element ? vehicle_element.value : '';
-    fillVehicles(all_vehicles, new_vehicle);
-    if (old_vehicle != new_vehicle) {
-        onVehicleChange(new_vehicle);
+    let old_vehicle_id = vehicle_element ? vehicle_element.value : '';
+    fillVehicles(all_vehicles, new_vehicle_id);
+    if (old_vehicle_id != new_vehicle_id) {
+        onVehicleChange(new_vehicle_id);
     }
 }
 
-function onVehicleChange(new_vehicle) {
+function onVehicleChange(new_vehicle_id) {
     // following elemets will be blocked (disabled) when we make the request
     let elements_to_block = ['vehicle', 'version', 'board', 'submit', 'reset_def', 'exp_col_button'];
     enableDisableElementsById(elements_to_block, false);
-    let request_url = '/get_versions/'+new_vehicle;
+    let request_url = '/get_versions/'+new_vehicle_id;
     setSpinnerToDiv('version_list', 'Fetching versions...');
     pending_update_calls += 1;
     sendAjaxRequestForJsonResponse(request_url)
@@ -374,12 +374,12 @@ function onVersionChange(new_version) {
     // following elemets will be blocked (disabled) when we make the request
     let elements_to_block = ['vehicle', 'version', 'board', 'submit', 'reset_def', 'exp_col_button'];
     enableDisableElementsById(elements_to_block, false);
-    let vehicle = document.getElementById("vehicle").value;
+    let vehicle_id = document.getElementById("vehicle").value;
     let arr = new_version.split("/");
     let remote = arr.shift();
     let commit_ref = arr.join("/");
     commit_ref = btoa(commit_ref).replace(/\//g, "_").replace(/\+/g, "-"); // url-safe base64 encoding
-    let request_url = `/boards_and_features/${vehicle}/${remote}/${commit_ref}`;
+    let request_url = `/boards_and_features/${vehicle_id}/${remote}/${commit_ref}`;
 
     // create a temporary container to set spinner inside it
     let temp_container = document.createElement('div');
@@ -590,16 +590,16 @@ function sendAjaxRequestForJsonResponse(url) {
     });
 }
 
-function fillVehicles(vehicles, vehicle_to_select) {
+function fillVehicles(vehicles, vehicle_id_to_select) {
     var output = document.getElementById('vehicle_list');
     output.innerHTML =  '<label for="vehicle" class="form-label"><strong>Select Vehicle</strong></label>' +
                         '<select name="vehicle" id="vehicle" class="form-select" aria-label="Select Vehicle" onchange="onVehicleChange(this.value);"></select>';
     vehicleList = document.getElementById("vehicle");
-    vehicles.forEach(vehicle_name => {
+    vehicles.forEach(vehicle => {
         opt = document.createElement('option');
-        opt.value = vehicle_name;
-        opt.innerHTML = vehicle_name;
-        opt.selected = (vehicle_name === vehicle_to_select);
+        opt.value = vehicle.id;
+        opt.innerHTML = vehicle.name;
+        opt.selected = (vehicle.id === vehicle_id_to_select);
         vehicleList.appendChild(opt);
     });
 }

+ 1 - 1
web/static/js/index.js

@@ -83,7 +83,7 @@ function updateBuildsTable(builds) {
                                 <td class="align-middle">${build_age}</td>
                                 <td class="align-middle"><a href="https://github.com/ArduPilot/ardupilot/commit/${build_info['git_hash']}">${build_info['git_hash'].substring(0,8)}</a></td>
                                 <td class="align-middle">${build_info['board']}</td>
-                                <td class="align-middle">${build_info['vehicle']}</td>
+                                <td class="align-middle">${build_info['vehicle_id']}</td>
                                 <td class="align-middle" id="${row_num}_features">
                                         ${features_string.substring(0, 100)}... 
                                         <span id="${row_num}_features_all" style="display:none;">${features_string}</span>