Index restore report action updates
This commit is contained in:
@@ -740,3 +740,20 @@ func TestExecuteRestoreInstallsOptionalPrerequisiteHistory(t *testing.T) {
|
||||
root := artifacts.SessionWorkDirForCampaign(workspaceRoot, cfg.Session.Campaign, cfg.Session.SessionID)
|
||||
mustReadEquals(t, filepath.Join(root, "inputs", "previous", "artifacts", "session_recap.md"), "optional history\n")
|
||||
}
|
||||
|
||||
func TestRestoreReportUpdatesFirstDuplicateAndCountsUnmatched(t *testing.T) {
|
||||
action := RestoreAction{Kind: RestoreActionDownload, LocalRelativePath: "inputs/a", RemoteKey: "remote/a"}
|
||||
report, err := newRestoreReport(&RemoteCurrentState{}, &RestorePlan{Actions: []RestoreAction{action, action}}, RestorePlanOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
report.markDownloaded(action)
|
||||
report.markFailed(RestoreAction{LocalRelativePath: "missing", RemoteKey: "missing"}, nil)
|
||||
if report.Actions[0].Status != "downloaded" || report.Actions[1].Status != "planned_download" || report.Execution.Downloaded != 1 || report.Execution.Failed != 1 {
|
||||
t.Fatalf("unexpected report: %+v", report)
|
||||
}
|
||||
data, err := json.Marshal(report)
|
||||
if err != nil || strings.Contains(string(data), "actionIndices") {
|
||||
t.Fatalf("serialized report = %s, error = %v", data, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,12 @@ type RestoreReport struct {
|
||||
Satisfied []string `json:"projected_satisfied,omitempty"`
|
||||
Remaining []string `json:"projected_remaining,omitempty"`
|
||||
reportPathRel string
|
||||
actionIndices map[restoreActionKey]int
|
||||
}
|
||||
|
||||
type restoreActionKey struct {
|
||||
localRelativePath string
|
||||
remoteKey string
|
||||
}
|
||||
|
||||
type RestorePlanSummary struct {
|
||||
@@ -75,11 +81,16 @@ func newRestoreReport(current *RemoteCurrentState, plan *RestorePlan, opts Resto
|
||||
SkipSame: plan.SkipSameCount,
|
||||
Conflicts: plan.ConflictCount,
|
||||
},
|
||||
Actions: make([]RestoreReportAction, 0, len(plan.Actions)),
|
||||
Satisfied: append([]string(nil), plan.Satisfied...),
|
||||
Remaining: append([]string(nil), plan.Remaining...),
|
||||
Actions: make([]RestoreReportAction, 0, len(plan.Actions)),
|
||||
Satisfied: append([]string(nil), plan.Satisfied...),
|
||||
Remaining: append([]string(nil), plan.Remaining...),
|
||||
actionIndices: make(map[restoreActionKey]int, len(plan.Actions)),
|
||||
}
|
||||
for _, action := range plan.Actions {
|
||||
key := restoreActionKey{action.LocalRelativePath, action.RemoteKey}
|
||||
if _, exists := r.actionIndices[key]; !exists {
|
||||
r.actionIndices[key] = len(r.Actions)
|
||||
}
|
||||
r.Actions = append(r.Actions, RestoreReportAction{
|
||||
Kind: string(action.Kind),
|
||||
SourceID: action.SourceID,
|
||||
@@ -109,7 +120,7 @@ func (r *RestoreReport) markDownloaded(action RestoreAction) {
|
||||
if r == nil {
|
||||
return
|
||||
}
|
||||
if idx := r.findAction(action); idx >= 0 {
|
||||
if idx, ok := r.actionIndices[restoreActionKey{action.LocalRelativePath, action.RemoteKey}]; ok {
|
||||
r.Actions[idx].Status = "downloaded"
|
||||
r.Actions[idx].Error = ""
|
||||
}
|
||||
@@ -120,7 +131,7 @@ func (r *RestoreReport) markFailed(action RestoreAction, err error) {
|
||||
if r == nil {
|
||||
return
|
||||
}
|
||||
if idx := r.findAction(action); idx >= 0 {
|
||||
if idx, ok := r.actionIndices[restoreActionKey{action.LocalRelativePath, action.RemoteKey}]; ok {
|
||||
r.Actions[idx].Status = "failed"
|
||||
if err != nil {
|
||||
r.Actions[idx].Error = err.Error()
|
||||
@@ -147,18 +158,6 @@ func (r *RestoreReport) setSucceeded() {
|
||||
r.Error = ""
|
||||
}
|
||||
|
||||
func (r *RestoreReport) findAction(action RestoreAction) int {
|
||||
if r == nil {
|
||||
return -1
|
||||
}
|
||||
for i := range r.Actions {
|
||||
if r.Actions[i].LocalRelativePath == action.LocalRelativePath && r.Actions[i].RemoteKey == action.RemoteKey {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func persistRestoreReport(store artifacts.Store, cfg *config.Config, report *RestoreReport) (string, error) {
|
||||
if store == nil {
|
||||
return "", fmt.Errorf("artifact store is required")
|
||||
|
||||
Reference in New Issue
Block a user