-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstim.cc
More file actions
765 lines (625 loc) · 19.2 KB
/
Copy pathstim.cc
File metadata and controls
765 lines (625 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#include "stim.hh"
// -----------------------------------------------------------------------
// TEMPORARY
// -----------------------------------------------------------------------
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <unistd.h>
using std::vector;
// Ensure Stim environment exists and is useful
void EnsureStimEnvironment(const char* szHomeDir, const char* szLogFile, bool bInitialise = false)
{
struct stat sb;
string s;
// check that home directory exists
s = szHomeDir;
if (stat(szHomeDir, &sb) == 0)
{
// check whether it's a directory
if (!S_ISDIR(sb.st_mode))
{
// we can't do anything with this
throw(s + " exists but is not a directory");
}
}
else
{
if (bInitialise)
{
// create directory
printf("Going to try to make the directory '%s'\n", szHomeDir);
if (mkdir(szHomeDir, 0700) != 0)
{
string s = szHomeDir;
throw("mkdir " + s + " failed");
}
}
else
{
throw(s + " does not exist");
}
}
// check that the log file exists
s = szLogFile;
if (stat(szLogFile, &sb) == 0)
{
// check whether it's a readable/writable file
if (access(szLogFile, R_OK | W_OK) != 0)
{
// we can't do anything with this
throw(s + " cannot be read and written");
}
}
else
{
if (bInitialise)
{
// create file
if (mknod(szLogFile, S_IFREG | 0600, 0) != 0)
{
throw(s + " could not be created");
}
}
else
{
throw(s + " does not exist");
}
}
}
vector<string> split(const string& sSplittee, char cSplitter)
{
int iIndex, iLast = 0;
vector<string> vChunks;
// split off chunks until we can split no more
while ((iIndex = sSplittee.find_first_of(cSplitter, iLast))
!= string::npos)
{
vChunks.push_back(sSplittee.substr(iLast, iIndex-iLast));
iLast = iIndex + 1;
}
vChunks.push_back(sSplittee.substr(iLast));
return vChunks;
}
bool ReadNextLine(fstream& fTextFile, string& sLine)
{
// create line buffer and read next line into it
std::stringbuf sLineBuffer;
fTextFile.get(sLineBuffer);
// load string with string contents of line buffer
sLine = sLineBuffer.str();
// current character is either a newline, or EOF
if (fTextFile.peek() != EOF)
{
// advance input stream past newline
fTextFile.seekg(sizeof(char), ios::cur);
}
// return whether there's any more data
return (fTextFile.peek() != EOF);
}
void SecondsToHms(int iSeconds, string& sHms)
{
// break down seconds into hours, minutes, seconds
int iMinutes = iSeconds / 60;
iSeconds %= 60;
int iHours = iMinutes / 60;
iMinutes %= 60;
// build into string of format HH:MM:SS
char szHms[12];
sprintf(szHms, "%02d:%02d:%02d", iHours, iMinutes, iSeconds);
sHms = szHms;
}
// create sortable timestamp from given time
// szDate should be allocated at least 18 characters
void GkMakeTimestamp(time_t aTime, char* szDate)
{
// convert to struct we can examine
struct tm* pTm = localtime(&aTime);
// build time string in the format "20041027 00:26:23"
sprintf(szDate,"%4d%02d%02d %02d:%02d:%02d",
pTm->tm_year + 1900,
pTm->tm_mon + 1,
pTm->tm_mday,
pTm->tm_hour,
pTm->tm_min,
pTm->tm_sec);
}
void GkGrokTimestamp(time_t& aTime, const char* szDate)
{
// break down timestamp
int iYear, iMonth, iDay, iHour, iMinute, iSecond;
sscanf(szDate, "%04d%02d%02d %02d:%02d:%02d",
&iYear,
&iMonth,
&iDay,
&iHour,
&iMinute,
&iSecond);
// convert timestamp into calendar time
struct tm tTm;
tTm.tm_hour = iHour;
tTm.tm_min = iMinute;
tTm.tm_sec = iSecond;
tTm.tm_mday = iDay;
tTm.tm_mon = (iMonth - 1);
tTm.tm_year = (iYear - 1900);
tTm.tm_isdst = -1;
/* struct tm tTm =
{
iSecond, iMinute, iHour,
iDay, (iMonth - 1), (iYear - 1900)
};*/
aTime = mktime(&tTm);
if (aTime < 0)
throw "Stim::GkGrokTimestamp: mktime() returned -1!";
}
// -----------------------------------------------------------------------
// INITIALISATION AND DESTRUCTION
// -----------------------------------------------------------------------
Stim::Stim(const char* szStimName, const char* szContract)
{
m_sStimDir = szStimName;
m_sContract = szContract;
// determine file names
m_sStimLog = m_sStimDir + "/" + m_sContract + ".log";
// basic initialisation
m_bLogReadOnly = true;
Stim::Trace(("Log file: " + m_sStimLog).c_str());
}
Stim::~Stim(void)
{
// destroy self
this->Destroy();
}
void Stim::Initialise(void)
{
// actively ensure the home environent is set up
EnsureStimEnvironment(m_sStimDir.c_str(), m_sStimLog.c_str(), true);
}
void Stim::EnsureInitialised(void)
{
Stim::Trace("Initialising Stim (trace on)");
// ensure the home environment is set up
EnsureStimEnvironment(m_sStimDir.c_str(), m_sStimLog.c_str());
// open the log document
m_fLog.open(m_sStimLog.c_str(), ios::in);
if (!m_fLog)
throw ("Failed to open log file: " + m_sStimLog).c_str();
// only opening for reads by default
m_bLogReadOnly = true;
}
void Stim::Destroy(void)
{
Stim::Trace("Destroying Stim");
// close log file
m_fLog.close();
}
void Stim::WriteLog(
const string& sTimestamp,
const string& sEvent,
const string& sDetail = "")
{
// make sure containers are initialised
this->EnsureInitialised();
// ensure log is open in read/write
if (m_bLogReadOnly)
{
// TODO: would be nice if there was a better way to do this
// TODO: verify open worked
m_fLog.close();
m_fLog.clear();
m_fLog.open(m_sStimLog.c_str(), ios::in | ios::out);
m_bLogReadOnly = false;
}
// record event to log file
m_fLog.seekp(0, ios::end);
m_fLog << sTimestamp << " " << sEvent;
if (sDetail.length() > 0)
m_fLog << " " << sDetail;
m_fLog << endl;
}
bool Stim::ReadLog(
string& sTimestamp,
string& sEvent,
string& sDetail)
{
Stim::Trace("Beginning of ReadLog");
// read next line
string sLogLine;
bool bMoreData = ReadNextLine(this->m_fLog, sLogLine);
// parse out timestamp
sTimestamp = sLogLine.substr(0, 17);
// parse out event
int iEndOfEvent = sLogLine.find_first_of(' ', 18);
int iEventLength = iEndOfEvent - 18;
sEvent = sLogLine.substr(18, iEventLength);
// the rest is detail
sDetail = sLogLine.substr(iEndOfEvent + 1);
Stim::Trace("Leaving ReadLog");
return bMoreData;
}
void Stim::StartTask(time_t aStartTime, const string& sTaskPath)
{
char szDate[18];
GkMakeTimestamp(aStartTime, szDate);
WriteLog(szDate, STIM_TASK_START, sTaskPath);
}
void Stim::StopTask(time_t aStartTime)
{
char szDate[18];
GkMakeTimestamp(aStartTime, szDate);
WriteLog(szDate, STIM_TASK_STOP);
}
void Stim::LogTask(time_t aStartTime, const string& sMessage)
{
char szDate[18];
GkMakeTimestamp(aStartTime, szDate);
WriteLog(szDate, STIM_TASK_LOG, sMessage);
}
// cribbed from "Thinking in C++", 2nd Ed., Vol. 2
int stringcmpi(const string& s1, const string& s2)
{
// select the first element of each string
string::const_iterator p1 = s1.begin(), p2 = s2.begin();
// iterate through characters in each string
while(p1 != s1.end() && p2 != s2.end())
{
// convert each to uppercase and compare
if(toupper(*p1) != toupper(*p2))
{
// report which was lexically greater
return (toupper(*p1)<toupper(*p2))? -1 : 1;
}
// move on
p1++;
p2++;
}
// they either match up exactly, or one is longer than the other and
// they match up to the end of the shorter. In the latter case, say
// which was longer.
return(s2.size() - s1.size());
}
time_t DetermineStartOfDay(time_t aTime)
{
// convert to struct we can examine
struct tm* pTm = localtime(&aTime);
// clear out hour, minute, second
pTm->tm_hour = pTm->tm_min = pTm->tm_sec = 0;
// convert back into timestamp
time_t aMidnight = mktime(pTm);
if (aMidnight < -1)
throw "mktime() returned -1";
return aMidnight;
}
time_t DetermineStartOfDay(const string& sDate)
{
// break down datestamp
int iYear, iMonth, iDay;
sscanf(sDate.c_str(), "%04d%02d%02d", &iYear, &iMonth, &iDay);
// convert datestamp into calendar time
struct tm tTm;
tTm.tm_hour = 0;
tTm.tm_min = 0;
tTm.tm_sec = 0;
tTm.tm_mday = iDay;
tTm.tm_mon = (iMonth - 1);
tTm.tm_year = (iYear - 1900);
/* struct tm tTm =
{
0, 0, 0,
iDay, (iMonth - 1), (iYear - 1900)
};*/
time_t aTime = mktime(&tTm);
if (aTime < 0)
throw "Stim::GkGrokTimestamp: mktime() returned -1!";
return aTime;
}
bool stringtrim(string& s)
{
s.erase(0, s.find_first_not_of(" \t"));
s.erase(s.find_last_not_of(" \t") + 1);
if (s.length() == 0)
return false;
return true;
}
void DeterminePeriod(
time_t tNow,
const string& sDateRange,
time_t& aPeriodStart,
time_t& aPeriodEnd)
{
if (stringcmpi(sDateRange, STIM_DATE_TODAY) == 0)
{
aPeriodStart = DetermineStartOfDay(tNow);
aPeriodEnd = aPeriodStart + SECONDS_IN_DAY - 1;
}
else if (stringcmpi(sDateRange, STIM_DATE_YESTERDAY) == 0)
{
aPeriodStart = DetermineStartOfDay(tNow - SECONDS_IN_DAY);
aPeriodEnd = aPeriodStart + SECONDS_IN_DAY - 1;
}
else // format is "<date>", "-<date>", "<date>-", "<date1>-<date2>", or ""
{
int iDash = sDateRange.find('-');
if (iDash == string::npos)
{
// one day specified
aPeriodStart = DetermineStartOfDay(sDateRange);
aPeriodEnd = aPeriodStart + SECONDS_IN_DAY - 1;
}
else
{
// split into "<date1>-<date2>"
string sStart = sDateRange.substr(0, iDash);
string sEnd = sDateRange.substr(iDash + 1);
// if start specified
if (stringtrim(sStart))
aPeriodStart = DetermineStartOfDay(sStart);
else
aPeriodStart = 0;
// if end specified
if (stringtrim(sEnd))
aPeriodEnd = DetermineStartOfDay(sEnd) + SECONDS_IN_DAY - 1;
else
aPeriodEnd = tNow;
}
}
}
bool Stim::FindPeriodStart(time_t aPeriodStart, time_t aPeriodEnd)
{
// seek to beginning of log file
this->m_fLog.seekg(0, ios::beg);
// and here we have a linear search, folks
string sTimestamp, sEvent, sDetail;
time_t aTime;
bool bMoreLog = true;
while (bMoreLog)
{
// get next line
streampos tLogPos = this->m_fLog.tellg();
bMoreLog = ReadLog(sTimestamp, sEvent, sDetail);
// compare timestamp and that it's a START event, not left over from
// last session
GkGrokTimestamp(aTime, sTimestamp.c_str());
if (aTime >= aPeriodStart && sEvent == STIM_TASK_START)
{
// check that we haven't overshot
if (aTime >= aPeriodEnd)
return false;
// rewind to beginning of record
this->m_fLog.clear(); // this is necessary to reset the eof bit
this->m_fLog.seekg(tLogPos);
return true;
}
}
return false;
}
void AddToTaskTotals(
map<string, time_t>& vPeriodTime,
string sTask,
time_t tTimeSpent)
{
map<string, time_t>::iterator it;
// first time with this task, this period?
if ((it = vPeriodTime.find(sTask)) == vPeriodTime.end())
vPeriodTime[sTask] = tTimeSpent;
else
vPeriodTime[sTask] += tTimeSpent;
}
time_t GetTotalTime(map<string, time_t>& vTaskTime)
{
map<string, time_t>::iterator it;
time_t tTotal = 0;
for (it = vTaskTime.begin();
it != vTaskTime.end();
it++)
{
tTotal += it->second;
}
return tTotal;
}
void PrintOutTotals(const string& sStart, map<string, time_t>& vTaskTime)
{
map<string, time_t>::iterator it;
time_t tTotal = 0;
for (it = vTaskTime.begin();
it != vTaskTime.end();
it++)
{
time_t tSeconds = it->second;
string sElapsed;
SecondsToHms(tSeconds, sElapsed);
printf("%-60s %s\n", it->first.c_str(), sElapsed.c_str());
tTotal += it->second;
}
string sTotalElapsed;
SecondsToHms(tTotal, sTotalElapsed);
printf("%60s %s\n", "TOTAL", sTotalElapsed.c_str());
}
bool Stim::Status(time_t tNow, TSessionStatus& tSession)
{
// make sure containers are initialised
this->EnsureInitialised();
// determine period for reporting
string sDateRange = "today";
time_t aPeriodStart, aPeriodEnd;
DeterminePeriod(tNow, sDateRange, aPeriodStart, aPeriodEnd);
// seek to beginning of range
/* TODO: this should seek to beginning of session; it currently seeks
* to first entry of today. Consider working late and starting a new
* session at 11:45. Work starts on a new task at 1:40 in the morning,
* but it's still the same session. Status will report only on the
* later task.
*/
Stim::Trace("Seeking to beginning of range");
if (!FindPeriodStart(aPeriodStart, aPeriodEnd))
return false;
Stim::Trace("Found beginning of range.");
// parse line by line
string sLogLine;
time_t tLastTime = STIM_TIME_NOTIME;
string sLastTask;
map<string, time_t> vSessionTime;
string sSessionStartDate = "";
string sTimestamp, sEvent, sDetail;
bool bContinue = true;
bool bRunning = false;
while (bContinue)
{
Stim::Trace("here we are... about to read a log line...");
// read the next record
bContinue = ReadLog(sTimestamp, sEvent, sDetail);
if (bContinue)
Stim::Trace("finished reading log line (more to follow)");
else
Stim::Trace("finished reading log line (that's it)");
// trace the line
Stim::Trace(("LOG>"
+ sTimestamp + " > " + sEvent + " > " + sDetail).c_str());
// skip log messages
if (sEvent == STIM_TASK_LOG)
continue;
// parse timestamp
time_t tTimeStamp;
GkGrokTimestamp(tTimeStamp, sTimestamp.c_str());
// if starting a new session
if (!bRunning)
{
// if this is the first entry of a new day, it must be a start event
// or FindStartOfDay wouldn't have started here
tLastTime = tTimeStamp;
sLastTask = sDetail;
bRunning = true;
// status report doesn't distinguish between sessions, so there is
// nothing to report: just go on to next task
}
// otherwise, was working on a task, done with it for now
else
{
// calculate time difference
time_t tTimeDiff = tTimeStamp - tLastTime;
// add to task totals
AddToTaskTotals(vSessionTime, sLastTask, tTimeDiff);
// starting or stopping?
if (sEvent == STIM_TASK_START)
{
Stim::Trace("Starting new task");
bRunning = true;
// this task becomes the previous task
sLastTask = sDetail;
}
else if (sEvent == STIM_TASK_STOP)
{
Stim::Trace("Stopping task");
// for next time: there is no previous task
//tLastTime = STIM_TIME_NOTIME;
// if it were true, you'd better catch it
bRunning = false;
}
// this is the start time of the current time chunk
tLastTime = tTimeStamp;
}
}
// determine total session time, excluding current task
time_t tTotalTime = 0;
if (!vSessionTime.empty())
{
tTotalTime = GetTotalTime(vSessionTime);
}
// fill out struct
tSession.aSessionTime = tTotalTime;
tSession.aTaskTime = vSessionTime[sLastTask];
tSession.aTransitionTime = tLastTime;
tSession.sCurrentTask = sLastTask;
tSession.bRunning = bRunning;
return true;
}
bool Stim::ReportTime(
time_t tNow,
const string& sDateRange,
vector<string>& vTaskPaths,
TTimeSpent& vTimeSpent)
{
// make sure containers are initialised
this->EnsureInitialised();
// determine period for reporting
time_t aPeriodStart, aPeriodEnd;
DeterminePeriod(tNow, sDateRange, aPeriodStart, aPeriodEnd);
// seek to beginning of range
if (!FindPeriodStart(aPeriodStart, aPeriodEnd))
return false;
// parse line by line
string sLogLine;
time_t tLastTime = STIM_TIME_NOTIME;
string sLastTask;
map<string, time_t> vSessionTime, vPeriodTime;
string sSessionStartDate = "";
string sTimestamp, sEvent, sDetail;
bool bContinue = true;
TTimeChunk tCurrentTimeChunk;
while (bContinue)
{
// read the next record
bContinue = ReadLog(sTimestamp, sEvent, sDetail);
// trace the line
Stim::Trace(("LOG>"
+ sTimestamp + " > " + sEvent + " > " + sDetail).c_str());
// parse timestamp
time_t tTimeStamp;
GkGrokTimestamp(tTimeStamp, sTimestamp.c_str());
// if new chunk of time
if (tCurrentTimeChunk.aStartTime == STIM_TIME_NOTIME)
{
// check if this is outside of period bounds
if (tTimeStamp > aPeriodEnd)
break;
// new time; new session?
if (sEvent == STIM_TASK_START)
{
tCurrentTimeChunk.aStartTime = tTimeStamp;
tCurrentTimeChunk.sTaskPath = sDetail;
}
//else
// throw "Stim::ReportTime: somehow continued nonexistent session";
}
// more records for current chunk of time
else
{
// are we logging something for the task?
if (sEvent == STIM_TASK_LOG)
{
TLogEntry tLogEntry = { tTimeStamp, sDetail };
tCurrentTimeChunk.vLogMessages.push_back(tLogEntry);
}
else // assume we're stopping (or starting a new task)
{
// assign stop time
tCurrentTimeChunk.aStopTime = tTimeStamp;
// add time record to vector thereof
vTimeSpent.push_back(tCurrentTimeChunk);
// reset time record
tCurrentTimeChunk.vLogMessages.clear();
if (sEvent == STIM_TASK_START)
{
tCurrentTimeChunk.aStartTime = tTimeStamp;
tCurrentTimeChunk.sTaskPath = sDetail;
}
else
tCurrentTimeChunk.aStartTime = STIM_TIME_NOTIME;
}
}
}
// check if we've logged time
return !(vTimeSpent.empty());
}
// -----------------------------------------------------------------------
// HELPERS
// -----------------------------------------------------------------------
void Stim::Trace(const char* szMessage)
{
#ifdef DEBUG
cerr << "STIM>" << szMessage << endl;
#endif
}