ListSessions returns a list of sessions for the dashboard
(c *gin.Context)
| 537 | |
| 538 | // ListSessions returns a list of sessions for the dashboard |
| 539 | func (h *DashboardHandler) ListSessions(c *gin.Context) { |
| 540 | ctx := c.Request.Context() |
| 541 | |
| 542 | // 解析分页参数 |
| 543 | limit := 50 |
| 544 | offset := 0 |
| 545 | |
| 546 | if limitStr := c.Query("limit"); limitStr != "" { |
| 547 | if l, err := strconv.Atoi(limitStr); err == nil && l > 0 { |
| 548 | limit = l |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | if offsetStr := c.Query("offset"); offsetStr != "" { |
| 553 | if o, err := strconv.Atoi(offsetStr); err == nil && o >= 0 { |
| 554 | offset = o |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | // 从 store 获取 sessions |
| 559 | sessions := []SessionSummary{} |
| 560 | |
| 561 | // 尝试从 sessions bucket 获取数据 |
| 562 | // 注意:List 返回的是完整的 JSON 对象列表,不是 key 列表 |
| 563 | items, err := (*h.store).List(ctx, "sessions") |
| 564 | if err != nil { |
| 565 | // bucket 不存在或为空,返回空列表 |
| 566 | logging.Info(ctx, "dashboard.sessions.list.empty", map[string]any{ |
| 567 | "error": err.Error(), |
| 568 | }) |
| 569 | c.JSON(http.StatusOK, gin.H{ |
| 570 | "success": true, |
| 571 | "data": SessionListResult{ |
| 572 | Sessions: sessions, |
| 573 | Total: 0, |
| 574 | HasMore: false, |
| 575 | }, |
| 576 | }) |
| 577 | return |
| 578 | } |
| 579 | |
| 580 | // 解析每个 session 记录 |
| 581 | for i, item := range items { |
| 582 | if i < offset { |
| 583 | continue |
| 584 | } |
| 585 | if len(sessions) >= limit { |
| 586 | break |
| 587 | } |
| 588 | |
| 589 | // 使用 DecodeValue 将 any 转换为 SessionRecord |
| 590 | var record SessionRecord |
| 591 | if err := store.DecodeValue(item, &record); err != nil { |
| 592 | logging.Warn(ctx, "dashboard.sessions.decode.error", map[string]any{ |
| 593 | "error": err.Error(), |
| 594 | }) |
| 595 | continue |
| 596 | } |