diff --git a/go.mod b/go.mod index 375b3eb..01d69ea 100644 --- a/go.mod +++ b/go.mod @@ -1,16 +1,25 @@ module git.phc.dm.unipi.it/phc/website -go 1.13 +go 1.18 require ( github.com/alecthomas/chroma v0.9.4 github.com/gofiber/fiber/v2 v2.34.0 github.com/gofiber/redirect/v2 v2.1.23 github.com/joho/godotenv v1.4.0 - github.com/klauspost/compress v1.15.6 // indirect github.com/litao91/goldmark-mathjax v0.0.0-20210217064022-a43cf739a50f github.com/yuin/goldmark v1.4.4 github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01 - golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) + +require ( + github.com/andybalholm/brotli v1.0.4 // indirect + github.com/dlclark/regexp2 v1.4.0 // indirect + github.com/klauspost/compress v1.15.6 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.37.0 // indirect + github.com/valyala/tcplisten v1.0.0 // indirect + golang.org/x/exp v0.0.0-20220602145555-4a0574d9293f // indirect + golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect +) diff --git a/go.sum b/go.sum index 4ffcd98..3e649fb 100644 --- a/go.sum +++ b/go.sum @@ -52,6 +52,8 @@ github.com/yuin/goldmark v1.4.4/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2 github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01 h1:0SJnXjE4jDClMW6grE0xpNhwpqbPwkBTn8zpVw5C0SI= github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01/go.mod h1:TwKQPa5XkCCRC2GRZ5wtfNUTQ2+9/i19mGRijFeJ4BE= golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/exp v0.0.0-20220602145555-4a0574d9293f h1:KK6mxegmt5hGJRcAnEDjSNLxIRhZxDcgwMbcO/lMCRM= +golang.org/x/exp v0.0.0-20220602145555-4a0574d9293f/go.mod h1:yh0Ynu2b5ZUe3MQfp2nM0ecK7wsgouWTDN0FNeJuIys= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/main.go b/main.go index 76db371..3e98ba4 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "encoding/json" "html/template" "git.phc.dm.unipi.it/phc/website/articles" @@ -45,7 +44,7 @@ func main() { "/": "home.html", "/link": "link.html", "/login": "login.html", - "/storia": "storia.html", + "/utenti": "utenti.html", } for route, view := range actuallyStaticRoutes { @@ -62,19 +61,18 @@ func main() { return err } - c.Type("json") - return json.NewEncoder(c).Encode(utenti) + return c.JSON(utenti) }) - app.Get("/utenti", func(c *fiber.Ctx) error { - utenti, err := GetUtenti() + app.Get("/storia", func(c *fiber.Ctx) error { + storia, err := GetStoria() if err != nil { return err } c.Type("html") - return renderer.Render(c, "utenti.html", util.H{ - "Utenti": utenti, + return renderer.Render(c, "storia.html", util.H{ + "Storia": storia, }) }) diff --git a/public/style.css b/public/style.css index 2ec9004..555b98f 100644 --- a/public/style.css +++ b/public/style.css @@ -800,7 +800,7 @@ form .field-set input { } .page-storia .history-container .events .spacer { - height: 4rem; + height: calc(var(--size) * 1rem); } @keyframes rotate { diff --git a/storia.go b/storia.go index 03a08d9..e9a2bef 100644 --- a/storia.go +++ b/storia.go @@ -3,40 +3,127 @@ package main import ( "encoding/json" "io/ioutil" + "math" + "sort" + "strconv" + "strings" + + "git.phc.dm.unipi.it/phc/website/util" ) +type byEventDateDescending []*GenericEvent + +func (m byEventDateDescending) Len() int { + return len(m) +} +func (m byEventDateDescending) Swap(i, j int) { + m[i], m[j] = m[j], m[i] +} +func (m byEventDateDescending) Less(i, j int) bool { + return m[i].Date > m[j].Date +} + type Macchinista struct { - Name string `json:"username"` - Uid string `json:"password"` - Date string `json:"date"` - Type string `json:"type"` + Uid string `json:"uid"` + FullName string `json:"fullName"` + EntryDate string `json:"entryDate"` + ExitDate string `json:"exitDate"` } type GenericEvent struct { + // Campi principali + Type string `json:"type"` + Date string `json:"date"` + + // Altri campi utilizzati ogni tanto Title string `json:"title"` Description string `json:"description"` - Date string `json:"date"` - Type string `json:"type"` + + // Se il tipo di evento è riferito ad un utente usiamo anche questo campo apposta + Uid string `json:"uid"` + FullName string `json:"fullName"` + + // Se il campo è uno "spacer" questa è la sua dimensione + Size int `json:"size"` } -type EventsDB struct { - Macchinisti map[string]*Macchinista `json:"macchinisti"` - GenericEvent map[string]*GenericEvent `json:"eventi,omitempty"` +type HistoryDB struct { + Macchinisti []*Macchinista `json:"macchinisti"` + GenericEvent []*GenericEvent `json:"eventi"` } -func GetEvents() ([]EventsDB, error) { - var events []EventsDB +func GetRawHistory() (*HistoryDB, error) { + var rawHistory HistoryDB - eventsJsonData, err := ioutil.ReadFile("./storia.json") + historyDB, err := ioutil.ReadFile("./storia.json") if err != nil { return nil, err } - if err := json.Unmarshal(eventsJsonData, &events); err != nil { + if err := json.Unmarshal(historyDB, &rawHistory); err != nil { return nil, err } - return events, nil + return &rawHistory, nil +} + +func getDateYear(date string) int { + year, err := strconv.Atoi(strings.Split(date, "/")[0]) + if err != nil { + panic(err) // Tanto nel caso si nota in fase di sviluppo visto che "storia.json" è statico + } + + return year +} + +func withSpacers(events []*GenericEvent) []*GenericEvent { + // crea un nuovo slice con capacità già almeno "len(events)" + newEvents := make([]*GenericEvent, 0, len(events)) + + for i := 0; i < len(events)-1; i++ { + newEvents = append(newEvents, events[i]) + + delta := util.Abs(getDateYear(events[i+1].Date) - getDateYear(events[i].Date)) + if delta > 1 { + logDelta := int(math.Log(float64(delta))) + newEvents = append(newEvents, &GenericEvent{ + Type: "spacer", + Size: logDelta, + }) + } + } + newEvents = append(newEvents, events[len(events)-1]) + + return newEvents } -// TODO: Logica per ordinare gli eventi +func GetStoria() ([]*GenericEvent, error) { + history, err := GetRawHistory() + if err != nil { + return nil, err + } + + events := history.GenericEvent[:] + for _, m := range history.Macchinisti { + if m.ExitDate != "" { + events = append(events, &GenericEvent{ + Type: "exit-macchinista", + Uid: m.Uid, + FullName: m.FullName, + Date: m.ExitDate, + }) + } + if m.EntryDate != "" { + events = append(events, &GenericEvent{ + Type: "entry-macchinista", + Uid: m.Uid, + FullName: m.FullName, + Date: m.EntryDate, + }) + } + } + + sort.Sort(byEventDateDescending(events)) + + return withSpacers(events), nil +} diff --git a/storia.json b/storia.json index 6a865b1..50123a8 100644 --- a/storia.json +++ b/storia.json @@ -1,22 +1,44 @@ -[ - "macchinisti": +{ + "macchinisti": [ { - "nome": "Francesco Minnocci", "uid": "minnocci", - "date": "2022/02", - "type": "entry" + "fullName": "Francesco Minnocci", + "entryDate": "2022/02" + }, + { + "uid": "manicastri", + "fullName": "Francesco Manicastri", + "entryDate": "2022/05" + }, + { + "uid": "delucreziis", + "fullName": "Antonio De Lucreziis", + "entryDate": "2019/??" }, { - "nome": "Francesco Caporali", "uid": "caporali", - "date": "201?/??", - "type": "exit" + "fullName": "Francesco Caporali", + "entryDate": "2018/??", + "exitDate": "2022/03" + }, + { + "uid": "dachille", + "fullName": "Letizia D'Achille", + "entryDate": "2018/??", + "exitDate": "2022/03" } - }, - "eventi": + ], + "eventi": [ { + "type": "simple", "title": "Nuovo sito del PHC", "description": "TBA", "date": "2022/??" + }, + { + "type": "simple", + "title": "Apertura del PHC", + "date": "1992" } -] + ] +} diff --git a/util/math.go b/util/math.go new file mode 100644 index 0000000..df56471 --- /dev/null +++ b/util/math.go @@ -0,0 +1,17 @@ +package util + +import ( + "golang.org/x/exp/constraints" +) + +type Numeric interface { + constraints.Float | constraints.Integer +} + +func Abs[T Numeric](a T) T { + if a < 0 { + return -a + } + + return a +} diff --git a/views/storia.html b/views/storia.html index 876e677..3ae5641 100644 --- a/views/storia.html +++ b/views/storia.html @@ -12,68 +12,39 @@