Skip to content

Commit

Permalink
Added showing size of artifact, also use it as detection of its prese…
Browse files Browse the repository at this point in the history
…nce.
  • Loading branch information
tvrzna committed Nov 2, 2023
1 parent 8e2f952 commit d74719f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 10 deletions.
8 changes: 8 additions & 0 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,11 @@ func (b *Job) SaveParams() error {
func (b *Job) LoadParams() {
b.params = loadParams(filepath.Join(b.dir, "params"))
}

func (b *Job) ArtifactSize() int64 {
stat, err := os.Stat(b.ArtifactPath())
if err != nil {
return -1
}
return stat.Size()
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ rm -rf .git/
- [X] Custom name of application (0.3.0)
- [ ] Periodical watcher (running custom script saving state of last check)
- [ ] Pipelining (jobs started according the result status)
- [ ] Size and existance of artifact
- [X] Size and existance of artifact
59 changes: 52 additions & 7 deletions rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,46 @@ type DomainProject struct {
}

type DomainJob struct {
Name string `json:"name"`
Status JobStatus `json:"status"`
StartDate time.Time `json:"startDate"`
EndDate time.Time `json:"endDate"`
Output string `json:"output,omitempty"`
Params map[string]string `json:"params,omitempty"`
Name string `json:"name"`
Status JobStatus `json:"status"`
StartDate time.Time `json:"startDate"`
EndDate time.Time `json:"endDate"`
Output string `json:"output,omitempty"`
Params map[string]string `json:"params,omitempty"`
ArtifactSize float64 `json:"artifactSize,omitempty"`
ArtifactUnit MemoryUnit `json:"artifactUnit"`
}

type DomainStatus struct {
Message string `json:"message"`
Code int `json:"code"`
}

type MemoryUnit byte

const (
UnitB MemoryUnit = iota
UnitK
UnitM
UnitG
UnitT
)

func (b MemoryUnit) String() string {
return []string{"", "Ki", "Mi", "Gi", "Ti", "Pi"}[int(b)]
}

func (b MemoryUnit) MarshalJSON() ([]byte, error) {
return []byte("\"" + b.String() + "\""), nil
}

func (b *MemoryUnit) UnmarshalJSON(data []byte) error {
var v string
json.Unmarshal(data, &v)
*b = map[string]MemoryUnit{"": UnitB, "Ki": UnitK, "Mi": UnitM, "Gi": UnitG, "Ti": UnitT}[v]
return nil
}

type RestService struct {
c *Context
}
Expand Down Expand Up @@ -216,12 +243,30 @@ func (s RestService) jobDetail(projectName, jobNumber string, w http.ResponseWri
}

status := b.Status()
artifactSize := float64(-1)
artifactUnit := UnitB
if s.c.IsBeingBuilt(b) {
status = InProgress
} else {
artifactSize, artifactUnit = tidyUnit(b.ArtifactSize(), 0)
}

output, _ := b.ReadOutput()

e := json.NewEncoder(w)
e.Encode(DomainJob{Name: b.name, Status: status, StartDate: b.StartDate(), EndDate: b.EndDate(), Output: output})
e.Encode(DomainJob{Name: b.name, Status: status, StartDate: b.StartDate(), EndDate: b.EndDate(), Output: output, ArtifactSize: artifactSize, ArtifactUnit: artifactUnit})
}

func tidyUnit(value int64, start byte) (float64, MemoryUnit) {
result := float64(value)
resultUnit := MemoryUnit(start)
for i := start; i < 5; i++ {
if val := result / 1024; val < 1 {
break
} else {
result = val
resultUnit = MemoryUnit(i + 1)
}
}
return result, resultUnit
}
2 changes: 2 additions & 0 deletions web.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"mime"
"net/http"
"os"
"strconv"
"strings"
)

Expand Down Expand Up @@ -96,6 +97,7 @@ func (s *WebService) downloadArtifact(w http.ResponseWriter, r *http.Request) {

w.Header().Set("content-type", s.getMimeType(j.ArtifactPath()))
w.Header().Set("content-disposition", fmt.Sprintf("attachment; filename=\"%s_%s.tar.gz\"", p.name, j.name))
w.Header().Set("content-length", strconv.FormatInt(j.ArtifactSize(), 10))

w.WriteHeader(http.StatusOK)
io.Copy(w, f)
Expand Down
11 changes: 11 additions & 0 deletions www/static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,17 @@ function initApp(name) {
else "";
};

context.artifactExists = () => {
return context.selectedJob != undefined && context.selectedJob.artifactSize != undefined && context.selectedJob.artifactSize > 0;
};

context.artifactSize = () => {
if (context.selectedJob != undefined && context.selectedJob.artifactSize != undefined && context.selectedJob.artifactSize > 0) {
return context.selectedJob.artifactSize.toFixed(2) + ' ' + context.selectedJob.artifactUnit + 'B';
}
return "";
}

context.downloadArtifact = (event) => {
if (event != undefined) {
event.preventDefault();
Expand Down
6 changes: 4 additions & 2 deletions www/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ <h1>{{ .Name }}</h1>
<span class="label">Started:</span>
<span ajsf-text="startDateValue"></span>
<span ajsf-text="jobLength"></span>
<span class="label" ajsf-hide="selectedJob.status | eq 'inprogress'">Artifact:</span>
<a ajsf-href="artifactDownloadUrl()" ajsf-click="downloadArtifact" ajsf-hide="selectedJob.status | eq 'inprogress'">Download</a>
<a ajsf-href="artifactDownloadUrl()" ajsf-click="downloadArtifact" ajsf-show="artifactExists()">
<span class="label" >Artifact</span>
<span ajsf-text="'(' | suffix artifactSize | suffix ')'"></span>
</a>
</div>
<span class="resize" ajsf-click="maximize"></span>
<span class="indicator"></span>
Expand Down

0 comments on commit d74719f

Please sign in to comment.