Skip to content

Commit

Permalink
Merge pull request #73 from bitfinexcom/beta
Browse files Browse the repository at this point in the history
Update Master with Beta
  • Loading branch information
ezewer authored Jan 28, 2021
2 parents 6abbd31 + 7584907 commit c1b2607
Show file tree
Hide file tree
Showing 11 changed files with 409 additions and 20 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"async": "^2.6.1",
"bfx-svc-test-helper": "git+https://github.com/bitfinexcom/bfx-svc-test-helper.git",
"bittorrent-dht": "^8.4.0",
"cron-validate": "^1.4.2",
"ed25519-supercop": "^2.0.1",
"electron-alert": "^0.1.11",
"electron-serve": "^1.0.0",
"find-free-port": "^2.0.0",
"grenache-grape": "^0.9.8",
Expand Down
41 changes: 26 additions & 15 deletions scripts/build-ui.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

set -x

export CI_ENVIRONMENT_NAME=production

ROOT="$PWD"
frontendFolder="$ROOT/bfx-report-ui"
pathToTriggerElectronLoad="$frontendFolder/src/utils/triggerElectronLoad.js"
Expand Down Expand Up @@ -58,34 +60,43 @@ if ! [ -s "$frontendFolder/package.json" ]; then
exit 1
fi

cp -f "$frontendFolder/.env.example" "$frontendFolder/.env"
sed -i -e \
"s/REACT_APP_ELECTRON=.*/REACT_APP_ELECTRON=true/g" \
$frontendFolder/.env

npm i --no-audit

sed -i -e \
"s/API_URL: .*,/API_URL: \'http:\/\/localhost:34343\/api\',/g" \
$frontendFolder/src/var/config.js
$frontendFolder/src/config.js
sed -i -e \
"s/WS_ADDRESS: .*,/WS_ADDRESS: \'ws:\/\/localhost:34343\/ws\',/g" \
$frontendFolder/src/var/config.js
$frontendFolder/src/config.js

if [ $isDevEnv != 0 ]; then
export CI_ENVIRONMENT_NAME=development

sed -i -e \
"s/KEY_URL: .*,/KEY_URL: \'https:\/\/test.bitfinex.com\/api\',/g" \
$frontendFolder/src/var/config.js
"s/KEY_URL: .*,/KEY_URL: \'https:\/\/api.staging.bitfinex.com\/api\',/g" \
$frontendFolder/src/config.js
fi

sed -i -e \
"s/showAuthPage: .*,/showAuthPage: true,/g" \
$frontendFolder/src/var/config.js
"s/localExport: false/localExport: true/g" \
$frontendFolder/src/config.js
sed -i -e \
"s/showAuthPage: false/showAuthPage: true/g" \
$frontendFolder/src/config.js
sed -i -e \
"s/showFrameworkMode: .*,/showFrameworkMode: true,/g" \
$frontendFolder/src/var/config.js
"s/isElectronApp: false/isElectronApp: true/g" \
$frontendFolder/src/config.js
sed -i -e \
"s/showFrameworkMode: false/showFrameworkMode: true/g" \
$frontendFolder/src/config.js

rm -f "$ROOT/.eslintrc"

npm i --no-audit
npm run build

if ! [ -s "$frontendFolder/build/index.html" ]; then
exit 1
fi

mv -f $frontendFolder/build/* $uiBuildFolder
cp $pathToTriggerElectronLoad $uiBuildFolder/triggerElectronLoad.js
touch $uiReadyFile
4 changes: 3 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ let isMigrationsError = false
try {
const pathToUserData = process.env.PATH_TO_USER_DATA
const pathToUserCsv = process.env.PATH_TO_USER_CSV
const schedulerRule = process.env.SCHEDULER_RULE
const secretKey = process.env.SECRET_KEY

if (!secretKey) {
Expand Down Expand Up @@ -120,7 +121,8 @@ let isMigrationsError = false
`--logsFolder=${pathToUserData}/logs`,
`--dbFolder=${pathToUserData}`,
`--grape=${grape}`,
`--secretKey=${secretKey}`
`--secretKey=${secretKey}`,
`--schedulerRule=${schedulerRule || ''}`
], {
env,
cwd: process.cwd(),
Expand Down
245 changes: 245 additions & 0 deletions src/change-sync-frequency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
'use strict'

const electron = require('electron')
const Alert = require('electron-alert')
const cronValidate = require('cron-validate')
const path = require('path')
const fs = require('fs')

const modalDialogStyle = fs.readFileSync(path.join(
__dirname, 'modal-dialog-src/modal-dialog.css'
))
const modalDialogScript = fs.readFileSync(path.join(
__dirname, 'modal-dialog-src/modal-dialog.js'
))

const {
SyncFrequencyChangingError
} = require('./errors')
const showErrorModalDialog = require('./show-error-modal-dialog')
const pauseApp = require('./pause-app')
const relaunch = require('./relaunch')
const { getConfigsKeeperByName } = require('./configs-keeper')

const _getSchedulerRule = (timeFormat, alertRes) => {
if (timeFormat.value === 'days') {
return `0 0 */${alertRes.value} * *`
}
if (timeFormat.value === 'hours') {
return `0 */${alertRes.value} * * *`
}

return `*/${alertRes.value} * * * *`
}

const _testTime = (time) => {
return (
time &&
typeof time === 'string' &&
/^\*\/\d{1,2}$/i.test(time)
)
}

const _getTime = (timeFormat, time) => {
return {
timeFormat,
value: time.replace('*/', '')
}
}

const _getTimeDataFromRule = (rule) => {
const cronResult = cronValidate(rule)

if (!cronResult.isValid()) {
return { timeFormat: 'hours', value: 2 }
}

const value = cronResult.getValue()

if (_testTime(value.daysOfMonth)) {
return _getTime('days', value.daysOfMonth)
}
if (_testTime(value.hours)) {
return _getTime('hours', value.hours)
}
if (_testTime(value.minutes)) {
return _getTime('mins', value.minutes)
}

return { timeFormat: 'hours', value: 2 }
}

const style = `<style>${modalDialogStyle}</style>`
const script = `<script type="text/javascript">${modalDialogScript}</script>`

module.exports = () => {
const configsKeeper = getConfigsKeeperByName('main')
const timeFormatAlert = new Alert([style])
const alert = new Alert([style, script])

const closeTimeFormatAlert = () => {
if (!timeFormatAlert.browserWindow) return

timeFormatAlert.browserWindow.close()
}
const closeAlert = () => {
if (!alert.browserWindow) return

alert.browserWindow.close()
}

const timeFormatAlertOptions = {
title: 'Set time format',
type: 'question',
background: '#172d3e',
customClass: {
title: 'titleColor',
content: 'textColor',
input: 'textColor radioInput'
},
focusConfirm: true,
showCancelButton: true,
progressSteps: [1, 2],
currentProgressStep: 0,
input: 'radio',
inputValue: 'hours',
inputOptions: {
mins: 'Mins',
hours: 'Hours',
days: 'Days'
},
onBeforeOpen: () => {
if (!timeFormatAlert.browserWindow) return

timeFormatAlert.browserWindow.once('blur', closeTimeFormatAlert)
}
}
const alertOptions = {
title: 'Set sync frequency',
type: 'question',
background: '#172d3e',
customClass: {
title: 'titleColor',
content: 'textColor',
input: 'textColor rangeInput'
},
focusConfirm: true,
showCancelButton: true,
progressSteps: [1, 2],
currentProgressStep: 1,
input: 'range',
onBeforeOpen: () => {
if (!alert.browserWindow) return

alert.browserWindow.once('blur', closeAlert)
}
}
const sound = { freq: 'F2', type: 'triange', duration: 1.5 }

const getAlertOpts = (timeFormat, timeData) => {
const { inputOptions } = timeFormatAlertOptions
const text = inputOptions[timeFormat.value]

if (timeFormat.value === 'days') {
return {
...alertOptions,
text,
inputValue: timeFormat.value === timeData.timeFormat
? timeData.value : 1,
inputAttributes: {
min: 1,
max: 31,
step: 1
}
}
}
if (timeFormat.value === 'hours') {
return {
...alertOptions,
text,
inputValue: timeFormat.value === timeData.timeFormat
? timeData.value : 2,
inputAttributes: {
min: 1,
max: 23,
step: 1
}
}
}

return {
...alertOptions,
text,
inputValue: timeFormat.value === timeData.timeFormat
? timeData.value : 20,
inputAttributes: {
min: 10,
max: 59,
step: 1
}
}
}

return async () => {
const win = electron.BrowserWindow.getFocusedWindow()
win.once('closed', closeTimeFormatAlert)
win.once('closed', closeAlert)

try {
const savedSchedulerRule = await configsKeeper
.getConfigByName('schedulerRule')
const timeData = _getTimeDataFromRule(savedSchedulerRule)

const timeFormat = await timeFormatAlert.fireFrameless(
{
...timeFormatAlertOptions,
inputValue: timeData.timeFormat
},
null, true, false, sound
)
win.removeListener('closed', closeTimeFormatAlert)

if (timeFormat.dismiss) {
return
}

const alertRes = await alert.fireFrameless(
getAlertOpts(timeFormat, timeData),
null, true, false, sound
)
win.removeListener('closed', closeAlert)

if (alertRes.dismiss) {
return
}

const schedulerRule = _getSchedulerRule(
timeFormat,
alertRes
)

if (savedSchedulerRule === schedulerRule) {
return
}

await pauseApp()
const isSaved = await configsKeeper
.saveConfigs({ schedulerRule })

if (!isSaved) {
throw new SyncFrequencyChangingError()
}

relaunch()
} catch (err) {
try {
await showErrorModalDialog(win, 'Change sync frequency', err)
} catch (err) {
console.error(err)
}

console.error(err)
relaunch()
}
}
}
6 changes: 6 additions & 0 deletions src/create-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const exportDB = require('./export-db')
const importDB = require('./import-db')
const removeDB = require('./remove-db')
const changeReportsFolder = require('./change-reports-folder')
const changeSyncFrequency = require('./change-sync-frequency')
const triggerElectronLoad = require('./trigger-electron-load')
const showAboutModalDialog = require('./show-about-modal-dialog')

Expand Down Expand Up @@ -81,6 +82,11 @@ module.exports = ({
label: 'Change reports folder',
accelerator: 'CmdOrCtrl+F',
click: changeReportsFolder({ pathToUserDocuments })
},
{
label: 'Change sync frequency',
accelerator: 'CmdOrCtrl+S',
click: changeSyncFrequency()
}
]
},
Expand Down
9 changes: 8 additions & 1 deletion src/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ class ReportsFolderChangingError extends BaseError {
}
}

class SyncFrequencyChangingError extends BaseError {
constructor (message = 'ERR_SYNC_FREQUENCY_HAS_NOT_CHANGED') {
super(message)
}
}

module.exports = {
BaseError,
InvalidFilePathError,
Expand All @@ -103,5 +109,6 @@ module.exports = {
WrongPathToUserDataError,
WrongPathToUserCsvError,
WrongSecretKeyError,
ReportsFolderChangingError
ReportsFolderChangingError,
SyncFrequencyChangingError
}
Loading

0 comments on commit c1b2607

Please sign in to comment.