2018-08-23 20:19:16 +03:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2018-08-23 22:24:57 +03:00
|
|
|
"encoding/gob"
|
2020-02-25 23:35:57 +03:00
|
|
|
"fmt"
|
2020-06-26 00:18:17 +03:00
|
|
|
"time"
|
2018-08-23 22:24:57 +03:00
|
|
|
|
2018-08-23 20:19:16 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
2019-08-11 15:08:03 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2019-02-19 01:16:47 +03:00
|
|
|
"github.com/MichaelMure/git-bug/identity"
|
2018-09-11 23:04:16 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/lamport"
|
2018-08-23 20:19:16 +03:00
|
|
|
)
|
|
|
|
|
2019-02-23 15:01:46 +03:00
|
|
|
// Package initialisation used to register the type for (de)serialization
|
|
|
|
func init() {
|
|
|
|
gob.Register(BugExcerpt{})
|
|
|
|
}
|
|
|
|
|
2018-08-23 20:19:16 +03:00
|
|
|
// BugExcerpt hold a subset of the bug values to be able to sort and filter bugs
|
|
|
|
// efficiently without having to read and compile each raw bugs.
|
|
|
|
type BugExcerpt struct {
|
2019-08-12 17:12:14 +03:00
|
|
|
Id entity.Id
|
2018-08-23 20:19:16 +03:00
|
|
|
|
2018-09-11 23:04:16 +03:00
|
|
|
CreateLamportTime lamport.Time
|
|
|
|
EditLamportTime lamport.Time
|
2020-07-28 21:39:07 +03:00
|
|
|
CreateUnixTime int64
|
|
|
|
EditUnixTime int64
|
2018-08-23 20:19:16 +03:00
|
|
|
|
2019-03-31 23:32:35 +03:00
|
|
|
Status bug.Status
|
|
|
|
Labels []bug.Label
|
|
|
|
Title string
|
|
|
|
LenComments int
|
2019-08-12 17:12:14 +03:00
|
|
|
Actors []entity.Id
|
|
|
|
Participants []entity.Id
|
2019-03-02 21:28:15 +03:00
|
|
|
|
|
|
|
// If author is identity.Bare, LegacyAuthor is set
|
|
|
|
// If author is identity.Identity, AuthorId is set and data is deported
|
|
|
|
// in a IdentityExcerpt
|
|
|
|
LegacyAuthor LegacyAuthorExcerpt
|
2019-08-12 17:12:14 +03:00
|
|
|
AuthorId entity.Id
|
2018-10-01 22:55:41 +03:00
|
|
|
|
|
|
|
CreateMetadata map[string]string
|
2018-08-23 20:19:16 +03:00
|
|
|
}
|
|
|
|
|
2019-02-19 01:16:47 +03:00
|
|
|
// identity.Bare data are directly embedded in the bug excerpt
|
|
|
|
type LegacyAuthorExcerpt struct {
|
2020-02-25 23:35:57 +03:00
|
|
|
Name string
|
|
|
|
Login string
|
2019-02-19 01:16:47 +03:00
|
|
|
}
|
|
|
|
|
2019-04-11 21:04:44 +03:00
|
|
|
func (l LegacyAuthorExcerpt) DisplayName() string {
|
2020-02-25 23:35:57 +03:00
|
|
|
switch {
|
|
|
|
case l.Name == "" && l.Login != "":
|
|
|
|
return l.Login
|
|
|
|
case l.Name != "" && l.Login == "":
|
|
|
|
return l.Name
|
|
|
|
case l.Name != "" && l.Login != "":
|
|
|
|
return fmt.Sprintf("%s (%s)", l.Name, l.Login)
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("invalid person data")
|
2019-04-11 21:04:44 +03:00
|
|
|
}
|
|
|
|
|
2018-09-09 21:21:49 +03:00
|
|
|
func NewBugExcerpt(b bug.Interface, snap *bug.Snapshot) *BugExcerpt {
|
2020-02-04 00:02:01 +03:00
|
|
|
participantsIds := make([]entity.Id, 0, len(snap.Participants))
|
|
|
|
for _, participant := range snap.Participants {
|
|
|
|
if _, ok := participant.(*identity.Identity); ok {
|
|
|
|
participantsIds = append(participantsIds, participant.Id())
|
|
|
|
}
|
2019-03-31 23:32:35 +03:00
|
|
|
}
|
|
|
|
|
2020-02-04 00:02:01 +03:00
|
|
|
actorsIds := make([]entity.Id, 0, len(snap.Actors))
|
|
|
|
for _, actor := range snap.Actors {
|
|
|
|
if _, ok := actor.(*identity.Identity); ok {
|
|
|
|
actorsIds = append(actorsIds, actor.Id())
|
|
|
|
}
|
2019-03-31 23:32:35 +03:00
|
|
|
}
|
|
|
|
|
2019-02-19 01:16:47 +03:00
|
|
|
e := &BugExcerpt{
|
2018-08-23 20:19:16 +03:00
|
|
|
Id: b.Id(),
|
|
|
|
CreateLamportTime: b.CreateLamportTime(),
|
|
|
|
EditLamportTime: b.EditLamportTime(),
|
2020-07-28 21:39:07 +03:00
|
|
|
CreateUnixTime: b.FirstOp().Time().Unix(),
|
|
|
|
EditUnixTime: snap.EditTime().Unix(),
|
2018-08-23 20:19:16 +03:00
|
|
|
Status: snap.Status,
|
2019-02-18 16:11:37 +03:00
|
|
|
Labels: snap.Labels,
|
2019-03-31 23:32:35 +03:00
|
|
|
Actors: actorsIds,
|
|
|
|
Participants: participantsIds,
|
2019-03-02 21:28:15 +03:00
|
|
|
Title: snap.Title,
|
2019-03-01 17:30:07 +03:00
|
|
|
LenComments: len(snap.Comments),
|
2019-02-18 16:11:37 +03:00
|
|
|
CreateMetadata: b.FirstOp().AllMetadata(),
|
2018-08-23 20:19:16 +03:00
|
|
|
}
|
2019-02-19 01:16:47 +03:00
|
|
|
|
2019-03-02 21:28:15 +03:00
|
|
|
switch snap.Author.(type) {
|
|
|
|
case *identity.Identity:
|
|
|
|
e.AuthorId = snap.Author.Id()
|
|
|
|
case *identity.Bare:
|
|
|
|
e.LegacyAuthor = LegacyAuthorExcerpt{
|
2020-03-05 23:56:43 +03:00
|
|
|
Login: snap.Author.Login(),
|
|
|
|
Name: snap.Author.Name(),
|
2019-03-02 21:28:15 +03:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("unhandled identity type")
|
|
|
|
}
|
|
|
|
|
2019-02-19 01:16:47 +03:00
|
|
|
return e
|
2018-08-23 20:19:16 +03:00
|
|
|
}
|
|
|
|
|
2020-06-26 00:18:17 +03:00
|
|
|
func (b *BugExcerpt) CreateTime() time.Time {
|
2020-07-28 21:39:07 +03:00
|
|
|
return time.Unix(b.CreateUnixTime, 0)
|
2020-06-26 00:18:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BugExcerpt) EditTime() time.Time {
|
2020-07-28 21:39:07 +03:00
|
|
|
return time.Unix(b.EditUnixTime, 0)
|
2020-06-26 00:18:17 +03:00
|
|
|
}
|
|
|
|
|
2018-08-23 20:19:16 +03:00
|
|
|
/*
|
|
|
|
* Sorting
|
|
|
|
*/
|
|
|
|
|
2018-09-09 21:21:49 +03:00
|
|
|
type BugsById []*BugExcerpt
|
|
|
|
|
|
|
|
func (b BugsById) Len() int {
|
|
|
|
return len(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b BugsById) Less(i, j int) bool {
|
|
|
|
return b[i].Id < b[j].Id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b BugsById) Swap(i, j int) {
|
|
|
|
b[i], b[j] = b[j], b[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
type BugsByCreationTime []*BugExcerpt
|
2018-08-23 20:19:16 +03:00
|
|
|
|
|
|
|
func (b BugsByCreationTime) Len() int {
|
|
|
|
return len(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b BugsByCreationTime) Less(i, j int) bool {
|
|
|
|
if b[i].CreateLamportTime < b[j].CreateLamportTime {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if b[i].CreateLamportTime > b[j].CreateLamportTime {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the logical clocks are identical, that means we had a concurrent
|
|
|
|
// edition. In this case we rely on the timestamp. While the timestamp might
|
|
|
|
// be incorrect due to a badly set clock, the drift in sorting is bounded
|
|
|
|
// by the first sorting using the logical clock. That means that if users
|
|
|
|
// synchronize their bugs regularly, the timestamp will rarely be used, and
|
|
|
|
// should still provide a kinda accurate sorting when needed.
|
2020-07-28 21:39:07 +03:00
|
|
|
return b[i].CreateUnixTime < b[j].CreateUnixTime
|
2018-08-23 20:19:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b BugsByCreationTime) Swap(i, j int) {
|
|
|
|
b[i], b[j] = b[j], b[i]
|
|
|
|
}
|
|
|
|
|
2018-09-09 21:21:49 +03:00
|
|
|
type BugsByEditTime []*BugExcerpt
|
2018-08-23 20:19:16 +03:00
|
|
|
|
|
|
|
func (b BugsByEditTime) Len() int {
|
|
|
|
return len(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b BugsByEditTime) Less(i, j int) bool {
|
|
|
|
if b[i].EditLamportTime < b[j].EditLamportTime {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if b[i].EditLamportTime > b[j].EditLamportTime {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the logical clocks are identical, that means we had a concurrent
|
|
|
|
// edition. In this case we rely on the timestamp. While the timestamp might
|
|
|
|
// be incorrect due to a badly set clock, the drift in sorting is bounded
|
|
|
|
// by the first sorting using the logical clock. That means that if users
|
|
|
|
// synchronize their bugs regularly, the timestamp will rarely be used, and
|
|
|
|
// should still provide a kinda accurate sorting when needed.
|
2020-07-28 21:39:07 +03:00
|
|
|
return b[i].EditUnixTime < b[j].EditUnixTime
|
2018-08-23 20:19:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b BugsByEditTime) Swap(i, j int) {
|
|
|
|
b[i], b[j] = b[j], b[i]
|
|
|
|
}
|