void GCGV_ReqGetNodes::BuildReply() {
    size_t pwdLen = parent->PWD().ToString().length() +1;
    reply.StartArray("nodes");
        for (SortedNodes::reverse_iterator it = sortedNodes.rbegin();
             it != sortedNodes.rend();
             ++it)
        {
            NodePtr node = it->second;

            long total = node->Costs()[0];
            long average = double(total) / node->Calls();
            std::string path =
                    GCGV_Callgraph::GetPath(node).ToString();

            if (path.length() > pwdLen) {
                path = path.c_str() + pwdLen;
            }

            reply.StartAnonymousObject();
                reply.Add("path", path);
                reply.Add("calls", node->Calls());
                reply.Add("cost", total);
                reply.Add("avCost", average);
            reply.EndObject();
        }
    reply.EndArray();
}
void GCGV_ReqGetNodes::SortedNodes::AddNode(NodePtr node, SORT_BY sort) {
    long idx = 0;

    switch (sort) {
        case COST:
            idx = node->Costs()[0];
            break;
        case AV_COST:
            idx = node->Costs()[0] / node->Calls();
            break;
        default:
            throw InvalidRequestException { 0, "Invalid Sort Type" };
    }

    insert(SortedNodes::value_type(idx, node));
}
void GCGVCallgraph_ReqGraph::AddNode(NodePtr node, size_t node_depth) {
    if (node_depth == request.Get<depth>()) {
       // The top-level node lives in the top-level JSON, so does not need an
       // object wrapper
    } else {
        builder.StartAnonymousObject();
    }

    builder.Add("name",node->Name());
    if (node->Name().length() > 50) {
        builder.Add("shortName",node->Name().substr(0,50));
    } else  {
        builder.Add("shortName",node->Name());
    }
    builder.Add("cost",node->Costs()[0]);

    if (node_depth>0 && node->NumChildren() > 0)
    {
        builder.StartArray("children");
        node-> ForEach([&] (NodePtr child) -> void {
            this->AddNode(child,node_depth-1);
        });
        builder.EndArray();

    }

    if (node_depth == request.Get<depth>()) {
       // The top-level node lives in the top-level JSON, so does not need an
       // object wrapper
    } else {
        builder.EndObject();
    }

}