| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package upload |
| 6 |
|
| 7 |
import ( |
| 8 |
"os" |
| 9 |
"path/filepath" |
| 10 |
"strings" |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
type work struct { |
| 15 |
|
| 16 |
countfiles []string |
| 17 |
readyfiles []string |
| 18 |
|
| 19 |
uploaded map[string]bool |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
func (u *uploader) findWork() work { |
| 26 |
localdir, uploaddir := u.dir.LocalDir(), u.dir.UploadDir() |
| 27 |
var ans work |
| 28 |
fis, err := os.ReadDir(localdir) |
| 29 |
if err != nil { |
| 30 |
u.logger.Printf("Could not find work: failed to read local dir %s: %v", localdir, err) |
| 31 |
return ans |
| 32 |
} |
| 33 |
|
| 34 |
mode, asof := u.dir.Mode() |
| 35 |
u.logger.Printf("Finding work: mode %s, asof %s", mode, asof) |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
for _, fi := range fis { |
| 41 |
if strings.HasSuffix(fi.Name(), ".v1.count") { |
| 42 |
fname := filepath.Join(localdir, fi.Name()) |
| 43 |
_, expiry, err := u.counterDateSpan(fname) |
| 44 |
switch { |
| 45 |
case err != nil: |
| 46 |
u.logger.Printf("Error reading expiry for count file %s: %v", fi.Name(), err) |
| 47 |
case expiry.After(u.startTime): |
| 48 |
u.logger.Printf("Skipping count file %s: still active", fi.Name()) |
| 49 |
default: |
| 50 |
u.logger.Printf("Collecting count file %s", fi.Name()) |
| 51 |
ans.countfiles = append(ans.countfiles, fname) |
| 52 |
} |
| 53 |
} else if strings.HasPrefix(fi.Name(), "local.") { |
| 54 |
|
| 55 |
} else if strings.HasSuffix(fi.Name(), ".json") && mode == "on" { |
| 56 |
|
| 57 |
reportDate := u.uploadReportDate(fi.Name()) |
| 58 |
if !asof.IsZero() && !reportDate.IsZero() { |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
if asof.Before(reportDate) { |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
u.logger.Printf("Uploadable: %s", fi.Name()) |
| 72 |
ans.readyfiles = append(ans.readyfiles, filepath.Join(localdir, fi.Name())) |
| 73 |
} |
| 74 |
} else { |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
u.logger.Printf("Uploadable (missing date): %s", fi.Name()) |
| 82 |
ans.readyfiles = append(ans.readyfiles, filepath.Join(localdir, fi.Name())) |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
fis, err = os.ReadDir(uploaddir) |
| 88 |
if err != nil { |
| 89 |
os.MkdirAll(uploaddir, 0777) |
| 90 |
return ans |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
ans.uploaded = make(map[string]bool) |
| 95 |
for _, fi := range fis { |
| 96 |
if strings.HasSuffix(fi.Name(), ".json") { |
| 97 |
u.logger.Printf("Already uploaded: %s", fi.Name()) |
| 98 |
ans.uploaded[fi.Name()] = true |
| 99 |
} |
| 100 |
} |
| 101 |
return ans |
| 102 |
} |