planner makes plans now

planner
simon 2024-12-13 17:32:41 +01:00
parent 8629bb0420
commit 568c1dcf7d
4 changed files with 91 additions and 42 deletions

View File

@ -30,6 +30,10 @@ planner: $(PLF)
db: $(DBF) db: $(DBF)
gcc -c $(CFLAGS) $(DBF) gcc -c $(CFLAGS) $(DBF)
edit:
nvim $(PLF) Makefile $(LLST) src/test.c
clean: clean:
rm -rf *.o debugOut src/*.gch rm -rf *.o debugOut src/*.gch

View File

@ -15,6 +15,9 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
const uint intervalLen = 45; // min
const uint pauseLen = 10; //
const char taskFormat[] = const char taskFormat[] =
"Task { %.64s = {\n created={%lu},\n deadline={%lu},\n " "Task { %.64s = {\n created={%lu},\n deadline={%lu},\n "
"priority={%d},\n spare={%lu}\n}\n"; "priority={%d},\n spare={%lu}\n}\n";
@ -35,6 +38,7 @@ void printEvent(Event *s) {
ctime_r(&s->plannedStartTime, st); ctime_r(&s->plannedStartTime, st);
ctime_r(&s->plannedEndTime, e); ctime_r(&s->plannedEndTime, e);
printf(eventFormat, s->task->name, st, e, s->spare); printf(eventFormat, s->task->name, st, e, s->spare);
printTask(s->task);
} }
void llistPrintE(llist *head) { void llistPrintE(llist *head) {
llist *c = head; llist *c = head;
@ -152,7 +156,7 @@ int cmpEvent(const void *a, const void *b) {
return cmpTaskN(aa->task, bb->task); return cmpTaskN(aa->task, bb->task);
} }
llist *genPlan(llist *head) { llist *genPlan(llist *head, time_t timeAvail) {
// map llist to pointer arr & sort by priority // map llist to pointer arr & sort by priority
// second arr sorted bby name // second arr sorted bby name
@ -161,9 +165,9 @@ llist *genPlan(llist *head) {
printf("len: %d\n", lLen); printf("len: %d\n", lLen);
llistPrintT(head); llistPrintT(head);
Task *sortedNames = calloc(lLen, sizeof(Task)); // Task *sortedNames = calloc(lLen, sizeof(Task));
Task *sortedPrio = calloc(lLen, sizeof(Task)); Task *sortedPrio = calloc(lLen, sizeof(Task));
if (sortedNames == NULL || sortedPrio == NULL) { if (/*sortedNames == NULL ||*/ sortedPrio == NULL) {
planLog("gen plan : calloc failed!!\n", true); planLog("gen plan : calloc failed!!\n", true);
return NULL; return NULL;
} }
@ -171,20 +175,22 @@ llist *genPlan(llist *head) {
// add Tasks from llist to arr // add Tasks from llist to arr
llist *c = head; llist *c = head;
for (int i = 0; c != NULL; i++) { for (int i = 0; c != NULL; i++) {
sortedNames[i] = *(Task *)c->data; // sortedNames[i] = *copyTask(c->data);
// sortedPrio[i] = *copyTask(c->data);
// sortedNames[i] = *(Task *)c->data;
sortedPrio[i] = *(Task *)c->data; sortedPrio[i] = *(Task *)c->data;
c = c->next; c = c->next;
} }
// sort // sort
qsort(sortedNames, lLen, sizeof(Task), cmpTaskN); // qsort(sortedNames, lLen, sizeof(Task), cmpTaskN);
qsort(sortedPrio, lLen, sizeof(Task), cmpTaskP); qsort(sortedPrio, lLen, sizeof(Task), cmpTaskP);
// test print // // test print
planLog("sortendName", 0); // planLog("sortendName", 0);
for (int i = 0; i < lLen; i++) { // for (int i = 0; i < lLen; i++) {
printTask(sortedNames + i); // printTask(sortedNames + i);
} // }
planLog("sortendPrio", 0); planLog("sortendPrio", 0);
for (int i = 0; i < lLen; i++) { for (int i = 0; i < lLen; i++) {
@ -193,26 +199,60 @@ llist *genPlan(llist *head) {
planLog("creating eventList", false); planLog("creating eventList", false);
// genertate plan basen on priorities and available time
time_t now = time(NULL); time_t now = time(NULL);
struct tm *lc = localtime(&now); struct tm *lc = localtime(&now);
llist *events_ll = llistNew(NULL, cmpEvent); llist *events_ll = llistNew(NULL, cmpEvent);
for (int i = 0; i < lLen; i++) { // time_t avail = mktime(timeAvail);
time_t start = mktime(lc); time_t start, end;
// (mktime(lc) < mktime(timeAvail))
do {
start = mktime(lc); // start now
lc->tm_min += intervalLen;
end = mktime(lc); //
printf("start:: %s\n", ctime(&start)); Event *c =
lc->tm_min += 45; newEvent(sortedPrio, start, end, 0); // use elem with wighest priority
time_t end = mktime(lc);
printf("end:: %s\n", ctime(&end));
Event *c = newEvent(sortedPrio + i, start, end, 0);
printEvent(c);
lc->tm_min += 15;
llistAppend(events_ll, c); llistAppend(events_ll, c);
} printEvent(c);
llistPrintE(events_ll->next); // decrement priority of first elem and resort list
(*sortedPrio).priority -= 1;
qsort(sortedPrio, lLen, sizeof(Task), cmpTaskP);
lc->tm_min += pauseLen; // add pause
} while (mktime(lc) < timeAvail);
// for (int i = 0; i < lLen; i++) {
// time_t start = mktime(lc);
//
// printf("start:: %s\n", ctime(&start));
// lc->tm_min += 45;
// time_t end = mktime(lc);
// printf("end:: %s\n", ctime(&end));
//
// Event *c = newEvent(sortedPrio + i, start, end, 0);
// printEvent(c);
// lc->tm_min += 15;
// llistAppend(events_ll, c);
// }
//
// free empty head
llist *tmp = events_ll;
events_ll = events_ll->next;
tmp->next = NULL;
llistFreeE(tmp);
llistPrintE(events_ll);
// for (int i = 0; i < lLen; i++) {
// printTask(sortedPrio + i);
// }
planLog("END GEN PLAN", 0); planLog("END GEN PLAN", 0);
llistFreeT(head);
// send updated tasks to db for storage
return events_ll; return events_ll;
} }

View File

@ -81,6 +81,6 @@ int cmpEvent(const void *a, const void *b);
/* /*
* takes llist of tasks and returns llist of events * takes llist of tasks and returns llist of events
*/ */
llist *genPlan(llist *head); llist *genPlan(llist *head, time_t timeAvail);
#endif // !PLANNER #endif // !PLANNER

View File

@ -7,6 +7,7 @@
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
int main() { int main() {
@ -33,29 +34,33 @@ int main() {
llistAppend(list1, t3); llistAppend(list1, t3);
llistAppend(list1, t4); llistAppend(list1, t4);
llist *l1 = genPlan(list1); // gnerate plan from task list in time
struct tm *lc = localtime(&now);
lc->tm_hour += 9; // add available time;
time_t maxTime = mktime(lc); // create timestamp
llist *l1 = genPlan(list1, maxTime); // return inked list of event;
llistFreeE(l1); llistFreeE(l1);
// print test tasks // print test tasks
printTask(t1); // printTask(t1);
printTask(t2);
// find in list & modify // find in list & modify
Task search = {.name = "Phys"}; // key to look for. cmpTask only compares // Task search = {.name = "Analysis"}; // key to look for. cmpTask only
// names using strcmp(a.name, b.name) // compares
llist *found = llistGet(list1, &search); // // names using strcmp(a.name, b.name)
if (found != NULL) { // llist *found = llistGet(list1, &search);
((Task *)found->data)->deadline = time(NULL) + days(10); // if (found != NULL) {
((Task *)found->data)->priority = 9; // ((Task *)found->data)->deadline = time(NULL) + days(10);
printTask(found->data); // // ((Task *)found->data)->priority Names= 9;
} else { // // printTask(found->data);
printf("%s not in List!\n", search.name); // } else {
} // printf("%s not in List!\n", search.name);
// }
char *t1Str = taskToStr(t1); //
printf("%s\n", t1Str); // char *t1Str = taskToStr(t1);
llistFreeT(list1); // // printf("%s\n", t1Str);
free(t1Str); // llistFreeT(list1);
// free(t1Str);
//
return 0; return 0;
} }