1 // Copyright 2021 Chaos Mesh Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 16 package core 17 18 import ( 19 "context" 20 "time" 21 22 "github.com/jinzhu/gorm" 23 ) 24 25 // ExperimentStore defines operations for working with experiments. 26 type ExperimentStore interface { 27 // ListMeta returns experiment metadata list from the datastore. 28 ListMeta(ctx context.Context, kind, namespace, name string, archived bool) ([]*ExperimentMeta, error) 29 30 // FindByUID returns an experiment by UID. 31 FindByUID(ctx context.Context, UID string) (*Experiment, error) 32 33 // FindManagedByNamespaceName returns experiment list which are managed by schedule or workflow. 34 FindManagedByNamespaceName(ctx context.Context, namespace, name string) ([]*Experiment, error) 35 36 // FindMetaByUID returns an experiment metadata by UID. 37 FindMetaByUID(context.Context, string) (*ExperimentMeta, error) 38 39 // Set saves the experiment to datastore. 40 Set(context.Context, *Experiment) error 41 42 // Archive archives experiments which "archived" field is false. 43 Archive(ctx context.Context, namespace, name string) error 44 45 // Delete deletes the archive from the datastore. 46 Delete(context.Context, *Experiment) error 47 48 // DeleteByFinishTime deletes archives which time difference is greater than the given time from FinishTime. 49 DeleteByFinishTime(context.Context, time.Duration) error 50 51 // DeleteByUIDs deletes archives by the uid list. 52 DeleteByUIDs(context.Context, []string) error 53 54 // DeleteIncompleteExperiments deletes all incomplete experiments. 55 // If the chaos-dashboard was restarted and the experiment is completed during the restart, 56 // which means the experiment would never save the finish_time. 57 // DeleteIncompleteExperiments can be used to delete all incomplete experiments to avoid this case. 58 DeleteIncompleteExperiments(context.Context) error 59 } 60 61 // Experiment represents an experiment instance. Use in db. 62 type Experiment struct { 63 ExperimentMeta 64 Experiment string `gorm:"type:text;size:32768"` // JSON string 65 } 66 67 // ExperimentMeta defines the metadata of an experiment. Use in db. 68 type ExperimentMeta struct { 69 gorm.Model 70 UID string `gorm:"index:uid" json:"uid"` 71 Kind string `json:"kind"` 72 Name string `json:"name"` 73 Namespace string `json:"namespace"` 74 Action string `json:"action"` 75 StartTime time.Time `json:"start_time"` 76 FinishTime *time.Time `json:"finish_time"` 77 Archived bool `json:"archived"` 78 } 79