Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to sync candles once per day #334

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = (ctx) => {
)

const paramsOrder = [
'syncedAt',
'symbol',
'timeframe',
'baseStart',
Expand Down
23 changes: 19 additions & 4 deletions workers/loc.api/sync/data.inserter/data.checker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,8 @@ class DataChecker {
)
const shouldFreshSyncBeAdded = this._shouldFreshSyncBeAdded(
syncUserStepData,
currMts
currMts,
{ dayOfYear: 1 }
)

if (
Expand Down Expand Up @@ -502,7 +503,8 @@ class DataChecker {
) {
const {
measure = 'minutes',
allowedTimeDiff = 60
allowedTimeDiff = 60,
dayOfYear
} = allowedDiff ?? {}

const baseEnd = (
Expand All @@ -517,15 +519,28 @@ class DataChecker {
)
? syncUserStepData.currEnd
: 0
const syncedAt = syncUserStepData.hasSyncedAt
? syncUserStepData.syncedAt
: 0

const momentBaseEnd = moment.utc(baseEnd)
const momentCurrEnd = moment.utc(currEnd)
const momentCurrMts = moment.utc(currMts)
const momentSyncedAt = moment.utc(syncedAt)

const momentMaxEnd = moment.max(momentBaseEnd, momentCurrEnd)
const momentDiff = momentCurrMts.diff(momentMaxEnd, measure)

return momentDiff > allowedTimeDiff
const yearsDiff = momentCurrMts.year() - momentSyncedAt.year()
const dayOfYearDiff = momentCurrMts.dayOfYear() - momentSyncedAt.dayOfYear()

return (
momentDiff > allowedTimeDiff &&
(
!Number.isFinite(dayOfYear) ||
yearsDiff >= 1 ||
dayOfYearDiff >= dayOfYear
)
)
}

_wasStartPointChanged (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ class SyncUserStepManager {
currStart,
currEnd,
isBaseStepReady = false,
isCurrStepReady = false
isCurrStepReady = false,
syncedAt
} = syncUserStepInfo ?? {}

const baseStart = this._getMinStart(_baseStart)
Expand All @@ -348,7 +349,8 @@ class SyncUserStepManager {
baseEnd: baseEnd ?? currMts,
isBaseStepReady,
symbol,
timeframe
timeframe,
syncedAt
})

return {
Expand All @@ -365,7 +367,8 @@ class SyncUserStepManager {
isBaseStepReady,
isCurrStepReady,
symbol,
timeframe
timeframe,
syncedAt
})

if (!isCurrStepReady) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { decorateInjectable } = require('../../../di/utils')
*/
class SyncUserStepData {
constructor () {
this.syncedAt = null
this.symbol = null
this.timeframe = null
this.baseStart = null
Expand All @@ -23,6 +24,7 @@ class SyncUserStepData {
}

/**
* @param {?number} [syncedAt] - Used to specify synced mts point
* @param {?string} [symbol] - Used to specify synced symbol, can be `_ALL` if all ones are syncing
* @param {?string} [timeframe] - Used to specify synced timeframe, eg. for candles collection
* @param {?number} [baseStart] - Used to specify base start mts point to continue first sync
Expand All @@ -34,6 +36,7 @@ class SyncUserStepData {
*/
setParams (params = {}) {
const {
syncedAt = this.syncedAt,
symbol = this.symbol,
timeframe = this.timeframe,
baseStart = this.baseStart,
Expand All @@ -44,6 +47,7 @@ class SyncUserStepData {
isCurrStepReady = this.isCurrStepReady
} = params ?? {}

this.syncedAt = syncedAt
this.symbol = symbol
this.timeframe = timeframe
this.baseStart = baseStart
Expand Down Expand Up @@ -117,6 +121,10 @@ class SyncUserStepData {
get hasCurrStart () {
return Number.isInteger(this.currStart)
}

get hasSyncedAt () {
return Number.isInteger(this.syncedAt)
}
}

decorateInjectable(SyncUserStepData)
Expand Down