17 #include <rapidjson/document.h>
18 #include <rapidjson/stringbuffer.h>
19 #include <rapidjson/writer.h>
23 using namespace rapidjson;
52 static bool profile() {
56 static string getDefaultServerURL() {
57 return "http://bridges-cs.herokuapp.com";
60 bool jsonFlag =
false;
63 bool element_labelFlag =
false, link_labelFlag =
false;
65 bool post_visualization_link =
true;
67 string user_name = string(),
71 bool map_as_json =
false;
73 string description = string();
75 string title = string();
77 unsigned int assn_num = 0;
81 string server_url =
"http://bridges-cs.herokuapp.com";
83 string BASE_URL = server_url +
"/assignments/";
86 string map_overlay_options[3] = {
"cartesian",
"albersusa",
"equirectangular"};
87 bool map_overlay =
false;
88 string coord_system_type =
"cartesian";
91 vector<double> wc_window;
93 unsigned int lastAssignNum = 0, subAssignNum = 0;
96 rapidjson::Writer<rapidjson::StringBuffer> json_obj;
115 Bridges (
const string& name,
const string& key) {
138 Bridges (
unsigned int num,
const string& name,
const string& key) {
154 element_labelFlag = flag;
165 link_labelFlag = flag;
175 return element_labelFlag;
185 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;
359 setDataStructure(&ds);
383 char* force = getenv(
"FORCE_BRIDGES_APISERVER");
384 if (force !=
nullptr) {
388 if (server_type ==
"live")
389 server_url =
"http://bridges-cs.herokuapp.com";
390 else if (server_type ==
"clone")
391 server_url =
"http://bridges-clone.herokuapp.com";
392 else if (server_type ==
"games")
393 server_url =
"http://bridges-games.herokuapp.com";
394 else if (server_type ==
"local")
395 server_url =
"http://127.0.0.1:3000";
397 BASE_URL = server_url +
"/assignments/";
408 map_overlay = overlay_flag;
461 string url =
"http://bridgesdata.herokuapp.com/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);
537 return BASE_URL + to_string(getAssignment()) +
"/" + getUserName();
547 std::chrono::time_point<std::chrono::system_clock> start;
548 std::chrono::time_point<std::chrono::system_clock> end;
549 std::chrono::time_point<std::chrono::system_clock> jsonbuild_start;
550 std::chrono::time_point<std::chrono::system_clock> jsonbuild_end;
551 std::chrono::time_point<std::chrono::system_clock> httprequest_start;
552 std::chrono::time_point<std::chrono::system_clock> httprequest_end;
555 start = std::chrono::system_clock::now();
557 if (getAssignment() != lastAssignNum) {
558 lastAssignNum = getAssignment();
561 if (subAssignNum == 99) {
562 cout <<
"#sub-assignments limit(99) exceeded, visualization not generated .."
567 cerr <<
"Error: Data Structure handle null! Visualization not generated.";
576 jsonbuild_start = std::chrono::system_clock::now();
583 Writer<StringBuffer> json_writer(sb);
586 string ds_part_json = ds_handle->getDataStructureRepresentation();
588 ds_part_json.erase(0, 1);
589 ds_json = getJSONHeader() + ds_part_json;
591 else if (ds_handle->
getDStype() ==
"us_map") {
592 setMap((
USMap*) ds_handle);
601 ds_json = getJSONHeader() + ds_handle->getDataStructureRepresentation();
630 ds_json = getJSONHeader() + ds_handle->getDataStructureRepresentation();
633 jsonbuild_end = std::chrono::system_clock::now();
639 cout <<
"JSON[" + ds_handle->
getDStype() +
"]:\t" << ds_json << endl;
643 httprequest_start = std::chrono::system_clock::now();
646 ServerComm::makeRequest(BASE_URL + to_string(getAssignment()) +
"." +
647 (subAssignNum > 9 ?
"" :
"0") + to_string(subAssignNum) +
"?apikey=" + getApiKey() +
648 "&username=" + getUserName(), {
"Content-Type: text/plain"}, ds_json);
650 if (post_visualization_link) {
651 cout <<
"Success: Assignment posted to the server. " << endl
652 <<
"Check out your visualization at:" << endl << endl
653 << getVisualizeURL() << endl << endl;
657 catch (
const string& error_str) {
658 cerr <<
"\nPosting assignment to the server failed!" << endl
659 << error_str << endl << endl;
660 cerr <<
"Provided Bridges Credentials:" << endl <<
661 "\t User Name: " << getUserName() << endl <<
662 "\t API Key: " << getApiKey() << endl <<
663 "\t Assignment Number: " << getAssignment() << endl;
666 cerr <<
"\nPosting assignment to the server failed!" << endl;
668 cerr <<
"Provided Bridges Credentials are incorrect:" << endl <<
669 "\t ServerURL: " << getServerURL() << endl <<
670 "\t User Name: " << getUserName() << endl <<
671 "\t API Key: " << getApiKey() << endl <<
672 "\t Assignment Number: " << getAssignment() << endl;
675 cerr <<
"Assignment is too large." << endl;
676 cerr <<
"In general the assignment should be smaller than 16MB once serialized to JSON." << endl;
679 std::cerr << he.
what() << endl;
683 httprequest_end = std::chrono::system_clock::now();
686 end = std::chrono::system_clock::now();
688 std::chrono::duration<double> totaltime = end - start;
689 std::chrono::duration<double> jsonbuildtime = jsonbuild_end - jsonbuild_start;
690 std::chrono::duration<double> httptime = httprequest_end - httprequest_start;
691 std::cerr <<
"total visualize() time:" << totaltime.count() <<
" seconds"
692 <<
" (including JSON build time: " << jsonbuildtime.count() <<
" seconds"
693 <<
" and HTTP request time: " << httptime.count() <<
" seconds)."
699 string getServerURL()
const {
703 string getJSONHeader(Document& d) {
706 key.SetString(
"visual", d.GetAllocator());
707 value.SetString(ds_handle->
getDStype().c_str(), d.GetAllocator());
708 d.AddMember(key, value, d.GetAllocator());
710 key.SetString(
"title", d.GetAllocator());
711 value.SetString(getTitle().c_str(), d.GetAllocator());
712 d.AddMember(key, value, d.GetAllocator());
714 key.SetString(
"description", d.GetAllocator());
715 value.SetString(getDescription().c_str(), d.GetAllocator());
716 d.AddMember(key, value, d.GetAllocator());
718 key.SetString(
"map", d.GetAllocator());
719 value.SetString(map.c_str(), d.GetAllocator());
720 d.AddMember(key, value, d.GetAllocator());
722 key.SetString(
"map_overlay", d.GetAllocator());
723 value.SetBool((map_overlay) ?
true :
false);
724 d.AddMember(key, value, d.GetAllocator());
726 key.SetString(
"element_label_flag", d.GetAllocator());
727 value.SetBool(element_labelFlag);
728 d.AddMember(key, value, d.GetAllocator());
730 key.SetString(
"link_label_flag", d.GetAllocator());
731 value.SetBool(link_labelFlag);
732 d.AddMember(key, value, d.GetAllocator());
734 key.SetString(
"coord_system_type", d.GetAllocator());
735 value.SetString(getCoordSystemType().c_str(), d.GetAllocator());
736 d.AddMember(key, value, d.GetAllocator());
738 if (wc_window.size() == 4) {
741 Value w_array(kArrayType);
742 w_array.PushBack(v.SetDouble(wc_window[0]), d.GetAllocator());
743 w_array.PushBack(v.SetDouble(wc_window[1]), d.GetAllocator());
744 w_array.PushBack(v.SetDouble(wc_window[2]), d.GetAllocator());
745 w_array.PushBack(v.SetDouble(wc_window[3]), d.GetAllocator());
747 d.AddMember(
"window", w_array, d.GetAllocator());
751 Writer<StringBuffer> writer(s);
754 return s.GetString();
757 string getJSONHeader () {
765 ((map_overlay) ?
"true" :
"false") +
COMMA;
771 json_header +=
QUOTE +
"element_label_flag" +
QUOTE +
COLON + ((element_labelFlag) ?
"true" :
"false") +
COMMA +
776 if (wc_window.size() == 4) {
778 json_header += std::to_string(wc_window[0]) +
COMMA +
779 std::to_string(wc_window[1]) +
COMMA +
780 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:50
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:184
void setMap(string map_str)
Sets the type of map overlay to use.
Definition: Bridges.h:422
Bridges(const string &name, const string &key)
constructor to bridges
Definition: Bridges.h:115
void setMapOverlay(bool overlay_flag)
Turns on map overlay for subsequent visualizations - used with location specific datasets.
Definition: Bridges.h:407
void visualize()
Definition: Bridges.h:546
void setUserName(string name)
Set user name.
Definition: Bridges.h:239
bool getElementLabelFlag() const
status of flag for element labels
Definition: Bridges.h:174
void setDataStructure(DataStructure &ds)
Definition: Bridges.h:358
void setMap(const Map &map)
Definition: Bridges.h:446
void setServer(string server_type)
Definition: Bridges.h:382
void setApiKey(string key)
Set API key credentials.
Definition: Bridges.h:265
DataStructure * getDataStructure()
Definition: Bridges.h:367
void setDataStructure(DataStructure *ds)
Definition: Bridges.h:347
void postVisualizationLink(bool link_url)
Definition: Bridges.h:207
const string & getTitle() const
Definition: Bridges.h:310
void setMapAsJSON(bool b)
Definition: Bridges.h:452
const string & getApiKey() const
Definition: Bridges.h:252
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:336
unsigned int getAssignment() const
Definition: Bridges.h:278
Bridges(unsigned int num, const string &name, const string &key)
constructor to bridges
Definition: Bridges.h:138
Bridges()
Definition: Bridges.h:100
const string & getCoordSystemType()
Definition: Bridges.h:500
void setLinkLabelFlag(bool flag)
Flag that controls if labels of links(edges) are to be turned on/off.
Definition: Bridges.h:164
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:194
void setMap(const Map *map)
Definition: Bridges.h:435
const string & getDescription() const
Definition: Bridges.h:329
void setElementLabelFlag(bool flag)
Flag that controls if labels of elements (nodes) are to be turned on.
Definition: Bridges.h:153
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:52
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
Support for drawing Bar charts.
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: ServerComm.h:17
virtual const char * what() const noexcept
Definition: ServerComm.h:35
long httpcode
Definition: ServerComm.h:19