Skip to content

Commit

Permalink
Merge pull request #1955 from juan0101/b4.1.1
Browse files Browse the repository at this point in the history
B4.1.1
  • Loading branch information
MarceloPilatti authored Aug 25, 2020
2 parents 7ecd60e + 195dc7e commit e9118b0
Show file tree
Hide file tree
Showing 13 changed files with 355 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ DECLARE
number_of_attributes INTEGER;
number_of_affected_rows BIGINT;

finaltable_last_id_inserted INTEGER;
is_dynamictable_info_exists BOOLEAN;
pk_output_table TEXT;

query TEXT;
result RECORD;
BEGIN
Expand All @@ -197,17 +201,21 @@ BEGIN
-- Getting date_column_from_dynamic_table
EXECUTE format('SELECT get_date_column_from_dynamic_table(''%s'')', dynamic_table_name_handler) INTO date_column_from_dynamic_table;

-- Getting last time collected from analysis_metadata
EXECUTE format('SELECT COUNT(*) FROM terrama2.analysis_metadata WHERE key = ''last_analysis'' AND terrama2.analysis_metadata.analysis_id = %s', analysis_id) INTO number_of_rows_metadata;
final_table := format('%s_%s', output_table_name, analysis_id);

EXECUTE format('SELECT MAX(value::TIMESTAMP) FROM terrama2.analysis_metadata WHERE key = ''last_analysis'' AND terrama2.analysis_metadata.analysis_id = %s', analysis_id) INTO last_analysis;
EXECUTE format('SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = ''public'' AND table_name = ''%s'')', output_table_name) INTO is_dynamictable_info_exists;

final_table := format('%s_%s', output_table_name, analysis_id);
IF (is_dynamictable_info_exists) THEN
EXECUTE 'SELECT get_primary_key($1)' INTO pk_output_table USING output_table_name;
EXECUTE format('SELECT last_id FROM (SELECT * FROM public.%s ORDER BY %s DESC LIMIT 1) AS last_data',output_table_name,pk_output_table) INTO finaltable_last_id_inserted;
ELSE
finaltable_last_id_inserted := 0;
END IF;

EXECUTE 'SELECT get_primary_key($1)' INTO pk_static_table USING static_table_name;
EXECUTE 'SELECT get_primary_key($1)' INTO pk_dynamic_table USING dynamic_table_name_handler;

EXECUTE format('SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = ''public'' AND table_name = ''%s'')', final_table) INTO is_table_exists;
EXECUTE format('SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = ''public'' AND table_name = ''%s'')', final_table) INTO is_table_exists;

EXECUTE format('SELECT COUNT(*) FROM information_schema.columns WHERE table_name = ''%s'' ', final_table) INTO number_of_columns;

Expand All @@ -219,12 +227,7 @@ BEGIN
-- 6 this is the default number of attributes: mo nitored_id, intersect_id, execution_date, intersection_geom, calculated_area_ha, final_table_id;

IF (is_table_exists AND number_of_columns = number_of_attributes) THEN
-- Creating analysis filter
IF number_of_rows_metadata > 0 THEN
analysis_filter := format(' %s.%s > ''%s'' ', dynamic_table_name_handler, date_column_from_dynamic_table, last_analysis);
ELSE
analysis_filter := '1 = 1';
END IF;
analysis_filter := format(' %s.%s > %s ', dynamic_table_name_handler, pk_dynamic_table, finaltable_last_id_inserted);

date_filter := '1 = 1';

Expand Down Expand Up @@ -278,7 +281,7 @@ BEGIN
GET DIAGNOSTICS number_of_affected_rows = ROW_COUNT;
ELSE

analysis_filter := '1 = 1';
analysis_filter := format(' %s.%s > %s ', dynamic_table_name_handler, pk_dynamic_table, finaltable_last_id_inserted);

date_filter := '1 = 1';

Expand Down
166 changes: 166 additions & 0 deletions src/terrama2/core/utility/FilterUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

//Terralib
#include <terralib/datatype/TimeInstant.h>

// Qt
#include <string>
#include <QDate>
Expand All @@ -46,6 +49,8 @@
#include <QRegExp>
#include <QStringList>

#include <time.h>

// TODO: This method is being call for every check of the mask, this should only be called once to avoid a costy operation.
std::string terrama2::core::terramaMask2Regex(const std::string& mask)
{
Expand Down Expand Up @@ -182,6 +187,167 @@ std::shared_ptr<te::dt::TimeInstantTZ> terrama2::core::getFileTimestamp(const st
return timeStamp;
}

int terrama2::core::getMaxDay(int month, int year) {
if(month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11)
return 31;
else if(month == 3 || month == 5 || month == 8 || month == 10)
return 30;
else {
if(year % 4 == 0) {
if(year % 100 == 0) {
if(year % 400 == 0)
return 29;
return 28;
}
return 29;
}
return 28;
}
}

std::shared_ptr<te::dt::TimeInstantTZ> terrama2::core::gridSampleVerifyMask(std::string mask, std::shared_ptr<te::dt::TimeInstantTZ> actualDate)
{
std::shared_ptr<te::dt::TimeInstantTZ> dateReturn;
auto temp = QString::fromStdString(mask);
auto list = temp.split("%", QString::SkipEmptyParts);

time_t theTime = time(NULL);
struct tm *aTime = localtime(&theTime);

int day = aTime->tm_mday;
int month = aTime->tm_mon + 1; // Month is 0 - 11, add 1 to get a jan-dec 1-12 concept
int year = aTime->tm_year + 1900; // Year is # years since 1900

std::vector<std::string> options;
options.push_back("YYYY");
options.push_back("MM");
options.push_back("DD");
options.push_back("hh");
options.push_back("mm");
options.push_back("ss");

int optIndice = 0;

std::map<std::string, std::string> mapDate;

for(int i = 0; i < list.size(); ++i)
{
auto& parts = list[i];
auto listParts = parts.split("$", QString::SkipEmptyParts);

auto& part = listParts[0];
std::string partStr = part.toUtf8().constData();

auto& partContent = listParts[1];
std::string partContentStr = partContent.toUtf8().constData();

for(int y=optIndice; y < options.size(); ++y)
{
if(partStr.compare(options[y]) == 0)
{
switch (y) {
case 0:
mapDate.insert({"year", partContentStr});
break;
case 1:
mapDate.insert({"month", partContentStr});
break;
case 2:
mapDate.insert({"day", partContentStr});
break;
case 3:
mapDate.insert({"hour", partContentStr});
break;
case 4:
mapDate.insert({"minute", partContentStr});
break;
case 5:
mapDate.insert({"second", partContentStr});
break;
default:
std::cout << "DEFAULT" << std::endl;
}

optIndice++;
if((i+1) == list.size())
{
continue;
}
break;
}
else
{
int yearAux = 0;
int monthAux = 0;
switch (y) {
case 0:
mapDate.insert({"year", std::to_string(year)});
break;
case 1:
if(month < 10)
{
mapDate.insert({"month", "0"+std::to_string(month)});
}
else
{
mapDate.insert({"month", std::to_string(month)});
}
break;
case 2:
yearAux = std::stoi(mapDate.find("year")->second);
monthAux = std::stoi(mapDate.find("month")->second) - 1;
day = getMaxDay(monthAux,yearAux);
mapDate.insert({"day", std::to_string(day)});
break;
case 3:
mapDate.insert({"hour", "00"});
break;
case 4:
mapDate.insert({"minute", "00"});
break;
case 5:
mapDate.insert({"second", "00"});
break;
default:
std::cout << "DEFAULT" << std::endl;
}
optIndice++;
}
}
}

if(!mapDate.empty())
{

auto mapyear = mapDate.find("year");
auto mapmonth = mapDate.find("month");
auto mapday = mapDate.find("day");
auto maphour = mapDate.find("hour");
auto mapminute = mapDate.find("minute");
auto mapsecond = mapDate.find("second");

std::string finalDate = mapyear->second+"-";
finalDate = finalDate + mapmonth->second+"-";
finalDate = finalDate + mapday->second;
finalDate = finalDate + "T";
finalDate = finalDate + maphour->second+":";
finalDate = finalDate + mapminute->second+":";
finalDate = finalDate + mapsecond->second;
finalDate = finalDate + "-00";

dateReturn = terrama2::core::TimeUtils::stringToTimestamp(finalDate, terrama2::core::TimeUtils::webgui_timefacet);

return dateReturn;
}
else
{
QString errMsg = QObject::tr("The mask don't have the minimal needed parameters of a date!");
TERRAMA2_LOG_ERROR() << errMsg;
throw terrama2::core::UtilityException() << ErrorDescription(errMsg);
}
return nullptr;
}

bool terrama2::core::isValidDataSetName(const std::string& mask,
const Filter& filter,
const std::string& timezone,
Expand Down
4 changes: 4 additions & 0 deletions src/terrama2/core/utility/FilterUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ namespace terrama2
* @return Regex style mask
*/
TMCOREEXPORT std::string terramaMask2Regex(const std::string& mask);

TMCOREEXPORT std::shared_ptr<te::dt::TimeInstantTZ> gridSampleVerifyMask(std::string mask, std::shared_ptr<te::dt::TimeInstantTZ> actualDate);

TMCOREEXPORT int getMaxDay(int month, int year);
} // end namespace core
} // end namespace terrama2

Expand Down
1 change: 1 addition & 0 deletions src/terrama2/impl/DataStoragerTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ void terrama2::core::DataStoragerTable::store(DataSetSeries series, DataSetPtr o
transactorDestination->add(newDataSetType->getName(), series.syncDataSet->dataset().get(), {});

scopedTransaction.commit();

}

std::string terrama2::core::DataStoragerTable::getGeometryPropertyName(DataSetPtr dataSet) const
Expand Down
4 changes: 4 additions & 0 deletions src/terrama2/services/analysis/core/AnalysisExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,11 @@ void terrama2::services::analysis::core::AnalysisExecutor::runVectorialProcessin

auto result = analysisOperator->getResultDataSet();

auto lastId = analysisOperator->getLastId();

context->addAttribute("table_name", te::dt::STRING_TYPE);
context->addAttribute("affected_rows", te::dt::DOUBLE_TYPE);
context->addAttribute("last_id", te::dt::DOUBLE_TYPE);

int currentLine = 0;

Expand All @@ -520,6 +523,7 @@ void terrama2::services::analysis::core::AnalysisExecutor::runVectorialProcessin
{
context->setAnalysisResult(++currentLine, "table_name", result->getString("table_name"));
context->setAnalysisResult(currentLine, "affected_rows", result->getDouble("affected_rows"));
context->setAnalysisResult(currentLine, "last_id", lastId);

affectedRows = result->getDouble("affected_rows");
}
Expand Down
19 changes: 16 additions & 3 deletions src/terrama2/services/analysis/core/grid/Operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "../ContextManager.hpp"
#include "../../../../core/data-model/DataSetGrid.hpp"
#include "../../../../core/utility/Logger.hpp"
#include "../../../../core/utility/FilterUtils.hpp"
#include "../utility/Utils.hpp"
#include "../utility/Verify.hpp"
#include "../GridContext.hpp"
Expand Down Expand Up @@ -59,12 +60,12 @@ double terrama2::services::analysis::core::grid::getValue(std::shared_ptr<te::rs
return value;
}

double terrama2::services::analysis::core::grid::sample(const std::string& dataSeriesName, size_t bandIdx)
double terrama2::services::analysis::core::grid::sample(const std::string& dataSeriesName, size_t bandIdx, const std::string& date)
{
OperatorCache cache;
terrama2::services::analysis::core::python::readInfoFromDict(cache);
auto& contextManager = ContextManager::getInstance();
auto analysis = cache.analysisPtr;
auto analysis = cache.analysisPtr;

try
{
Expand Down Expand Up @@ -113,12 +114,24 @@ double terrama2::services::analysis::core::grid::sample(const std::string& dataS
{
auto startTime = context->getStartTime();

filter.discardAfter = startTime;
if(date.compare("") != 0)
{
filter.discardAfter = terrama2::core::gridSampleVerifyMask(date, context->getStartTime());
}
else
{
filter.discardAfter = startTime;
}

filter.lastValues = std::make_shared<std::size_t>(1);
filter.isReprocessingHistoricalData = true;
}
else
{
if(date.compare("") != 0)
{
filter.discardAfter = terrama2::core::gridSampleVerifyMask(date, context->getStartTime());
}
filter.lastValues = std::make_shared<size_t>(1);
}

Expand Down
2 changes: 1 addition & 1 deletion src/terrama2/services/analysis/core/grid/Operator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace terrama2
\param dataSeriesName DataSeries name.
\return The current pixel value for the selected data series.
*/
TMANALYSISEXPORT double sample(const std::string& dataSeriesName, size_t bandIdx = 0);
TMANALYSISEXPORT double sample(const std::string& dataSeriesName, size_t bandIdx = 0, const std::string& date="");

TMANALYSISEXPORT double getValue(std::shared_ptr<te::rst::Raster> raster, std::shared_ptr<terrama2::core::SynchronizedInterpolator> interpolator, double column, double row, size_t bandIdx);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void terrama2::services::analysis::core::python::Grid::registerFunctions()
#pragma GCC diagnostic ignored "-Wunused-local-typedef"

// // Declaration needed for default parameter restriction
BOOST_PYTHON_FUNCTION_OVERLOADS(gridSample_overloads, terrama2::services::analysis::core::grid::sample, 1, 2)
BOOST_PYTHON_FUNCTION_OVERLOADS(gridSample_overloads, terrama2::services::analysis::core::grid::sample, 1, 3)

// closing "-Wunused-local-typedef" pragma
#pragma GCC diagnostic pop
Expand All @@ -81,7 +81,7 @@ void terrama2::services::analysis::core::python::Grid::registerGridFunctions()
scope gridScope = gridModule;

// export functions inside grid namespace
def("sample", terrama2::services::analysis::core::grid::sample, gridSample_overloads(args("dataSeriesName", "band"), "Grid sample operator."));
def("sample", terrama2::services::analysis::core::grid::sample, gridSample_overloads(args("dataSeriesName", "band", "date"), "Grid sample operator."));
}

// pragma to silence python macros warnings
Expand Down
Loading

0 comments on commit e9118b0

Please sign in to comment.