-
Notifications
You must be signed in to change notification settings - Fork 0
/
processing.go
145 lines (130 loc) · 4.51 KB
/
processing.go
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2018 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package model
import (
"bytes"
"compress/zlib"
"encoding/json"
"io"
"time"
"github.com/golang/protobuf/jsonpb"
structpb "github.com/golang/protobuf/ptypes/struct"
"go.chromium.org/gae/service/datastore"
"github.com/TriggerMail/luci-go/common/errors"
"github.com/TriggerMail/luci-go/common/proto/google"
api "github.com/TriggerMail/luci-go/cipd/api/cipd/v1"
)
// ProcessingResult holds information extracted from the package instance file.
//
// It is obtained during an asynchronous post processing step triggered after
// the instance is uploaded. Immutable.
//
// Entity ID is a processor name used to extract it. Parent entity is
// PackageInstance the information was extracted from.
type ProcessingResult struct {
_kind string `gae:"$kind,ProcessingResult"`
_extra datastore.PropertyMap `gae:"-,extra"`
ProcID string `gae:"$id"` // processor that generated the result
Instance *datastore.Key `gae:"$parent"` // instance it was generated from
CreatedTs time.Time `gae:"created_ts"` // when it was generated
Success bool `gae:"success"` // mostly for for indexing
Error string `gae:"error,noindex"` // for Success == false
ResultRaw []byte `gae:"result,noindex"` // for Success == true
}
// WriteResult overwrites ResultRaw field with compressed JSON-serialized 'r'.
//
// 'r' should serialize into a JSON object, e.g '{...}'.
func (p *ProcessingResult) WriteResult(r interface{}) error {
blob, err := json.Marshal(r)
switch {
case err != nil:
return errors.Annotate(err, "failed to serialize the result").Err()
case len(blob) == 0 || blob[0] != '{':
return errors.Reason("the result is not a JSON object").Err()
}
out := bytes.Buffer{}
z := zlib.NewWriter(&out)
if _, err := io.Copy(z, bytes.NewReader(blob)); err != nil {
z.Close()
return errors.Annotate(err, "failed to compress the result").Err()
}
if err := z.Close(); err != nil {
return errors.Annotate(err, "failed to close zlib writer").Err()
}
p.ResultRaw = out.Bytes()
return nil
}
// ReadResult deserializes the result into the given variable.
//
// Does nothing if there's no results stored.
func (p *ProcessingResult) ReadResult(r interface{}) error {
if len(p.ResultRaw) == 0 {
return nil
}
z, err := zlib.NewReader(bytes.NewReader(p.ResultRaw))
if err != nil {
return errors.Annotate(err, "failed to open the blob for zlib decompression").Err()
}
if err := json.NewDecoder(z).Decode(r); err != nil {
z.Close()
return errors.Annotate(err, "failed to decompress or deserialize the result").Err()
}
if err := z.Close(); err != nil {
return errors.Annotate(err, "failed to close zlib reader").Err()
}
return nil
}
// ReadResultIntoStruct deserializes the result into the protobuf.Struct.
//
// Does nothing if there's no results stored.
func (p *ProcessingResult) ReadResultIntoStruct(s *structpb.Struct) error {
if len(p.ResultRaw) == 0 {
return nil
}
z, err := zlib.NewReader(bytes.NewReader(p.ResultRaw))
if err != nil {
return errors.Annotate(err, "failed to open the blob for zlib decompression").Err()
}
if err := (&jsonpb.Unmarshaler{}).Unmarshal(z, s); err != nil {
z.Close()
return errors.Annotate(err, "failed to decompress or deserialize the result").Err()
}
if err := z.Close(); err != nil {
return errors.Annotate(err, "failed to close zlib reader").Err()
}
return nil
}
// Proto returns cipd.Processor proto with information from this entity.
func (p *ProcessingResult) Proto() (*api.Processor, error) {
out := &api.Processor{Id: p.ProcID}
if p.CreatedTs.IsZero() {
out.State = api.Processor_PENDING // no result yet
return out, nil
}
out.FinishedTs = google.NewTimestamp(p.CreatedTs)
if p.Success {
out.State = api.Processor_SUCCEEDED
res := &structpb.Struct{}
if err := p.ReadResultIntoStruct(res); err != nil {
return nil, err
}
if len(res.Fields) != 0 {
out.Result = res
}
} else {
out.State = api.Processor_FAILED
out.Error = p.Error
}
return out, nil
}