// Usage example:
// vmanager -batch -c "load dump_results_csv; analyze_runs -vsof my_session/my_session.vsof; dump_results_csv()"
<'
extend global {
  dump_results_csv ( result_file_name:string=NULL ) is {
    if result_file_name == NULL { result_file_name = "results.csv"; };

    // Get all loaded sessions (there can be more than one)
    var all_sessions := vm_manager.get_all_sessions();
    // Get an analysis "context"
    var ctx : vm_context = vm_manager.create_context(all_sessions.as_a(list of vm_attribute_container), "Default");
    // Get all the runs from that context
    var all_runs := ctx.get_runs();

    // Create the output file and header
    var result_file := files.open(result_file_name,"w","Text file");
    files.write(result_file,"Test, Seed, Status, First Failure");

    // Get a handle to the sv_seed attribute type
    var attr_seed  := vm_manager.get_attribute_by_name("sv_seed");
    // Get a handle to the test_name attribute type
    var attr_title := vm_manager.get_attribute_by_name("test_name");

    // Iterate through all the runs
    for each (run) in all_runs {
      var line :string;
      line = append(run.get_attribute_value_or_default(attr_title), ", ");
      line = append(line, run.get_attribute_value_or_default(attr_seed), ", ");
      line = append(line, run.get_status(), ", ");
      if (run.get_status_enum() == failed) {
        // var all_failures := run.get_failures(); // Alternative if you want all failures
        var first_failure := run.get_first_failure();
        line = append(line, first_failure.get_value("description"));
      } else {
        line = append(line, "n/a");
      };
      files.write(result_file,line);
    };
    files.close(result_file);
  };
};
'>