|
| 1 | +// Copyright 2025 syzkaller project authors. All rights reserved. |
| 2 | +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. |
| 3 | + |
| 4 | +package aidb |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "cloud.google.com/go/spanner" |
| 12 | + "github.com/google/uuid" |
| 13 | + "google.golang.org/appengine/v2" |
| 14 | +) |
| 15 | + |
| 16 | +func LoadWorkflows(ctx context.Context) ([]*Workflow, error) { |
| 17 | + client, err := dbClient(ctx) |
| 18 | + if err != nil { |
| 19 | + return nil, err |
| 20 | + } |
| 21 | + defer client.Close() |
| 22 | + iter := client.Single().Query(ctx, spanner.Statement{ |
| 23 | + SQL: `select * from Workflows`, |
| 24 | + }) |
| 25 | + defer iter.Stop() |
| 26 | + var workflows []*Workflow |
| 27 | + err = spanner.SelectAll(iter, &workflows) |
| 28 | + return workflows, err |
| 29 | +} |
| 30 | + |
| 31 | +func LoadActiveWorkflows(ctx context.Context) ([]string, error) { |
| 32 | + workflows, err := LoadWorkflows(ctx) |
| 33 | + if err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + var active []string |
| 37 | + for _, flow := range workflows { |
| 38 | + if flow.Active { |
| 39 | + active = append(active, flow.Name) |
| 40 | + } |
| 41 | + } |
| 42 | + return active, nil |
| 43 | +} |
| 44 | + |
| 45 | +func UpdateWorkflows(ctx context.Context, active []string) error { |
| 46 | + workflows, err := LoadWorkflows(ctx) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + m := make(map[string]bool) |
| 51 | + for _, flow := range active { |
| 52 | + m[flow] = true |
| 53 | + } |
| 54 | + update := false |
| 55 | + for _, flow := range workflows { |
| 56 | + active := m[flow.Name] |
| 57 | + delete(m, flow.Name) |
| 58 | + if flow.Active != active { |
| 59 | + update = true |
| 60 | + flow.Active = active |
| 61 | + } |
| 62 | + } |
| 63 | + for name := range m { |
| 64 | + update = true |
| 65 | + workflows = append(workflows, &Workflow{ |
| 66 | + Name: name, |
| 67 | + Type: WorkflowType(strings.Split(name, "-")[0]), |
| 68 | + Active: true, |
| 69 | + }) |
| 70 | + } |
| 71 | + if !update { |
| 72 | + return nil |
| 73 | + } |
| 74 | + client, err := dbClient(ctx) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + defer client.Close() |
| 79 | + var mutations []*spanner.Mutation |
| 80 | + for _, flow := range workflows { |
| 81 | + mut, err := spanner.InsertOrUpdateStruct("Workflows", flow) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + mutations = append(mutations, mut) |
| 86 | + } |
| 87 | + _, err = client.Apply(ctx, mutations) |
| 88 | + return err |
| 89 | +} |
| 90 | + |
| 91 | +func CreatePatchingJob(ctx context.Context, workflow string, patchingJob *PatchingJob) error { |
| 92 | + client, err := dbClient(ctx) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + job := &Job{ |
| 97 | + ID: uuid.NewString(), |
| 98 | + Type: WorkflowPatching, |
| 99 | + Workflow: workflow, |
| 100 | + Created: TimeNow(ctx), |
| 101 | + } |
| 102 | + patchingJob.ID = job.ID |
| 103 | + insertJob, err := spanner.InsertStruct("Jobs", job) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + insertPatchingJob, err := spanner.InsertStruct("PatchingJobs", patchingJob) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + _, err = client.Apply(ctx, []*spanner.Mutation{insertJob, insertPatchingJob}) |
| 112 | + return err |
| 113 | +} |
| 114 | + |
| 115 | +var TimeNow = func(ctx context.Context) time.Time { |
| 116 | + return time.Now() |
| 117 | +} |
| 118 | + |
| 119 | +func dbClient(ctx context.Context) (*spanner.Client, error) { |
| 120 | + database := "projects/" + appengine.AppID(ctx) + "/instances/syzbot/databases/ai" |
| 121 | + return spanner.NewClient(ctx, database) |
| 122 | +} |
0 commit comments