feat(cli): add sync logs listing commands for the user CLI (#18195)

### Summary
```
RAGFlow(api/default)> LIST SYNC LOGS WITH PAGE_SIZE 2 PAGE 2;
Total: 764
+----------------------------------+-------------------------+-------------+-----------+----------------------------------+----------------------------------+---------+------------------+------------+--------------+--------+-----------+---------------------+--------------------+---------------------+
| connector_id                     | docs_removed_from_index | error_count | error_msg | id                               | kb_id                            | kb_name | new_docs_indexed | prune_freq | refresh_freq | status | task_type | time_started        | total_docs_indexed | update_date         |
+----------------------------------+-------------------------+-------------+-----------+----------------------------------+----------------------------------+---------+------------------+------------+--------------+--------+-----------+---------------------+--------------------+---------------------+
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |           | fb55b5f1fb754475834cd9eac8b52054 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:45 | 16                 | 2026-08-13 09:49:46 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |           | 3cad779d7d2e4efb8d8d2dbf05d45653 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:42 | 16                 | 2026-08-13 09:49:43 |
+----------------------------------+-------------------------+-------------+-----------+----------------------------------+----------------------------------+---------+------------------+------------+--------------+--------+-----------+---------------------+--------------------+---------------------+

```
```
RAGFlow(api/default)> LIST SYNC LOGS;
Total: 764
+----------------------------------+-------------------------+-------------+--------------------------------------------------------------------------------------------+----------------------------------+----------------------------------+---------+------------------+------------+--------------+--------+-----------+---------------------+--------------------+---------------------+
| connector_id                     | docs_removed_from_index | error_count | error_msg                                                                                  | id                               | kb_id                            | kb_name | new_docs_indexed | prune_freq | refresh_freq | status | task_type | time_started        | total_docs_indexed | update_date         |
+----------------------------------+-------------------------+-------------+--------------------------------------------------------------------------------------------+----------------------------------+----------------------------------+---------+------------------+------------+--------------+--------+-----------+---------------------+--------------------+---------------------+
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 1           | sync task failed: failed to fetch URL: Get "https://xxxxxxxxxxxx/rss.xml": EOF             | 87592e5623fc4284bf69b9290f322718 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 4      | sync      | 2026-08-13 09:49:48 | 16                 | 2026-08-13 09:51:01 |
| 570138201cf44e1180711f1ebdf8492f | 0                       | 1           | sync task failed: failed to fetch URL: Get "https://www.ruanyifeng.com/blog/atom.xml": EOF | 456adbbf21a147af99adfce4405abd99 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 1            | 4      | sync      | 2026-08-13 09:50:00 | 3                  | 2026-08-13 09:50:18 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | fb55b5f1fb754475834cd9eac8b52054 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:45 | 16                 | 2026-08-13 09:49:46 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | 3cad779d7d2e4efb8d8d2dbf05d45653 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:42 | 16                 | 2026-08-13 09:49:43 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | dd09320aa5b34d6cbcfa7b03ede68c4a | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:36 | 16                 | 2026-08-13 09:49:38 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | afbcee73240f420ca1997e6506f387fb | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:33 | 16                 | 2026-08-13 09:49:34 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | 7d018146a0f14a25b7263d5980fdc30f | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:30 | 16                 | 2026-08-13 09:49:32 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | 520e0d6f421e40628ab541fc4fee1a69 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:27 | 16                 | 2026-08-13 09:49:28 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | 3d3ce49825de4ef99642e7994ae954e3 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:21 | 16                 | 2026-08-13 09:49:22 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | 1c511f9b4a7c425fb30a956589ca56fa | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:18 | 16                 | 2026-08-13 09:49:19 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | b9ab73c3f48c4f47a84fd55b542b32b4 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:15 | 16                 | 2026-08-13 09:49:16 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | db0cad90a7744d758b24d2e3d3828963 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:12 | 16                 | 2026-08-13 09:49:13 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | a501eadb2cce452187f3c86431352c70 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:08 | 16                 | 2026-08-13 09:49:10 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | b4265a3b7baf4466a525b91a9fe21b71 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:03 | 16                 | 2026-08-13 09:49:05 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | a6ad590931c046dda5fc3222b4010c06 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:00 | 16                 | 2026-08-13 09:49:02 |
......
+----------------------------------+-------------------------+-------------+--------------------------------------------------------------------------------------------+----------------------------------+----------------------------------+---------+------------------+------------+--------------+--------+-----------+---------------------+--------------------+---------------------+

```
list all logs
```
RAGFlow(api/default)> LIST DATASET 'test' SYNC LOGS;
Total: 764
+----------------------------------+-------------------------+-------------+--------------------------------------------------------------------------------------------+----------------------------------+----------------------------------+---------+------------------+------------+--------------+--------+-----------+---------------------+--------------------+---------------------+
| connector_id                     | docs_removed_from_index | error_count | error_msg                                                                                  | id                               | kb_id                            | kb_name | new_docs_indexed | prune_freq | refresh_freq | status | task_type | time_started        | total_docs_indexed | update_date         |
+----------------------------------+-------------------------+-------------+--------------------------------------------------------------------------------------------+----------------------------------+----------------------------------+---------+------------------+------------+--------------+--------+-----------+---------------------+--------------------+---------------------+
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 1           | sync task failed: failed to fetch URL: Get "https://xxxxxxxxxxxx/rss.xml": EOF             | 87592e5623fc4284bf69b9290f322718 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 4      | sync      | 2026-08-13 09:49:48 | 16                 | 2026-08-13 09:51:01 |
| 570138201cf44e1180711f1ebdf8492f | 0                       | 1           | sync task failed: failed to fetch URL: Get "https://www.ruanyifeng.com/blog/atom.xml": EOF | 456adbbf21a147af99adfce4405abd99 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 1            | 4      | sync      | 2026-08-13 09:50:00 | 3                  | 2026-08-13 09:50:18 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | fb55b5f1fb754475834cd9eac8b52054 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:45 | 16                 | 2026-08-13 09:49:46 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | 3cad779d7d2e4efb8d8d2dbf05d45653 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:42 | 16                 | 2026-08-13 09:49:43 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | dd09320aa5b34d6cbcfa7b03ede68c4a | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:36 | 16                 | 2026-08-13 09:49:38 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | afbcee73240f420ca1997e6506f387fb | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:33 | 16                 | 2026-08-13 09:49:34 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | 7d018146a0f14a25b7263d5980fdc30f | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:30 | 16                 | 2026-08-13 09:49:32 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | 520e0d6f421e40628ab541fc4fee1a69 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:27 | 16                 | 2026-08-13 09:49:28 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | 3d3ce49825de4ef99642e7994ae954e3 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:21 | 16                 | 2026-08-13 09:49:22 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | 1c511f9b4a7c425fb30a956589ca56fa | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:18 | 16                 | 2026-08-13 09:49:19 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | b9ab73c3f48c4f47a84fd55b542b32b4 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:15 | 16                 | 2026-08-13 09:49:16 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | db0cad90a7744d758b24d2e3d3828963 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:12 | 16                 | 2026-08-13 09:49:13 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | a501eadb2cce452187f3c86431352c70 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:08 | 16                 | 2026-08-13 09:49:10 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | b4265a3b7baf4466a525b91a9fe21b71 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:03 | 16                 | 2026-08-13 09:49:05 |
| 02b5b14183884445bd55ad6b9d322204 | 0                       | 0           |                                                                                            | a6ad590931c046dda5fc3222b4010c06 | 0e6e28e65b7044e58070e0e4252319a6 | test    | 0                | 5          | 0            | 3      | sync      | 2026-08-13 09:49:00 | 16                 | 2026-08-13 09:49:02 |
......
+----------------------------------+-------------------------+-------------+--------------------------------------------------------------------------------------------+----------------------------------+----------------------------------+---------+------------------+------------+--------------+--------+-----------+---------------------+--------------------+---------------------+
```
This commit is contained in:
mkaaad
2026-08-13 18:39:29 +08:00
committed by GitHub
parent 96f7c39753
commit 0dbfaa8b67
15 changed files with 701 additions and 1 deletions

View File

@@ -149,6 +149,10 @@ CREATE DATASET 'my_dataset' WITH EMBEDDING 'text-embedding-ada-002' PARSER 'naiv
LIST DATASETS;
DROP DATASET 'my_dataset';
-- Data sync logs
LIST SYNC_LOGS;
LIST DATASET 'my_dataset' SYNC_LOGS;
-- Model configuration
SET DEFAULT LLM 'gpt-4';
SET DEFAULT EMBEDDING 'text-embedding-ada-002';

View File

@@ -484,6 +484,8 @@ func (c *CLI) ExecuteUserCommand(cmd *Command) (ResponseIf, error) {
return c.APIStopIngestionCommand(cmd)
case "api_list_ingestion_tasks":
return c.APIListIngestionTasks(cmd)
case "api_list_sync_logs":
return c.APIListSyncLogsCommand(cmd)
case "api_remove_task":
return c.APIRemoveTaskCommand(cmd)
case "user_parse_local_file_command":

View File

@@ -411,6 +411,8 @@ func (l *Lexer) lookupIdent(ident string) Token {
return Token{Type: TokenAsync, Value: ident}
case "SYNC":
return Token{Type: TokenSync, Value: ident}
case "SYNC_LOGS":
return Token{Type: TokenSyncLogs, Value: ident}
case "BENCHMARK":
return Token{Type: TokenBenchmark, Value: ident}
case "PING":

View File

@@ -213,6 +213,42 @@ func (r *ListDocumentsResponse) PrintOut() {
}
}
type ListSyncLogsResponse struct {
Code int `json:"code"`
Data map[string]interface{} `json:"data"`
Message string `json:"message"`
Duration float64
OutputFormat OutputFormat
}
func (r *ListSyncLogsResponse) Type() string {
return "list_sync_logs"
}
func (r *ListSyncLogsResponse) TimeCost() float64 {
return r.Duration
}
func (r *ListSyncLogsResponse) SetOutputFormat(format OutputFormat) {
r.OutputFormat = format
}
func (r *ListSyncLogsResponse) PrintOut() {
if r.Code == 0 {
total := r.Data["total"].(float64)
fmt.Printf("Total: %0.0f\n", total)
logs := r.Data["logs"].([]interface{})
table := make([]map[string]interface{}, 0)
for _, log := range logs {
table = append(table, log.(map[string]interface{}))
}
PrintTableSimpleByFormat(table, r.OutputFormat)
} else {
fmt.Println("ERROR")
fmt.Printf("%d, %s\n", r.Code, r.Message)
}
}
type ListAgentsResponse struct {
Code int `json:"code"`
Data map[string]interface{} `json:"data"`

View File

@@ -129,6 +129,7 @@ const (
TokenDimension
TokenAsync
TokenSync
TokenSyncLogs
TokenBenchmark
TokenPing
TokenToken

View File

@@ -3467,6 +3467,76 @@ func (c *CLI) APIListIngestionTasks(cmd *Command) (ResponseIf, error) {
return HandleCommonResponse(resp, "list ingestion tasks")
}
// APIListSyncLogsCommand lists sync logs (user mode).
// LIST SYNC_LOGS; lists the sync logs of all datasets.
// LIST SYNC_LOGS FROM 'dataset_id'; and LIST DATASET 'dataset_name' SYNC_LOGS;
// restrict the listing to one dataset.
func (c *CLI) APIListSyncLogsCommand(cmd *Command) (ResponseIf, error) {
if c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].APIKey == nil && c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].LoginToken == nil {
return nil, fmt.Errorf("API key not set. Please login first")
}
if c.Config.CLIMode != APIMode {
return nil, fmt.Errorf("this command is only allowed in USER mode")
}
datasetID := ""
if rawID, ok := cmd.Params["dataset_id"].(string); ok {
datasetID = strings.TrimSpace(rawID)
}
if datasetName, ok := cmd.Params["dataset_name"].(string); ok && datasetName != "" {
id, err := c.getDatasetID(datasetName)
if err != nil {
return nil, err
}
datasetID = id
}
url := "/connectors/sync_logs"
query := netUrl.Values{}
if datasetID != "" {
query.Set("dataset_id", datasetID)
}
page, hasPage := cmd.Params["page"].(int)
pageSize, hasPageSize := cmd.Params["page_size"].(int)
switch {
case hasPage && hasPageSize:
query.Set("page", fmt.Sprintf("%d", page))
query.Set("page_size", fmt.Sprintf("%d", pageSize))
case hasPage:
query.Set("page", fmt.Sprintf("%d", page))
case hasPageSize:
query.Set("page_size", fmt.Sprintf("%d", pageSize))
default:
// No pagination requested: ask the API for every matching row.
query.Set("page_size", "0")
}
if encoded := query.Encode(); encoded != "" {
url += "?" + encoded
}
resp, err := c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("GET", url, "web", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list sync logs: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to list sync logs: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result ListSyncLogsResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("list sync logs failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// APIShowLogLevelCommand sets the log level for the system.
func (c *CLI) APIShowLogLevelCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != APIMode {

View File

@@ -151,6 +151,8 @@ func (p *Parser) parseAPIListCommands() (*Command, error) {
return p.parseAPIListAllModels()
case TokenIngestion:
return p.parseAPIListIngestionTasks()
case TokenSyncLogs:
return p.parseAPIListSyncLogs()
case TokenDefault:
return p.parseAPIListDefaultModels()
case TokenAvailable:
@@ -194,6 +196,8 @@ func (p *Parser) parseAPIListDatasetCommands() (*Command, error) {
return p.parseAPIListDatasetFiles(datasetID)
case TokenIngestion:
return p.parseAPIListDatasetIngestionTasks(datasetID)
case TokenSyncLogs:
return p.parseAPIListDatasetSyncLogs(datasetID)
default:
return nil, fmt.Errorf("unknown LIST target: %s", p.curToken.Value)
}
@@ -242,6 +246,94 @@ func (p *Parser) parseAPIListDatasetIngestionTasks(datasetName string) (*Command
return cmd, nil
}
// LIST SYNC_LOGS;
// LIST SYNC_LOGS FROM 'dataset_id'
// LIST SYNC_LOGS [FROM 'dataset_id'] WITH PAGE 2 PAGE_SIZE 50
func (p *Parser) parseAPIListSyncLogs() (*Command, error) {
p.nextToken() // consume SYNC_LOGS
cmd := NewCommand("api_list_sync_logs")
if p.curToken.Type == TokenFrom {
p.nextToken()
datasetID, err := p.parseQuotedString()
if err != nil {
return nil, err
}
cmd.Params["dataset_id"] = datasetID
p.nextToken() // move past the dataset id
}
if err := p.parseSyncLogsWithOptions(cmd); err != nil {
return nil, err
}
// Semicolon is optional
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
return cmd, nil
}
// LIST DATASET 'dataset_name' SYNC_LOGS [WITH PAGE 2 PAGE_SIZE 50];
func (p *Parser) parseAPIListDatasetSyncLogs(datasetName string) (*Command, error) {
p.nextToken() // consume SYNC_LOGS
cmd := NewCommand("api_list_sync_logs")
cmd.Params["dataset_name"] = datasetName
if err := p.parseSyncLogsWithOptions(cmd); err != nil {
return nil, err
}
// Semicolon is optional
if p.curToken.Type == TokenSemicolon {
p.nextToken()
}
return cmd, nil
}
// parseSyncLogsWithOptions parses the optional WITH clause of the sync logs
// listing commands. Only PAGE and PAGE_SIZE are accepted, both as integers,
// mirroring the search command's space-separated WITH syntax.
func (p *Parser) parseSyncLogsWithOptions(cmd *Command) error {
if p.curToken.Type != TokenWith && !(p.curToken.Type == TokenIdentifier && strings.EqualFold(p.curToken.Value, "with")) {
return nil
}
p.nextToken() // consume WITH
for p.curToken.Type != TokenEOF && p.curToken.Type != TokenSemicolon {
if p.curToken.Type == TokenComma {
return fmt.Errorf("syntax error: WITH options must be space-separated, not comma-separated")
}
if p.curToken.Type != TokenIdentifier {
break
}
paramName := strings.ToLower(p.curToken.Value)
p.nextToken()
if p.curToken.Type != TokenInteger {
if p.curToken.Type == TokenEOF || p.curToken.Type == TokenSemicolon {
return fmt.Errorf("WITH option %q is missing a value", paramName)
}
return fmt.Errorf("WITH option %q must be an integer, got %s", paramName, tokenTypeDescription(p.curToken.Type, p.curToken))
}
value, err := p.parseNumber()
if err != nil {
return err
}
p.nextToken()
switch paramName {
case "page", "page_size":
cmd.Params[paramName] = value
default:
return fmt.Errorf("unknown WITH option %q", paramName)
}
}
return nil
}
func (p *Parser) parseAPIListAgents() (*Command, error) {
p.nextToken() // consume AGENTS

View File

@@ -170,3 +170,133 @@ func TestParseAddModelWithDimensions(t *testing.T) {
})
}
}
func TestParseListSyncLogs(t *testing.T) {
tests := []struct {
name string
input string
expected *Command
wantErr bool
}{
{
name: "LIST SYNC_LOGS",
input: "LIST SYNC_LOGS;",
expected: &Command{
Type: "api_list_sync_logs",
Params: map[string]interface{}{},
},
},
{
name: "LIST SYNC_LOGS FROM dataset_id",
input: "LIST SYNC_LOGS FROM 'kb-1';",
expected: &Command{
Type: "api_list_sync_logs",
Params: map[string]interface{}{
"dataset_id": "kb-1",
},
},
},
{
name: "LIST DATASET name SYNC_LOGS",
input: "LIST DATASET 'my dataset' SYNC_LOGS;",
expected: &Command{
Type: "api_list_sync_logs",
Params: map[string]interface{}{
"dataset_name": "my dataset",
},
},
},
{
name: "LIST SYNC_LOGS WITH page and page_size",
input: "LIST SYNC_LOGS WITH PAGE 2 PAGE_SIZE 50;",
expected: &Command{
Type: "api_list_sync_logs",
Params: map[string]interface{}{
"page": 2,
"page_size": 50,
},
},
},
{
name: "LIST SYNC_LOGS FROM dataset_id WITH page",
input: "LIST SYNC_LOGS FROM 'kb-1' WITH PAGE 3;",
expected: &Command{
Type: "api_list_sync_logs",
Params: map[string]interface{}{
"dataset_id": "kb-1",
"page": 3,
},
},
},
{
name: "LIST DATASET name SYNC_LOGS WITH page_size",
input: "LIST DATASET 'my dataset' SYNC_LOGS WITH PAGE_SIZE 20;",
expected: &Command{
Type: "api_list_sync_logs",
Params: map[string]interface{}{
"dataset_name": "my dataset",
"page_size": 20,
},
},
},
{
name: "unknown WITH option",
input: "LIST SYNC_LOGS WITH FOO 1;",
wantErr: true,
},
{
name: "non-integer WITH option",
input: "LIST SYNC_LOGS WITH PAGE 'x';",
wantErr: true,
},
{
name: "comma-separated WITH options",
input: "LIST SYNC_LOGS WITH PAGE 2, PAGE_SIZE 50;",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := NewParser(tt.input)
cmd, err := p.Parse(APIMode)
if tt.wantErr {
if err == nil {
t.Fatalf("Parse() error = nil, want error")
}
return
}
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
if cmd.Type != tt.expected.Type {
t.Errorf("Command Type = %v, expected = %v", cmd.Type, tt.expected.Type)
}
gotDatasetID, _ := cmd.Params["dataset_id"].(string)
expectedDatasetID, _ := tt.expected.Params["dataset_id"].(string)
if gotDatasetID != expectedDatasetID {
t.Errorf("dataset_id = %v, expected = %v", gotDatasetID, expectedDatasetID)
}
gotDatasetName, _ := cmd.Params["dataset_name"].(string)
expectedDatasetName, _ := tt.expected.Params["dataset_name"].(string)
if gotDatasetName != expectedDatasetName {
t.Errorf("dataset_name = %v, expected = %v", gotDatasetName, expectedDatasetName)
}
gotPage, _ := cmd.Params["page"].(int)
expectedPage, _ := tt.expected.Params["page"].(int)
if gotPage != expectedPage {
t.Errorf("page = %v, expected = %v", gotPage, expectedPage)
}
gotPageSize, _ := cmd.Params["page_size"].(int)
expectedPageSize, _ := tt.expected.Params["page_size"].(int)
if gotPageSize != expectedPageSize {
t.Errorf("page_size = %v, expected = %v", gotPageSize, expectedPageSize)
}
})
}
}