|
|
package services |
|
|
|
|
|
import ( |
|
|
"context" |
|
|
|
|
|
"github.com/mudler/LocalAI/core/config" |
|
|
"github.com/mudler/LocalAI/pkg/xsync" |
|
|
) |
|
|
|
|
|
type GalleryOp[T any, E any] struct { |
|
|
ID string |
|
|
GalleryElementName string |
|
|
Delete bool |
|
|
|
|
|
Req T |
|
|
|
|
|
|
|
|
GalleryElement *E |
|
|
|
|
|
Galleries []config.Gallery |
|
|
BackendGalleries []config.Gallery |
|
|
|
|
|
|
|
|
Context context.Context |
|
|
CancelFunc context.CancelFunc |
|
|
|
|
|
|
|
|
|
|
|
ExternalURI string |
|
|
ExternalName string |
|
|
ExternalAlias string |
|
|
} |
|
|
|
|
|
type GalleryOpStatus struct { |
|
|
Deletion bool `json:"deletion"` |
|
|
FileName string `json:"file_name"` |
|
|
Error error `json:"error"` |
|
|
Processed bool `json:"processed"` |
|
|
Message string `json:"message"` |
|
|
Progress float64 `json:"progress"` |
|
|
TotalFileSize string `json:"file_size"` |
|
|
DownloadedFileSize string `json:"downloaded_size"` |
|
|
GalleryElementName string `json:"gallery_element_name"` |
|
|
Cancelled bool `json:"cancelled"` |
|
|
Cancellable bool `json:"cancellable"` |
|
|
} |
|
|
|
|
|
type OpCache struct { |
|
|
status *xsync.SyncedMap[string, string] |
|
|
backendOps *xsync.SyncedMap[string, bool] |
|
|
galleryService *GalleryService |
|
|
} |
|
|
|
|
|
func NewOpCache(galleryService *GalleryService) *OpCache { |
|
|
return &OpCache{ |
|
|
status: xsync.NewSyncedMap[string, string](), |
|
|
backendOps: xsync.NewSyncedMap[string, bool](), |
|
|
galleryService: galleryService, |
|
|
} |
|
|
} |
|
|
|
|
|
func (m *OpCache) Set(key string, value string) { |
|
|
m.status.Set(key, value) |
|
|
} |
|
|
|
|
|
|
|
|
func (m *OpCache) SetBackend(key string, value string) { |
|
|
m.status.Set(key, value) |
|
|
m.backendOps.Set(key, true) |
|
|
} |
|
|
|
|
|
|
|
|
func (m *OpCache) IsBackendOp(key string) bool { |
|
|
return m.backendOps.Get(key) |
|
|
} |
|
|
|
|
|
func (m *OpCache) Get(key string) string { |
|
|
return m.status.Get(key) |
|
|
} |
|
|
|
|
|
func (m *OpCache) DeleteUUID(uuid string) { |
|
|
for _, k := range m.status.Keys() { |
|
|
if m.status.Get(k) == uuid { |
|
|
m.status.Delete(k) |
|
|
m.backendOps.Delete(k) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
func (m *OpCache) Map() map[string]string { |
|
|
return m.status.Map() |
|
|
} |
|
|
|
|
|
func (m *OpCache) Exists(key string) bool { |
|
|
return m.status.Exists(key) |
|
|
} |
|
|
|
|
|
func (m *OpCache) GetStatus() (map[string]string, map[string]string) { |
|
|
processingModelsData := m.Map() |
|
|
|
|
|
taskTypes := map[string]string{} |
|
|
|
|
|
for k, v := range processingModelsData { |
|
|
status := m.galleryService.GetStatus(v) |
|
|
taskTypes[k] = "Installation" |
|
|
if status != nil && status.Deletion { |
|
|
taskTypes[k] = "Deletion" |
|
|
} else if status == nil { |
|
|
taskTypes[k] = "Waiting" |
|
|
} |
|
|
} |
|
|
|
|
|
return processingModelsData, taskTypes |
|
|
} |
|
|
|