tdaq-develop-2025-02-12
ProgressBar.cc
1 #include "otsdaq/ProgressBar/ProgressBar.h"
2 #include "otsdaq/Macros/CoutMacros.h"
3 #include "otsdaq/Macros/StringMacros.h"
4 #include "otsdaq/MessageFacility/MessageFacility.h"
5 
6 #include <dirent.h> //for DIR
7 #include <sys/stat.h>
8 #include <cassert>
9 #include <cstdio>
10 #include <cstdlib>
11 #include <cstring>
12 #include <iostream>
13 
14 using namespace ots;
15 
16 //==============================================================================
17 ProgressBar::ProgressBar()
18  : cProgressBarFilePath_(std::string(__ENV__("SERVICE_DATA_PATH")) +
19  "/ProgressBarData/")
20  , cProgressBarFileExtension_(".txt")
21  , totalStepsFileName_("")
22  , stepCount_(0)
23  , stepsToComplete_(0)
24  , started_(false)
25 {
26  std::string path = cProgressBarFilePath_;
27  DIR* dir = opendir(path.c_str());
28  if(dir)
29  closedir(dir);
30  else if(-1 == mkdir(path.c_str(), 0755))
31  {
32  // lets create the service folder (for first time)
33  __SS__ << "Service directory creation failed: " << path << std::endl;
34  __SS_THROW__;
35  }
36 } //end constructor()
37 
38 //==============================================================================
41 void ProgressBar::reset(std::string file, std::string lineNumber, int id)
42 {
43  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
44 
45  stepCount_ = 0;
46  stepsToComplete_ = 0;
47 
48  // try to load stepsToComplete based on file, lineNumber and id
49  char fn[1000];
50  sprintf(fn, "%s_%s_%d", file.c_str(), lineNumber.c_str(), id);
51 
52  for(unsigned int c = 0; c < strlen(fn); ++c)
53  if(!((fn[c] >= '0' && fn[c] <= '9') || (fn[c] >= 'a' && fn[c] <= 'z') ||
54  (fn[c] >= 'A' && fn[c] <= 'Z')))
55  fn[c] = '_';
56  totalStepsFileName_ = cProgressBarFilePath_ + fn + cProgressBarFileExtension_;
57  __COUTVS__(10, totalStepsFileName_);
58 
59  FILE* fp = fopen(totalStepsFileName_.c_str(), "r");
60  if(fp)
61  {
62  fscanf(fp, "%d", &stepsToComplete_);
63  fclose(fp);
64  __COUT_TYPE__(TLVL_DEBUG + 10)
65  << __COUT_HDR__ << "File Found - stepsToComplete = " << stepsToComplete_
66  << std::endl;
67  }
68  else
69  __COUTT__ << "File Not there: " << totalStepsFileName_ << __E__;
70 
71  started_ = true;
72 } //end reset()
73 
74 //==============================================================================
76 {
77  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
78  ++stepCount_;
79 
80  // do not allow to get to 100% until complete (in case stepsToComplete is not constant for all time)
81  if(stepsToComplete_ && stepCount_ >= stepsToComplete_)
82  stepsToComplete_ = stepCount_ + 1;
83 
84  __COUT_TYPE__(TLVL_DEBUG + 10) << __COUT_HDR__ << totalStepsFileName_ << " "
85  << readPercentageString() << "% complete" << std::endl;
86 } //end step()
87 
88 //==============================================================================
90 {
91  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
92  return !started_;
93 } //end isComplete()
94 
95 //==============================================================================
97 {
98  step(); // consider complete as a step
99 
100  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
101 
102  stepsToComplete_ = stepCount_;
103  started_ = false;
104 
105  // done, save steps to file
106 
107  __COUT_TYPE__(TLVL_DEBUG + 10) << __COUT_HDR__ << totalStepsFileName_ << std::endl;
108 
109  FILE* fp = fopen(totalStepsFileName_.c_str(), "w");
110  if(fp)
111  {
112  fprintf(fp, "%d", stepsToComplete_);
113  fclose(fp);
114  }
115  else
116  __COUT_ERR__ << "Critical ERROR!" << std::endl;
117 } //end complete()
118 
119 //==============================================================================
122 {
123  std::lock_guard<std::mutex> lock(theMutex_); // lock out for remainder of scope
124 
125  if(!started_)
126  return 100; // if no progress started, always return 100% complete
127 
128  if(stepsToComplete_)
129  return stepCount_ * 100.0 / stepsToComplete_;
130 
131  return stepCount_ ? 50 : 0;
132 } //end read()
133 
134 //==============================================================================
137 {
138  char pct[5];
139  sprintf(pct, "%d", read());
140  return pct;
141 } //end readPercentageString()
std::string readPercentageString()
return percentage complete as std::string
Definition: ProgressBar.cc:136
bool isComplete()
get functions
Definition: ProgressBar.cc:89
void step()
thread safe
Definition: ProgressBar.cc:75
int read()
if stepsToComplete==0, then define any progress as 50%, thread safe
Definition: ProgressBar.cc:121
void complete()
declare complete, thread safe
Definition: ProgressBar.cc:96