update
This commit is contained in:
211
app.cpp
211
app.cpp
@@ -60,7 +60,41 @@ GymLogDbParsed *parseDb(Arena *arena, string database) {
|
||||
return dbParsed;
|
||||
}
|
||||
|
||||
int gymTrackerWorkForExercise(Arena *arena, uint32 exerciseId) {
|
||||
list<GymLogEntry> loadEntryLog(Arena *arena, string fileLocation) {
|
||||
list<GymLogEntry> result = {0};
|
||||
string logfile = readEntireFile(arena, LOG_FILE_LOCATION);
|
||||
|
||||
if (logfile.length % sizeof(GymLogEntry) != 0) {
|
||||
log("Log file corrupted.\n");
|
||||
} else {
|
||||
size_t entryCount = logfile.length / sizeof(GymLogEntry);
|
||||
result = { (GymLogEntry *)logfile.str, entryCount, entryCount };
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
struct WorkSummary {
|
||||
real32 totalWork;
|
||||
uint32 restTime;
|
||||
};
|
||||
|
||||
WorkSummary workSummaryForExercise(list<GymLogEntry> entries) {
|
||||
WorkSummary result = {0};
|
||||
UnixTimestamp lastTimestamp = 0;
|
||||
for (EachInReversed(entries, i)) {
|
||||
GymLogEntry logEntry = entries.data[i];
|
||||
result.totalWork += logEntry.weightRepsInfo.weight * logEntry.weightRepsInfo.reps;
|
||||
if (lastTimestamp > 0) {
|
||||
result.restTime += (uint32)(lastTimestamp - logEntry.timestamp);
|
||||
}
|
||||
lastTimestamp = logEntry.timestamp;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int gymTrackerWorkToday(Arena *arena, uint32 exerciseId, string exerciseName) {
|
||||
int statusCode = 0;
|
||||
string logfile = readEntireFile(arena, LOG_FILE_LOCATION);
|
||||
|
||||
@@ -69,23 +103,27 @@ int gymTrackerWorkForExercise(Arena *arena, uint32 exerciseId) {
|
||||
statusCode = 1;
|
||||
} else {
|
||||
size_t entryCount = logfile.length / sizeof(GymLogEntry);
|
||||
int currentDay = 0;
|
||||
int currentYear = 0;
|
||||
list<GymLogEntry> logEntries = { (GymLogEntry *)logfile.str, entryCount, entryCount };
|
||||
|
||||
UnixTimestamp todayUnix = getSystemUnixTime();
|
||||
Timestamp todayTs = timestampFromUnixTime(&todayUnix);
|
||||
|
||||
real32 work = 0;
|
||||
for (EachIn(logEntries, i)) {
|
||||
list<GymLogEntry> todaysEntries = {0};
|
||||
todaysEntries.data = logEntries.data;
|
||||
for (EachInReversed(logEntries, i)) {
|
||||
GymLogEntry logEntry = logEntries.data[i];
|
||||
Timestamp logTs = timestampFromUnixTime(&logEntry.timestamp);
|
||||
if (logTs.tm_yday == todayTs.tm_yday && todayTs.tm_year == logTs.tm_year) {
|
||||
work += logEntry.weightRepsInfo.weight * logEntry.weightRepsInfo.reps;
|
||||
todaysEntries.head += 1;
|
||||
todaysEntries.data = &logEntries.data[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
if (todaysEntries.data) {
|
||||
todaysEntries.length = todaysEntries.head;
|
||||
WorkSummary summary = workSummaryForExercise(todaysEntries);
|
||||
log("Total work today for %S:\n%.2fkg in ~%.2fmin.\n", exerciseName, summary.totalWork, (real32)summary.restTime / 60.0f);
|
||||
}
|
||||
|
||||
log("Total work for this exercise today:\n%.2f J * m/ex\n", work);
|
||||
}
|
||||
|
||||
return statusCode;
|
||||
@@ -101,94 +139,115 @@ int gymTrackerStatus(Arena *arena, list<string> args) {
|
||||
puts("Log file corrupted.");
|
||||
statusCode = 1;
|
||||
} else {
|
||||
Timestamp stopTs = {0};
|
||||
if (args.length > 0 && strEql(args.data[0], "--today"_s)) {
|
||||
UnixTimestamp nowUnix = getSystemUnixTime();
|
||||
stopTs = timestampFromUnixTime(&nowUnix);
|
||||
} else if (args.length > 1 && strEql(args.data[0], "--days"_s) || strEql(args.data[0], "-d"_s)) {
|
||||
size_t l;
|
||||
int numDays = parsePositiveInt(args.data[1], &l);
|
||||
Timestamp startTs = {0};
|
||||
int numDays = 1;
|
||||
bool showAll = args.length == 1 && strEql(args.data[0], "--all"_s);
|
||||
if (!showAll) {
|
||||
if (args.length == 2 && (strEql(args.data[0], "--days"_s) || strEql(args.data[0], "-d"_s))) {
|
||||
size_t l;
|
||||
numDays = parsePositiveInt(args.data[1], &l);
|
||||
}
|
||||
if (numDays == -1) {
|
||||
puts("Bad argument for --days parameter.");
|
||||
puts("Bad argument for --days (-d) parameter.");
|
||||
statusCode = 1;
|
||||
} else {
|
||||
uint64 todayUnix = getSystemUnixTime();
|
||||
UnixTimestamp stopUnix = todayUnix - numDays * 24 * 60 * 60;
|
||||
stopTs = timestampFromUnixTime(&stopUnix);
|
||||
UnixTimestamp startUnix = todayUnix - numDays * 24 * 60 * 60;
|
||||
startTs = timestampFromUnixTime(&startUnix);
|
||||
}
|
||||
}
|
||||
|
||||
if (statusCode == 0) {
|
||||
int lastDay = -1;
|
||||
int lastYear = -1;
|
||||
size_t entryCount = file.length / sizeof(GymLogEntry);
|
||||
int currentDay = -1;
|
||||
int currentYear = -1;
|
||||
list<GymLogEntry> logEntries = { (GymLogEntry *)file.str, entryCount, entryCount };
|
||||
|
||||
list<real32> workPerExerciseByDay = PushFullList(arena, real32, db->header.nextId);
|
||||
list<string> nameByExercise = PushFullList(arena, string, db->header.nextId);
|
||||
zeroListFull(&workPerExerciseByDay);
|
||||
zeroListFull(&nameByExercise);
|
||||
list<string> nameByExercise = PushFullListZero(arena, string, db->header.nextId);
|
||||
|
||||
list<real32> workPerExerciseByDay = PushFullListZero(arena, real32, db->header.nextId);
|
||||
list<uint32> restPerExerciseByDay = PushFullListZero(arena, uint32, db->header.nextId);
|
||||
list<uint32> lastTsPerExerciseByDay = PushFullListZero(arena, uint32, db->header.nextId);
|
||||
|
||||
int dayCount = 0;
|
||||
|
||||
Timestamp timestamp = {0};
|
||||
if (logEntries.length > 0) {
|
||||
timestamp = timestampFromUnixTime(&logEntries.data[logEntries.length - 1].timestamp);
|
||||
}
|
||||
|
||||
for (EachInReversed(logEntries, i)) {
|
||||
GymLogEntry entry = logEntries.data[i];
|
||||
if (timestamp.tm_yday != currentDay || timestamp.tm_year != currentYear) {
|
||||
if (timestamp.tm_year < stopTs.tm_year || timestamp.tm_yday < stopTs.tm_yday) {
|
||||
break;
|
||||
}
|
||||
GymLogEntry *prevEntry = 0;
|
||||
GymLogEntry *entry = 0;
|
||||
for (EachIn(logEntries, i)) {
|
||||
prevEntry = entry;
|
||||
entry = &logEntries.data[i];
|
||||
|
||||
timestamp = timestampFromUnixTime(&entry->timestamp);
|
||||
|
||||
if (timestamp.tm_year < startTs.tm_year || timestamp.tm_yday < startTs.tm_yday) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (timestamp.tm_yday != lastDay || timestamp.tm_year != lastYear) {
|
||||
if (dayCount > 0) {
|
||||
log("\n");
|
||||
}
|
||||
log("--- %S ---\n", formatTimeYmd(arena, ×tamp));
|
||||
currentDay = timestamp.tm_yday;
|
||||
currentYear = timestamp.tm_year;
|
||||
log("================ %S =================================\n", formatTimeYmd(arena, ×tamp));
|
||||
lastDay = timestamp.tm_yday;
|
||||
lastYear = timestamp.tm_year;
|
||||
dayCount++;
|
||||
}
|
||||
|
||||
workPerExerciseByDay.data[entry.exerciseId] += entry.weightRepsInfo.reps * entry.weightRepsInfo.weight;
|
||||
workPerExerciseByDay.data[entry->exerciseId] += entry->weightRepsInfo.reps * entry->weightRepsInfo.weight;
|
||||
uint32 lastTsForExercise = lastTsPerExerciseByDay.data[entry->exerciseId];
|
||||
if (lastTsForExercise > 0) {
|
||||
restPerExerciseByDay.data[entry->exerciseId] += (uint32)(entry->timestamp - lastTsForExercise);
|
||||
}
|
||||
lastTsPerExerciseByDay.data[entry->exerciseId] = (uint32)entry->timestamp;
|
||||
|
||||
const char *format;
|
||||
if (entry.weightRepsInfo.weight == (int32)entry.weightRepsInfo.weight) {
|
||||
format = "%S: %S, %.0fkg X %i\n";
|
||||
if (entry->weightRepsInfo.weight == (int32)entry->weightRepsInfo.weight) {
|
||||
format = "%S: %S %.0fkg X %i\n";
|
||||
} else {
|
||||
format = "%S: %S, %.2fkg X %i\n";
|
||||
format = "%S: %S %.2fkg X %i\n";
|
||||
}
|
||||
|
||||
string *exerciseName = &(nameByExercise.data[entry.exerciseId]);
|
||||
string *exerciseName = &(nameByExercise.data[entry->exerciseId]);
|
||||
if (exerciseName->str == 0) {
|
||||
for (EachIn(db->entries, j)) {
|
||||
GymLogDbParsedEntry dbEntry = db->entries.data[j];
|
||||
if (dbEntry.id == entry.exerciseId) {
|
||||
if (dbEntry.id == entry->exerciseId) {
|
||||
*exerciseName = dbEntry.name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string nameToPrint = {0};
|
||||
if (prevEntry && entry->exerciseId == prevEntry->exerciseId) {
|
||||
nameToPrint = PushStringFill(arena, exerciseName->length, '.');
|
||||
} else {
|
||||
nameToPrint = exerciseName->str ? *exerciseName : "unknown-exercise"_s;
|
||||
log("\n");
|
||||
}
|
||||
log(format,
|
||||
formatTimeHms(arena, ×tamp),
|
||||
exerciseName->str ? *exerciseName : "unknown-exercise"_s,
|
||||
entry.weightRepsInfo.weight,
|
||||
entry.weightRepsInfo.reps);
|
||||
nameToPrint,
|
||||
entry->weightRepsInfo.weight,
|
||||
entry->weightRepsInfo.reps);
|
||||
|
||||
if (i > 0) {
|
||||
timestamp = timestampFromUnixTime(&logEntries.data[i - 1].timestamp);
|
||||
Timestamp nextTimestamp = {0};
|
||||
if (i < logEntries.head - 1) {
|
||||
nextTimestamp = timestampFromUnixTime(&logEntries.data[i + 1].timestamp);
|
||||
}
|
||||
|
||||
if (i == 0 || timestamp.tm_yday != currentDay || timestamp.tm_year != currentYear) {
|
||||
if (i == logEntries.head + 1 || nextTimestamp.tm_yday != lastDay || nextTimestamp.tm_year != lastYear) {
|
||||
log("\n");
|
||||
log("Work summary:\n");
|
||||
for (size_t j = 0; j < workPerExerciseByDay.length; j++) {
|
||||
if (workPerExerciseByDay.data[j] != 0.0f) {
|
||||
log("%S: %.2f J * m/ex\n", nameByExercise.data[j], workPerExerciseByDay.data[j]);
|
||||
log("%S: %.2fkg in %.2fmin\n", nameByExercise.data[j], workPerExerciseByDay.data[j], (real32)restPerExerciseByDay.data[j] / 60.0f);
|
||||
}
|
||||
}
|
||||
zeroListFull(&workPerExerciseByDay);
|
||||
zeroListFull(&restPerExerciseByDay);
|
||||
zeroListFull(&lastTsPerExerciseByDay);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -197,6 +256,32 @@ int gymTrackerStatus(Arena *arena, list<string> args) {
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
int gymTrackerDeleteEntries(Arena *arena, list<string> args) {
|
||||
int statusCode = 0;
|
||||
|
||||
if (args.length == 0) {
|
||||
log("Please pass the number of entries to delete starting from the most recent.");
|
||||
statusCode = 1;
|
||||
} else {
|
||||
size_t position = 0;
|
||||
int numToDelete = parsePositiveInt(args.data[0], &position);
|
||||
if (numToDelete != -1) {
|
||||
list<GymLogEntry> logEntries = loadEntryLog(arena, LOG_FILE_LOCATION);
|
||||
if (numToDelete > logEntries.length) {
|
||||
log("%i is more than the current number of log entries (%i). Aborting.", numToDelete, logEntries.length);
|
||||
statusCode = 1;
|
||||
} else {
|
||||
writeEntireFile(arena, LOG_FILE_LOCATION, (byte *)logEntries.data, (logEntries.length - numToDelete) * sizeof(GymLogEntry));
|
||||
}
|
||||
} else {
|
||||
log("Invalid number to delete.\n");
|
||||
statusCode = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
// Syntax: do <exercise-name> weightKg reps
|
||||
int gymTrackerDo(Arena *arena, list<string> args) {
|
||||
int statusCode = 0;
|
||||
@@ -214,12 +299,14 @@ int gymTrackerDo(Arena *arena, list<string> args) {
|
||||
GymLogDbParsed *db = parseDb(arena, readEntireFile(arena, DB_FILE_LOCATION));
|
||||
for (EachIn(db->entries, i)) {
|
||||
GymLogDbParsedEntry entry = db->entries.data[i];
|
||||
if (strEql(entry.name, newExerciseName)) {
|
||||
if (strStartsWith(entry.name, newExerciseName)) {
|
||||
existingEntry = &entry;
|
||||
log("Assuming exercise \"%S\".\n\n", entry.name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!existingEntry) {
|
||||
log("The exercise \"%S\" hasn't been registered.", newExerciseName);
|
||||
statusCode = 1;
|
||||
}
|
||||
}
|
||||
@@ -241,7 +328,23 @@ int gymTrackerDo(Arena *arena, list<string> args) {
|
||||
};
|
||||
|
||||
fileAppend(arena, LOG_FILE_LOCATION, (byte *)&entry, sizeof(entry));
|
||||
statusCode = gymTrackerWorkForExercise(arena, exerciseId);
|
||||
statusCode = gymTrackerWorkToday(arena, exerciseId, newExerciseName);
|
||||
}
|
||||
}
|
||||
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
int gymTrackerListExercises(Arena *arena, list<string> args) {
|
||||
int statusCode = 0;
|
||||
GymLogDbParsed *db = parseDb(arena, readEntireFile(arena, DB_FILE_LOCATION));
|
||||
|
||||
if (db->entries.length == 0) {
|
||||
log("No entries currently registered in the exercise database.");
|
||||
} else {
|
||||
log("%i entries currently registered in the exercise database:\n\n", db->entries.length);
|
||||
for (EachIn(db->entries, i)) {
|
||||
log("#%i: %S\n", i + 1, db->entries.data[i].name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,6 +353,8 @@ int gymTrackerDo(Arena *arena, list<string> args) {
|
||||
|
||||
int gymTrackerAddExercise(Arena *arena, list<string> args) {
|
||||
int statusCode = 0;
|
||||
GymLogDbParsed *db = parseDb(arena, readEntireFile(arena, DB_FILE_LOCATION));
|
||||
|
||||
string newExerciseName = args.data[0];
|
||||
if (newExerciseName.length == 0) {
|
||||
log("No exercise name provided.\n");
|
||||
@@ -329,6 +434,10 @@ int main(int argc, char **argv) {
|
||||
statusCode = gymTrackerStatus(arena, listSlice(args, 1));
|
||||
} else if (strEql(args.data[0], "do"_s)) {
|
||||
statusCode = gymTrackerDo(arena, listSlice(args, 1));
|
||||
} else if (strEql(args.data[0], "delete"_s)) {
|
||||
statusCode = gymTrackerDeleteEntries(arena, listSlice(args, 1));
|
||||
} else if (strEql(args.data[0], "list"_s)) {
|
||||
statusCode = gymTrackerListExercises(arena, listSlice(args, 1));
|
||||
} else if (strEql(args.data[0], "add"_s)) {
|
||||
statusCode = gymTrackerAddExercise(arena, listSlice(args, 1));
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user