File size: 1,559 Bytes
8059bf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package schema

import (
	"encoding/json"
	"fmt"

	"github.com/Wei-Shaw/sub2api/ent/schema/mixins"

	"entgo.io/ent"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
)

// UsageCleanupTask 定义使用记录清理任务的 schema。
type UsageCleanupTask struct {
	ent.Schema
}

func (UsageCleanupTask) Annotations() []schema.Annotation {
	return []schema.Annotation{
		entsql.Annotation{Table: "usage_cleanup_tasks"},
	}
}

func (UsageCleanupTask) Mixin() []ent.Mixin {
	return []ent.Mixin{
		mixins.TimeMixin{},
	}
}

func (UsageCleanupTask) Fields() []ent.Field {
	return []ent.Field{
		field.String("status").
			MaxLen(20).
			Validate(validateUsageCleanupStatus),
		field.JSON("filters", json.RawMessage{}),
		field.Int64("created_by"),
		field.Int64("deleted_rows").
			Default(0),
		field.String("error_message").
			Optional().
			Nillable(),
		field.Int64("canceled_by").
			Optional().
			Nillable(),
		field.Time("canceled_at").
			Optional().
			Nillable(),
		field.Time("started_at").
			Optional().
			Nillable(),
		field.Time("finished_at").
			Optional().
			Nillable(),
	}
}

func (UsageCleanupTask) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("status", "created_at"),
		index.Fields("created_at"),
		index.Fields("canceled_at"),
	}
}

func validateUsageCleanupStatus(status string) error {
	switch status {
	case "pending", "running", "succeeded", "failed", "canceled":
		return nil
	default:
		return fmt.Errorf("invalid usage cleanup status: %s", status)
	}
}