github.com/vlifesystems/rulehunter@v0.0.0-20180501090014-673078aa4a83/html/front.go (about)

     1  // Copyright (C) 2016-2017 vLife Systems Ltd <http://vlifesystems.com>
     2  // Licensed under an MIT licence.  Please see LICENSE.md for details.
     3  
     4  package html
     5  
     6  import (
     7  	"html/template"
     8  	"io/ioutil"
     9  	"path/filepath"
    10  	"sort"
    11  
    12  	"github.com/vlifesystems/rulehunter/config"
    13  	"github.com/vlifesystems/rulehunter/progress"
    14  	"github.com/vlifesystems/rulehunter/report"
    15  )
    16  
    17  func generateFront(
    18  	cfg *config.Config,
    19  	pm *progress.Monitor,
    20  ) error {
    21  	const maxNumReports = 10
    22  	type TplExperiment struct {
    23  		Title    string
    24  		Category string
    25  		Status   string
    26  		Msg      string
    27  		Percent  float64
    28  	}
    29  	type TplData struct {
    30  		Categories  map[string]string
    31  		Tags        map[string]string
    32  		Reports     []*TplReport
    33  		Experiments []*TplExperiment
    34  		Html        map[string]template.HTML
    35  	}
    36  	categories := map[string]string{}
    37  	tags := map[string]string{}
    38  
    39  	reportFiles, err := ioutil.ReadDir(filepath.Join(cfg.BuildDir, "reports"))
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	tplReports := []*TplReport{}
    45  	numReports := 0
    46  	sort.Slice(reportFiles, func(i, j int) bool {
    47  		return reportFiles[i].ModTime().After(reportFiles[j].ModTime())
    48  	})
    49  	for _, file := range reportFiles {
    50  		if !file.IsDir() {
    51  			r, err := report.LoadJSON(cfg, file.Name(), maxReportLoadAttempts)
    52  			if err != nil {
    53  				return err
    54  			}
    55  			tags = joinURLMaps(tags, makeTagLinks(r.Tags))
    56  			if categoryName := escapeString(r.Category); categoryName != "" {
    57  				categories[r.Category] = makeCategoryLink(r.Category)
    58  			}
    59  
    60  			if numReports < maxNumReports {
    61  				tplReports = append(
    62  					tplReports,
    63  					newTplReport(
    64  						r.Mode,
    65  						r.Title,
    66  						makeTagLinks(r.Tags),
    67  						r.Category,
    68  						makeCategoryLink(r.Category),
    69  						genReportURLDir(r.Mode, r.Category, r.Title),
    70  						r.Stamp,
    71  					),
    72  				)
    73  			} else {
    74  				break
    75  			}
    76  			numReports++
    77  		}
    78  	}
    79  
    80  	tplExperiments := []*TplExperiment{}
    81  	experiments := pm.GetExperiments()
    82  	for _, experiment := range experiments {
    83  		if experiment.Status.State == progress.Processing {
    84  			tplExperiments = append(
    85  				tplExperiments,
    86  				&TplExperiment{
    87  					experiment.Title,
    88  					experiment.Category,
    89  					experiment.Status.State.String(),
    90  					experiment.Status.Msg,
    91  					experiment.Status.Percent,
    92  				},
    93  			)
    94  			break
    95  		}
    96  	}
    97  
    98  	tplData := TplData{
    99  		Categories:  categories,
   100  		Tags:        tags,
   101  		Reports:     tplReports,
   102  		Experiments: tplExperiments,
   103  		Html:        makeHtml(cfg, "front"),
   104  	}
   105  
   106  	outputFilename := "index.html"
   107  	return writeTemplate(cfg, outputFilename, frontTpl, tplData)
   108  }
   109  
   110  func joinURLMaps(a, b map[string]string) map[string]string {
   111  	r := map[string]string{}
   112  	for n, u := range a {
   113  		r[n] = u
   114  	}
   115  	for n, u := range b {
   116  		r[n] = u
   117  	}
   118  	return r
   119  }