18#include <rapidjson/document.h>
19#include <rapidjson/stringbuffer.h>
20#include <rapidjson/writer.h>
24 using namespace rapidjson;
53 static bool profile() {
57 static string getDefaultServerURL() {
58 return "http://assignments.bridgesuncc.org";
61 bool jsonFlag =
false;
64 bool element_labelFlag =
false, link_labelFlag =
false;
66 bool post_visualization_link =
true;
68 string user_name = string(),
72 bool map_as_json =
false;
74 string description = string();
76 string title = string();
78 unsigned int assn_num = 0;
82 string server_url =
"http://assignments.bridgesuncc.org";
84 string BASE_URL = server_url +
"/assignments/";
87 string map_overlay_options[3] = {
"cartesian",
"albersusa",
"equirectangular"};
88 bool map_overlay =
false;
89 string coord_system_type =
"cartesian";
92 vector<double> wc_window;
94 unsigned int lastAssignNum = 0, subAssignNum = 0;
97 rapidjson::Writer<rapidjson::StringBuffer> json_obj;
116 Bridges (
const string& name,
const string& key) {
139 Bridges (
unsigned int num,
const string& name,
const string& key) {
155 element_labelFlag = flag;
166 link_labelFlag = flag;
176 return element_labelFlag;
186 return link_labelFlag;
208 post_visualization_link = link_url;
240 char* force = getenv(
"FORCE_BRIDGES_USERNAME");
241 if (force !=
nullptr) {
266 char* force = getenv(
"FORCE_BRIDGES_APIKEY");
267 if (force !=
nullptr) {
291 char* force = getenv(
"FORCE_BRIDGES_ASSIGNMENT");
292 if (force !=
nullptr) {
293 num = std::atoi(force);
298 if (assn_num != lastAssignNum) {
299 lastAssignNum = assn_num;
385 char* force = getenv(
"FORCE_BRIDGES_APISERVER");
386 if (force !=
nullptr) {
390 if (server_type ==
"live")
391 server_url =
"http://assignments.bridgesuncc.org";
392 else if (server_type ==
"clone")
393 server_url =
"http://assignments-clone.bridgesuncc.org";
394 else if (server_type ==
"games")
395 server_url =
"http://games.bridgesuncc.org";
396 else if (server_type ==
"games-clone")
397 server_url =
"http://games-clone.bridgesuncc.org";
398 else if (server_type ==
"local")
399 server_url =
"http://127.0.0.1:3000";
401 BASE_URL = server_url +
"/assignments/";
412 map_overlay = overlay_flag;
461 string url =
"http://static-data.bridgesuncc.org/api/us_map?state=";
481 std::transform(coord.begin(), coord.end(), coord.begin(), ::tolower);
482 if (coord ==
"cartesian" || coord ==
"albersusa" || coord ==
"equirectangular"
483 || coord ==
"window")
484 coord_system_type = coord;
486 cout <<
"Unrecognized coordinate system \'" + coord +
"\', defaulting to "
487 <<
"cartesian. Options:";
488 for (
auto proj : map_overlay_options)
490 coord_system_type =
"cartesian";
501 return coord_system_type;
514 void setWindow (
int xmin,
int xmax,
int ymin,
int ymax) {
515 setWindow(
double(xmin),
double(xmax),
double(ymin),
double(ymax));
528 void setWindow (
double xmin,
double xmax,
double ymin,
double ymax) {
530 wc_window.push_back(xmin);
531 wc_window.push_back(xmax);
532 wc_window.push_back(ymin);
533 wc_window.push_back(ymax);
548 std::chrono::time_point<std::chrono::system_clock> start;
549 std::chrono::time_point<std::chrono::system_clock> end;
550 std::chrono::time_point<std::chrono::system_clock> jsonbuild_start;
551 std::chrono::time_point<std::chrono::system_clock> jsonbuild_end;
552 std::chrono::time_point<std::chrono::system_clock> httprequest_start;
553 std::chrono::time_point<std::chrono::system_clock> httprequest_end;
556 start = std::chrono::system_clock::now();
562 if (subAssignNum == 99) {
563 cout <<
"#sub-assignments limit(99) exceeded, visualization not generated .."
568 cerr <<
"Error: Data Structure handle null! Visualization not generated.";
577 jsonbuild_start = std::chrono::system_clock::now();
584 Writer<StringBuffer> json_writer(sb);
587 string ds_part_json = ds_handle->getDataStructureRepresentation();
589 ds_part_json.erase(0, 1);
590 ds_json = getJSONHeader() + ds_part_json;
592 else if (ds_handle->
getDStype() ==
"us_map") {
602 ds_json = getJSONHeader() + ds_handle->getDataStructureRepresentation();
605 else if (ds_handle->
getDStype() ==
"world_map") {
607 ds_json = getJSONHeader() + ds_handle->getDataStructureRepresentation();
636 ds_json = getJSONHeader() + ds_handle->getDataStructureRepresentation();
639 jsonbuild_end = std::chrono::system_clock::now();
645 cout <<
"JSON[" + ds_handle->
getDStype() +
"]:\t" << ds_json << endl;
649 httprequest_start = std::chrono::system_clock::now();
652 ServerComm::makeRequest(BASE_URL + to_string(
getAssignment()) +
"." +
653 (subAssignNum > 9 ?
"" :
"0") + to_string(subAssignNum) +
"?apikey=" +
getApiKey() +
654 "&username=" +
getUserName(), {
"Content-Type: text/plain"}, ds_json);
656 if (post_visualization_link) {
657 cout <<
"Success: Assignment posted to the server. " << endl
658 <<
"Check out your visualization at:" << endl << endl
663 catch (
const string& error_str) {
664 cerr <<
"\nPosting assignment to the server failed!" << endl
665 << error_str << endl << endl;
666 cerr <<
"Provided Bridges Credentials:" << endl <<
673 cerr <<
"\nPosting assignment to the server failed!" << endl;
675 cerr <<
"Provided Bridges Credentials are incorrect:" << endl <<
676 "\t ServerURL: " << getServerURL() << endl <<
682 cerr <<
"Assignment is too large." << endl;
683 cerr <<
"In general the assignment should be smaller than 16MB once serialized to JSON." << endl;
686 std::cerr << he.
what() << endl;
691 httprequest_end = std::chrono::system_clock::now();
694 end = std::chrono::system_clock::now();
696 std::chrono::duration<double> totaltime = end - start;
697 std::chrono::duration<double> jsonbuildtime = jsonbuild_end - jsonbuild_start;
698 std::chrono::duration<double> httptime = httprequest_end - httprequest_start;
699 std::cerr <<
"total visualize() time:" << totaltime.count() <<
" seconds"
700 <<
" (including JSON build time: " << jsonbuildtime.count() <<
" seconds"
701 <<
" and HTTP request time: " << httptime.count() <<
" seconds)."
707 string getServerURL()
const {
711 string getJSONHeader(Document& d) {
714 key.SetString(
"visual", d.GetAllocator());
715 value.SetString(ds_handle->
getDStype().c_str(), d.GetAllocator());
716 d.AddMember(key, value, d.GetAllocator());
718 key.SetString(
"title", d.GetAllocator());
719 value.SetString(
getTitle().c_str(), d.GetAllocator());
720 d.AddMember(key, value, d.GetAllocator());
722 key.SetString(
"description", d.GetAllocator());
724 d.AddMember(key, value, d.GetAllocator());
726 key.SetString(
"map", d.GetAllocator());
727 value.SetString(map.c_str(), d.GetAllocator());
728 d.AddMember(key, value, d.GetAllocator());
730 key.SetString(
"map_overlay", d.GetAllocator());
731 value.SetBool((map_overlay) ?
true :
false);
732 d.AddMember(key, value, d.GetAllocator());
734 key.SetString(
"element_label_flag", d.GetAllocator());
735 value.SetBool(element_labelFlag);
736 d.AddMember(key, value, d.GetAllocator());
738 key.SetString(
"link_label_flag", d.GetAllocator());
739 value.SetBool(link_labelFlag);
740 d.AddMember(key, value, d.GetAllocator());
742 key.SetString(
"coord_system_type", d.GetAllocator());
744 d.AddMember(key, value, d.GetAllocator());
746 if (wc_window.size() == 4) {
749 Value w_array(kArrayType);
750 w_array.PushBack(v.SetDouble(wc_window[0]), d.GetAllocator());
751 w_array.PushBack(v.SetDouble(wc_window[1]), d.GetAllocator());
752 w_array.PushBack(v.SetDouble(wc_window[2]), d.GetAllocator());
753 w_array.PushBack(v.SetDouble(wc_window[3]), d.GetAllocator());
755 d.AddMember(
"window", w_array, d.GetAllocator());
759 Writer<StringBuffer> writer(s);
762 return s.GetString();
765 string getJSONHeader () {
773 ((map_overlay) ?
"true" :
"false") +
COMMA;
779 json_header +=
QUOTE +
"element_label_flag" +
QUOTE +
COLON + ((element_labelFlag) ?
"true" :
"false") +
COMMA +
784 if (wc_window.size() == 4) {
786 json_header += std::to_string(wc_window[0]) +
COMMA +
787 std::to_string(wc_window[1]) +
COMMA +
788 std::to_string(wc_window[2]) +
COMMA + std::to_string(wc_window[3]);
This class contains methods to connect and transmit a user's data structure representation to the Bri...
Definition: Bridges.h:51
string getVisualizeURL() const
Definition: Bridges.h:536
void setWindow(int xmin, int xmax, int ymin, int ymax)
Definition: Bridges.h:514
bool getLinkLabelFlag() const
Return status of flag for link labels.
Definition: Bridges.h:185
void setMap(string map_str)
Sets the type of map overlay to use.
Definition: Bridges.h:423
Bridges(const string &name, const string &key)
constructor to bridges
Definition: Bridges.h:116
void setMapOverlay(bool overlay_flag)
Turns on map overlay for subsequent visualizations - used with location specific datasets.
Definition: Bridges.h:411
void visualize()
Definition: Bridges.h:547
const string & getApiKey() const
Definition: Bridges.h:252
void setUserName(string name)
Set user name.
Definition: Bridges.h:239
bool getElementLabelFlag() const
status of flag for element labels
Definition: Bridges.h:175
void setDataStructure(DataStructure &ds)
Definition: Bridges.h:360
void setMap(const Map &map)
Definition: Bridges.h:447
const string & getDescription() const
Definition: Bridges.h:330
void setServer(string server_type)
Definition: Bridges.h:384
void setApiKey(string key)
Set API key credentials.
Definition: Bridges.h:265
const string & getCoordSystemType()
Definition: Bridges.h:500
void setDataStructure(DataStructure *ds)
Definition: Bridges.h:349
void postVisualizationLink(bool link_url)
Definition: Bridges.h:207
void setMapAsJSON(bool b)
Definition: Bridges.h:452
void setWindow(double xmin, double xmax, double ymin, double ymax)
sets the world coordinate window defining the space of the user defined objects (or nodes)
Definition: Bridges.h:528
void setDescription(const string &descr)
Definition: Bridges.h:338
unsigned int getAssignment() const
Definition: Bridges.h:278
Bridges(unsigned int num, const string &name, const string &key)
constructor to bridges
Definition: Bridges.h:139
Bridges()
Definition: Bridges.h:101
const string & getTitle() const
Definition: Bridges.h:310
void setLinkLabelFlag(bool flag)
Flag that controls if labels of links(edges) are to be turned on/off.
Definition: Bridges.h:165
void setTitle(const string &t)
Definition: Bridges.h:321
void setAssignment(unsigned int num)
Definition: Bridges.h:290
void setCoordSystemType(string coord)
Definition: Bridges.h:480
const string & getUserName() const
Definition: Bridges.h:227
void setJSONFlag(bool flag)
Definition: Bridges.h:216
string getMap(vector< string > states)
Definition: Bridges.h:457
bool getJSONFlag() const
Definition: Bridges.h:195
void setMap(const Map *map)
Definition: Bridges.h:436
void setElementLabelFlag(bool flag)
Flag that controls if labels of elements (nodes) are to be turned on.
Definition: Bridges.h:154
DataStructure * getDataStructure()
Definition: Bridges.h:369
This class provides an API to various data sources used in BRIDGES.
Definition: DataSource.h:69
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:74
virtual const string getDStype() const =0
Abstract class for Map API.
Definition: Map.h:16
virtual const string getProjection() const =0
virtual const bool getOverlay() const =0
virtual const string getMapRepresentation() const =0
This class provides an API to building, displaying and manipulating US maps and counties in BRIDGES.
Definition: USMap.h:56
This class provides an API to building, displaying and manipulating World maps and countries in BRIDG...
Definition: WorldMap.h:51
This is meant to be an internal class, not something that the library user will use.
Definition: SocketConnection.h:48
std::string JSONencode(const T &d)
Definition: JSONutil.h:38
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
const string COLON
Definition: DataStructure.h:52
const string OPEN_BOX
Definition: DataStructure.h:55
const string COMMA
Definition: DataStructure.h:51
const string OPEN_CURLY
Definition: DataStructure.h:53
const string CLOSE_BOX
Definition: DataStructure.h:56
const string QUOTE
Definition: DataStructure.h:50
Definition: bridges_exception.h:18
long httpcode
Definition: bridges_exception.h:20
Definition: bridges_exception.h:6
virtual const char * what() const noexcept
Definition: bridges_exception.h:13