Skip to content

Commit

Permalink
update storage scores
Browse files Browse the repository at this point in the history
  • Loading branch information
berejant committed Oct 1, 2024
1 parent cdf3306 commit cba7cf5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
30 changes: 13 additions & 17 deletions updatedScoresImporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"database/sql"
"encoding/binary"
"encoding/json"
"fmt"
dekanatEvents "github.com/kneu-messenger-pigeon/dekanat-events"
Expand All @@ -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)
Expand Down Expand Up @@ -194,34 +191,33 @@ 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)
}
}

return importer.lastRegDate
}

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
Expand Down
34 changes: 23 additions & 11 deletions updatedScoresImporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"database/sql"
"encoding/binary"
"encoding/json"
"errors"
"github.com/DATA-DOG/go-sqlmock"
Expand All @@ -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
}

Expand All @@ -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(
Expand Down Expand Up @@ -80,6 +85,7 @@ func TestExecuteImportUpdatedScores(t *testing.T) {
IsDeleted: true,
},
SyncedAt: syncedAtRewrite,
UpdatedAt: syncedAtRewrite,
ScoreSource: events.Realtime,
}

Expand All @@ -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,
)

Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -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,
Expand All @@ -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())
})
}

0 comments on commit cba7cf5

Please sign in to comment.