| 490 | } |
| 491 | |
| 492 | char *cbm_activity_agents(sqlite3 *db, const char *project, int64_t after, int limit) { |
| 493 | if (!db) |
| 494 | return NULL; |
| 495 | if (limit < 1 || limit > 500) |
| 496 | limit = 200; |
| 497 | yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); |
| 498 | yyjson_mut_val *obj = yyjson_mut_obj(doc), *rows = yyjson_mut_arr(doc); |
| 499 | yyjson_mut_doc_set_root(doc, obj); |
| 500 | yyjson_mut_obj_add_val(doc, obj, "events", rows); |
| 501 | if (!generation(db, doc, obj)) { |
| 502 | yyjson_mut_doc_free(doc); |
| 503 | return NULL; |
| 504 | } |
| 505 | sqlite3_stmt *st = NULL; |
| 506 | int64_t oldest = 0, newest = 0, count = 0; |
| 507 | if (sqlite3_prepare_v2(db, |
| 508 | "SELECT coalesce(min(id),0),coalesce(max(id),0),count(*) FROM " |
| 509 | "agent_events WHERE project=?1", |
| 510 | -1, &st, NULL) != SQLITE_OK) |
| 511 | goto fail; |
| 512 | sqlite3_bind_text(st, 1, project, -1, SQLITE_STATIC); |
| 513 | if (sqlite3_step(st) == SQLITE_ROW) { |
| 514 | oldest = sqlite3_column_int64(st, 0); |
| 515 | newest = sqlite3_column_int64(st, 1); |
| 516 | count = sqlite3_column_int64(st, 2); |
| 517 | } else |
| 518 | goto fail; |
| 519 | sqlite3_finalize(st); |
| 520 | st = NULL; |
| 521 | int64_t pruned = 0; |
| 522 | if (sqlite3_prepare_v2(db, "SELECT pruned_through FROM agent_retention WHERE project=?1", -1, |
| 523 | &st, NULL) != SQLITE_OK) |
| 524 | goto fail; |
| 525 | sqlite3_bind_text(st, 1, project, -1, SQLITE_STATIC); |
| 526 | int prune_status = sqlite3_step(st); |
| 527 | if (prune_status == SQLITE_ROW) |
| 528 | pruned = sqlite3_column_int64(st, 0); |
| 529 | else if (prune_status != SQLITE_DONE) |
| 530 | goto fail; |
| 531 | sqlite3_finalize(st); |
| 532 | st = NULL; |
| 533 | bool reset = after > newest && newest > 0; |
| 534 | if (reset) |
| 535 | after = 0; |
| 536 | if (sqlite3_prepare_v2( |
| 537 | db, |
| 538 | "SELECT id,payload FROM agent_events WHERE project=?1 AND id>?2 ORDER BY id LIMIT ?3", |
| 539 | -1, &st, NULL) != SQLITE_OK) |
| 540 | goto fail; |
| 541 | sqlite3_bind_text(st, 1, project, -1, SQLITE_STATIC); |
| 542 | sqlite3_bind_int64(st, 2, after); |
| 543 | sqlite3_bind_int(st, 3, limit); |
| 544 | int64_t cursor = after; |
| 545 | int row_status; |
| 546 | while ((row_status = sqlite3_step(st)) == SQLITE_ROW) { |
| 547 | cursor = sqlite3_column_int64(st, 0); |
| 548 | const char *payload = (const char *)sqlite3_column_text(st, 1); |
| 549 | yyjson_doc *row = yyjson_read(payload, strlen(payload), 0); |
no test coverage detected