/*
* Copyright (c) 2014 Devicescape Software, Inc.
* This file is part of aws_dynamo, a C library for AWS DynamoDB.
*
* aws_dynamo is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* aws_dynamo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with aws_dynamo. If not, see .
*/
#include
#include
#include
#include
#include
#include
#define SOME_ID_ATTRIBUTE_NAME "some_id"
#define SCORE_ATTRIBUTE_NAME "some_score"
#define SCORE_ATTRIBUTE_INDEX 0
namespace boss{
namespace Dig_CP{
double getScores::retrieveScore(struct aws_handle *aws, std::string tableName, std::string wtp)
{
struct aws_dynamo_attribute attributes[] = {
{
/* .name = */ SCORE_ATTRIBUTE_NAME,
/* .name_len = */ strlen(SCORE_ATTRIBUTE_NAME),
/* .type = */ AWS_DYNAMO_STRING,
},
};
/* C++ doesn't allow for initializing a particular union member.
Set the value unions's type member after the structure above
has been initialized. */
//attributes[LAST_SEEN_ATTRIBUTE_INDEX].value.number.type = AWS_DYNAMO_NUMBER_INTEGER;
struct aws_dynamo_get_item_response *r = NULL;
struct aws_dynamo_attribute *score;
wtp.append("\n");
std::string req ;
req.append("{\"TableName\":\"");
req.append(tableName);
req.append("\",\"Key\":{\"HashKeyElement\":{\"S\":\"");
req.append(wtp);
req.append("\"}},\"AttributesToGet\":[\"");
req.append(SCORE_ATTRIBUTE_NAME);
req.append("\"]}");
req.append("\n");
try
{
r = aws_dynamo_get_item(aws, req.c_str(), attributes,
sizeof(attributes) / sizeof(attributes[0]));
}
catch(...)
{
_pLogger->info ( "exception occured for wtp:", req.c_str());
}
if (r == NULL) {
_pLogger->debug ( "nothing found");
return -1;
}
if (r->item.attributes == NULL) {
_pLogger->debug ( "not found");
return -1;
}
score = &(r->item.attributes[SCORE_ATTRIBUTE_INDEX]);
_pLogger->debug ( "score = %s",score->value.string);
/* prints: "John Doe was last seen at 1391883778." */
//std::cout << score->value.string << " score "<< std::endl;
std::string scoreStr = score->value.string;
aws_dynamo_free_get_item_response(r);
return atof(scoreStr.c_str());
}
getScores::getScores()
{
log4cpp::Category& logger = log4cpp::Category::getInstance("getScores");
_pLogger = &logger;
}
double getScores::getScoresWRTWTP(std::string wtp)
{
struct aws_handle *aws = aws_init(NULL, NULL);
if (aws == NULL) {
return 0;
}
_pLogger->debug ( "wtp = %s ",wtp.c_str());
std::string tableName = "good_wtp";
//std::string wtp = "05e843ad-1840-4759-8223-5717f0585fc3";
double score = retrieveScore(aws,tableName,wtp);
aws_deinit(aws);
_pLogger->debug ( "score float= %f",score);
return score;
}
}
}