1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

fix: use custom date for summary model to support sqlite deserialization

This commit is contained in:
Ferdinand Mütsch 2020-10-16 14:49:22 +02:00
parent f7520b2b4a
commit 67a59561c8
5 changed files with 27 additions and 27 deletions

View File

@ -64,11 +64,11 @@ func NewSummariesFrom(summaries []*models.Summary, filters *models.Filters) *Sum
for i, s := range summaries { for i, s := range summaries {
data[i] = newDataFrom(s) data[i] = newDataFrom(s)
if s.FromTime.Before(minDate) { if s.FromTime.T().Before(minDate) {
minDate = s.FromTime minDate = s.FromTime.T()
} }
if s.ToTime.After(maxDate) { if s.ToTime.T().After(maxDate) {
maxDate = s.ToTime maxDate = s.ToTime.T()
} }
} }
@ -101,8 +101,8 @@ func newDataFrom(s *models.Summary) *summariesData {
}, },
Range: &summariesRange{ Range: &summariesRange{
Date: time.Now().Format(time.RFC3339), Date: time.Now().Format(time.RFC3339),
End: s.ToTime, End: s.ToTime.T(),
Start: s.FromTime, Start: s.FromTime.T(),
Text: "", Text: "",
Timezone: zone, Timezone: zone,
}, },

View File

@ -36,8 +36,8 @@ const UnknownSummaryKey = "unknown"
type Summary struct { type Summary struct {
ID uint `json:"-" gorm:"primary_key"` ID uint `json:"-" gorm:"primary_key"`
UserID string `json:"user_id" gorm:"not null; index:idx_time_summary_user"` UserID string `json:"user_id" gorm:"not null; index:idx_time_summary_user"`
FromTime time.Time `json:"from" gorm:"not null; type:timestamp(3); default:CURRENT_TIMESTAMP(3); index:idx_time_summary_user"` FromTime CustomTime `json:"from" gorm:"not null; type:timestamp(3); default:CURRENT_TIMESTAMP(3); index:idx_time_summary_user"`
ToTime time.Time `json:"to" gorm:"not null; type:timestamp(3); default:CURRENT_TIMESTAMP(3); index:idx_time_summary_user"` ToTime CustomTime `json:"to" gorm:"not null; type:timestamp(3); default:CURRENT_TIMESTAMP(3); index:idx_time_summary_user"`
Projects []*SummaryItem `json:"projects"` Projects []*SummaryItem `json:"projects"`
Languages []*SummaryItem `json:"languages"` Languages []*SummaryItem `json:"languages"`
Editors []*SummaryItem `json:"editors"` Editors []*SummaryItem `json:"editors"`

View File

@ -98,7 +98,7 @@ func (srv *AggregationService) trigger(jobs chan<- *AggregationJob) error {
userSummaryTimes := make(map[string]time.Time) userSummaryTimes := make(map[string]time.Time)
for _, s := range latestSummaries { for _, s := range latestSummaries {
userSummaryTimes[s.UserID] = s.ToTime userSummaryTimes[s.UserID] = s.ToTime.T()
} }
missingUserIDs := make([]string, 0) missingUserIDs := make([]string, 0)

View File

@ -105,8 +105,8 @@ func (srv *SummaryService) Construct(from, to time.Time, user *models.User, reco
realFrom, realTo := from, to realFrom, realTo := from, to
if len(existingSummaries) > 0 { if len(existingSummaries) > 0 {
realFrom = existingSummaries[0].FromTime realFrom = existingSummaries[0].FromTime.T()
realTo = existingSummaries[len(existingSummaries)-1].ToTime realTo = existingSummaries[len(existingSummaries)-1].ToTime.T()
for _, summary := range existingSummaries { for _, summary := range existingSummaries {
summary.FillUnknown() summary.FillUnknown()
@ -124,8 +124,8 @@ func (srv *SummaryService) Construct(from, to time.Time, user *models.User, reco
aggregatedSummary := &models.Summary{ aggregatedSummary := &models.Summary{
UserID: user.ID, UserID: user.ID,
FromTime: realFrom, FromTime: models.CustomTime(realFrom),
ToTime: realTo, ToTime: models.CustomTime(realTo),
Projects: projectItems, Projects: projectItems,
Languages: languageItems, Languages: languageItems,
Editors: editorItems, Editors: editorItems,
@ -256,13 +256,13 @@ func getMissingIntervals(from, to time.Time, existingSummaries []*models.Summary
intervals := make([]*Interval, 0) intervals := make([]*Interval, 0)
// Pre // Pre
if from.Before(existingSummaries[0].FromTime) { if from.Before(existingSummaries[0].FromTime.T()) {
intervals = append(intervals, &Interval{from, existingSummaries[0].FromTime}) intervals = append(intervals, &Interval{from, existingSummaries[0].FromTime.T()})
} }
// Between // Between
for i := 0; i < len(existingSummaries)-1; i++ { for i := 0; i < len(existingSummaries)-1; i++ {
t1, t2 := existingSummaries[i].ToTime, existingSummaries[i+1].FromTime t1, t2 := existingSummaries[i].ToTime.T(), existingSummaries[i+1].FromTime.T()
if t1.Equal(t2) { if t1.Equal(t2) {
continue continue
} }
@ -272,13 +272,13 @@ func getMissingIntervals(from, to time.Time, existingSummaries []*models.Summary
td2 := time.Date(t2.Year(), t2.Month(), t2.Day(), 0, 0, 0, 0, t2.Location()) td2 := time.Date(t2.Year(), t2.Month(), t2.Day(), 0, 0, 0, 0, t2.Location())
// one or more day missing in between? // one or more day missing in between?
if td1.Before(td2) { if td1.Before(td2) {
intervals = append(intervals, &Interval{existingSummaries[i].ToTime, existingSummaries[i+1].FromTime}) intervals = append(intervals, &Interval{existingSummaries[i].ToTime.T(), existingSummaries[i+1].FromTime.T()})
} }
} }
// Post // Post
if to.After(existingSummaries[len(existingSummaries)-1].ToTime) { if to.After(existingSummaries[len(existingSummaries)-1].ToTime.T()) {
intervals = append(intervals, &Interval{existingSummaries[len(existingSummaries)-1].ToTime, to}) intervals = append(intervals, &Interval{existingSummaries[len(existingSummaries)-1].ToTime.T(), to})
} }
return intervals return intervals
@ -306,12 +306,12 @@ func mergeSummaries(summaries []*models.Summary) (*models.Summary, error) {
return nil, errors.New("users don't match") return nil, errors.New("users don't match")
} }
if s.FromTime.Before(minTime) { if s.FromTime.T().Before(minTime) {
minTime = s.FromTime minTime = s.FromTime.T()
} }
if s.ToTime.After(maxTime) { if s.ToTime.T().After(maxTime) {
maxTime = s.ToTime maxTime = s.ToTime.T()
} }
finalSummary.Projects = mergeSummaryItems(finalSummary.Projects, s.Projects) finalSummary.Projects = mergeSummaryItems(finalSummary.Projects, s.Projects)
@ -321,8 +321,8 @@ func mergeSummaries(summaries []*models.Summary) (*models.Summary, error) {
finalSummary.Machines = mergeSummaryItems(finalSummary.Machines, s.Machines) finalSummary.Machines = mergeSummaryItems(finalSummary.Machines, s.Machines)
} }
finalSummary.FromTime = minTime finalSummary.FromTime = models.CustomTime(minTime)
finalSummary.ToTime = maxTime finalSummary.ToTime = models.CustomTime(maxTime)
return finalSummary, nil return finalSummary, nil
} }

View File

@ -55,8 +55,8 @@
<div class="flex justify-center"> <div class="flex justify-center">
<div class="p-1"> <div class="p-1">
<div class="flex justify-center p-4 bg-white rounded shadow"> <div class="flex justify-center p-4 bg-white rounded shadow">
<p class="mx-2"><strong>▶️</strong> <span title="Start Time">{{ .FromTime | date }}</span></p> <p class="mx-2"><strong>▶️</strong> <span title="Start Time">{{ .FromTime.T | date }}</span></p>
<p class="mx-2"><strong></strong> <span title="End Time">{{ .ToTime | date }}</span></p> <p class="mx-2"><strong></strong> <span title="End Time">{{ .ToTime.T | date }}</span></p>
<p class="mx-2"><strong></strong> <span id="total-span" title="Total Hours"></span></p> <p class="mx-2"><strong></strong> <span id="total-span" title="Total Hours"></span></p>
</div> </div>
</div> </div>