Skip to content

Commit 183ff13

Browse files
authored
Jobs Indexing Operations (#1754)
* vendor changes * added changes for indexing operations * generated mocks * reversed modules.txt change * fixed integration test * added other change
1 parent 968664f commit 183ff13

10 files changed

+1003
-32
lines changed

commands/displayers/genai.go

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,165 @@ func (v *KnowledgeBaseDataSource) KV() []map[string]any {
201201
return out
202202
}
203203

204+
// IndexingJobDataSource displayer
205+
type IndexingJobDataSource struct {
206+
IndexingJobDataSources do.IndexingJobDataSources
207+
}
208+
209+
var _ Displayable = &IndexingJobDataSource{}
210+
211+
func (v *IndexingJobDataSource) JSON(out io.Writer) error {
212+
return writeJSON(v.IndexingJobDataSources, out)
213+
}
214+
215+
func (v *IndexingJobDataSource) ColMap() map[string]string {
216+
return map[string]string{
217+
"DataSourceUuid": "Data Source UUID",
218+
"Status": "Status",
219+
"StartedAt": "Started At",
220+
"CompletedAt": "Completed At",
221+
"IndexedItemCount": "Indexed Items",
222+
"FailedItemCount": "Failed Items",
223+
"SkippedItemCount": "Skipped Items",
224+
"RemovedItemCount": "Removed Items",
225+
"IndexedFileCount": "Indexed Files",
226+
"TotalFileCount": "Total Files",
227+
"TotalBytes": "Total Bytes",
228+
"TotalBytesIndexed": "Total Bytes Indexed",
229+
"ErrorMsg": "Error Message",
230+
"ErrorDetails": "Error Details",
231+
}
232+
}
233+
234+
func (v *IndexingJobDataSource) Cols() []string {
235+
return []string{
236+
"DataSourceUuid",
237+
"Status",
238+
"StartedAt",
239+
"CompletedAt",
240+
"IndexedItemCount",
241+
"FailedItemCount",
242+
"SkippedItemCount",
243+
"IndexedFileCount",
244+
"TotalFileCount",
245+
}
246+
}
247+
248+
func (v *IndexingJobDataSource) KV() []map[string]any {
249+
if v == nil || v.IndexingJobDataSources == nil {
250+
return []map[string]any{}
251+
}
252+
out := make([]map[string]any, 0, len(v.IndexingJobDataSources))
253+
for _, ds := range v.IndexingJobDataSources {
254+
startedAt := ""
255+
if ds.StartedAt != nil {
256+
startedAt = ds.StartedAt.String()
257+
}
258+
completedAt := ""
259+
if ds.CompletedAt != nil {
260+
completedAt = ds.CompletedAt.String()
261+
}
262+
263+
out = append(out, map[string]any{
264+
"DataSourceUuid": ds.DataSourceUuid,
265+
"Status": ds.Status,
266+
"StartedAt": startedAt,
267+
"CompletedAt": completedAt,
268+
"IndexedItemCount": ds.IndexedItemCount,
269+
"FailedItemCount": ds.FailedItemCount,
270+
"SkippedItemCount": ds.SkippedItemCount,
271+
"RemovedItemCount": ds.RemovedItemCount,
272+
"IndexedFileCount": ds.IndexedFileCount,
273+
"TotalFileCount": ds.TotalFileCount,
274+
"TotalBytes": ds.TotalBytes,
275+
"TotalBytesIndexed": ds.TotalBytesIndexed,
276+
"ErrorMsg": ds.ErrorMsg,
277+
"ErrorDetails": ds.ErrorDetails,
278+
})
279+
}
280+
return out
281+
}
282+
283+
// IndexingJob displayer
284+
type IndexingJob struct {
285+
IndexingJobs do.IndexingJobs
286+
}
287+
288+
var _ Displayable = &IndexingJob{}
289+
290+
func (v *IndexingJob) JSON(out io.Writer) error {
291+
return writeJSON(v.IndexingJobs, out)
292+
}
293+
294+
func (v *IndexingJob) ColMap() map[string]string {
295+
return map[string]string{
296+
"CompletedDatasources": "Completed Datasources",
297+
"CreatedAt": "Created At",
298+
"DataSourceUuids": "Data Source UUIDs",
299+
"FinishedAt": "Finished At",
300+
"KnowledgeBaseUuid": "Knowledge Base UUID",
301+
"Phase": "Phase",
302+
"StartedAt": "Started At",
303+
"Status": "Status",
304+
"Tokens": "Tokens",
305+
"TotalDatasources": "Total Datasources",
306+
"TotalItemsFailed": "Total Items Failed",
307+
"TotalItemsIndexed": "Total Items Indexed",
308+
"TotalItemsSkipped": "Total Items Skipped",
309+
"UpdatedAt": "Updated At",
310+
"UUID": "UUID",
311+
}
312+
}
313+
314+
func (v *IndexingJob) Cols() []string {
315+
return []string{
316+
"UUID",
317+
"KnowledgeBaseUuid",
318+
"Phase",
319+
"Status",
320+
"CompletedDatasources",
321+
"TotalDatasources",
322+
"Tokens",
323+
"TotalItemsIndexed",
324+
"TotalItemsFailed",
325+
"TotalItemsSkipped",
326+
"CreatedAt",
327+
"StartedAt",
328+
"FinishedAt",
329+
"UpdatedAt",
330+
}
331+
}
332+
333+
func (v *IndexingJob) KV() []map[string]any {
334+
if v == nil || v.IndexingJobs == nil {
335+
return []map[string]any{}
336+
}
337+
out := make([]map[string]any, 0, len(v.IndexingJobs))
338+
339+
for _, job := range v.IndexingJobs {
340+
o := map[string]any{
341+
"CompletedDatasources": job.CompletedDatasources,
342+
"CreatedAt": job.CreatedAt,
343+
"DataSourceUuids": job.DataSourceUuids,
344+
"FinishedAt": job.FinishedAt,
345+
"KnowledgeBaseUuid": job.KnowledgeBaseUuid,
346+
"Phase": job.Phase,
347+
"StartedAt": job.StartedAt,
348+
"Status": job.Status,
349+
"Tokens": job.Tokens,
350+
"TotalDatasources": job.TotalDatasources,
351+
"TotalItemsFailed": job.TotalItemsFailed,
352+
"TotalItemsIndexed": job.TotalItemsIndexed,
353+
"TotalItemsSkipped": job.TotalItemsSkipped,
354+
"UpdatedAt": job.UpdatedAt,
355+
"UUID": job.Uuid,
356+
}
357+
out = append(out, o)
358+
}
359+
360+
return out
361+
}
362+
204363
type FunctionRoute struct {
205364
Agent do.Agent
206365
}

commands/genai_knowledge_base.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,73 @@ func KnowledgeBaseCmd() *Command {
161161
cmdDataSourceDelete.Example = "The following example deletes data source having uuid like " + `00000000-0000-0000-0000-000000000000` + " from a Knowledge Base having uuid " + "`" + `f81d4fae-7dec-11d0-a765-00a0c91e6bf6` + " \nUsing the following command `" +
162162
" : `doctl genai knowledge-base delete-datasource f81d4fae-7dec-11d0-a765-00a0c91e6bf6 00000000-0000-0000-0000-000000000000`"
163163

164+
cmdIndexingJobsList := "List all indexing jobs for knowledge bases. Each indexing job contains the following information:\n" +
165+
" - The indexing job UUID\n" +
166+
" - The knowledge base UUID\n" +
167+
" - The current phase of the job\n" +
168+
" - The job status\n" +
169+
" - The number of completed datasources\n" +
170+
" - The total number of datasources\n" +
171+
" - The number of tokens processed\n" +
172+
" - The number of items indexed\n" +
173+
" - The number of items failed\n" +
174+
" - The number of items skipped\n" +
175+
" - The creation timestamp\n" +
176+
" - The start timestamp\n" +
177+
" - The finish timestamp\n" +
178+
" - The update timestamp\n" +
179+
" - The data source UUIDs being processed"
180+
cmdIndexingJobList := CmdBuilder(
181+
cmd,
182+
RunKnowledgeBaseListIndexingJobs,
183+
"list-indexing-jobs",
184+
"List all indexing jobs for knowledge bases",
185+
cmdIndexingJobsList,
186+
Writer, aliasOpt("ls-jobs"),
187+
displayerType(&displayers.IndexingJob{}),
188+
)
189+
cmdIndexingJobList.Example = "The following command lists all indexing jobs for knowledge bases: " +
190+
"`doctl genai knowledge-base list-indexing-jobs`"
191+
192+
cmdGetIndexingJobDetails := "Retrieve the status of a specific indexing job by its UUID. This includes phase, status, progress information, and timestamps."
193+
cmdGetIndexingJob := CmdBuilder(
194+
cmd,
195+
RunKnowledgeBaseGetIndexingJob,
196+
"get-indexing-job <indexing-job-uuid>",
197+
"Retrieve status of indexing job for a knowledge base",
198+
cmdGetIndexingJobDetails,
199+
Writer, aliasOpt("get-job"),
200+
displayerType(&displayers.IndexingJob{}),
201+
)
202+
cmdGetIndexingJob.Example = "The following command retrieves the status of an indexing job with UUID `12345678-1234-1234-1234-123456789012`: " +
203+
"`doctl genai knowledge-base get-indexing-job 12345678-1234-1234-1234-123456789012`"
204+
205+
cmdCancelIndexingJobDetails := "Cancel a running indexing job by its UUID. This will stop the indexing process and update the job status."
206+
cmdCancelIndexingJob := CmdBuilder(
207+
cmd,
208+
RunKnowledgeBaseCancelIndexingJob,
209+
"cancel-indexing-job <indexing-job-uuid>",
210+
"Cancel indexing job for a knowledge base",
211+
cmdCancelIndexingJobDetails,
212+
Writer, aliasOpt("cancel-job"),
213+
displayerType(&displayers.IndexingJob{}),
214+
)
215+
cmdCancelIndexingJob.Example = "The following command cancels an indexing job with UUID `12345678-1234-1234-1234-123456789012`: " +
216+
"`doctl genai knowledge-base cancel-indexing-job 12345678-1234-1234-1234-123456789012`"
217+
218+
cmdListIndexingJobDataSourcesDetails := "List all data sources for a specific indexing job by its UUID. This shows the status and progress of each data source being processed."
219+
cmdListIndexingJobDataSources := CmdBuilder(
220+
cmd,
221+
RunKnowledgeBaseListIndexingJobDataSources,
222+
"list-indexing-job-data-sources <indexing-job-uuid>",
223+
"List data sources for indexing job for a knowledge base",
224+
cmdListIndexingJobDataSourcesDetails,
225+
Writer, aliasOpt("ls-job-ds"),
226+
displayerType(&displayers.IndexingJobDataSource{}),
227+
)
228+
cmdListIndexingJobDataSources.Example = "The following command lists all data sources for an indexing job with UUID `12345678-1234-1234-1234-123456789012`: " +
229+
"`doctl genai knowledge-base list-indexing-job-data-sources 12345678-1234-1234-1234-123456789012`"
230+
164231
cmdAttachKnowledgeBaseDetails := "Attach a knowledge base to an agent using knowledge base uuid and agent uuid. It returns the information of corresponding agent."
165232
cmdAttachKnowledgeBase := CmdBuilder(
166233
cmd,
@@ -462,3 +529,48 @@ func RunDetachKnowledgeBase(c *CmdConfig) error {
462529
return fmt.Errorf("operation aborted")
463530
}
464531
}
532+
533+
// RunKnowledgeBaseListIndexingJobs lists all indexing jobs for knowledge bases.
534+
func RunKnowledgeBaseListIndexingJobs(c *CmdConfig) error {
535+
indexingJobs, err := c.GenAI().ListIndexingJobs()
536+
if err != nil {
537+
return err
538+
}
539+
return c.Display(&displayers.IndexingJob{IndexingJobs: indexingJobs})
540+
}
541+
542+
// RunKnowledgeBaseGetIndexingJob retrieves the status of a specific indexing job.
543+
func RunKnowledgeBaseGetIndexingJob(c *CmdConfig) error {
544+
if len(c.Args) < 1 {
545+
return doctl.NewMissingArgsErr(c.NS)
546+
}
547+
indexingJob, err := c.GenAI().GetIndexingJob(c.Args[0])
548+
if err != nil {
549+
return err
550+
}
551+
return c.Display(&displayers.IndexingJob{IndexingJobs: do.IndexingJobs{*indexingJob}})
552+
}
553+
554+
// RunKnowledgeBaseCancelIndexingJob cancels a specific indexing job.
555+
func RunKnowledgeBaseCancelIndexingJob(c *CmdConfig) error {
556+
if len(c.Args) < 1 {
557+
return doctl.NewMissingArgsErr(c.NS)
558+
}
559+
indexingJob, err := c.GenAI().CancelIndexingJob(c.Args[0])
560+
if err != nil {
561+
return err
562+
}
563+
return c.Display(&displayers.IndexingJob{IndexingJobs: do.IndexingJobs{*indexingJob}})
564+
}
565+
566+
// RunKnowledgeBaseListIndexingJobDataSources lists all data sources for a specific indexing job.
567+
func RunKnowledgeBaseListIndexingJobDataSources(c *CmdConfig) error {
568+
if len(c.Args) < 1 {
569+
return doctl.NewMissingArgsErr(c.NS)
570+
}
571+
dataSources, err := c.GenAI().ListIndexingJobDataSources(c.Args[0])
572+
if err != nil {
573+
return err
574+
}
575+
return c.Display(&displayers.IndexingJobDataSource{IndexingJobDataSources: dataSources})
576+
}

commands/genai_knowledge_base_test.go

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package commands
22

33
import (
44
"testing"
5+
"time"
56

67
"github.com/digitalocean/doctl"
78
"github.com/digitalocean/doctl/do"
@@ -28,12 +29,48 @@ var (
2829
Uuid: "data-source-id",
2930
},
3031
}
32+
33+
testIndexingJob = do.IndexingJob{
34+
LastIndexingJob: &godo.LastIndexingJob{
35+
CompletedDatasources: 1,
36+
CreatedAt: &godo.Timestamp{Time: time.Now()},
37+
DataSourceUuids: []string{"data-source-uuid-1", "data-source-uuid-2"},
38+
FinishedAt: &godo.Timestamp{Time: time.Now()},
39+
KnowledgeBaseUuid: "kb-uuid-123",
40+
Phase: "BATCH_JOB_PHASE_SUCCEEDED",
41+
StartedAt: &godo.Timestamp{Time: time.Now()},
42+
Status: "INDEX_JOB_STATUS_COMPLETED",
43+
Tokens: 1000,
44+
TotalDatasources: 2,
45+
TotalItemsFailed: "0",
46+
TotalItemsIndexed: "100",
47+
TotalItemsSkipped: "5",
48+
UpdatedAt: &godo.Timestamp{Time: time.Now()},
49+
Uuid: "indexing-job-uuid-123",
50+
},
51+
}
52+
53+
testIndexingJobDataSource = do.IndexingJobDataSource{
54+
IndexedDataSource: &godo.IndexedDataSource{
55+
CompletedAt: &godo.Timestamp{Time: time.Now()},
56+
DataSourceUuid: "data-source-uuid-1",
57+
StartedAt: &godo.Timestamp{Time: time.Now()},
58+
Status: "DATA_SOURCE_STATUS_COMPLETED",
59+
IndexedItemCount: "100",
60+
FailedItemCount: "0",
61+
SkippedItemCount: "5",
62+
IndexedFileCount: "50",
63+
TotalFileCount: "50",
64+
TotalBytes: "1024000",
65+
TotalBytesIndexed: "1024000",
66+
},
67+
}
3168
)
3269

3370
func TestKnowledgeBasesCommand(t *testing.T) {
3471
cmd := KnowledgeBaseCmd()
3572
assert.NotNil(t, cmd)
36-
assertCommandNames(t, cmd, "add-datasource", "attach", "create", "delete", "delete-datasource", "detach", "get", "list", "list-datasources", "update")
73+
assertCommandNames(t, cmd, "add-datasource", "attach", "cancel-indexing-job", "create", "delete", "delete-datasource", "detach", "get", "get-indexing-job", "list", "list-datasources", "list-indexing-job-data-sources", "list-indexing-jobs", "update")
3774
}
3875

3976
func TestKnowledgeBaseGet(t *testing.T) {
@@ -214,3 +251,41 @@ func TestKnowledgeBaseDetach(t *testing.T) {
214251
assert.NoError(t, err)
215252
})
216253
}
254+
255+
func TestKnowledgeBaseListIndexingJobs(t *testing.T) {
256+
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
257+
tm.genAI.EXPECT().ListIndexingJobs().Return(do.IndexingJobs{testIndexingJob}, nil)
258+
err := RunKnowledgeBaseListIndexingJobs(config)
259+
assert.NoError(t, err)
260+
})
261+
}
262+
263+
func TestKnowledgeBaseGetIndexingJob(t *testing.T) {
264+
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
265+
indexing_job_id := "indexing-job-uuid-123"
266+
config.Args = append(config.Args, indexing_job_id)
267+
tm.genAI.EXPECT().GetIndexingJob(indexing_job_id).Return(&testIndexingJob, nil)
268+
err := RunKnowledgeBaseGetIndexingJob(config)
269+
assert.NoError(t, err)
270+
})
271+
}
272+
273+
func TestKnowledgeBaseCancelIndexingJob(t *testing.T) {
274+
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
275+
indexing_job_id := "indexing-job-uuid-123"
276+
config.Args = append(config.Args, indexing_job_id)
277+
tm.genAI.EXPECT().CancelIndexingJob(indexing_job_id).Return(&testIndexingJob, nil)
278+
err := RunKnowledgeBaseCancelIndexingJob(config)
279+
assert.NoError(t, err)
280+
})
281+
}
282+
283+
func TestKnowledgeBaseListIndexingJobDataSources(t *testing.T) {
284+
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
285+
indexing_job_id := "indexing-job-uuid-123"
286+
config.Args = append(config.Args, indexing_job_id)
287+
tm.genAI.EXPECT().ListIndexingJobDataSources(indexing_job_id).Return(do.IndexingJobDataSources{testIndexingJobDataSource}, nil)
288+
err := RunKnowledgeBaseListIndexingJobDataSources(config)
289+
assert.NoError(t, err)
290+
})
291+
}

0 commit comments

Comments
 (0)