Debian Codegraph @

src/plugins/schedulers/tj/taskjuggler/Resource.cpp

raw
1 /*
2 * Resource.cpp - TaskJuggler
3 *
4 * SPDX-FileCopyrightText: 2001, 2002, 2003, 2004, 2005 Chris Schlaeger <[email protected]>
5 * SPDX-FileCopyrightText: 2011 Dag Andersen <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * $Id$
12 */
13
14 // clazy:excludeall=qstring-arg
15 #include "Resource.h"
16
17 #include <KLocalizedString>
18
19 #include <assert.h>
20
21 #include "ResourceTreeIterator.h"
22
23 #include "Project.h"
24 #include "ShiftSelection.h"
25 #include "BookingList.h"
26 // #include "Account.h"
27 #include "UsageLimits.h"
28 #include "TjMessageHandler.h"
29 #include "tjlib-internal.h"
30 // #include "ReportXML.h"
31 #include "CustomAttributeDefinition.h"
32
33 namespace TJ
34 {
35
36 /*
37 * Calls to sbIndex are fairly expensive due to the floating point
38 * division. We therefor use buffers that stores the index of the
39 * first/last slot of a day/week/month for each slot.
40 */
41 static uint* DayStartIndex = nullptr;
42 static uint* WeekStartIndex = nullptr;
43 static uint* MonthStartIndex = nullptr;
44 static uint* DayEndIndex = nullptr;
45 static uint* WeekEndIndex = nullptr;
46 static uint* MonthEndIndex = nullptr;
47
48
49 Resource::Resource(Project* p, const QString& i, const QString& n,
50 Resource* pr, const QString& df, uint dl) :
51 CoreAttributes(p, i, n, pr, df, dl),
52 minEffort(0.0),
53 // journal(),
54 limits(nullptr),
55 efficiency(0.0),
56 rate(0.0),
57 workingHours(),
58 shifts(),
59 vacations(),
60 scoreboard(nullptr),
61 sbSize((p->getEnd() + 1 - p->getStart()) / p->getScheduleGranularity() + 1),
62 specifiedBookings(new SbBooking**[p->getMaxScenarios()]),
63 scoreboards(new SbBooking**[p->getMaxScenarios()]),
64 scenarios(new ResourceScenario[p->getMaxScenarios()]),
65 allocationProbability(new double[p->getMaxScenarios()])
66 {
67 // vacations.setAutoDelete(true);
68 // shifts.setAutoDelete(true);
69
70 p->addResource(this);
71
72 for (int sc = 0; sc < p->getMaxScenarios(); sc++)
73 {
74 scoreboards[sc] = nullptr;
75 specifiedBookings[sc] = nullptr;
76 }
77
78 for (int i = 0; i < p->getMaxScenarios(); ++i)
79 allocationProbability[i] = 0;
80
81 if (!DayStartIndex)
82 {
83 DayStartIndex = new uint[sbSize];
84 WeekStartIndex = new uint[sbSize];
85 MonthStartIndex = new uint[sbSize];
86 long i = 0;
87 uint dayStart = 0;
88 uint weekStart = 0;
89 uint monthStart = 0;
90 bool weekStartsMonday = project->getWeekStartsMonday();
91 for (time_t ts = p->getStart(); i < (long) sbSize; ts +=
92 p->getScheduleGranularity(), ++i)
93 {
94 if (ts == midnight(ts))
95 dayStart = i;
96 DayStartIndex[i] = dayStart;
97
98 if (ts == beginOfWeek(ts, weekStartsMonday))
99 weekStart = i;
100 WeekStartIndex[i] = weekStart;
101
102 if (ts == beginOfMonth(ts))
103 monthStart = i;
104 MonthStartIndex[i] = monthStart;
105 }
106
107 DayEndIndex = new uint[sbSize];
108 WeekEndIndex = new uint[sbSize];
109 MonthEndIndex = new uint[sbSize];
110 i = sbSize - 1;
111 uint dayEnd = i;
112 uint weekEnd = i;
113 uint monthEnd = i;
114 // WTF does p->getEnd not return the 1st sec after the time frame!!!
115 for (time_t ts = p->getEnd() + 1; i >= 0;
116 ts -= p->getScheduleGranularity(), --i)
117 {
118 DayEndIndex[i] = dayEnd;
119 if (ts - midnight(ts) < (int) p->getScheduleGranularity())
120 dayEnd = i > 0 ? i - 1 : 0;
121
122 WeekEndIndex[i] = weekEnd;
123 if (ts - beginOfWeek(ts, weekStartsMonday) <
124 (int) p->getScheduleGranularity())
125 weekEnd = i > 0 ? i - 1 : 0;
126
127 MonthEndIndex[i] = monthEnd;
128 if (ts - beginOfMonth(ts) < (int) p->getScheduleGranularity())
129 monthEnd = i > 0 ? i - 1 : 0;
130 }
131 }
132
133 for (int i = 0; i < 7; i++)
134 {
135 workingHours[i] = new QList<Interval*>();
136 // workingHours[i]->setAutoDelete(true);
137 }
138 }
139
140 Resource::~Resource()
141 {
142 int i;
143 for (i = 0; i < 7; i++) {
144 while (!workingHours[i]->isEmpty()) delete workingHours[i]->takeFirst();
145 delete workingHours[i];
146 }
147 for (int sc = 0; sc < project->getMaxScenarios(); sc++)
148 {
149 if (scoreboards[sc])
150 {
151 for (uint i = 0; i < sbSize; i++)
152 if (scoreboards[sc][i] >= (SbBooking*) 4)
153 {
154 uint j;
155 for (j = i + 1; j < sbSize &&
156 scoreboards[sc][i] == scoreboards[sc][j]; j++)
157 ;
158 delete scoreboards[sc][i];
159 i = j - 1;
160 }
161 delete [] scoreboards[sc];
162 scoreboards[sc] = nullptr;
163 }
164 if (specifiedBookings[sc])
165 {
166 for (uint i = 0; i < sbSize; i++)
167 if (specifiedBookings[sc][i] >= (SbBooking*) 4)
168 {
169 uint j;
170 for (j = i + 1; j < sbSize &&
171 specifiedBookings[sc][i] == specifiedBookings[sc][j];
172 j++)
173 ;
174 delete specifiedBookings[sc][i];
175 i = j - 1;
176 }
177 delete [] specifiedBookings[sc];
178 specifiedBookings[sc] = nullptr;
179 }
180 }
181 delete [] allocationProbability;
182 delete [] specifiedBookings;
183 delete [] scoreboards;
184 delete [] scenarios;
185 qDeleteAll(shifts);
186
187 delete limits;
188
189 project->deleteResource(this);
190 }
191
192 void
193 Resource::deleteStaticData()
194 {
195 delete [] DayStartIndex;
196 delete [] WeekStartIndex;
197 delete [] MonthStartIndex;
198 delete [] DayEndIndex;
199 delete [] WeekEndIndex;
200 delete [] MonthEndIndex;
201 DayStartIndex = nullptr;
202 WeekStartIndex = nullptr;
203 MonthStartIndex = nullptr;
204 DayEndIndex = nullptr;
205 WeekEndIndex = nullptr;
206 MonthEndIndex = nullptr;
207 }
208
209 void
210 Resource::inheritValues()
211 {
212 Resource* pr = (Resource*) parent;
213
214 if (pr)
215 {
216 // Inherit flags from parent resource.
217 for (QStringList::ConstIterator it = pr->flags.constBegin();
218 it != pr->flags.constEnd(); ++it)
219 addFlag(*it);
220
221 // Inherit default working hours from parent resource.
222 for (int i = 0; i < 7; i++)
223 {
224 while (!workingHours[i]->isEmpty()) delete workingHours[i]->takeFirst();
225 delete workingHours[i];
226 workingHours[i] = new QList<Interval*>();
227 // workingHours[i]->setAutoDelete(true);
228 for (QListIterator<Interval*> ivi(*pr->workingHours[i]); ivi.hasNext();)
229 workingHours[i]->append(new Interval(*(ivi.next())));
230 }
231
232 // Inherit vacation intervals from parent resource.
233 for (QListIterator<Interval*> vli(pr->vacations); vli.hasNext();)
234 vacations.append(new Interval(*(vli.next())));
235
236 minEffort = pr->minEffort;
237
238 if (pr->limits)
239 limits = new UsageLimits(*pr->limits);
240 else
241 limits = nullptr;
242
243 rate = pr->rate;
244 efficiency = pr->efficiency;
245
246 // Inherit inheritable custom attributes
247 inheritCustomAttributes(project->getResourceAttributeDict());
248 }
249 else
250 {
251 // Inherit from default working hours project defaults.
252 for (int i = 0; i < 7; i++)
253 {
254 while (!workingHours[i]->isEmpty()) delete workingHours[i]->takeFirst();
255 delete workingHours[i];
256 workingHours[i] = new QList<Interval*>();
257 // workingHours[i]->setAutoDelete(true);
258 for (QListIterator<Interval*>ivi(project->getWorkingHoursIterator(i)); ivi.hasNext();) {
259 workingHours[i]->append(new Interval(*(ivi.next())));
260 }
261 }
262
263 minEffort = project->getMinEffort();
264
265 if (project->getResourceLimits())
266 limits = new UsageLimits(*project->getResourceLimits());
267 else
268 limits = nullptr;
269
270 // rate = project->getRate();
271 efficiency = 1.0;
272 }
273 }
274
275 void
276 Resource::setLimits(UsageLimits* l)
277 {
278 if (limits)
279 delete limits;
280 limits = l;
281 }
282
283 void
284 Resource::initScoreboard()
285 {
286 scoreboard = new SbBooking*[sbSize];
287
288 // First mark all scoreboard slots as unavailable (1).
289 for (uint i = 0; i < sbSize; i++)
290 scoreboard[i] = (SbBooking*) 1;
291
292 // Then change all worktime slots to 0 (available) again.
293 for (time_t t = project->getStart(); t < project->getEnd() + 1;
294 t += project->getScheduleGranularity())
295 {
296 if (isOnShift(Interval(t, t + project->getScheduleGranularity() - 1))) {
297 scoreboard[sbIndex(t)] = (SbBooking*) nullptr;
298 }
299 }
300 // Then mark all resource specific vacation slots as such (2).
301 for (QListIterator<Interval*> ivi(vacations); ivi.hasNext();) {
302 Interval *i = ivi.next();
303 for (time_t date = i->getStart() > project->getStart() ?
304 i->getStart() : project->getStart();
305 date < i->getEnd() && date < project->getEnd() + 1;
306 date += project->getScheduleGranularity()) {
307 scoreboard[sbIndex(date)] = (SbBooking*) 2;
308 }
309 }
310 // Mark all global vacation slots as such (2)
311 for (VacationList::Iterator ivi(project->getVacationListIterator()); ivi.hasNext();)
312 {
313 Interval *i = ivi.next();
314 if (i->getStart() > project->getEnd() ||
315 i->getEnd() < project->getStart())
316 continue;
317 uint startIdx = sbIndex(i->getStart() >= project->getStart() ?
318 i->getStart() : project->getStart());
319 uint endIdx = sbIndex(i->getEnd() >= project->getStart() ?
320 i->getEnd() : project->getEnd());
321 for (uint idx = startIdx; idx <= endIdx; ++idx)
322 scoreboard[idx] = (SbBooking*) 2;
323 }
324 }
325
326 uint
327 Resource::sbIndex(time_t date) const
328 {
329 if (date < project->getStart()) qDebug()<<"Resource::sbIndex:"<<time2ISO(date)<<time2ISO(project->getStart());
330 assert(date >= project->getStart());
331 if (date > project->getEnd()) qDebug()<<"Resource::sbIndex:"<<time2ISO(date)<<time2ISO(project->getEnd());
332 assert(date <= project->getEnd());
333 // Convert date to corresponding scoreboard index.
334 uint sbIdx = (date - project->getStart()) /
335 project->getScheduleGranularity();
336 assert(sbIdx < sbSize);
337 return sbIdx;
338 }
339
340 time_t
341 Resource::index2start(uint idx) const
342 {
343 return project->getStart() + idx *
344 project->getScheduleGranularity();
345 }
346
347 time_t
348 Resource::index2end(uint idx) const
349 {
350 return project->getStart() + (idx + 1) *
351 project->getScheduleGranularity() - 1;
352 }
353
354 /**
355 * \retval 0 { resource is available }
356 * \retval 1 { resource is unavailable }
357 * \retval 2 { resource is on vacation }
358 * \retval 3 { undefined }
359 * \retval 4 { resource is allocated to a task }
360 */
361 int
362 Resource::isAvailable(time_t date)
363 {
364 /* The scoreboard of a resource is only generated on demand, so that large
365 * resource lists that are only scarcely used for the project do not slow
366 * TJ down too much. */
367 if (!scoreboard)
368 initScoreboard();
369 // Check if the interval is booked or blocked already.
370 uint sbIdx = sbIndex(date);
371 if (scoreboard[sbIdx])
372 {
373 QString reason;
374 if (scoreboard[sbIdx] == ((SbBooking*) 1)) {
375 reason = "off-hour";
376 } else if (scoreboard[sbIdx] == ((SbBooking*) 2)) {
377 reason = "vacation";
378 } else if (scoreboard[sbIdx] == ((SbBooking*) 3)) {
379 reason = "UNDEFINED";
380 } else {
381 reason = "allocated to " + scoreboard[sbIdx]->getTask()->getName();
382 }
383 if (DEBUGRS(6)) {
384 qDebug()<<QString(" Resource %1 is busy (%2) at: %3").arg(name).arg(reason).arg(time2ISO(date));
385 }
386 return scoreboard[sbIdx] < ((SbBooking*) 4) ? 1 : 4;
387 }
388
389 if (!limits) {
390 // TJMH.debugMessage(QString("Resource is available today (%1) ").arg(time2ISO(date)), this);
391 return 0;
392 }
393 if (limits && limits->getDailyUnits() > 0) {
394 int bookedSlots = 1;
395 int workSlots = 0;
396 for (uint i = DayStartIndex[sbIdx]; i <= DayEndIndex[sbIdx]; i++) {
397 SbBooking* b = scoreboard[i];
398 if (b == (SbBooking*) nullptr) {
399 ++workSlots;
400 } else if (b >= (SbBooking*) 4) {
401 ++workSlots;
402 ++bookedSlots;
403 }
404 }
405 if (workSlots > 0) {
406 workSlots = (workSlots * limits->getDailyUnits()) / 100;
407 if (workSlots == 0) {
408 workSlots = 1;
409 }
410 }
411 if (bookedSlots > workSlots) {
412 if (DEBUGRS(2)) {
413 qDebug()<<"Resource is overloaded:"<<name<<"units="<<limits->getDailyUnits()<<"work="<<workSlots<<"booked="<<bookedSlots;
414 }
415 // TJMH.debugMessage(QString("Resource is overloaded today: %1 (%2 slots)").arg(time2ISO(date)).arg(bookedSlots), this);
416 return 2; //TODO review
417 }
418 }
419 else if ((limits && limits->getDailyMax() > 0))
420 {
421 // Now check that the resource is not overloaded on this day.
422 uint bookedSlots = 1;
423
424 for (uint i = DayStartIndex[sbIdx]; i <= DayEndIndex[sbIdx]; i++)
425 {
426 SbBooking* b = scoreboard[i];
427 if (b < (SbBooking*) 4)
428 continue;
429
430 bookedSlots++;
431 }
432
433 if (limits && limits->getDailyMax() > 0 &&
434 bookedSlots > limits->getDailyMax())
435 {
436 if (DEBUGRS(6))
437 qDebug()<<QString(" Resource %1 overloaded today (%2)").arg(name).arg(bookedSlots);
438
439 // TJMH.debugMessage(QString("Resource is overloaded today: %1 (%2 slots)").arg(time2ISO(date)).arg(bookedSlots), this);
440 return 2;
441 }
442 }
443 if ((limits && limits->getWeeklyMax() > 0))
444 {
445 // Now check that the resource is not overloaded on this week.
446 uint bookedSlots = 1;
447
448 for (uint i = WeekStartIndex[sbIdx]; i <= WeekEndIndex[sbIdx]; i++)
449 {
450 SbBooking* b = scoreboard[i];
451 if (b < (SbBooking*) 4)
452 continue;
453
454 bookedSlots++;
455 }
456
457 if (limits && limits->getWeeklyMax() > 0 &&
458 bookedSlots > limits->getWeeklyMax())
459 {
460 if (DEBUGRS(6))
461 qDebug()<<QString(" Resource %1 overloaded this week (%2)").arg(name).arg(bookedSlots);
462 return 2;
463 }
464 }
465 if ((limits && limits->getMonthlyMax() > 0))
466 {
467 // Now check that the resource is not overloaded on this month.
468 uint bookedSlots = 1;
469
470 for (uint i = MonthStartIndex[sbIdx]; i <= MonthEndIndex[sbIdx]; i++)
471 {
472 SbBooking* b = scoreboard[i];
473 if (b < (SbBooking*) 4)
474 continue;
475
476 bookedSlots++;
477 }
478
479 if (limits && limits->getMonthlyMax() > 0 &&
480 bookedSlots > limits->getMonthlyMax())
481 {
482 if (DEBUGRS(6))
483 qDebug()<<QString(" Resource %1 overloaded this month (%2)").arg(name).arg(bookedSlots);
484 return 2;
485 }
486 }
487 return 0;
488 }
489
490 bool
491 Resource::book(Booking* nb)
492 {
493 uint idx = sbIndex(nb->getStart());
494
495 return bookSlot(idx, nb);
496 }
497
498 bool
499 Resource::bookSlot(uint idx, SbBooking* nb)
500 {
501 // Make sure that the time slot is still available.
502 if (scoreboard[idx] > (SbBooking*) nullptr)
503 {
504 delete nb;
505 return false;
506 }
507
508 SbBooking* b;
509 // Try to merge the booking with the booking in the previous slot.
510 if (idx > 0 && (b = scoreboard[idx - 1]) >= (SbBooking*) 4 &&
511 b->getTask() == nb->getTask())
512 {
513 scoreboard[idx] = b;
514 delete nb;
515 return true;
516 }
517 // Try to merge the booking with the booking in the following slot.
518 if (idx < sbSize - 1 && (b = scoreboard[idx + 1]) >= (SbBooking*) 4 &&
519 b->getTask() == nb->getTask())
520 {
521 scoreboard[idx] = b;
522 delete nb;
523 return true;
524 }
525 scoreboard[idx] = nb;
526 return true;
527 }
528
529 bool
530 Resource::bookInterval(int scIndex, const Interval &interval, uint reason)
531 {
532 if (reason >= 4) {
533 return false;
534 }
535 if (interval.getStart() > project->getEnd()) {
536 return false;
537 }
538 if (interval.getEnd() <= project->getStart()) {
539 return false;
540 }
541 if (!scoreboard) {
542 initScoreboard();
543 scoreboards[scIndex] = scoreboard;
544 }
545 // Limit to part of interval that overlaps project
546 uint idxStart = sbIndex(std::max(interval.getStart(), project->getStart()));
547 uint idxEnd = sbIndex(std::min(interval.getEnd(), project->getEnd()));
548 for (uint idx = idxStart; idx <= idxEnd; ++idx) {
549 SbBooking *b = scoreboard[idx];
550 if (b >= (SbBooking*) 4) {
551 if (scoreboard[idx-1] != b && scoreboard[idx+1] != b) {
552 // not merged
553 delete b;
554 }
555 }
556 scoreboard[idx] = (SbBooking*) reason;
557 }
558 return true;
559 }
560
561 //bool
562 //Resource::bookInterval(Booking* nb, int sc, int sloppy, int overtime)
563 //{
564 // uint sIdx = sbIndex(nb->getStart());
565 // uint eIdx = sbIndex(nb->getEnd());
566
567 // bool conflict = false;
568
569 // for (uint i = sIdx; i <= eIdx; i++)
570 // if (scoreboard[i] > (SbBooking*) overtime)
571 // {
572 // uint j;
573 // for (j = i + 1 ; j <= eIdx &&
574 // scoreboard[i] == scoreboard[j]; j++)
575 // ;
576 // if (scoreboard[i] == (SbBooking*) 1)
577 // {
578 // if (sloppy > 0)
579 // {
580 // i = j;
581 // continue;
582 // }
583 // TJMH.errorMessage(xi18nc("@info/plain 1=datetime 2=task name", "Resource is unavailable at %1. It cannot be assigned to task %2.", formatTime(index2start(i)), nb->getTask()->getName()), this);
584 // }
585 // else if (scoreboard[i] == (SbBooking*) 2)
586 // {
587 // if (sloppy > 1)
588 // {
589 // i = j;
590 // continue;
591 // }
592 // TJMH.errorMessage(xi18nc("@info/plain 1=datetime 2=task name", "Resource is on vacation at %1. It cannot be assigned to task %2.", formatTime(index2start(i)), nb->getTask()->getName()), this);
593 // }
594 // else
595 // {
596 // if (sloppy > 2)
597 // {
598 // i = j;
599 // continue;
600 // }
601 // TJMH.errorMessage(xi18nc("@info/plain 1=datetime 2=task name 3=task name", "Allocation conflict at %1. Conflicting tasks are %2 and %3.", formatTime(index2start(i)), scoreboard[i]->getTask()->getName(), nb->getTask()->getName()), this);
602 // }
603
604 // conflict = true;
605 // i = j;
606 // }
607
608 // if (conflict)
609 // return false;
610
611 // for (uint i = sIdx; i <= eIdx; i++)
612 // if (scoreboard[i] <= static_cast<SbBooking*>(1))
613 // bookSlot(i, new SbBooking(*nb), overtime);
614
615 // return true;
616 //}
617
618 //bool
619 //Resource::addBooking(int sc, Booking* nb, int sloppy, int overtime)
620 //{
621 // SbBooking** tmp = scoreboard;
622
623 // if (scoreboards[sc])
624 // scoreboard = scoreboards[sc];
625 // else
626 // initScoreboard();
627 // bool retVal = bookInterval(nb, sc, sloppy, overtime);
628 // // Cross register booking with task.
629 // if (retVal && nb->getTask())
630 // nb->getTask()->addBookedResource(sc, this);
631 // delete nb;
632 // scoreboards[sc] = scoreboard;
633 // scoreboard = tmp;
634
635 // return retVal;
636 //}
637
638 bool
639 Resource::addShift(const Interval& i, Shift* s)
640 {
641 return shifts.insert(new ShiftSelection(i, s));
642 }
643
644 bool
645 Resource::addShift(ShiftSelection* s)
646 {
647 return shifts.insert(s);
648 }
649
650 void
651 Resource::addVacation(Interval* i)
652 {
653 vacations.append(i);
654 }
655
656 bool
657 Resource::isWorker() const
658 {
659 for (ConstResourceTreeIterator rti(this); *rti; ++rti)
660 if ((*rti)->efficiency == 0.0)
661 return false;
662
663 return true;
664 }
665
666 double
667 Resource::getCurrentLoad(const Interval& period, const Task* task) const
668 {
669 Interval iv(period);
670 if (!iv.overlap(Interval(project->getStart(), project->getEnd())))
671 return 0.0;
672
673 return efficiency * project->convertToDailyLoad
674 (getCurrentLoadSub(sbIndex(iv.getStart()), sbIndex(iv.getEnd()), task) *
675 project->getScheduleGranularity());
676 }
677
678 long
679 Resource::getCurrentLoadSub(uint startIdx, uint endIdx, const Task* task) const
680 {
681 long bookings = 0;
682
683 for (ResourceListIterator rli(*sub); rli.hasNext();) {
684 Resource *r = static_cast<Resource*>(rli.next());
685 bookings += r->getCurrentLoadSub(startIdx, endIdx, task);
686 }
687 if (!scoreboard)
688 return bookings;
689
690 for (uint i = startIdx; i <= endIdx && i < sbSize; i++)
691 {
692 SbBooking* b = scoreboard[i];
693 if (b < (SbBooking*) 4)
694 continue;
695 if (!task || task == b->getTask() || b->getTask()->isDescendantOf(task))
696 bookings++;
697 }
698
699 return bookings;
700 }
701
702 uint
703 Resource::getWorkSlots(time_t date) const
704 {
705 if (!scoreboard) {
706 return 0;
707 }
708 uint workSlots = 0;
709 uint sbIdx = sbIndex(date);
710 for (uint i = DayStartIndex[sbIdx]; i <= DayEndIndex[sbIdx]; i++) {
711 SbBooking* b = scoreboard[i];
712 if (b == (SbBooking*) nullptr || b >= (SbBooking*) 4) {
713 ++workSlots;
714 }
715 }
716 return workSlots;
717 }
718
719 uint
720 Resource::getCurrentDaySlots(time_t date, const Task* t)
721 {
722 /* Return the number of slots this resource is allocated to in the current
723 * scenario. If a task is given, only the slots allocated to this task
724 * will be counted. */
725
726 if (hasSubs())
727 {
728 uint timeSlots = 0;
729 for (ResourceListIterator rli(getSubListIterator()); rli.hasNext();) {
730 Resource *r = static_cast<Resource*>(rli.next());
731 timeSlots += r->getCurrentDaySlots(date, t);
732 }
733 return timeSlots;
734 }
735
736 if (!scoreboard)
737 return 0;
738
739 uint sbIdx = sbIndex(date);
740
741 uint bookedSlots = 0;
742
743 for (uint i = DayStartIndex[sbIdx]; i <= DayEndIndex[sbIdx]; i++)
744 {
745 SbBooking* b = scoreboard[i];
746 if (b < (SbBooking*) 4)
747 continue;
748
749 if (!t || b->getTask() == t || b->getTask()->isDescendantOf(t))
750 bookedSlots++;
751 }
752
753 return bookedSlots;
754 }
755
756 uint
757 Resource::getCurrentWeekSlots(time_t date, const Task* t)
758 {
759 /* Return the number of slots this resource is allocated to in the current
760 * scenario. If a task is given, only the slots allocated to this task
761 * will be counted. */
762
763 if (hasSubs())
764 {
765 uint timeSlots = 0;
766 for (ResourceListIterator rli(getSubListIterator()); rli.hasNext();) {
767 timeSlots += (*rli)->getCurrentWeekSlots(date, t);
768 }
769 return timeSlots;
770 }
771
772 if (!scoreboard)
773 return 0;
774
775 uint sbIdx = sbIndex(date);
776
777 uint bookedSlots = 0;
778
779 for (uint i = WeekStartIndex[sbIdx]; i <= WeekEndIndex[sbIdx]; i++)
780 {
781 SbBooking* b = scoreboard[i];
782 if (b < (SbBooking*) 4)
783 continue;
784
785 if (!t || b->getTask() == t || b->getTask()->isDescendantOf(t))
786 bookedSlots++;
787 }
788
789 return bookedSlots;
790 }
791
792 uint
793 Resource::getCurrentMonthSlots(time_t date, const Task* t)
794 {
795 /* Return the number of slots this resource is allocated to in the current
796 * scenario. If a task is given, only the slots allocated to this task
797 * will be counted. */
798
799 if (hasSubs())
800 {
801 uint timeSlots = 0;
802 for (ResourceListIterator rli(getSubListIterator()); rli.hasNext();) {
803 timeSlots += (*rli)->getCurrentMonthSlots(date, t);
804 }
805 return timeSlots;
806 }
807
808 if (!scoreboard)
809 return 0;
810
811 uint sbIdx = sbIndex(date);
812
813 uint bookedSlots = 0;
814
815 for (uint i = MonthStartIndex[sbIdx]; i <= MonthEndIndex[sbIdx]; i++)
816 {
817 SbBooking* b = scoreboard[i];
818 if (b < (SbBooking*) 4)
819 continue;
820
821 if (!t || b->getTask() == t || b->getTask()->isDescendantOf(t))
822 bookedSlots++;
823 }
824
825 return bookedSlots;
826 }
827
828 double
829 Resource::getEffectiveLoad(int sc, const Interval& period, AccountType acctType,
830 const Task* task) const
831 {
832 double load = 0.0;
833 Interval iv(period);
834 if (!iv.overlap(Interval(project->getStart(), project->getEnd())))
835 return 0.0;
836
837 if (hasSubs())
838 {
839 for (ResourceListIterator rli(*sub); rli.hasNext();) {
840 Resource *r = static_cast<Resource*>(rli.next());
841 load += r->getEffectiveLoad(sc, iv, acctType, task);
842 }
843 }
844 else
845 {
846 uint startIdx = sbIndex(iv.getStart());
847 uint endIdx = sbIndex(iv.getEnd());
848 load = project->convertToDailyLoad
849 (getAllocatedSlots(sc, startIdx, endIdx, acctType, task) *
850 project->getScheduleGranularity()) * efficiency;
851 }
852
853 return load;
854 }
855
856 double
857 Resource::getAllocatedTimeLoad(int sc, const Interval& period,
858 AccountType acctType, const Task* task) const
859 {
860 return project->convertToDailyLoad
861 (getAllocatedTime(sc, period, acctType, task));
862 }
863
864 long
865 Resource::getAllocatedTime(int sc, const Interval& period, AccountType acctType,
866 const Task* task) const
867 {
868 Interval iv(period);
869 if (!iv.overlap(Interval(project->getStart(), project->getEnd())))
870 return 0;
871 uint startIdx = sbIndex(iv.getStart());
872 uint endIdx = sbIndex(iv.getEnd());
873 if (scenarios[sc].firstSlot > 0 && scenarios[sc].lastSlot > 0)
874 {
875 if (startIdx < (uint) scenarios[sc].firstSlot)
876 startIdx = scenarios[sc].firstSlot;
877 if (endIdx > (uint) scenarios[sc].lastSlot)
878 endIdx = scenarios[sc].lastSlot;
879 }
880
881 return getAllocatedSlots(sc, startIdx, endIdx, acctType, task) *
882 project->getScheduleGranularity();
883 }
884
885 long
886 Resource::getAllocatedSlots(int sc, uint startIdx, uint endIdx,
887 AccountType acctType, const Task* task) const
888 {
889 long bookings = 0;
890
891 if (isGroup())
892 {
893 for (ResourceListIterator rli(*sub); rli.hasNext();) {
894 Resource *r = static_cast<Resource*>(rli.next());
895 bookings += r->getAllocatedSlots(sc, startIdx, endIdx,
896 acctType, task);
897 }
898 return bookings;
899 }
900
901 // If the scoreboard has not been initialized there is no load.
902 if (!scoreboards[sc])
903 return bookings;
904
905 if (scenarios[sc].firstSlot > 0 && scenarios[sc].lastSlot > 0)
906 {
907 if (task)
908 {
909 /* If the load is to be calculated for a certain task, we check
910 * whether this task is in the resource allocation list. Only then
911 * we calculate the real number of allocated slots. */
912 bool isAllocated = false;
913 for (TaskListIterator tli(scenarios[sc].allocatedTasks); tli.hasNext();) {
914 Task *t = static_cast<Task*>(tli.next());
915 if (task == t || t->isDescendantOf(task))
916 {
917 isAllocated = true;
918 break;
919 }
920 }
921 if (!isAllocated)
922 return bookings;
923 }
924
925 if (startIdx < (uint) scenarios[sc].firstSlot)
926 startIdx = scenarios[sc].firstSlot;
927 if (endIdx > (uint) scenarios[sc].lastSlot)
928 endIdx = scenarios[sc].lastSlot;
929 }
930 for (uint i = startIdx; i <= endIdx && i < sbSize; i++)
931 {
932 SbBooking* b = scoreboards[sc][i];
933 if (b < (SbBooking*) 4)
934 continue;
935 if ((task == nullptr ||
936 (task != nullptr && (task == b->getTask() ||
937 b->getTask()->isDescendantOf(task))))/* &&
938 (acctType == AllAccounts ||
939 (b->getTask()->getAccount() && b->getTask()->getAccount()->getAcctType() == acctType))*/)
940 bookings++;
941 }
942
943 return bookings;
944 }
945
946 double
947 Resource::getEffectiveFreeLoad(int sc, const Interval& period)
948 {
949 double load = 0.0;
950 Interval iv(period);
951 if (!iv.overlap(Interval(project->getStart(), project->getEnd())))
952 return 0.0;
953
954 if (hasSubs())
955 {
956 for (ResourceListIterator rli(*sub); *rli != nullptr; ++rli)
957 load += (*rli)->getEffectiveFreeLoad(sc, iv);
958 }
959 else
960 {
961 uint startIdx = sbIndex(iv.getStart());
962 uint endIdx = sbIndex(iv.getEnd());
963 load = project->convertToDailyLoad
964 (getAvailableSlots(sc, startIdx, endIdx) *
965 project->getScheduleGranularity()) * efficiency;
966 }
967
968 return load;
969 }
970
971 double
972 Resource::getAvailableTimeLoad(int sc, const Interval& period)
973 {
974 return project->convertToDailyLoad(getAvailableTime(sc, period));
975 }
976
977 long
978 Resource::getAvailableTime(int sc, const Interval& period)
979 {
980 Interval iv(period);
981 if (!iv.overlap(Interval(project->getStart(), project->getEnd())))
982 return 0;
983
984 return getAvailableSlots(sc, sbIndex(iv.getStart()),
985 sbIndex(iv.getEnd())) *
986 project->getScheduleGranularity();
987 }
988
989 long
990 Resource::getAvailableSlots(int sc, uint startIdx, uint endIdx)
991 {
992 long availSlots = 0;
993
994 if (!sub->isEmpty())
995 {
996 for (ResourceListIterator rli(*sub); *rli != nullptr; ++rli)
997 availSlots += (*rli)->getAvailableSlots(sc, startIdx, endIdx);
998 }
999 else
1000 {
1001 if (!scoreboards[sc])
1002 {
1003 scoreboard = scoreboards[sc];
1004 initScoreboard();
1005 scoreboards[sc] = scoreboard;
1006 }
1007
1008 for (uint i = startIdx; i <= endIdx; i++)
1009 if (scoreboards[sc][i] == nullptr)
1010 availSlots++;
1011 }
1012
1013 return availSlots;
1014 }
1015
1016 double
1017 Resource::getCredits(int sc, const Interval& period, AccountType acctType,
1018 const Task* task) const
1019 {
1020 return project->convertToDailyLoad
1021 (getAllocatedTime(sc, period, acctType, task)) * rate;
1022 }
1023
1024 bool
1025 Resource::isAllocated(int sc, const Interval& period, const QString& prjId)
1026 const
1027 {
1028 Interval iv(period);
1029 if (!iv.overlap(Interval(project->getStart(), project->getEnd())))
1030 return false;
1031
1032 uint startIdx = sbIndex(iv.getStart());
1033 uint endIdx = sbIndex(iv.getEnd());
1034 if (scenarios[sc].firstSlot > 0 && scenarios[sc].lastSlot > 0)
1035 {
1036 if (startIdx < (uint) scenarios[sc].firstSlot)
1037 startIdx = scenarios[sc].firstSlot;
1038 if (endIdx > (uint) scenarios[sc].lastSlot)
1039 endIdx = scenarios[sc].lastSlot;
1040 }
1041
1042 if (endIdx < startIdx)
1043 return false;
1044
1045 return isAllocatedSub(sc, startIdx, endIdx, prjId);
1046 }
1047
1048 bool
1049 Resource::isAllocatedSub(int sc, uint startIdx, uint endIdx, const QString&
1050 prjId) const
1051 {
1052 /* If resource is a group, check members first. */
1053 for (ResourceListIterator rli(*sub); *rli != nullptr; ++rli)
1054 if ((*rli)->isAllocatedSub(sc, startIdx, endIdx, prjId))
1055 return true;
1056
1057 if (!scoreboards[sc])
1058 return false;
1059 for (uint i = startIdx; i <= endIdx; i++)
1060 {
1061 SbBooking* b = scoreboards[sc][i];
1062 if (b < (SbBooking*) 4)
1063 continue;
1064 if (prjId.isNull() || b->getTask()->getProjectId() == prjId)
1065 return true;
1066 }
1067 return false;
1068 }
1069
1070 bool
1071 Resource::isAllocated(int sc, const Interval& period, const Task* task) const
1072 {
1073 Interval iv(period);
1074 if (!iv.overlap(Interval(project->getStart(), project->getEnd())))
1075 return false;
1076
1077 uint startIdx = sbIndex(iv.getStart());
1078 uint endIdx = sbIndex(iv.getEnd());
1079 if (scenarios[sc].firstSlot > 0 && scenarios[sc].lastSlot > 0)
1080 {
1081 if (startIdx < (uint) scenarios[sc].firstSlot)
1082 startIdx = scenarios[sc].firstSlot;
1083 if (endIdx > (uint) scenarios[sc].lastSlot)
1084 endIdx = scenarios[sc].lastSlot;
1085 }
1086 if (endIdx < startIdx)
1087 return false;
1088
1089 return isAllocatedSub(sc, startIdx, endIdx, task);
1090 }
1091
1092 bool
1093 Resource::isAllocatedSub(int sc, uint startIdx, uint endIdx, const Task* task)
1094 const
1095 {
1096 /* If resource is a group, check members first. */
1097 for (ResourceListIterator rli(*sub); *rli != nullptr; ++rli)
1098 if ((*rli)->isAllocatedSub(sc, startIdx, endIdx, task))
1099 return true;
1100
1101 if (!scoreboards[sc])
1102 return false;
1103 for (uint i = startIdx; i <= endIdx; i++)
1104 {
1105 SbBooking* b = scoreboards[sc][i];
1106 if (b < (SbBooking*) 4)
1107 continue;
1108 if (!task || b->getTask() == task || b->getTask()->isDescendantOf(task))
1109 return true;
1110 }
1111 return false;
1112 }
1113
1114 void
1115 Resource::getPIDs(int sc, const Interval& period, const Task* task,
1116 QStringList& pids) const
1117 {
1118 Interval iv(period);
1119 if (!iv.overlap(Interval(project->getStart(), project->getEnd())))
1120 return;
1121
1122 for (ResourceListIterator rli(*sub); *rli != nullptr; ++rli)
1123 (*rli)->getPIDs(sc, iv, task, pids);
1124
1125 if (!scoreboards[sc])
1126 return;
1127 for (uint i = sbIndex(iv.getStart());
1128 i <= sbIndex(iv.getEnd()) && i < sbSize; i++)
1129 {
1130 SbBooking* b = scoreboards[sc][i];
1131 if (b < (SbBooking*) 4)
1132 continue;
1133 if ((!task || task == b->getTask() ||
1134 b->getTask()->isDescendantOf(task)) &&
1135 pids.indexOf(b->getTask()->getProjectId()) == -1)
1136 {
1137 pids.append(b->getTask()->getProjectId());
1138 }
1139 }
1140 }
1141
1142 QString
1143 Resource::getProjectIDs(int sc, const Interval& period, const Task* task) const
1144 {
1145 QStringList pids;
1146 getPIDs(sc, period, task, pids);
1147 QString pidStr;
1148 for (QStringList::ConstIterator it = pids.constBegin(); it != pids.constEnd(); ++it)
1149 pidStr += QString(it != pids.constBegin() ? ", " : "") + *it;
1150
1151 return pidStr;
1152 }
1153
1154 bool
1155 Resource::hasVacationDay(time_t day) const
1156 {
1157 Interval fullDay(midnight(day),
1158 sameTimeNextDay(midnight(day)) - 1);
1159 for (QListIterator<Interval*> vli(vacations); vli.hasNext();) {
1160 if (vli.next()->overlaps(fullDay))
1161 return true;
1162 }
1163
1164 if (shifts.isVacationDay(day))
1165 return true;
1166
1167 if (workingHours[dayOfWeek(day, false)]->isEmpty())
1168 return true;
1169
1170 return false;
1171 }
1172
1173 bool
1174 Resource::isOnShift(const Interval& slot) const
1175 {
1176 for (ShiftSelectionList::Iterator ssli(shifts); ssli.hasNext();) {
1177 ShiftSelection *s = ssli.next();
1178 if (s->getPeriod().contains(slot))
1179 return s->getShift()->isOnShift(slot);
1180 }
1181 int dow = dayOfWeek(slot.getStart(), false);
1182 for (QListIterator<Interval*> ivi(*workingHours[dow]); ivi.hasNext();) {
1183 if (ivi.next()->contains(Interval(secondsOfDay(slot.getStart()), secondsOfDay(slot.getEnd())))) {
1184 return true;
1185 }
1186 }
1187 return false;
1188 }
1189
1190 void
1191 Resource::setWorkingHours(int day, const QList<Interval*>& l)
1192 {
1193 while (!workingHours[day]->isEmpty()) delete workingHours[day]->takeFirst();
1194 delete workingHours[day];
1195
1196 // Create a deep copy of the interval list.
1197 workingHours[day] = new QList<Interval*>;
1198 // workingHours[day]->setAutoDelete(true);
1199 for (QListIterator<Interval*> pli(l); pli.hasNext();)
1200 workingHours[day]->append(new Interval(*(pli.next())));
1201 }
1202
1203 BookingList
1204 Resource::getJobs(int sc) const
1205 {
1206 BookingList bl;
1207 if (scoreboards[sc])
1208 {
1209 SbBooking* b = nullptr;
1210 uint startIdx = 0;
1211 for (uint i = 0; i < sbSize; i++)
1212 if (scoreboards[sc][i] != b)
1213 {
1214 if (b)
1215 bl.append(new Booking(Interval(index2start(startIdx),
1216 index2end(i - 1)),
1217 scoreboards[sc][startIdx]));
1218 if (scoreboards[sc][i] >= (SbBooking*) 4)
1219 {
1220 b = scoreboards[sc][i];
1221 startIdx = i;
1222 }
1223 else
1224 b = nullptr;
1225 }
1226 }
1227 return bl;
1228 }
1229
1230 QVector<Interval> Resource::getBookedIntervals(int sc, const TJ::Task* task) const
1231 {
1232 QVector<Interval> lst;
1233 if (scoreboards[sc] == nullptr)
1234 return lst;
1235 for (uint i = 0; i < sbSize; ++i)
1236 {
1237 if (scoreboards[sc][i] > ((SbBooking*) 3) && scoreboards[sc][i]->getTask() == task) {
1238 time_t s = index2start(i);
1239 time_t e = index2end(i);
1240 Interval ti(s, e);
1241 if (!lst.isEmpty() && lst.last().append(ti)) {
1242 continue;
1243 }
1244 lst << ti;
1245 }
1246 }
1247 return lst;
1248 }
1249
1250 time_t
1251 Resource::getStartOfFirstSlot(int sc, const Task* task)
1252 {
1253 if (scoreboards[sc] == nullptr)
1254 return 0;
1255 for (uint i = 0; i < sbSize; ++i)
1256 {
1257 if (scoreboards[sc][i] > ((SbBooking*) 3) &&
1258 scoreboards[sc][i]->getTask() == task)
1259 return index2start(i);
1260 }
1261
1262 return 0;
1263 }
1264
1265 time_t
1266 Resource::getEndOfLastSlot(int sc, const Task* task)
1267 {
1268 if (scoreboards[sc] == nullptr)
1269 return 0;
1270 int i = sbSize;
1271 for (; ;)
1272 {
1273 --i;
1274 if (scoreboards[sc][i] > ((SbBooking*) 3) &&
1275 scoreboards[sc][i]->getTask() == task)
1276 return index2end(i);
1277 if (i == 0)
1278 break;
1279 }
1280
1281 return 0;
1282 }
1283
1284 void
1285 Resource::copyBookings(int sc, SbBooking*** src, SbBooking*** dst)
1286 {
1287 /* This function copies a set of bookings the specified scenario. If the
1288 * destination set already contains bookings it is cleared first.
1289 */
1290 if (dst[sc])
1291 for (uint i = 0; i < sbSize; i++)
1292 if (dst[sc][i] >= (SbBooking*) 4)
1293 {
1294 /* Small pointers are fake bookings. We can safely ignore
1295 * them. Identical pointers in successive slots must only be
1296 * deleted once. */
1297 uint j;
1298 for (j = i + 1; j < sbSize &&
1299 dst[sc][i] == dst[sc][j]; j++)
1300 ;
1301 delete dst[sc][i];
1302 i = j - 1;
1303 }
1304
1305 // Now copy the source set to the destination.
1306 if (src[sc])
1307 {
1308 if (!dst[sc])
1309 dst[sc] = new SbBooking*[sbSize];
1310 for (uint i = 0; i < sbSize; i++)
1311 if (src[sc][i] >= (SbBooking*) 4)
1312 {
1313 /* Small pointers can just be copied. Identical successive
1314 * pointers need to be allocated once and can then be assigned
1315 * to all destination slots. */
1316 dst[sc][i] = new SbBooking(src[sc][i]);
1317 uint j;
1318 for (j = i + 1; j < sbSize &&
1319 src[sc][i] == src[sc][j]; j++)
1320 dst[sc][j] = dst[sc][i];
1321 i = j - 1;
1322 }
1323 else {
1324 dst[sc][i] = src[sc][i];
1325 }
1326 }
1327 else
1328 {
1329 delete [] dst[sc];
1330 dst[sc] = nullptr;
1331 }
1332 }
1333
1334 void
1335 Resource::saveSpecifiedBookings()
1336 {
1337 for (int sc = 0; sc < project->getMaxScenarios(); sc++)
1338 copyBookings(sc, scoreboards, specifiedBookings);
1339 }
1340
1341 void
1342 Resource::prepareScenario(int sc)
1343 {
1344 copyBookings(sc, specifiedBookings, scoreboards);
1345 scoreboard = scoreboards[sc];
1346
1347 updateSlotMarks(sc);
1348 }
1349
1350 void
1351 Resource::finishScenario(int sc)
1352 {
1353 scoreboards[sc] = scoreboard;
1354 updateSlotMarks(sc);
1355 }
1356
1357 bool
1358 Resource::bookingsOk(int sc)
1359 {
1360 if (scoreboards[sc] == nullptr)
1361 return true;
1362
1363 if (hasSubs())
1364 {
1365 TJMH.debugMessage(QString("Group resource may not have bookings"), this);
1366 return false;
1367 }
1368
1369 for (uint i = 0; i < sbSize; ++i)
1370 if (scoreboards[sc][i] >= ((SbBooking*) 4))
1371 {
1372 time_t start = index2start(i);
1373 time_t end = index2end(i);
1374 time_t tStart = scoreboards[sc][i]->getTask()->getStart(sc);
1375 time_t tEnd = scoreboards[sc][i]->getTask()->getEnd(sc);
1376 if (start < tStart || start > tEnd ||
1377 end < tStart || end > tEnd)
1378 {
1379 TJMH.errorMessage(xi18nc("@info/plain 1=task name, 2, 3, 4=datetime", "Booking on task '%1' at %2 is outside of task interval (%3 - %4)", scoreboards[sc][i]->getTask()->getName(),formatTime(start), formatTime(tStart), formatTime(tEnd)), this);
1380 return false;
1381 }
1382 }
1383
1384 return true;
1385 }
1386
1387 // void
1388 // Resource::addJournalEntry(JournalEntry* entry)
1389 // {
1390 // journal.append(entry);
1391 // }
1392 //
1393 // Journal::Iterator
1394 // Resource::getJournalIterator() const
1395 // {
1396 // return Journal::Iterator(journal);
1397 // }
1398
1399 void
1400 Resource::updateSlotMarks(int sc)
1401 {
1402 scenarios[sc].allocatedTasks.clear();
1403 scenarios[sc].firstSlot = -1;
1404 scenarios[sc].lastSlot = -1;
1405
1406 if (scoreboard)
1407 {
1408 for (uint i = 0; i < sbSize; i++)
1409 if (scoreboard[i] > (SbBooking*) 4)
1410 {
1411 if (scenarios[sc].firstSlot == -1)
1412 scenarios[sc].firstSlot = i;
1413 scenarios[sc].lastSlot = i;
1414 scenarios[sc].addTask(scoreboard[i]->getTask());
1415 }
1416 }
1417 }
1418
1419 QDomElement Resource::xmlIDElement(QDomDocument& doc) const
1420 {
1421 Q_UNUSED(doc);
1422 QDomElement elem;/* = ReportXML::createXMLElem(doc, "Resource", getName());
1423 elem.setAttribute("Id", getId());*/
1424
1425 return(elem);
1426 }
1427
1428 } // namespace TJ
1429
1430