Skip to content

Commit

Permalink
Ignore special values when suggesting next value
Browse files Browse the repository at this point in the history
  • Loading branch information
theospears committed Feb 16, 2024
1 parent 77d26ce commit def3688
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
10 changes: 9 additions & 1 deletion BeeKit/Goal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,15 @@ public class Goal {
/// A hint for the value the user is likely to enter, based on past data points
public var suggestedNextValue: NSNumber? {
guard let recentData = self.recent_data else { return nil }
return recentData.last?.value
for dataPoint in recentData.reversed() {
let comment = dataPoint.comment
// Ignore data points with comments suggesting they aren't a real value
if comment.contains("#DERAIL") || comment.contains("#SELFDESTRUCT") || comment.contains("#RESTART") || comment.contains("#TARE") {
continue
}
return dataPoint.value
}
return nil
}

/// The daystamp corresponding to the day of the goal's creation, thus the first day we should add data points for.
Expand Down
61 changes: 61 additions & 0 deletions BeeSwiftTests/GoalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,65 @@ final class GoalTests: XCTestCase {
XCTAssertEqual(goal.derived_rate, 0)
}

func testSuggestedNextValueBasedOnLastValue() throws {
var testJSON = requiredGoalJson()
testJSON["recent_data"] = [
["value": 1, "daystamp": "20221130"],
["value": 2, "daystamp": "20221126"],
["value": 3.5, "daystamp": "20221125"],
]
let goal = Goal(json: testJSON)

XCTAssertEqual(goal.suggestedNextValue, 1)
}

func testSuggestedNextValueEmptyIfNoData() throws {
var testJSON = requiredGoalJson()
testJSON["recent_data"] = []
let goal = Goal(json: testJSON)

XCTAssertEqual(goal.suggestedNextValue, nil)
}

func testSuggestedNextValueIgnoresDerailsAndSelfDestructs() throws {
var testJSON = requiredGoalJson()
testJSON["recent_data"] = [
["value": 0, "daystamp": "20221131", "comment": "Goal #RESTART Point"],
["value": 0, "daystamp": "20221131", "comment": "This will #SELFDESTRUCT"],
["value": 0, "daystamp": "20221130", "comment": "#DERAIL ON THE 1st"],
["value": 2, "daystamp": "20221126"],
["value": 3.5, "daystamp": "20221125"],
]
let goal = Goal(json: testJSON)

XCTAssertEqual(goal.suggestedNextValue, 2)
}

/// Return the minimum set of required attributes for creating a goal
func requiredGoalJson() -> JSON {
return JSON(parseJSON: """
{
"id": "737aaa34f0118a330852e4bd",
"title": "Goal for Testing Purposes",
"slug": "test-goal",
"initday": 1668963600,
"deadline": 0,
"leadtime": 0,
"alertstart": 34200,
"queued": false,
"losedate": 2000271599,
"runits": "d",
"yaxis": "cumulative total test-goal",
"won": false,
"yaw": 1,
"lane": 3582,
"dir": 1,
"use_defaults": true,
"pledge": 0,
"hhmmformat": false,
"todayta": false
}
""")

}
}

0 comments on commit def3688

Please sign in to comment.