From cba7cf51876a8193fc42cf9d103690e2d01a418c Mon Sep 17 00:00:00 2001 From: Anton Berezhnyi Date: Tue, 1 Oct 2024 16:05:09 -0400 Subject: [PATCH] update storage scores --- updatedScoresImporter.go | 30 +++++++++++++----------------- updatedScoresImporter_test.go | 34 +++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/updatedScoresImporter.go b/updatedScoresImporter.go index 7346c9c..a7f5a80 100644 --- a/updatedScoresImporter.go +++ b/updatedScoresImporter.go @@ -3,7 +3,6 @@ package main import ( "context" "database/sql" - "encoding/binary" "encoding/json" "fmt" dekanatEvents "github.com/kneu-messenger-pigeon/dekanat-events" @@ -19,8 +18,6 @@ const StorageTimeFormat = time.RFC3339 const UpdateScoreQuery = ScoreSelect + ` WHERE REGDATE > ? AND ID_T_PD_CMS IS NOT NULL ` + ScoreSelectOrderBy -const LastRegDateStorageLength = 8 - type UpdatedScoresImporterInterface interface { Execute(context context.Context) AddEvent(event dekanatEvents.ScoreEditEvent) @@ -194,18 +191,17 @@ func (importer *UpdatedScoresImporter) getLastRegDate() time.Time { if importer.lastRegDate.IsZero() { bytesValue, err := importer.storage.Get() - if bytesValue != nil && len(bytesValue) == LastRegDateStorageLength { - importer.lastRegDate = time.Unix( - int64(binary.LittleEndian.Uint64(bytesValue)), 0, - ) - } else if err == nil { // storage not exist or empty. Make initial value - importer.lastRegDate = time.Now().Add(-time.Minute) + if err == nil { + err = importer.lastRegDate.UnmarshalBinary(bytesValue) } if err != nil { fmt.Fprintf(importer.out, "[%s] Failed to get score Last Rag Date from file %v \n", t(), err) + } + + if importer.lastRegDate.IsZero() { // set non zero for prevent repeat error - importer.lastRegDate = time.Now() + importer.lastRegDate = time.Now().Add(-time.Minute) } } @@ -213,15 +209,15 @@ func (importer *UpdatedScoresImporter) getLastRegDate() time.Time { } func (importer *UpdatedScoresImporter) setLastRegDate(newLastRegDate time.Time) (err error) { - if importer.lastRegDate != newLastRegDate { - newLastRegDate.In(time.Local) - importer.lastRegDate = newLastRegDate - - value := make([]byte, LastRegDateStorageLength) - binary.LittleEndian.PutUint64(value, uint64(newLastRegDate.Unix())) + if !importer.lastRegDate.Equal(newLastRegDate) { + importer.lastRegDate = newLastRegDate.In(time.Local) + fmt.Printf("newLastRegDate: %+v\n", newLastRegDate) + var value []byte + value, _ = importer.lastRegDate.MarshalBinary() err = importer.storage.Set(value) + if err != nil { - fmt.Fprintf(importer.out, "[%s] Failed to write LessonMaxId %v \n", t(), err) + fmt.Fprintf(importer.out, "[%s] Failed to write setLastRegDate %v \n", t(), err) } } return diff --git a/updatedScoresImporter_test.go b/updatedScoresImporter_test.go index 6953360..ac92384 100644 --- a/updatedScoresImporter_test.go +++ b/updatedScoresImporter_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "database/sql" - "encoding/binary" "encoding/json" "errors" "github.com/DATA-DOG/go-sqlmock" @@ -31,8 +30,7 @@ var scoreSelectExpectedColumns = []string{ } func timeToBytes(t time.Time) []byte { - b := make([]byte, 8) - binary.LittleEndian.PutUint64(b, uint64(t.Unix())) + b, _ := t.MarshalBinary() return b } @@ -48,6 +46,13 @@ func TestExecuteImportUpdatedScores(t *testing.T) { var matchContext = mock.MatchedBy(func(ctx context.Context) bool { return true }) lastRegDate := time.Now().Add(-time.Minute * 20) + lastRegDate = lastRegDate.Add(-time.Duration(lastRegDate.Nanosecond())) + + lastRegDate = time.Date( + lastRegDate.Year(), lastRegDate.Month(), lastRegDate.Day(), + lastRegDate.Hour(), lastRegDate.Minute(), lastRegDate.Second(), + 0, lastRegDate.Location(), + ) syncedAtRewrite := time.Now() syncedAtRewrite = time.Date( @@ -80,6 +85,7 @@ func TestExecuteImportUpdatedScores(t *testing.T) { IsDeleted: true, }, SyncedAt: syncedAtRewrite, + UpdatedAt: syncedAtRewrite, ScoreSource: events.Realtime, } @@ -105,9 +111,11 @@ func TestExecuteImportUpdatedScores(t *testing.T) { } rows := sqlmock.NewRows(scoreSelectExpectedColumns).AddRow( - expectedEvent.Id, expectedEvent.StudentId, expectedEvent.LessonId, expectedEvent.LessonPart, + expectedEvent.Id, expectedEvent.StudentId, + expectedEvent.LessonId, expectedEvent.LessonPart, customGroupLessonIdSql, - expectedEvent.DisciplineId, expectedEvent.Semester, expectedEvent.Value, expectedEvent.IsAbsent, + expectedEvent.DisciplineId, expectedEvent.Semester, + expectedEvent.Value, expectedEvent.IsAbsent, expectedEvent.UpdatedAt, expectedEvent.IsDeleted, ) @@ -123,7 +131,7 @@ func TestExecuteImportUpdatedScores(t *testing.T) { dbMock.ExpectRollback() fileStorageMock := fileStorageMocks.NewInterface(t) - fileStorageMock.On("Get").Times(2).Return(nil, nil) + fileStorageMock.On("Get").Times(1).Return(nil, nil) fileStorageMock.On("Set", timeToBytes(expectedEvent.UpdatedAt)).Once().Return(nil) writerMock := eventsMocks.NewWriterInterface(t) @@ -484,7 +492,7 @@ func TestGetLastRegDate(t *testing.T) { storage: fileStorageMock, } - mixExpectedLastRegDate := time.Now() + mixExpectedLastRegDate := time.Now().Add(-time.Minute * 1) actualLastRegDate := updatedLessonsImporter.getLastRegDate() assert.True(t, actualLastRegDate.After(mixExpectedLastRegDate)) @@ -506,10 +514,14 @@ func TestSetLastRegDate(t *testing.T) { 0, now.Location(), ).Add(-time.Minute * 10) + var matchDatetimeContext = mock.MatchedBy(func(b []byte) bool { + var v time.Time + err := v.UnmarshalBinary(b) + return assert.NoError(t, err) && assert.Equal(t, newLastRegDate, v) + }) + fileStorageMock := fileStorageMocks.NewInterface(t) - fileStorageMock.On( - "Set", timeToBytes(newLastRegDate), - ).Once().Return(expectedError) + fileStorageMock.On("Set", matchDatetimeContext).Once().Return(expectedError) updatedLessonsImporter := &UpdatedScoresImporter{ out: &out, @@ -521,6 +533,6 @@ func TestSetLastRegDate(t *testing.T) { assert.Error(t, actualError) assert.Equal(t, expectedError, actualError) - assert.Contains(t, out.String(), "Failed to write LessonMaxId "+expectedError.Error()) + assert.Contains(t, out.String(), "Failed to write setLastRegDate "+expectedError.Error()) }) }