summaryrefslogtreecommitdiffstats
path: root/internal/formatter/buckets.go
blob: 9672b9d1f9b09f8335a2fca920550b8919524399 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package formatter

import (
	"encoding/json"
	"fmt"
	"sort"
	"strings"

	"git.db.org.ai/dborg/internal/models"
	"git.db.org.ai/dborg/internal/utils"
)

func FormatBucketsResults(response *models.BucketsSearchResponse, asJSON bool) error {
	if asJSON {
		return utils.PrintJSON(response)
	}

	PrintSection("Bucket Search Results")

	if response.Credits.Unlimited {
		fmt.Printf("%s: %s\n", Dim("Credits"), Green("Unlimited"))
	} else {
		fmt.Printf("%s: %s\n", Dim("Credits Remaining"), FormatCredits(int64(response.Credits.Remaining)))
	}

	if response.Results == nil {
		PrintWarning("No results found")
		return nil
	}

	results, ok := response.Results.(map[string]any)
	if !ok {
		return fmt.Errorf("unexpected results format")
	}

	buckets, ok := results["buckets"].([]any)
	if !ok || len(buckets) == 0 {
		PrintWarning("No buckets found")
		return nil
	}

	fmt.Printf("%s: %s\n\n", Dim("Total Buckets"), Yellow(fmt.Sprintf("%d", len(buckets))))

	bucketGroups := groupBucketsByType(buckets)

	for _, bType := range getOrderedTypes(bucketGroups) {
		formatBucketGroup(bType, bucketGroups[bType])
	}

	printBucketStats(buckets)

	return nil
}

func groupBucketsByType(buckets []any) map[string][]map[string]any {
	groups := make(map[string][]map[string]any)

	for _, bucket := range buckets {
		if bucketMap, ok := bucket.(map[string]any); ok {
			bucketType := "unknown"
			if bt, ok := bucketMap["type"].(string); ok {
				bucketType = bt
			}
			groups[bucketType] = append(groups[bucketType], bucketMap)
		}
	}

	return groups
}

func getOrderedTypes(groups map[string][]map[string]any) []string {
	var types []string
	for t := range groups {
		types = append(types, t)
	}

	sort.Slice(types, func(i, j int) bool {
		order := map[string]int{"aws": 1, "gcp": 2, "azure": 3, "dos": 4, "unknown": 99}
		iOrder, iOk := order[types[i]]
		jOrder, jOk := order[types[j]]
		if !iOk {
			iOrder = 50
		}
		if !jOk {
			jOrder = 50
		}
		if iOrder != jOrder {
			return iOrder < jOrder
		}
		return len(groups[types[i]]) > len(groups[types[j]])
	})

	return types
}

func formatBucketGroup(bucketType string, buckets []map[string]any) {
	typeHeader := getBucketTypeHeader(bucketType)
	fmt.Printf("%s %s\n", typeHeader, Gray(fmt.Sprintf("(%d)", len(buckets))))
	fmt.Println(Dim(strings.Repeat("─", 60)))

	sort.Slice(buckets, func(i, j int) bool {
		iCount := getFileCount(buckets[i])
		jCount := getFileCount(buckets[j])
		return iCount > jCount
	})

	for i, bucket := range buckets {
		formatSingleBucket(bucket, i+1)
	}

	fmt.Println()
}

func formatSingleBucket(bucket map[string]any, index int) {
	bucketName := "unknown"
	if name, ok := bucket["bucket"].(string); ok {
		bucketName = name
	}

	fileCount := getFileCount(bucket)
	id := getID(bucket)

	fmt.Printf("  %s %s\n", Gray(fmt.Sprintf("%d.", index)), truncateBucketName(bucketName))

	if fileCount > 0 {
		fileCountStr := formatFileCount(fileCount)
		fmt.Printf("     %s: %s", Cyan("Files"), fileCountStr)
	} else {
		fmt.Printf("     %s: %s", Cyan("Files"), Gray("Empty"))
	}

	if id > 0 {
		fmt.Printf("  %s %s", Dim("•"), Gray(fmt.Sprintf("ID: %d", id)))
	}

	fmt.Println()
}

func truncateBucketName(name string) string {
	maxLen := 50
	if len(name) <= maxLen {
		return Bold(name)
	}

	parts := strings.Split(name, ".")
	if len(parts) > 2 {
		provider := parts[len(parts)-2] + "." + parts[len(parts)-1]
		remaining := maxLen - len(provider) - 4
		if remaining > 0 {
			return Bold(TruncateString(strings.Join(parts[:len(parts)-2], "."), remaining) + "..." + provider)
		}
	}

	return Bold(TruncateString(name, maxLen))
}

func formatFileCount(count int) string {
	switch {
	case count > 10000:
		return Red(fmt.Sprintf("%s", formatNumber(count)))
	case count > 1000:
		return Yellow(fmt.Sprintf("%s", formatNumber(count)))
	case count > 100:
		return Green(fmt.Sprintf("%s", formatNumber(count)))
	default:
		return Cyan(fmt.Sprintf("%d", count))
	}
}

func formatNumber(n int) string {
	if n < 1000 {
		return fmt.Sprintf("%d", n)
	}

	str := fmt.Sprintf("%d", n)
	var result strings.Builder
	for i, r := range str {
		if i > 0 && (len(str)-i)%3 == 0 {
			result.WriteString(",")
		}
		result.WriteRune(r)
	}
	return result.String()
}

func getBucketTypeHeader(bucketType string) string {
	headers := map[string]string{
		"aws":   Bold(Yellow("☁ AWS S3 Buckets")),
		"gcp":   Bold(Blue("☁ Google Cloud Storage")),
		"azure": Bold(Cyan("☁ Azure Storage")),
		"dos":   Bold(Green("☁ DigitalOcean Spaces")),
	}

	if header, ok := headers[bucketType]; ok {
		return header
	}
	return Bold(Gray("☁ " + strings.Title(bucketType) + " Buckets"))
}

func getFileCount(bucket map[string]any) int {
	if count, ok := bucket["fileCount"].(float64); ok {
		return int(count)
	}
	return 0
}

func getID(bucket map[string]any) int {
	if id, ok := bucket["id"].(float64); ok {
		return int(id)
	}
	return 0
}

func printBucketStats(buckets []any) {
	totalFiles := 0
	emptyBuckets := 0
	largeBuckets := 0

	typeCounts := make(map[string]int)

	for _, bucket := range buckets {
		if bucketMap, ok := bucket.(map[string]any); ok {
			fileCount := getFileCount(bucketMap)
			totalFiles += fileCount

			if fileCount == 0 {
				emptyBuckets++
			} else if fileCount > 1000 {
				largeBuckets++
			}

			if bucketType, ok := bucketMap["type"].(string); ok {
				typeCounts[bucketType]++
			}
		}
	}

	fmt.Printf("%s\n", Bold("Summary Statistics"))
	fmt.Println(Dim(strings.Repeat("─", 60)))

	fmt.Printf("  %s: %s\n", Cyan("Total Files"), Yellow(formatNumber(totalFiles)))
	fmt.Printf("  %s: %s\n", Cyan("Average Files/Bucket"),
		Yellow(fmt.Sprintf("%.1f", float64(totalFiles)/float64(len(buckets)))))

	if emptyBuckets > 0 {
		fmt.Printf("  %s: %s\n", Cyan("Empty Buckets"), Gray(fmt.Sprintf("%d", emptyBuckets)))
	}

	if largeBuckets > 0 {
		fmt.Printf("  %s: %s\n", Cyan("Large Buckets (>1000 files)"),
			Red(fmt.Sprintf("%d", largeBuckets)))
	}

	fmt.Println()
}

func FormatBucketFilesResults(response *models.BucketsFilesSearchResponse, asJSON bool) error {
	if asJSON {
		return utils.PrintJSON(response)
	}

	PrintSection("Bucket Files Search Results")

	if response.Credits.Unlimited {
		fmt.Printf("%s: %s\n", Dim("Credits"), Green("Unlimited"))
	} else {
		fmt.Printf("%s: %s\n", Dim("Credits Remaining"), FormatCredits(int64(response.Credits.Remaining)))
	}

	if response.Results == nil {
		PrintWarning("No results found")
		return nil
	}

	results, ok := response.Results.(map[string]any)
	if !ok {
		resultsJSON, err := json.MarshalIndent(response.Results, "", "  ")
		if err != nil {
			return fmt.Errorf("failed to format results: %w", err)
		}
		fmt.Println(string(resultsJSON))
		return nil
	}

	files, ok := results["files"].([]any)
	if !ok || len(files) == 0 {
		PrintWarning("No files found")
		return nil
	}

	fmt.Printf("%s: %s\n\n", Dim("Total Files"), Yellow(fmt.Sprintf("%d", len(files))))

	fileGroups := groupFilesByBucket(files)

	for bucket, bucketFiles := range fileGroups {
		formatFileGroup(bucket, bucketFiles)
	}

	return nil
}

func groupFilesByBucket(files []any) map[string][]map[string]any {
	groups := make(map[string][]map[string]any)

	for _, file := range files {
		if fileMap, ok := file.(map[string]any); ok {
			bucket := "unknown"
			if b, ok := fileMap["bucket"].(string); ok && b != "" {
				bucket = b
			} else if url, ok := fileMap["url"].(string); ok && url != "" {
				bucket = extractBucketFromURL(url)
			}
			groups[bucket] = append(groups[bucket], fileMap)
		}
	}

	return groups
}

func extractBucketFromURL(url string) string {
	url = strings.TrimPrefix(url, "https://")
	url = strings.TrimPrefix(url, "http://")

	parts := strings.Split(url, "/")
	if len(parts) > 0 {
		return parts[0]
	}

	return "unknown"
}

func formatFileGroup(bucket string, files []map[string]any) {
	fmt.Printf("%s %s\n", Bold(truncateBucketName(bucket)), Gray(fmt.Sprintf("(%d files)", len(files))))
	fmt.Println(Dim(strings.Repeat("─", 60)))

	for i, file := range files {
		formatSingleFile(file, i+1)
	}

	fmt.Println()
}

func formatSingleFile(file map[string]any, index int) {
	fileName := "unknown"
	url := ""

	if name, ok := file["file"].(string); ok && name != "" {
		fileName = name
	} else if u, ok := file["url"].(string); ok && u != "" {
		url = u
		fileName = extractFileNameFromURL(u)
	}

	fmt.Printf("  %s %s\n", Gray(fmt.Sprintf("%d.", index)), formatFileName(fileName))

	if url != "" {
		fmt.Printf("     %s: %s\n", Cyan("URL"), Dim(url))
	}

	if size, ok := file["size"].(float64); ok && size > 0 {
		fmt.Printf("     %s: %s\n", Cyan("Size"), formatFileSize(int64(size)))
	}

	if modified, ok := file["lastModified"].(string); ok && modified != "" {
		fmt.Printf("     %s: %s\n", Cyan("Modified"), Dim(modified))
	}
}

func extractFileNameFromURL(url string) string {
	parts := strings.Split(url, "/")
	if len(parts) > 0 {
		fileName := parts[len(parts)-1]

		if qIndex := strings.Index(fileName, "?"); qIndex != -1 {
			fileName = fileName[:qIndex]
		}

		if hIndex := strings.Index(fileName, "#"); hIndex != -1 {
			fileName = fileName[:hIndex]
		}

		if fileName == "" {
			return "index"
		}

		return fileName
	}
	return "unknown"
}

func formatFileName(name string) string {
	if name == "" || name == "unknown" {
		return Gray("(unnamed file)")
	}

	parts := strings.Split(name, "/")
	if len(parts) > 1 {
		path := strings.Join(parts[:len(parts)-1], "/")
		file := parts[len(parts)-1]

		if file == "" {
			file = "(directory)"
		}

		file = decodeURLEncoding(file)

		ext := getFileExtension(file)
		color := getExtensionColor(ext)

		return Gray(path+"/") + Colorize(file, color)
	}

	name = decodeURLEncoding(name)
	ext := getFileExtension(name)
	color := getExtensionColor(ext)

	return Colorize(name, color)
}

func decodeURLEncoding(s string) string {
	decoded := strings.ReplaceAll(s, "%20", " ")
	decoded = strings.ReplaceAll(decoded, "%28", "(")
	decoded = strings.ReplaceAll(decoded, "%29", ")")
	decoded = strings.ReplaceAll(decoded, "_", " ")

	return decoded
}

func getFileExtension(filename string) string {
	parts := strings.Split(filename, ".")
	if len(parts) > 1 {
		return strings.ToLower(parts[len(parts)-1])
	}
	return ""
}

func getExtensionColor(ext string) string {
	switch ext {
	case "pdf", "doc", "docx", "txt":
		return ColorBlue
	case "jpg", "jpeg", "png", "gif", "svg":
		return ColorGreen
	case "mp4", "avi", "mov", "mkv":
		return ColorMagenta
	case "zip", "tar", "gz", "rar":
		return ColorYellow
	case "sql", "db", "sqlite":
		return ColorRed
	case "json", "xml", "yaml", "yml":
		return ColorCyan
	default:
		return ColorWhite
	}
}

func formatFileSize(bytes int64) string {
	color := ColorWhite
	switch {
	case bytes > 1024*1024*100:
		color = ColorRed
	case bytes > 1024*1024*10:
		color = ColorYellow
	case bytes > 1024*1024:
		color = ColorGreen
	default:
		color = ColorCyan
	}

	return Colorize(FormatBytes(bytes), color)
}