Commit 3d153fbb authored by Andrea Orlat's avatar Andrea Orlat
Browse files

class to manage the schedule report added (come with unit tests), it should be...

class to manage the schedule report added (come with unit tests), it should be now integrated inside scheduler code
parent 9a768f9d
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
@@ -677,6 +677,40 @@ public:
	  */
	 static bool fileExists(const IRA::CString& file);

	 /**
	  * Creates an empty file in the local filessystem
	  * @return true if the file could be successfully created
	  */
	 static bool createEmptyFile(const IRA::CString& file);

	 /**
	  * Deletes a file from the local filessystem
	  * @return true if the operation has been executed correctly
	  */
	 static bool deleteFile(const IRA::CString& file);

	 /**
	  * copy a file in an other location on local filessystem
	  * @src full path and name of the file to be copied
	  * @dst full path and name of the destination file
	  */
	 static bool copyFile(const IRA::CString& src,const IRA::CString& dst);

	 /**
	  *
	  */
	 //static bool batchCopyFile(const IRA::CString& srcPattern,const IRA::CString& dest);

	 /**
	  * Given a fully qualified file name (path+name+extension) it decompose it.
	  * @param fullPath fully qualified name of the file. Example (/system/archive/foo.boo)
	  * @param baseDir full path name. Example (/system/archive)
	  * @param baseName pure file name. Example (foo)
	  * @param extension extension of the file, if it exists. Example (boo)
	  */
	 static bool extractFileName(const IRA::CString& fullPath,IRA::CString& baseDir,IRA::CString& baseName,
			 IRA::CString& extension);

	 /**
	  * Round a double value to the nearest number with decimals precision
	  * @param val number to be rounded
+56 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
#include <sys/stat.h>
#include <dirent.h>
#include <sys/stat.h>
#include <libgen.h>

using namespace IRA;

@@ -1123,6 +1124,61 @@ bool CIRATools::fileExists(const IRA::CString& file)
	return (stat((const char *)file,&buffer)==0);
}

bool CIRATools::createEmptyFile(const IRA::CString& file)
{
	bool ret;
	std::fstream fs;
	fs.open((const char *)file,ios::out);
	if (fs.fail()) ret=false;
	else ret=true;
	fs.close();
	return ret;
}

bool CIRATools::deleteFile(const IRA::CString& file)
{
	if(remove((const char *)file)==0) {
		return true;
	}
	else {
		return false;
	}
}

bool CIRATools::copyFile(const IRA::CString& src,const IRA::CString& dst)
{
    std::ifstream in ((const char *)src);
    if (in.fail()) return false;
    std::ofstream out ((const char *)dst);
    if (out.fail()) return false;
    out << in.rdbuf();
    out.close();
    in.close();
    return true;
}

bool CIRATools::extractFileName(const IRA::CString& fullPath,IRA::CString& baseDir,IRA::CString& baseName,
		 IRA::CString& extension)
{
    char *dirc, *basec;
    dirc=strdup((const char *)fullPath); // the APIs called below might change the content of the input string
    basec=strdup((const char *)fullPath);
    baseDir=IRA::CString(dirname(dirc));
    baseName=IRA::CString(basename(basec));
    if ((baseDir.GetLength()==0) || (baseName.GetLength()==0)) {
    	return false;
    }
	int pos=baseName.Find('.');
	if (pos<0) { // not found
		extension="";
	}
	else {
		extension=baseName.Right(baseName.GetLength()-(pos+1));
		baseName=baseName.Mid(0,pos);
	}
	return true;
}

double CIRATools::roundNearest(const double& val,const long& decimals)
{
	long precision=(long)pow(10,decimals);
+108 −1
Original line number Diff line number Diff line
@@ -264,17 +264,122 @@ public:
		else {
			return ::testing::AssertionFailure() << "9) inverted receiver, inverted backend, resulting band should not be empty" ;
		}
		return ::testing::AssertionSuccess();
	}

	::testing::AssertionResult createEmptyFile_checkSuccess() {
		IRA::CString file;
		::testing::Test::RecordProperty("description","check the creation of an empty file");
		file=basePath+emptyFileName;
		if (IRA::CIRATools::createEmptyFile(file)) {
			if (IRA::CIRATools::fileExists(file)) {
				IRA::CString cleanCommand;
				int i;
				cleanCommand="rm "+file;
				i=system((const char *)cleanCommand);
				return ::testing::AssertionSuccess();
			}
			else {
				return ::testing::AssertionFailure() << (const char *) file << " file should not be created but (fileExists) does not detect it";
			}
		}
		else {
			return ::testing::AssertionFailure() << (const char *) file << " file cannot be created";
		}
	}

	::testing::AssertionResult createEmptyFile_checkFail() {
		IRA::CString file;
		::testing::Test::RecordProperty("description","check if the creation fails when it is the case");
		file="/foo/boo/dummy/boogie.tst";
		if (!IRA::CIRATools::createEmptyFile(file)) {
			return ::testing::AssertionSuccess();
		}
		else {
			return ::testing::AssertionFailure() << (const char *) file << " does not fail as expected";
		}
	}

	::testing::AssertionResult deleteFile_checkSuccess() {
		IRA::CString file;
		IRA::CString command;
		int i;
		::testing::Test::RecordProperty("description","check if a file can be deleted from filesystem");
		file=basePath+dummyFileName;
		command="touch "+file;
		i=system((const char *)command);
		if (IRA::CIRATools::deleteFile(file)) {
			return ::testing::AssertionSuccess();
		}
		else {
			return ::testing::AssertionFailure() << (const char *) file << " the file could not be deleted";
		}
	}

	::testing::AssertionResult extractFileName_checkSimpleDirectoryPath() {
		IRA::CString name;
		IRA::CString baseDir,baseName,extension;
		::testing::Test::RecordProperty("description","check is a fully qualified directory path can be decomposed correctly");
		// simple directory name
		name="/system/archive";
		if (!IRA::CIRATools::extractFileName(name,baseDir,baseName,extension)) {
			return ::testing::AssertionFailure() << "simple directory could not be decomposed";
		}
		else if (baseDir!="/system") {
			return ::testing::AssertionFailure() << "base directory of " << (const char *)name << " is unexpectedly " <<
					(const char *) baseDir;
		}
		else if (baseName!="archive") {
			return ::testing::AssertionFailure() << "base name of " << (const char *)name << " is unexpectedly " <<
					(const char *) baseName;
		}
		else if (extension!="") {
			return ::testing::AssertionFailure() << "extension token of " << (const char *)name << " is unexpectedly " <<
					(const char *) extension;
		}
		return ::testing::AssertionSuccess();
	}

	::testing::AssertionResult extractFileName_checkSimpleFilePath() {
		IRA::CString name;
		IRA::CString baseDir,baseName,extension;
		::testing::Test::RecordProperty("description","check is a fully qualified file path can be decomposed correctly");
		// simple directory name
		name="/system/archive/document.foo";
		if (!IRA::CIRATools::extractFileName(name,baseDir,baseName,extension)) {
			return ::testing::AssertionFailure() << "simple file path could not be decomposed";
		}
		else if (baseDir!="/system/archive") {
			return ::testing::AssertionFailure() << "base directory of " << (const char *)name << " is unexpectedly " <<
					(const char *) baseDir;
		}
		else if (baseName!="document") {
			return ::testing::AssertionFailure() << "base name of " << (const char *)name << " is unexpectedly " <<
					(const char *) baseName;
		}
		else if (extension!="foo") {
			return ::testing::AssertionFailure() << "extension token of " << (const char *)name << " is unexpectedly " <<
					(const char *) extension;
		}
		return ::testing::AssertionSuccess();
	}

protected:
	static IRA::CString simpleDirPath;
	static IRA::CString complexDirPath;
	static IRA::CString fileName;
	static IRA::CString emptyFileName;
	static IRA::CString dummyFileName;
	IRA::CString basePath;

	static void TearDownTestCase()
	{
	}

	static void SetUpTestCase()
	{
	}

	virtual void SetUp() {
		IRA::CString command;
		int i;
@@ -296,5 +401,7 @@ protected:

IRA::CString IRALibrary_IRATools::simpleDirPath = IRA::CString("/firstLevelDirectory");
IRA::CString IRALibrary_IRATools::complexDirPath = simpleDirPath+IRA::CString("/secondLevelDirectory");
IRA::CString IRALibrary_IRATools::fileName = IRA::CString("/dummyFile.tst");
IRA::CString IRALibrary_IRATools::fileName = IRA::CString("/normalFile.tst");
IRA::CString IRALibrary_IRATools::emptyFileName = IRA::CString("/emptyFile.tst");
IRA::CString IRALibrary_IRATools::dummyFileName = IRA::CString("/dummyFile.tst");
}
+24 −0
Original line number Diff line number Diff line
@@ -22,6 +22,26 @@ TEST_F(IRALibrary_IRATools, fileExists_checkNoExistance){
	EXPECT_TRUE(fileExists_checkNoExistance());
}

TEST_F(IRALibrary_IRATools, createEmptyFile_checkSuccess){
	EXPECT_TRUE(createEmptyFile_checkSuccess());
}

TEST_F(IRALibrary_IRATools, createEmptyFile_checkFail){
	EXPECT_TRUE(createEmptyFile_checkFail());
}

TEST_F(IRALibrary_IRATools, deleteFile_checkSuccess){
	EXPECT_TRUE(deleteFile_checkSuccess());
}

TEST_F(IRALibrary_IRATools,extractFileName_checkSimpleDirectoryPath){
	EXPECT_TRUE(extractFileName_checkSimpleDirectoryPath());
}

TEST_F(IRALibrary_IRATools,extractFileName_checkSimpleFilePath){
	EXPECT_TRUE(extractFileName_checkSimpleFilePath());
}

TEST_F(IRALibrary_IRATools, skyFrequency_noIntersection){
	EXPECT_TRUE(skyFrequency_noIntersection());
}
@@ -37,3 +57,7 @@ TEST_F(IRALibrary_FastQueue,FastQueue_checkLimits){
TEST_F(IRALibrary_FastQueue,FastQueue_checkConsistency){
	EXPECT_TRUE(FastQueue_checkConsistency());
}



+76 −0
Original line number Diff line number Diff line

/* **************************************************************************************************** */
/* DISCOS Project																						*/
/* This code is under GNU General Public Licence (GPL).                                                 */
/*                                                                                                      */
/* Who                                 when           What                                              */
/* Andrea Orlati(aorlati@ira.inaf.it)  10/01/2017     Creation                                          */

#ifndef SCHEDULEREPORT_H_
#define SCHEDULEREPORT_H_

#include <IRA>
#include <list>

/*
 * This class is in charge of creating and managing the schedule report.
 * The report is a text file that contains some information regarding the execution of schedule:
 * 1) the schedule file name
 * 2) the corresponding generated log file
 * 3) the sequence of folders (corresponding to the created scans) generated by the schedule
 * 4) TBD, AOB
 * The class also creates a lock files that can be used by external application to understand if the system
 * is acquiring data or not.
 * This class is not thread safe.
 */

class CScheduleReport {
public:
	/*
	 * Object constructor.
	 * @param logPath path to the log files folder
	 * @param reportPath this is the path in the local filessystem where the schedule report will be saved. If
	 *        empty the schedule report is not created.
	 * @param backupPath this is the path in the local file system where the schedule files are stored for
	 *        archiving purposes
	 * @param lock name of the file to be created as lock file. If empty string the lock file is not handled.
	 */
	CScheduleReport(const IRA::CString logPath,const IRA::CString& reportPath,const IRA::CString& backupPath,
			const IRA::CString& lock);
	~CScheduleReport();
	const IRA::CString& getLastError() const { return m_lastError; }
	void addScheduleName(const IRA::CString& fullName);
	void addAuxScheduleFile(const IRA::CString& fullName);
	IRA::CString getLogFileName() const;
	IRA::CString getReportFileName() const;
	IRA::CString getBackupFolder() const;
	IRA::CString getPostFix() const { return m_currentPostfix; }
	void addScanPath(const IRA::CString scan);
	bool activate();
	bool deactivate();
private:
	typedef std::list<IRA::CString> TTnternalList;
	typedef std::list<IRA::CString>::iterator TInternalListIter;
	void generatePostfix();
	bool createLock();
	bool removeLock();
	bool backupSchedule();
	bool writeReport();
	IRA::CString m_lastError;
	IRA::CString m_logFilePath;
	IRA::CString m_reportPath;
	IRA::CString m_backupPath;
	IRA::CString m_lockFile;
	IRA::CString m_scheduleBaseName;
	IRA::CString m_scheduleBasePath;
	IRA::CString m_scheduleExtension;
	IRA::CString m_currentPostfix;
	bool m_active;
	bool m_reportEnabled;
	bool m_lockFileEnabled;
	bool m_scheduleProvided;
	TTnternalList m_auxFiles;
	TTnternalList m_scanPaths;
};

#endif /* SCHEDULEREPORT_H_ */
Loading