@@ -57,7 +57,9 @@ func (u *Uploader) reports(todo *work) ([]string, error) {
57
57
if err != nil {
58
58
return nil , err
59
59
}
60
- todo .readyfiles = append (todo .readyfiles , fname )
60
+ if fname != "" {
61
+ todo .readyfiles = append (todo .readyfiles , fname )
62
+ }
61
63
}
62
64
return todo .readyfiles , nil
63
65
}
@@ -176,81 +178,91 @@ func (u *Uploader) createReport(start time.Time, expiryDate string, files []stri
176
178
if err := json .Unmarshal (localContents , & x ); err != nil {
177
179
return "" , fmt .Errorf ("failed to unmarshal local report (%v)" , err )
178
180
}
179
- // 2. create the uploadable version
180
- cfg := config .NewConfig (u .Config )
181
- upload := & telemetry.Report {
182
- Week : report .Week ,
183
- LastWeek : report .LastWeek ,
184
- X : report .X ,
185
- Config : report .Config ,
186
- }
187
- for _ , p := range report .Programs {
188
- // does the uploadConfig want this program?
189
- // if so, copy over the Stacks and Counters
190
- // that the uploadConfig mentions.
191
- if ! cfg .HasGoVersion (p .GoVersion ) || ! cfg .HasProgram (p .Program ) || ! cfg .HasVersion (p .Program , p .Version ) {
192
- continue
193
- }
194
- x := & telemetry.ProgramReport {
195
- Program : p .Program ,
196
- Version : p .Version ,
197
- GOOS : p .GOOS ,
198
- GOARCH : p .GOARCH ,
199
- GoVersion : p .GoVersion ,
200
- Counters : make (map [string ]int64 ),
201
- Stacks : make (map [string ]int64 ),
181
+
182
+ var uploadContents []byte
183
+ if uploadOK {
184
+ // 2. create the uploadable version
185
+ cfg := config .NewConfig (u .Config )
186
+ upload := & telemetry.Report {
187
+ Week : report .Week ,
188
+ LastWeek : report .LastWeek ,
189
+ X : report .X ,
190
+ Config : report .Config ,
202
191
}
203
- upload .Programs = append (upload .Programs , x )
204
- for k , v := range p .Counters {
205
- if cfg .HasCounter (p .Program , k ) && report .X <= cfg .Rate (p .Program , k ) {
206
- x .Counters [k ] = v
192
+ for _ , p := range report .Programs {
193
+ // does the uploadConfig want this program?
194
+ // if so, copy over the Stacks and Counters
195
+ // that the uploadConfig mentions.
196
+ if ! cfg .HasGoVersion (p .GoVersion ) || ! cfg .HasProgram (p .Program ) || ! cfg .HasVersion (p .Program , p .Version ) {
197
+ continue
207
198
}
208
- }
209
- // and the same for Stacks
210
- // this can be made more efficient, when it matters
211
- for k , v := range p .Stacks {
212
- before , _ , _ := strings .Cut (k , "\n " )
213
- if cfg .HasStack (p .Program , before ) && report .X <= cfg .Rate (p .Program , before ) {
214
- x .Stacks [k ] = v
199
+ x := & telemetry.ProgramReport {
200
+ Program : p .Program ,
201
+ Version : p .Version ,
202
+ GOOS : p .GOOS ,
203
+ GOARCH : p .GOARCH ,
204
+ GoVersion : p .GoVersion ,
205
+ Counters : make (map [string ]int64 ),
206
+ Stacks : make (map [string ]int64 ),
207
+ }
208
+ upload .Programs = append (upload .Programs , x )
209
+ for k , v := range p .Counters {
210
+ if cfg .HasCounter (p .Program , k ) && report .X <= cfg .Rate (p .Program , k ) {
211
+ x .Counters [k ] = v
212
+ }
213
+ }
214
+ // and the same for Stacks
215
+ // this can be made more efficient, when it matters
216
+ for k , v := range p .Stacks {
217
+ before , _ , _ := strings .Cut (k , "\n " )
218
+ if cfg .HasStack (p .Program , before ) && report .X <= cfg .Rate (p .Program , before ) {
219
+ x .Stacks [k ] = v
220
+ }
215
221
}
216
222
}
217
- }
218
223
219
- uploadContents , err := json .MarshalIndent (upload , "" , " " )
220
- if err != nil {
221
- return "" , fmt .Errorf ("failed to marshal upload report (%v)" , err )
224
+ uploadContents , err = json .MarshalIndent (upload , "" , " " )
225
+ if err != nil {
226
+ return "" , fmt .Errorf ("failed to marshal upload report (%v)" , err )
227
+ }
222
228
}
223
229
localFileName := filepath .Join (u .LocalDir , "local." + expiryDate + ".json" )
224
230
uploadFileName := filepath .Join (u .LocalDir , expiryDate + ".json" )
231
+
232
+ /* Prepare to write files */
225
233
// if either file exists, someone has been here ahead of us
226
234
// (there is still a race, but this check shortens the open window)
227
235
if _ , err := os .Stat (localFileName ); err == nil {
228
236
deleteFiles (files )
229
- return "" , fmt .Errorf ("report %s already exists" , localFileName )
237
+ return "" , fmt .Errorf ("local report %s already exists" , localFileName )
230
238
}
231
239
if _ , err := os .Stat (uploadFileName ); err == nil {
232
240
deleteFiles (files )
233
- return "" , fmt .Errorf ("local report %s already exists" , uploadFileName )
241
+ return "" , fmt .Errorf ("report %s already exists" , uploadFileName )
234
242
}
235
243
// write the uploadable file
236
244
var errUpload , errLocal error
237
245
if uploadOK {
238
246
errUpload = os .WriteFile (uploadFileName , uploadContents , 0644 )
239
247
}
240
- // save all local reports.
248
+ // write the local file
241
249
errLocal = os .WriteFile (localFileName , localContents , 0644 )
250
+ /* Wrote the files */
242
251
243
252
// even though these errors won't occur, what should happen
244
253
// if errUpload == nil and it is ok to upload, and errLocal != nil?
245
254
if errLocal != nil {
246
- return "" , fmt .Errorf ("failed to write local file %s (%v)" , uploadFileName , errLocal )
255
+ return "" , fmt .Errorf ("failed to write local file %s (%v)" , localFileName , errLocal )
247
256
}
248
257
if errUpload != nil {
249
258
return "" , fmt .Errorf ("failed to write upload file %s (%v)" , uploadFileName , errUpload )
250
259
}
251
- logger .Printf ("created %s , deleting %v" , uploadFileName , files )
260
+ logger .Printf ("created %q , deleting %v" , uploadFileName , files )
252
261
deleteFiles (files )
253
- return uploadFileName , nil
262
+ if uploadOK {
263
+ return uploadFileName , nil
264
+ }
265
+ return "" , nil
254
266
}
255
267
256
268
// return an existing ProgremReport, or create anew
0 commit comments