ccsync icon indicating copy to clipboard operation
ccsync copied to clipboard

Refactor AddTaskHandler and AddTaskToTaskwarrior to improve maintainability and performance

Open its-me-abhishek opened this issue 10 months ago • 1 comments

Description The current task creation pipeline (from AddTaskHandler to AddTaskToTaskwarrior) suffers from high memory overhead, unnecessary CLI/DB calls, and a brittle function signature. This refactor targets performance bottlenecks and improves maintainability by consolidating data structures.

Changes Required:

  1. Optimize Request Parsing

    • Replace io.ReadAll and json.Unmarshal with json.NewDecoder.
    • Reason: json.NewDecoder streams the request body directly, which is more memory-efficient than reading the entire body into a byte slice before parsing.
  2. Check dependencies only when they're not null

existingTasks, err := tw.FetchTasksFromTaskwarrior(email, encryptionSecret, origin, uuid)
		if err != nil {
			if err := utils.ValidateDependencies(depends, ""); err != nil {
				http.Error(w, fmt.Sprintf("Invalid dependencies: %v", err), http.StatusBadRequest)
				return
			}
		} else {
			taskDeps := make([]utils.TaskDependency, len(existingTasks))
			for i, task := range existingTasks {
				taskDeps[i] = utils.TaskDependency{
					UUID:    task.UUID,
					Depends: task.Depends,
					Status:  task.Status,
				}
			}

			if err := utils.ValidateCircularDependencies(depends, "", taskDeps); err != nil {
				http.Error(w, fmt.Sprintf("Invalid dependencies: %v", err), http.StatusBadRequest)
				return
			}
		}

This can prevent the unnecessary fetching of tasks. Which will then reduce the overall response time.

// Only fetch and validate if dependencies exist
if len(requestBody.Depends) > 0 {
    existingTasks, err := tw.FetchTasksFromTaskwarrior(requestBody.Email, ...)
    // ... validation logic
}
  1. Parameter Bloat: The function signature currently takes 15 arguments.
    • Fix: Change the signature to accept a models.AddTaskRequestBody. Can use this inside the AddTasktoTW as well as the handler.
    • Exception: dueDateStr should be passed as a second argument since it requires pre-processing/formatting before being sent to the TW CLI.
    • Quite similar to func AddTaskToTaskwarrior(req models.AddTaskRequestBody, dueDateStr string)
    • An alternative could be to compute this dueDateStr directly inside the AddTaskToTaskwarrior function. Making the handler cleaner

Acceptance Criteria

  • [x] Handler uses json.NewDecoder.
  • [x] Task fetching is skipped if Depends is empty.
  • [x] AddTaskToTaskwarrior signature is consolidated.
  • [x] Log output is detailed enough to reconstruct a failure without a debugger.
  • [ ] 100% test coverage on the new logic paths in AddTaskHandler.
  • [x] The code is manually reviewed and relevant comments are present inside the code-review.

its-me-abhishek avatar Jan 05 '26 14:01 its-me-abhishek

@its-me-abhishek I am picking this issue will raise the PR tommorow EOD.

My Approach :

  1. Use json.NewDecoder(r.Body).Decode(&requestBody) instead of io.ReadAll + json.Unmarshal More memory efficient streaming parse
  2. Add conditional check: Only fetch tasks if len(requestBody.Depends) > 0 Skips unnecessary Taskwarrior calls when no dependencies exist
  3. Change AddTaskToTaskwarrior signature from 15 params to (req models.AddTaskRequestBody, dueDateStr string) Consolidates parameters into single struct for cleaner code

Rustix69 avatar Jan 05 '26 17:01 Rustix69