MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / rg_to_json

Function rg_to_json

src/ui/layout_regions.c:924–980  ·  view source on GitHub ↗

Serialize the build to the level=regions payload. */

Source from the content-addressed store, hash-verified

922
923/* Serialize the build to the level=regions payload. */
924static char *rg_to_json(const rg_build_t *b, const float *xyz, const float *size) {
925 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
926 if (!doc)
927 return NULL;
928 yyjson_mut_val *root = yyjson_mut_obj(doc);
929 yyjson_mut_doc_set_root(doc, root);
930 yyjson_mut_obj_add_str(doc, root, "level", "regions");
931 yyjson_mut_obj_add_str(doc, root, "method", b->method);
932 yyjson_mut_obj_add_int(doc, root, "total_nodes", b->total_nodes);
933 yyjson_mut_obj_add_int(doc, root, "unmapped_nodes", b->unmapped_nodes);
934 yyjson_mut_val *arr = yyjson_mut_arr(doc);
935 for (int r = 0; r < b->region_count; r++) {
936 const region_t *rg = &b->regions[r];
937 yyjson_mut_val *o = yyjson_mut_obj(doc);
938 yyjson_mut_obj_add_int(doc, o, "id", r);
939 yyjson_mut_obj_add_strcpy(doc, o, "name", rg->name ? rg->name : "?");
940 if (rg->hub)
941 yyjson_mut_obj_add_strcpy(doc, o, "hub", rg->hub);
942 if (rg->why)
943 yyjson_mut_obj_add_strcpy(doc, o, "why", rg->why);
944 yyjson_mut_obj_add_int(doc, o, "files", rg->files);
945 yyjson_mut_obj_add_int(doc, o, "members", rg->members);
946 double denom = (double)rg->internal_edges + (double)rg->boundary_edges;
947 yyjson_mut_obj_add_real(doc, o, "cohesion",
948 denom > 0 ? (double)rg->internal_edges / denom : 0.0);
949 yyjson_mut_val *tops = yyjson_mut_arr(doc);
950 for (int t = 0; t < rg->top_count; t++)
951 yyjson_mut_arr_add_strcpy(doc, tops, rg->top_nodes[t]);
952 yyjson_mut_obj_add_val(doc, o, "top_nodes", tops);
953 yyjson_mut_obj_add_real(doc, o, "x", xyz[r * 3 + 0]);
954 yyjson_mut_obj_add_real(doc, o, "y", xyz[r * 3 + 1]);
955 yyjson_mut_obj_add_real(doc, o, "z", xyz[r * 3 + 2]);
956 yyjson_mut_obj_add_real(doc, o, "size", size[r]);
957 char color[8];
958 snprintf(color, sizeof(color), "#%06x", rg_wheel_color(r, b->region_count));
959 yyjson_mut_obj_add_strcpy(doc, o, "color", color);
960 yyjson_mut_arr_append(arr, o);
961 }
962 yyjson_mut_obj_add_val(doc, root, "regions", arr);
963 yyjson_mut_val *earr = yyjson_mut_arr(doc);
964 for (int s = 0; s < b->region_count; s++) {
965 for (int t = 0; t < b->region_count; t++) {
966 long long w = b->edge_w[s * b->region_count + t];
967 if (w <= 0)
968 continue;
969 yyjson_mut_val *e = yyjson_mut_obj(doc);
970 yyjson_mut_obj_add_int(doc, e, "source", s);
971 yyjson_mut_obj_add_int(doc, e, "target", t);
972 yyjson_mut_obj_add_int(doc, e, "weight", w);
973 yyjson_mut_arr_append(earr, e);
974 }
975 }
976 yyjson_mut_obj_add_val(doc, root, "edges", earr);
977 char *json = yyjson_mut_write_opts(doc, 0, cbm_json_writer_allocator(), NULL, NULL);
978 yyjson_mut_doc_free(doc);
979 return json;
980}
981

Callers 1

rg_rebuild_lockedFunction · 0.85

Calls 2

rg_wheel_colorFunction · 0.85

Tested by

no test coverage detected