Skip to content

Commit

Permalink
Merge pull request #1236 from janosimas/master
Browse files Browse the repository at this point in the history
get_analysis_date operator to retrieve current analysis date
  • Loading branch information
janosimas authored Aug 25, 2017
2 parents abdef1a + d19909d commit eb5ae54
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
66 changes: 66 additions & 0 deletions src/terrama2/services/analysis/core/python/PythonInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "../BufferMemory.hpp"

#include "../../../../core/utility/Logger.hpp"
#include "../../../../core/utility/TimeUtils.hpp"
#include "../../../../core/data-model/Filter.hpp"
#include "../utility/Verify.hpp"

Expand Down Expand Up @@ -411,6 +412,7 @@ BOOST_PYTHON_MODULE(terrama2)

def("add_value", terrama2::services::analysis::core::python::addValue);
def("get_attribute_value_as_json", terrama2::services::analysis::core::python::getAttributeValueAsJson);
def("get_current_execution_date", terrama2::services::analysis::core::python::getCurrentExecutionDate);

// Export BufferType enum to python
enum_<terrama2::services::analysis::core::BufferType>("BufferType")
Expand Down Expand Up @@ -545,6 +547,7 @@ std::string terrama2::services::analysis::core::python::prepareScript(AnalysisPt
formatedScript = " " + formatedScript;
formatedScript = "from terrama2 import *\n"
"import json\n"
"import datetime\n"
"def get_value(attr):\n"
" answer = get_attribute_value_as_json(attr)\n"
" if(answer):\n"
Expand All @@ -556,9 +559,13 @@ std::string terrama2::services::analysis::core::python::prepareScript(AnalysisPt
" return value\n"
" else:\n"
" return None\n\n"
"def get_analysis_date():\n"
" iso_string_date = get_current_execution_date()\n"
" return datetime.datetime.strptime(iso_string_date.translate(None, ':-'), '%Y%m%dT%H%M%S.%fZ')\n\n"
"def analysis():\n"
+ formatedScript;

std::cout << "\n\n\n" << formatedScript << "\n\n\n";
return formatedScript;
}

Expand Down Expand Up @@ -603,6 +610,65 @@ void terrama2::services::analysis::core::python::validateAnalysisScript(Analysis
}
}

std::string terrama2::services::analysis::core::python::getCurrentExecutionDate()
{
OperatorCache cache;
terrama2::services::analysis::core::python::readInfoFromDict(cache);
// After the operator lock is released it's not allowed to return any value because it doesn' have the interpreter lock.
// In case an exception is thrown, we need to set this boolean. Once the code left the lock is acquired we should return NAN.
bool exceptionOccurred = false;

auto& contextManager = ContextManager::getInstance();
auto analysis = cache.analysisPtr;

//////////////////////////////////////////////////////
// If this a monitored object analysis
try
{
terrama2::services::analysis::core::verify::analysisMonitoredObject(analysis);
terrama2::services::analysis::core::MonitoredObjectContextPtr context;
try
{
context = ContextManager::getInstance().getMonitoredObjectContext(cache.analysisHashCode);
}
catch(const terrama2::Exception& e)
{
TERRAMA2_LOG_ERROR() << boost::get_error_info<terrama2::ErrorDescription>(e)->toStdString();
return "";
}

return terrama2::core::TimeUtils::getISOString(context->getStartTime());
}
catch (const terrama2::core::VerifyException&)
{
}

//////////////////////////////////////////////////////
// If this a grid analysis
try
{
terrama2::services::analysis::core::verify::analysisGrid(analysis);
terrama2::services::analysis::core::GridContextPtr context;
try
{
context = ContextManager::getInstance().getGridContext(cache.analysisHashCode);
}
catch(const terrama2::Exception& e)
{
TERRAMA2_LOG_ERROR() << boost::get_error_info<terrama2::ErrorDescription>(e)->toStdString();
return "";
}

return terrama2::core::TimeUtils::getISOString(context->getStartTime());
}
catch (const terrama2::core::VerifyException&)
{
}

contextManager.addError(cache.analysisHashCode, QObject::tr("Error in analysis type for analysis %1.").arg(analysis->id).toStdString());
return "";
}

std::string terrama2::services::analysis::core::python::getAttributeValueAsJson(const std::string &attribute)
{
OperatorCache cache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ namespace terrama2
*/
std::string getAttributeValueAsJson(const std::string &attribute);

std::string getCurrentExecutionDate();

} // end namespace python
} // end namespace core
} // end namespace analysis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
"description": "It returns the value of the given attribute for the current monitored object",
"code": "get_value(\"<attribute_name>\")"
},
{
"name": "Get date",
"description": "Get date of execution of the analysis.",
"code": "get_analysis_date()"
},
{
"name": "Distance units",
"children": [
Expand Down Expand Up @@ -92,4 +97,4 @@
}
]
}
]
]

0 comments on commit eb5ae54

Please sign in to comment.