|
| 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 main |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "reflect" |
| 11 | + "slices" |
| 12 | + |
| 13 | + "cloud.google.com/go/spanner" |
| 14 | + "github.com/google/syzkaller/dashboard/app/aidb" |
| 15 | + "github.com/google/syzkaller/dashboard/dashapi" |
| 16 | + "google.golang.org/api/iterator" |
| 17 | + "google.golang.org/appengine/v2" |
| 18 | + "google.golang.org/appengine/v2/log" |
| 19 | +) |
| 20 | + |
| 21 | +type uiAIPage struct { |
| 22 | + Header *uiHeader |
| 23 | + Workflows *aidb.Workflows |
| 24 | +} |
| 25 | + |
| 26 | +func handleAIPage(ctx context.Context, w http.ResponseWriter, r *http.Request) error { |
| 27 | + hdr, err := commonHeader(ctx, r, w, "") |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + workflows, err := fetchAIConfig(ctx) |
| 32 | + if err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + page := &uiAIPage{ |
| 36 | + Header: hdr, |
| 37 | + Workflows: workflows, |
| 38 | + } |
| 39 | + return serveTemplate(w, "ai.html", page) |
| 40 | +} |
| 41 | + |
| 42 | +func apiAIJobPoll(ctx context.Context, req *dashapi.AIJobPollReq) (any, error) { |
| 43 | + if err := storeAIWorkflows(ctx, req); err != nil { |
| 44 | + log.Errorf(ctx, "storeAIWorkflows: %v", err) |
| 45 | + } |
| 46 | + resp := &dashapi.AIJobPollResp{} |
| 47 | + return resp, nil |
| 48 | +} |
| 49 | + |
| 50 | +func apiAIJobDone(ctx context.Context, req *dashapi.AIJobDoneReq) (any, error) { |
| 51 | + return nil, nil |
| 52 | +} |
| 53 | + |
| 54 | +func apiAIJournal(ctx context.Context, req *dashapi.AIJournalReq) (any, error) { |
| 55 | + return nil, nil |
| 56 | +} |
| 57 | + |
| 58 | +func storeAIWorkflows(ctx context.Context, req *dashapi.AIJobPollReq) error { |
| 59 | + have, err := fetchAIConfig(ctx) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + workflows := &aidb.Workflows{ |
| 64 | + Namespaces: req.Namespaces, |
| 65 | + Workflows: req.Workflows, |
| 66 | + } |
| 67 | + slices.Sort(workflows.Namespaces) |
| 68 | + slices.Sort(workflows.Workflows) |
| 69 | + if reflect.DeepEqual(have, workflows) { |
| 70 | + return nil |
| 71 | + } |
| 72 | + client, err := aiDBClient(ctx) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + defer client.Close() |
| 77 | + mut, err := spanner.InsertOrUpdateStruct("workflows", workflows) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + _, err = client.Apply(ctx, []*spanner.Mutation{mut}) |
| 82 | + return err |
| 83 | +} |
| 84 | + |
| 85 | +func fetchAIConfig(ctx context.Context) (*aidb.Workflows, error) { |
| 86 | + client, err := aiDBClient(ctx) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + defer client.Close() |
| 91 | + iter := client.Single().Query(ctx, spanner.Statement{ |
| 92 | + SQL: `select * from workflows where pk=0`, |
| 93 | + //Params: map[string]any{}, |
| 94 | + }) |
| 95 | + defer iter.Stop() |
| 96 | + row, err := iter.Next() |
| 97 | + v := new(aidb.Workflows) |
| 98 | + if err != nil { |
| 99 | + if err != iterator.Done { |
| 100 | + return nil, fmt.Errorf("fetchAIConfig: iter.Next: %w", err) |
| 101 | + } |
| 102 | + } else if err := row.ToStruct(v); err != nil { |
| 103 | + return nil, fmt.Errorf("fetchAIConfig: ToStruct: %w", err) |
| 104 | + } |
| 105 | + return v, nil |
| 106 | +} |
| 107 | + |
| 108 | +func aiDBClient(ctx context.Context) (*spanner.Client, error) { |
| 109 | + database := "projects/" + appengine.AppID(ctx) + "/instances/syzbot/databases/ai" |
| 110 | + return spanner.NewClient(ctx, database) |
| 111 | +} |
0 commit comments