Merge remote-tracking branch 'DataFiles/develop' into develop
This commit is contained in:
commit
05e6ab974f
12 files changed
+2091
No files matched your search
@@ -0,0 +1,2 @@
|
||||
# Auto detect text files and perform LF normalization
|
||||
* text=auto
|
||||
@@ -0,0 +1,46 @@
|
||||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to creating a positive environment include:
|
||||
|
||||
* Using welcoming and inclusive language
|
||||
* Being respectful of differing viewpoints and experiences
|
||||
* Gracefully accepting constructive criticism
|
||||
* Focusing on what is best for the community
|
||||
* Showing empathy towards other community members
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
* The use of sexualized language or imagery and unwelcome sexual attention or advances
|
||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or electronic address, without explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a professional setting
|
||||
|
||||
## Our Responsibilities
|
||||
|
||||
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
|
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team using the online form in the contacts page of the superalgos.org website. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
|
||||
|
||||
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
|
||||
|
||||
[homepage]: http://contributor-covenant.org
|
||||
[version]: http://contributor-covenant.org/version/1/4/
|
||||
@@ -0,0 +1,9 @@
|
||||
# Contributing
|
||||
|
||||
We are thrilled you are interested in contributing to this repository. Your help may be essential to the project's success and we want you in!
|
||||
|
||||
Superalgos is a collective project, currently at a very early stage. We not only welcome contributions but may even choose to incentivize relevant contributions via the project's native crypto token.
|
||||
|
||||
Please read the [Join the Dev Team]( https://superalgos.org/developers-join-dev-team.shtml) page in our website to learn more about how you may become a relevant member of the Community and even make a living out of contributing code to the project.
|
||||
|
||||
Contributions are deemed _voluntary work_, irrespective of the incentives that may be in place, and are subject to the conditions described in the LICENSE
|
||||
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -0,0 +1,19 @@
|
||||
# Data Files
|
||||
|
||||
This repo is one of the repositories built into Superalgos. For more information [read the docs](https://docs.superalgos.org/) or check the [Readme](https://github.com/Superalgos/Superalgos/) at the main repository of the project.
|
||||
|
||||
## Copyright and License Notice
|
||||
|
||||
© Copyright 2020 Superalgos. Please see the [License](/LICENSE) for information on how you can use this software.
|
||||
|
||||
## Disclaimer
|
||||
|
||||
THE SOFTWARE AND ITS ASSOCIATED PRODUCTS AND SERVICES ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, SUITABILITY FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
|
||||
|
||||
IN NO EVENT WILL SUPERALGOS BE LIABLE TO ANY PARTY FOR ANY DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY USE OF THE SOFTWARE, OR ANY OTHER ASSOCIATED SERVICE OR WEB SITE, INCLUDING, WITHOUT LIMITATION, ANY LOST PROFITS, BUSINESS INTERRUPTION, LOSS OF FUNDS, PROGRAMS OR OTHER DATA OR OTHERWISE, EVEN IF WE ARE EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
# Maintainer
|
||||
|
||||
[](https://github.com/Luis-Fernando-Molina) |
|
||||
---|
|
||||
[Luis-Fernando-Molina](https://github.com/Luis-Fernando-Molina) |
|
||||
@@ -0,0 +1,298 @@
|
||||
|
||||
function newDailyFiles() {
|
||||
const MODULE_NAME = 'Daily Files'
|
||||
const ERROR_LOG = true
|
||||
const logger = newWebDebugLog()
|
||||
logger.fileName = MODULE_NAME
|
||||
|
||||
let thisObject = {
|
||||
eventHandler: undefined,
|
||||
getFileCursor: getFileCursor,
|
||||
setDatetime: setDatetime,
|
||||
setTimeFrame: setTimeFrame,
|
||||
getExpectedFiles: getExpectedFiles,
|
||||
getFilesLoaded: getFilesLoaded,
|
||||
initialize: initialize,
|
||||
finalize: finalize
|
||||
}
|
||||
|
||||
let filesLoaded = 0
|
||||
let expectedFiles = 0
|
||||
let fileCloud
|
||||
let fileCursors = new Map()
|
||||
let callBackWhenFileReceived
|
||||
|
||||
thisObject.eventHandler = newEventHandler()
|
||||
|
||||
return thisObject
|
||||
|
||||
function finalize() {
|
||||
try {
|
||||
thisObject.eventHandler.finalize()
|
||||
thisObject.eventHandler = undefined
|
||||
|
||||
filesLoaded = undefined
|
||||
expectedFiles = undefined
|
||||
fileCloud = undefined
|
||||
|
||||
fileCursors.forEach(finalizeEach)
|
||||
|
||||
function finalizeEach(item, key, mapObj) {
|
||||
item.finalize()
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] finalize -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
|
||||
function initialize(pMine, pBot, pSession, pProduct, pDataset, pExchange, pMarket, pDatetime, pTimeFrame, pHost, pPort, pEventsServerClient, callBackFunction) {
|
||||
try {
|
||||
callBackWhenFileReceived = callBackFunction
|
||||
|
||||
let exchange = pExchange
|
||||
let beginDateRange
|
||||
let endDateRange
|
||||
let timeFrames
|
||||
|
||||
fileCloud = newFileCloud()
|
||||
fileCloud.initialize(pBot, pHost, pPort)
|
||||
|
||||
/* Config Validations */
|
||||
if (pDataset.config.validTimeFrames === undefined) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> err = Can not initialize Daily Files for bot ' + pBot.name) }
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> err = You need to define validTimeFrames at the Dataset config. ') }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
return
|
||||
}
|
||||
|
||||
getDataRangeFile()
|
||||
|
||||
function getDataRangeFile() {
|
||||
/* First we will get the Data Range */
|
||||
fileCloud.getFile(pMine, pBot, pSession, pProduct, pDataset, exchange, pMarket, undefined, undefined, undefined, true, undefined, onDataRangeReceived)
|
||||
|
||||
function onDataRangeReceived(err, pFile) {
|
||||
switch (err.result) {
|
||||
case GLOBAL.DEFAULT_OK_RESPONSE.result: {
|
||||
beginDateRange = pFile.begin
|
||||
endDateRange = pFile.end
|
||||
break
|
||||
}
|
||||
case GLOBAL.DEFAULT_FAIL_RESPONSE.result: {
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
return
|
||||
}
|
||||
case GLOBAL.CUSTOM_FAIL_RESPONSE.result: {
|
||||
if (err.message === 'File does not exist.') {
|
||||
beginDateRange = new Date()
|
||||
endDateRange = new Date()
|
||||
break
|
||||
}
|
||||
if (err.message === 'Missing Configuration.') {
|
||||
if (ERROR_LOG === true) { logger.write('[WARN] initialize -> onFileReceived -> The needed configuration for the dateRange at the dataSet of the product was not found.') }
|
||||
if (ERROR_LOG === true) { logger.write('[WARN] initialize -> onFileReceived -> maxDate will be set to current datetime.') }
|
||||
|
||||
endDateRange = new Date()
|
||||
break
|
||||
}
|
||||
callBackFunction(err)
|
||||
return
|
||||
}
|
||||
default: {
|
||||
callBackFunction(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
getTimeFramesFile()
|
||||
}
|
||||
}
|
||||
|
||||
function getTimeFramesFile() {
|
||||
/* First we will get the Data Range */
|
||||
fileCloud.getFile(pMine, pBot, pSession, pProduct, pDataset, exchange, pMarket, undefined, undefined, undefined, undefined, true, onTimeFramesReceived)
|
||||
|
||||
function onTimeFramesReceived(err, pFile) {
|
||||
switch (err.result) {
|
||||
case GLOBAL.DEFAULT_OK_RESPONSE.result: {
|
||||
timeFrames = pFile
|
||||
break
|
||||
}
|
||||
case GLOBAL.DEFAULT_FAIL_RESPONSE.result: {
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
return
|
||||
}
|
||||
case GLOBAL.CUSTOM_FAIL_RESPONSE.result: {
|
||||
if (err.message === 'File does not exist.') {
|
||||
/* It is ok, we will deal with this later */
|
||||
break
|
||||
}
|
||||
if (err.message === 'Configured to not Support This.') {
|
||||
/* It is ok, we will deal with this later */
|
||||
break
|
||||
}
|
||||
if (err.message === 'Missing Configuration.') {
|
||||
if (ERROR_LOG === true) { logger.write('[WARN] initialize -> onFileReceived -> The needed configuration to locate the timeFrames is missing at the dataSet.') }
|
||||
if (ERROR_LOG === true) { logger.write('[WARN] initialize -> onFileReceived -> Time Frames Filters will be ignored -> Product = ' + pProduct.config.codeName) }
|
||||
break
|
||||
}
|
||||
callBackFunction(err)
|
||||
return
|
||||
}
|
||||
default: {
|
||||
callBackFunction(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
createFileCursors()
|
||||
}
|
||||
}
|
||||
|
||||
function createFileCursors() {
|
||||
try {
|
||||
/* Now we will get the daily files */
|
||||
for (let i = 0; i < dailyFilePeriods.length; i++) {
|
||||
let periodTime = dailyFilePeriods[i][0]
|
||||
let periodName = dailyFilePeriods[i][1]
|
||||
|
||||
if (pDataset.config.validTimeFrames.includes(periodName) === false) {
|
||||
continue
|
||||
}
|
||||
if (timeFrames !== undefined) {
|
||||
if (timeFrames.includes(periodName) === false) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
let fileCursor = newFileCursor()
|
||||
fileCursor.eventHandler = thisObject.eventHandler // We share our event handler with each file cursor, so that they can raise events there when files are changed.s
|
||||
fileCursor.initialize(fileCloud, pMine, pBot, pSession, pProduct, pDataset, exchange, pMarket, periodName, periodTime, pDatetime, pTimeFrame, beginDateRange, endDateRange, pEventsServerClient, onInitialized)
|
||||
function onInitialized(err) {
|
||||
try {
|
||||
switch (err.result) {
|
||||
case GLOBAL.DEFAULT_OK_RESPONSE.result: {
|
||||
break
|
||||
}
|
||||
case GLOBAL.DEFAULT_FAIL_RESPONSE.result: {
|
||||
callBackWhenFileReceived(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
return
|
||||
}
|
||||
default: {
|
||||
callBackWhenFileReceived(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
fileCursors.set(periodTime, fileCursor)
|
||||
expectedFiles = expectedFiles + fileCursor.getExpectedFiles()
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> onFileReceived -> onInitialized -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
}
|
||||
loadThemAll()
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> createFileCursors -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
|
||||
function loadThemAll() {
|
||||
try {
|
||||
for (let i = 0; i < dailyFilePeriods.length; i++) {
|
||||
let periodTime = dailyFilePeriods[i][0]
|
||||
let periodName = dailyFilePeriods[i][1]
|
||||
|
||||
if (pDataset.config.validTimeFrames.includes(periodName) === true) {
|
||||
let fileCursor = fileCursors.get(periodTime)
|
||||
if (fileCursor !== undefined) {
|
||||
fileCursor.reload(onFileReceived)
|
||||
}
|
||||
}
|
||||
}
|
||||
callBackWhenFileReceived(GLOBAL.DEFAULT_OK_RESPONSE, thisObject)
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> loadThemAll -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
|
||||
function onFileReceived(err) {
|
||||
try {
|
||||
switch (err.result) {
|
||||
case GLOBAL.DEFAULT_OK_RESPONSE.result: {
|
||||
break
|
||||
}
|
||||
case GLOBAL.DEFAULT_FAIL_RESPONSE.result: {
|
||||
callBackWhenFileReceived(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
return
|
||||
}
|
||||
case GLOBAL.CUSTOM_FAIL_RESPONSE.result: {
|
||||
callBackWhenFileReceived(err)
|
||||
return
|
||||
}
|
||||
default: {
|
||||
callBackWhenFileReceived(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
filesLoaded++
|
||||
callBackWhenFileReceived(GLOBAL.DEFAULT_OK_RESPONSE, thisObject) // Note that the call back is called for every file loaded at each cursor.
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] onFileReceived -> err = ' + err.stack) }
|
||||
callBackWhenFileReceived(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
|
||||
function getFileCursor(pPeriod) {
|
||||
try {
|
||||
return fileCursors.get(pPeriod)
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFileCursor -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
|
||||
function setDatetime(pDatetime) {
|
||||
try {
|
||||
filesLoaded = 0
|
||||
expectedFiles = 0
|
||||
fileCursors.forEach(setDatetimeToEach)
|
||||
|
||||
function setDatetimeToEach(fileCursor, key, map) {
|
||||
fileCursor.setDatetime(pDatetime)
|
||||
expectedFiles = expectedFiles + fileCursor.getExpectedFiles()
|
||||
fileCursor.reload(onFileReceived)
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] setDatetime -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
|
||||
function setTimeFrame(pTimeFrame, pDatetime) {
|
||||
try {
|
||||
filesLoaded = 0
|
||||
expectedFiles = 0
|
||||
fileCursors.forEach(setTimeFrameToEach)
|
||||
|
||||
function setTimeFrameToEach(fileCursor, key, map) {
|
||||
fileCursor.setTimeFrame(pTimeFrame, pDatetime)
|
||||
expectedFiles = expectedFiles + fileCursor.getExpectedFiles()
|
||||
fileCursor.reload(onFileReceived)
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] setTimeFrame -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
|
||||
function getExpectedFiles() {
|
||||
return expectedFiles
|
||||
}
|
||||
|
||||
function getFilesLoaded() {
|
||||
return filesLoaded
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
|
||||
function newFileCloud () {
|
||||
const MODULE_NAME = 'File Cloud'
|
||||
const ERROR_LOG = true
|
||||
const logger = newWebDebugLog()
|
||||
logger.fileName = MODULE_NAME
|
||||
|
||||
/*
|
||||
This is the module in the system that actually connects to the cloud storage and grabs from there the needed files.
|
||||
*/
|
||||
|
||||
let thisObject = {
|
||||
getFile: getFile,
|
||||
initialize: initialize
|
||||
}
|
||||
|
||||
let fileStorage
|
||||
return thisObject
|
||||
|
||||
function initialize (pBot, pHost, pPort) {
|
||||
fileStorage = newFileStorage(pHost, pPort)
|
||||
}
|
||||
|
||||
function getFile (pMine, pBot, pSession, pProduct, pDataset, pExchange, pMarket, pPeriodName, pDatetime, pSequence, pDataRange, pTimeFrames, callBackFunction) {
|
||||
try {
|
||||
const MAX_RETRIES = 3
|
||||
getFileRecursively(0, pMine, pBot, pSession, pProduct, pDataset, pExchange, pMarket, pPeriodName, pDatetime, pSequence, pDataRange, pTimeFrames, callBackFunction)
|
||||
|
||||
function getFileRecursively (pRetryCounter, pMine, pBot, pSession, pProduct, pDataset, pExchange, pMarket, pPeriodName, pDatetime, pSequence, pDataRange, pTimeFrames, callBackFunction) {
|
||||
try {
|
||||
let fileName
|
||||
let filePath
|
||||
|
||||
if (pDataRange === undefined && pTimeFrames === undefined) {
|
||||
fileName = pDataset.config.fileName
|
||||
filePath = pDataset.config.filePath
|
||||
} else {
|
||||
if (pDataRange !== undefined) {
|
||||
if (pDataset.config.dataRange !== undefined) {
|
||||
fileName = pDataset.config.dataRange.fileName
|
||||
filePath = pDataset.config.dataRange.filePath
|
||||
} else {
|
||||
let customErr = {
|
||||
result: GLOBAL.CUSTOM_FAIL_RESPONSE.result,
|
||||
message: 'Missing Configuration.'
|
||||
}
|
||||
if (ERROR_LOG === true) { logger.write('[WARN] getFile -> getFileRecursively -> onFileReceived -> customErr.message = ' + customErr.message) }
|
||||
if (ERROR_LOG === true) { logger.write('[WARN] getFile -> getFileRecursively -> onFileReceived -> Data Range configuration could not be found at Dataset ' + pDataset.name) }
|
||||
callBackFunction(customErr)
|
||||
return
|
||||
}
|
||||
}
|
||||
if (pTimeFrames !== undefined) {
|
||||
if (pDataset.config.timeFrames !== undefined) {
|
||||
if (pDataset.config.timeFrames.fileName !== '') {
|
||||
fileName = pDataset.config.timeFrames.fileName
|
||||
filePath = pDataset.config.timeFrames.filePath
|
||||
} else {
|
||||
let customErr = {
|
||||
result: GLOBAL.CUSTOM_FAIL_RESPONSE.result,
|
||||
message: 'Configured to not Support This.'
|
||||
}
|
||||
callBackFunction(customErr)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
let customErr = {
|
||||
result: GLOBAL.CUSTOM_FAIL_RESPONSE.result,
|
||||
message: 'Missing Configuration.'
|
||||
}
|
||||
if (ERROR_LOG === true) { logger.write('[WARN] getFile -> getFileRecursively -> onFileReceived -> customErr.message = ' + customErr.message) }
|
||||
if (ERROR_LOG === true) { logger.write('[WARN] getFile -> getFileRecursively -> onFileReceived -> Time Frames configuration could not be found at Dataset ' + pDataset.name) }
|
||||
callBackFunction(customErr)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (fileName === undefined) {
|
||||
logger.write('[ERROR] getFile -> getFileRecursively -> Inconsistant data. Check the following: ')
|
||||
logger.write('[ERROR] getFile -> getFileRecursively -> pMine = ' + JSON.stringify(pMine))
|
||||
logger.write('[ERROR] getFile -> getFileRecursively -> pBot = ' + JSON.stringify(pBot))
|
||||
logger.write('[ERROR] getFile -> getFileRecursively -> pDataset = ' + JSON.stringify(pDataset))
|
||||
logger.write('[ERROR] getFile -> getFileRecursively -> pExchange = ' + JSON.stringify(pExchange))
|
||||
logger.write('[ERROR] getFile -> getFileRecursively -> pMarket = ' + JSON.stringify(pMarket))
|
||||
logger.write('[ERROR] getFile -> getFileRecursively -> pPeriodName = ' + JSON.stringify(pPeriodName))
|
||||
|
||||
throw ('Inconsistant data received.')
|
||||
}
|
||||
|
||||
if (pMarket !== undefined) {
|
||||
filePath = filePath.replace('@BaseAsset', pMarket.baseAsset)
|
||||
filePath = filePath.replace('@QuotedAsset', pMarket.quotedAsset)
|
||||
}
|
||||
|
||||
if (pMine !== undefined) {
|
||||
filePath = filePath.replace('@DataMine', pMine.config.codeName)
|
||||
}
|
||||
|
||||
if (pBot !== undefined) {
|
||||
filePath = filePath.replace('@Bot', pBot.config.codeName)
|
||||
}
|
||||
|
||||
if (pSession !== undefined) {
|
||||
let config
|
||||
let sessionFolderName = pSession.type.replace(' ', '-').replace(' ', '-') + '-' + pSession.id
|
||||
if (pSession.config !== undefined) {
|
||||
if (pSession.config.folderName !== undefined) {
|
||||
sessionFolderName = pSession.type.replace(' ', '-').replace(' ', '-') + '-' + pSession.config.folderName
|
||||
}
|
||||
}
|
||||
filePath = filePath.replace('@Session', sessionFolderName)
|
||||
}
|
||||
|
||||
if (pExchange !== undefined) {
|
||||
filePath = filePath.replace('@Exchange', pExchange.name)
|
||||
}
|
||||
|
||||
filePath = filePath.replace('@Period', pPeriodName)
|
||||
filePath = filePath.replace('@Dataset', pDataset.config.codeName)
|
||||
filePath = filePath.replace('@Product', pProduct.config.codeName)
|
||||
|
||||
if (pDatetime !== undefined) {
|
||||
filePath = filePath.replace('@Year', pDatetime.getUTCFullYear())
|
||||
filePath = filePath.replace('@Month', pad(pDatetime.getUTCMonth() + 1, 2))
|
||||
filePath = filePath.replace('@Day', pad(pDatetime.getUTCDate(), 2))
|
||||
filePath = filePath.replace('@Hour', pad(pDatetime.getUTCHours(), 2))
|
||||
filePath = filePath.replace('@Minute', pad(pDatetime.getUTCMinutes(), 2))
|
||||
}
|
||||
|
||||
if (pSequence !== undefined) {
|
||||
fileName = fileName.replace('@Sequence', pSequence)
|
||||
}
|
||||
|
||||
fileStorage.getFileFromHost(filePath + '/' + fileName, onFileReceived)
|
||||
|
||||
function onFileReceived (err, text, response) {
|
||||
try {
|
||||
let data
|
||||
|
||||
if (err && err.result !== GLOBAL.DEFAULT_OK_RESPONSE.result) {
|
||||
if (err.code === 'BlobNotFound' || err.code === 'FileNotFound' || err.code === 'ParentNotFound' || err.code === 'The specified key does not exist.') {
|
||||
let customErr = {
|
||||
result: GLOBAL.CUSTOM_FAIL_RESPONSE.result,
|
||||
message: 'File does not exist.'
|
||||
}
|
||||
callBackFunction(customErr)
|
||||
return
|
||||
}
|
||||
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFile -> getFileRecursively -> onFileReceived -> filePath = ' + filePath) }
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFile -> getFileRecursively -> onFileReceived -> fileName = ' + fileName) }
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFile -> getFileRecursively -> onFileReceived -> Unexpected Error Ocurred.') }
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFile -> getFileRecursively -> onFileReceived -> err = ' + err.stack) }
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFile -> getFileRecursively -> onFileReceived -> text = ' + text) }
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFile -> getFileRecursively -> onFileReceived -> response = ' + response) }
|
||||
|
||||
if (err.message === 'XHR error') {
|
||||
if (pRetryCounter < MAX_RETRIES) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFile -> getFileRecursively -> onFileReceived -> Retrying to get this file. ') }
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFile -> getFileRecursively -> onFileReceived -> MAX_RETRIES = ' + MAX_RETRIES) }
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFile -> getFileRecursively -> onFileReceived -> pRetryCounter = ' + pRetryCounter) }
|
||||
|
||||
getFileRecursively(pRetryCounter + 1, pMine, pBot, pDataset, pExchange, pMarket, pPeriodName, pDatetime, pSequence, pDataRange, callBackFunction)
|
||||
return
|
||||
} else {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFile -> getFileRecursively -> onFileReceived -> Could not get this file from storage. ') }
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFile -> getFileRecursively -> onFileReceived -> MAX_RETRIES = ' + MAX_RETRIES) }
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFile -> getFileRecursively -> onFileReceived -> pRetryCounter = ' + pRetryCounter) }
|
||||
}
|
||||
}
|
||||
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
return
|
||||
} else {
|
||||
try {
|
||||
data = JSON.parse(text)
|
||||
callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE, data)
|
||||
return
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[WARN] getFile -> getFileRecursively -> onFileReceived -> err = ' + err.stack) }
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFile -> getFileRecursively -> onFileReceived -> filePath = ' + filePath) }
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFile -> getFileRecursively -> onFileReceived -> fileName = ' + fileName) }
|
||||
|
||||
let customErr = {
|
||||
result: GLOBAL.CUSTOM_OK_RESPONSE.result,
|
||||
message: 'Data not in JSON Format.'
|
||||
}
|
||||
|
||||
if (ERROR_LOG === true) { logger.write('[WARN] getFile -> getFileRecursively -> onFileReceived -> customErr.message = ' + customErr.message) }
|
||||
|
||||
callBackFunction(customErr, text)
|
||||
return
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFile -> getFileRecursively -> onFileReceived -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFile -> getFileRecursively -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFile -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,579 @@
|
||||
|
||||
function newFileCursor() {
|
||||
const MODULE_NAME = 'File Cursor'
|
||||
const INFO_LOG = false
|
||||
const ERROR_LOG = true
|
||||
const logger = newWebDebugLog()
|
||||
logger.fileName = MODULE_NAME
|
||||
|
||||
let cursorDate
|
||||
|
||||
let thisObject = {
|
||||
eventHandler: undefined, // received from parent
|
||||
reload: reload,
|
||||
setDatetime: setDatetime,
|
||||
setTimeFrame: setTimeFrame,
|
||||
files: new Map(),
|
||||
getExpectedFiles: getExpectedFiles,
|
||||
initialize: initialize,
|
||||
finalize: finalize
|
||||
}
|
||||
|
||||
let minCursorSize = 10
|
||||
let maxCursorSize = 30
|
||||
|
||||
let market
|
||||
let exchange
|
||||
let fileCloud
|
||||
let mine
|
||||
let bot
|
||||
let session
|
||||
let product
|
||||
let dataset
|
||||
let periodName
|
||||
let timeFrame
|
||||
let beginDateRange
|
||||
let endDateRange
|
||||
let eventsServerClient
|
||||
|
||||
let finalized = false
|
||||
|
||||
let eventSubscriptionIdDatasetUpdated
|
||||
let eventSubscriptionIdDataRangeUpdated
|
||||
|
||||
return thisObject
|
||||
|
||||
function finalize() {
|
||||
try {
|
||||
eventsServerClient.stopListening('Dataset Updated', eventSubscriptionIdDatasetUpdated)
|
||||
eventsServerClient.stopListening('Data Range Updated', eventSubscriptionIdDataRangeUpdated)
|
||||
|
||||
thisObject.eventHandler = undefined
|
||||
|
||||
market = undefined
|
||||
exchange = undefined
|
||||
fileCloud = undefined
|
||||
mine = undefined
|
||||
bot = undefined
|
||||
session = undefined
|
||||
product = undefined
|
||||
dataset = undefined
|
||||
periodName = undefined
|
||||
timeFrame = undefined
|
||||
beginDateRange = undefined
|
||||
endDateRange = undefined
|
||||
eventsServerClient = undefined
|
||||
|
||||
thisObject.files = undefined
|
||||
cursorDate = undefined
|
||||
fileCloud = undefined
|
||||
periodName = undefined
|
||||
timeFrame = undefined
|
||||
finalized = true
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] finalize -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
|
||||
function initialize(
|
||||
pFileCloud,
|
||||
pMine,
|
||||
pBot,
|
||||
pSession,
|
||||
pProduct,
|
||||
pDataset,
|
||||
pExchange,
|
||||
pMarket,
|
||||
pPeriodName,
|
||||
pTimeFrame,
|
||||
pCursorDate,
|
||||
pCurrentTimeFrame,
|
||||
pBeginDateRange,
|
||||
pEndDateRange,
|
||||
pEventsServerClient,
|
||||
callBackFunction) {
|
||||
try {
|
||||
market = pMarket
|
||||
exchange = pExchange
|
||||
fileCloud = pFileCloud
|
||||
mine = pMine
|
||||
bot = pBot
|
||||
session = pSession
|
||||
product = pProduct
|
||||
dataset = pDataset
|
||||
periodName = pPeriodName
|
||||
cursorDate = removeTime(pCursorDate)
|
||||
timeFrame = pTimeFrame
|
||||
beginDateRange = pBeginDateRange
|
||||
endDateRange = pEndDateRange
|
||||
eventsServerClient = pEventsServerClient
|
||||
|
||||
let key = mine.config.codeName + '-' + bot.config.codeName + '-' + product.config.codeName + '-' + dataset.config.codeName + '-' + exchange.name + '-' + market.baseAsset + '/' + market.quotedAsset
|
||||
let callerId = key + '-' + periodName + newUniqueId()
|
||||
eventsServerClient.listenToEvent(key, 'Dataset Updated', undefined, callerId, onResponseDataSet, updateFiles)
|
||||
|
||||
key = mine.config.codeName + '-' + bot.config.codeName + '-' + product.config.codeName + '-' + exchange.name + '-' + market.baseAsset + '/' + market.quotedAsset
|
||||
callerId = key + '-' + periodName + newUniqueId()
|
||||
eventsServerClient.listenToEvent(key, 'Data Range Updated', undefined, callerId, onResponseDataRange, updateDataRange)
|
||||
|
||||
callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE)
|
||||
|
||||
function onResponseDataSet(message) {
|
||||
eventSubscriptionIdDatasetUpdated = message.eventSubscriptionId
|
||||
}
|
||||
|
||||
function onResponseDataRange(message) {
|
||||
eventSubscriptionIdDataRangeUpdated = message.eventSubscriptionId
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
|
||||
function updateDataRange(message) {
|
||||
try {
|
||||
if (finalized === true) { return }
|
||||
|
||||
if (message.event === undefined || message.event.dateRange === undefined) { return }
|
||||
|
||||
beginDateRange = new Date(message.event.dateRange.begin)
|
||||
endDateRange = new Date(message.event.dateRange.end)
|
||||
|
||||
let minDate = beginDateRange.valueOf()
|
||||
let maxDate = endDateRange.valueOf() + ONE_DAY_IN_MILISECONDS
|
||||
|
||||
for (let key of thisObject.files.keys()) {
|
||||
let keyDate = new Date(key)
|
||||
|
||||
if (keyDate.valueOf() < minDate || keyDate.valueOf() > maxDate) {
|
||||
thisObject.files.delete(key)
|
||||
thisObject.eventHandler.raiseEvent('Files Updated')
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] updateDataRange -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
|
||||
function updateFiles(message) {
|
||||
try {
|
||||
if (finalized === true) { return }
|
||||
|
||||
if (message.event === undefined || message.event.lastFile === undefined) { return }
|
||||
|
||||
let targetDate = new Date(message.event.lastFile)
|
||||
let dateString
|
||||
let file
|
||||
|
||||
dateString = targetDate.getUTCFullYear() + '-' + pad(targetDate.getUTCMonth() + 1, 2) + '-' + pad(targetDate.getUTCDate(), 2)
|
||||
|
||||
fileCloud.getFile(mine, bot, session, product, dataset, exchange, market, periodName, targetDate, undefined, undefined, undefined, onFileReceived)
|
||||
|
||||
function onFileReceived(err, file) {
|
||||
try {
|
||||
if (finalized === true) { return }
|
||||
|
||||
if (err.result === GLOBAL.DEFAULT_OK_RESPONSE.result) {
|
||||
thisObject.files.set(dateString, file)
|
||||
thisObject.eventHandler.raiseEvent('Files Updated')
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] updateFiles -> onFileReceived -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] updateFiles -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
|
||||
function setTimeFrame(pTimeFrame, pDatetime) {
|
||||
try {
|
||||
if (finalized === true) { return }
|
||||
|
||||
/*
|
||||
|
||||
We are implementing here an algorithm designed to save bandwidth, memory and processing power at the browser.
|
||||
We say there is a saving mode where the cursor is running at a minimum size. When the end user aproaches the time period the cursor
|
||||
is set, then it should exit the saving mode and go to its actual size.
|
||||
|
||||
To do this we are going to measure the distance from the Time Frame received to the one the cursors was initialized with.
|
||||
If these periods are consecutive, it means that the cursor should exit saving mode and load its full size.
|
||||
|
||||
*/
|
||||
|
||||
cursorDate = removeTime(pDatetime) // Adjust the cursor date to the one received.
|
||||
|
||||
let positionA
|
||||
|
||||
for (let i = 0; i < dailyFilePeriods.length; i++) {
|
||||
let period = dailyFilePeriods[i]
|
||||
|
||||
if (period[0] === pTimeFrame) {
|
||||
positionA = i
|
||||
}
|
||||
|
||||
if (period[0] === timeFrame) {
|
||||
positionB = i
|
||||
}
|
||||
}
|
||||
|
||||
if (Math.abs(positionB - positionA) <= 1) {
|
||||
exitSavingMode()
|
||||
} else {
|
||||
enterSavingMode()
|
||||
}
|
||||
|
||||
function enterSavingMode() {
|
||||
try {
|
||||
switch (timeFrame) {
|
||||
|
||||
case _45_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 3
|
||||
maxCursorSize = 30
|
||||
}
|
||||
break
|
||||
case _40_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 3
|
||||
maxCursorSize = 20
|
||||
}
|
||||
break
|
||||
case _30_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 3
|
||||
maxCursorSize = 15
|
||||
}
|
||||
break
|
||||
case _20_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 3
|
||||
maxCursorSize = 15
|
||||
}
|
||||
break
|
||||
case _15_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 3
|
||||
maxCursorSize = 15
|
||||
}
|
||||
break
|
||||
case _10_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 3
|
||||
maxCursorSize = 15
|
||||
}
|
||||
break
|
||||
case _5_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 3
|
||||
maxCursorSize = 15
|
||||
}
|
||||
break
|
||||
case _4_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 3
|
||||
maxCursorSize = 15
|
||||
}
|
||||
break
|
||||
case _3_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 3
|
||||
maxCursorSize = 15
|
||||
}
|
||||
break
|
||||
case _2_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 3
|
||||
maxCursorSize = 15
|
||||
}
|
||||
break
|
||||
case _1_MINUTE_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 3
|
||||
maxCursorSize = 15
|
||||
}
|
||||
break
|
||||
default:
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] setTimeFrame -> enterSavingMode -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
|
||||
function exitSavingMode() {
|
||||
try {
|
||||
switch (timeFrame) {
|
||||
|
||||
case _45_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 15
|
||||
maxCursorSize = 30
|
||||
}
|
||||
break
|
||||
case _40_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 13
|
||||
maxCursorSize = 20
|
||||
}
|
||||
break
|
||||
case _30_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 11
|
||||
maxCursorSize = 15
|
||||
}
|
||||
break
|
||||
case _20_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 9
|
||||
maxCursorSize = 15
|
||||
}
|
||||
break
|
||||
case _15_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 7
|
||||
maxCursorSize = 15
|
||||
}
|
||||
break
|
||||
case _10_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 7
|
||||
maxCursorSize = 15
|
||||
}
|
||||
break
|
||||
case _5_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 5
|
||||
maxCursorSize = 15
|
||||
}
|
||||
break
|
||||
case _4_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 5
|
||||
maxCursorSize = 15
|
||||
}
|
||||
break
|
||||
case _3_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 5
|
||||
maxCursorSize = 15
|
||||
}
|
||||
break
|
||||
case _2_MINUTES_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 5
|
||||
maxCursorSize = 15
|
||||
}
|
||||
break
|
||||
case _1_MINUTE_IN_MILISECONDS:
|
||||
{
|
||||
minCursorSize = 5
|
||||
maxCursorSize = 15
|
||||
}
|
||||
break
|
||||
default:
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] setTimeFrame -> exitSavingMode -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] setTimeFrame -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
|
||||
function setDatetime(pDatetime) {
|
||||
try {
|
||||
if (finalized === true) { return }
|
||||
|
||||
if (pDatetime === undefined) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] setDatetime -> Received undefined datetime.') }
|
||||
return
|
||||
}
|
||||
|
||||
cursorDate = removeTime(pDatetime)
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] setDatetime -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
|
||||
function reload(callBackFunction) {
|
||||
try {
|
||||
if (finalized === true) { return }
|
||||
|
||||
getFiles(callBackFunction)
|
||||
|
||||
collectGarbage(callBackFunction)
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] reload -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
|
||||
function getFiles(callBackFunction) {
|
||||
try {
|
||||
if (finalized === true) { return }
|
||||
|
||||
let i = 0
|
||||
let j = 0
|
||||
|
||||
let dateString
|
||||
|
||||
getNextFile()
|
||||
|
||||
function getNextFile() {
|
||||
try {
|
||||
let targetDate = new Date(cursorDate)
|
||||
targetDate.setUTCDate(targetDate.getUTCDate() + j)
|
||||
|
||||
/* Small algorith to allow load first the current date, then alternate between the most forwad and the most backwards ones. */
|
||||
if (j === 0) { j++ } else {
|
||||
if (j < 0) {
|
||||
j = -j
|
||||
j++
|
||||
} else {
|
||||
j = -j
|
||||
}
|
||||
}
|
||||
|
||||
let compareTargetDate = new Date(targetDate.valueOf())
|
||||
|
||||
if (beginDateRange !== undefined) {
|
||||
let compareBeginDate = removeTime(beginDateRange)
|
||||
|
||||
if (compareTargetDate.valueOf() < compareBeginDate.valueOf()) {
|
||||
controlLoop()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if (endDateRange !== undefined) {
|
||||
let compareEndDate = removeTime(endDateRange)
|
||||
|
||||
if (compareTargetDate.valueOf() >= compareEndDate.valueOf()) {
|
||||
controlLoop()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
dateString = targetDate.getUTCFullYear() + '-' + pad(targetDate.getUTCMonth() + 1, 2) + '-' + pad(targetDate.getUTCDate(), 2)
|
||||
|
||||
let currentDay = Math.trunc((new Date()).valueOf() / (24 * 60 * 60 * 1000))
|
||||
let targetDay = Math.trunc(targetDate.valueOf() / (24 * 60 * 60 * 1000))
|
||||
|
||||
if (targetDay > currentDay) {
|
||||
controlLoop()
|
||||
} else {
|
||||
if (thisObject.files.get(dateString) === undefined) {
|
||||
// We dont reload files we already have.
|
||||
|
||||
fileCloud.getFile(mine, bot, session, product, dataset, exchange, market, periodName, targetDate, undefined, undefined, undefined, onFileReceived)
|
||||
} else {
|
||||
controlLoop()
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFiles -> getNextFile -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
|
||||
function onFileReceived(err, file) {
|
||||
try {
|
||||
if (INFO_LOG === true) { logger.write('[INFO] getFiles -> onFileReceived -> Entering function.') }
|
||||
|
||||
switch (err.result) {
|
||||
case GLOBAL.DEFAULT_OK_RESPONSE.result: {
|
||||
if (INFO_LOG === true) { logger.write('[INFO] getFiles -> onFileReceived -> Received OK Response.') }
|
||||
break
|
||||
}
|
||||
|
||||
case GLOBAL.DEFAULT_FAIL_RESPONSE.result: {
|
||||
if (INFO_LOG === true) { logger.write('[INFO] getFiles -> onFileReceived -> Received FAIL Response.') }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
return
|
||||
}
|
||||
|
||||
case GLOBAL.CUSTOM_FAIL_RESPONSE.result: {
|
||||
if (INFO_LOG === true) { logger.write('[INFO] getFiles -> onFileReceived -> Received CUSTOM FAIL Response.') }
|
||||
if (INFO_LOG === true) { logger.write('[INFO] getFiles -> onFileReceived -> err.message = ' + err.message) }
|
||||
|
||||
if (err.message === 'File does not exist.') {
|
||||
controlLoop()
|
||||
return
|
||||
} else {
|
||||
callBackFunction(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
default: {
|
||||
if (INFO_LOG === true) { logger.write('[INFO] getFiles -> onFileReceived -> Received Unexpected Response.') }
|
||||
callBackFunction(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
thisObject.files.set(dateString, file)
|
||||
|
||||
controlLoop()
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFiles -> onFileReceived -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
|
||||
function controlLoop() {
|
||||
try {
|
||||
if (INFO_LOG === true) { logger.write('[INFO] getFiles -> controlLoop -> Entering function.') }
|
||||
|
||||
if (callBackFunction !== undefined) {
|
||||
callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE)
|
||||
}
|
||||
|
||||
i++
|
||||
|
||||
if (i < minCursorSize) {
|
||||
getNextFile()
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFiles -> controlLoop -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] getFiles -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
|
||||
function collectGarbage(callBackFunction) {
|
||||
try {
|
||||
if (finalized === true) { return }
|
||||
|
||||
date = removeTime(cursorDate)
|
||||
|
||||
let minDate = date.valueOf() - maxCursorSize * ONE_DAY_IN_MILISECONDS / 2
|
||||
let maxDate = date.valueOf() + maxCursorSize * ONE_DAY_IN_MILISECONDS / 2
|
||||
|
||||
for (let key of thisObject.files.keys()) {
|
||||
let keyDate = new Date(key)
|
||||
|
||||
if (keyDate.valueOf() < minDate || keyDate.valueOf() > maxDate) {
|
||||
thisObject.files.delete(key)
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] collectGarbage -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
|
||||
function getExpectedFiles() {
|
||||
if (finalized === true) { return }
|
||||
if (INFO_LOG === true) { logger.write('[INFO] getExpectedFiles -> Entering function.') }
|
||||
|
||||
return minCursorSize
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,296 @@
|
||||
|
||||
function newFileSequence () {
|
||||
const MODULE_NAME = 'File Sequence'
|
||||
const INFO_LOG = false
|
||||
const ERROR_LOG = true
|
||||
const logger = newWebDebugLog()
|
||||
logger.fileName = MODULE_NAME
|
||||
|
||||
let thisObject = {
|
||||
eventHandler: undefined,
|
||||
getFile: getFile,
|
||||
getExpectedFiles: getExpectedFiles,
|
||||
getFilesLoaded: getFilesLoaded,
|
||||
initialize: initialize,
|
||||
finalize: finalize
|
||||
}
|
||||
|
||||
let filesLoaded = 0
|
||||
let fileCloud
|
||||
let files = new Map()
|
||||
let maxSequence = -1 // This is replaced by the content of the sequence file, which contains an index that starts on zero. In the case that the sequence file is not found the default value is -1 so when you add 1 it gives you the amount of files in the sequence, zero.
|
||||
let market
|
||||
let mine
|
||||
let bot
|
||||
let session
|
||||
let dataset
|
||||
let product
|
||||
let finalized = false
|
||||
let initialized = false
|
||||
|
||||
thisObject.eventHandler = newEventHandler()
|
||||
|
||||
let eventSubscriptionIdDatasetUpdated
|
||||
let callerId
|
||||
let eventsServerClient
|
||||
|
||||
return thisObject
|
||||
|
||||
function finalize () {
|
||||
try {
|
||||
eventsServerClient.stopListening('Dataset Updated', eventSubscriptionIdDatasetUpdated)
|
||||
|
||||
thisObject.eventHandler.finalize()
|
||||
thisObject.eventHandler = undefined
|
||||
|
||||
filesLoaded = undefined
|
||||
fileCloud = undefined
|
||||
files = undefined
|
||||
maxSequence = undefined
|
||||
market = undefined
|
||||
mine = undefined
|
||||
bot = undefined
|
||||
session = undefined
|
||||
dataset = undefined
|
||||
product = undefined
|
||||
eventsServerClient = undefined
|
||||
|
||||
finalized = true
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] finalize -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
|
||||
function initialize (
|
||||
pMine,
|
||||
pBot,
|
||||
pSession,
|
||||
pProduct,
|
||||
pDataset,
|
||||
pExchange,
|
||||
pMarket,
|
||||
pHost,
|
||||
pPort,
|
||||
pEventsServerClient,
|
||||
callBackFunction) {
|
||||
try {
|
||||
exchange = pExchange
|
||||
market = pMarket
|
||||
mine = pMine
|
||||
bot = pBot
|
||||
session = pSession
|
||||
dataset = pDataset
|
||||
product = pProduct
|
||||
eventsServerClient = pEventsServerClient
|
||||
|
||||
fileCloud = newFileCloud()
|
||||
fileCloud.initialize(bot, pHost, pPort)
|
||||
|
||||
callerId = newUniqueId()
|
||||
|
||||
let key = mine.config.codeName + '-' + bot.config.codeName + '-' + product.config.codeName + '-' + dataset.config.codeName + '-' + exchange.name + '-' + market.baseAsset + '/' + market.quotedAsset
|
||||
eventsServerClient.listenToEvent(key, 'Dataset Updated', undefined, callerId, onResponse, updateFiles)
|
||||
|
||||
function onResponse (message) {
|
||||
eventSubscriptionIdDatasetUpdated = message.eventSubscriptionId
|
||||
}
|
||||
|
||||
/* First we will get the sequence max number */
|
||||
|
||||
fileCloud.getFile(mine, bot, session, product, dataset, exchange, market, undefined, undefined, 'Sequence', undefined, undefined, onSequenceFileReceived)
|
||||
|
||||
function onSequenceFileReceived (err, file) {
|
||||
try {
|
||||
if (finalized === true) { return }
|
||||
|
||||
switch (err.result) {
|
||||
case GLOBAL.DEFAULT_OK_RESPONSE.result: {
|
||||
break
|
||||
}
|
||||
|
||||
case GLOBAL.DEFAULT_FAIL_RESPONSE.result: {
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
return
|
||||
}
|
||||
|
||||
case GLOBAL.CUSTOM_OK_RESPONSE.result: {
|
||||
if (ERROR_LOG === true) { logger.write('[INFO] initialize -> onSequenceFileReceived -> err.message = ' + err.message) }
|
||||
initialized = true
|
||||
callBackFunction(err)
|
||||
return
|
||||
}
|
||||
|
||||
case GLOBAL.CUSTOM_FAIL_RESPONSE.result: {
|
||||
if (err.message === 'File does not exist.') { // We will assume that the process which generates the files has never been started, that does not imply that we can wait for data to come later.
|
||||
initialized = true
|
||||
callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE)
|
||||
return
|
||||
}
|
||||
if (ERROR_LOG === true) { logger.write('[INFO] initialize -> onSequenceFileReceived -> err.message = ' + err.message) }
|
||||
callBackFunction(err, thisObject)
|
||||
return
|
||||
}
|
||||
|
||||
default: {
|
||||
callBackFunction(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
initialized = true
|
||||
maxSequence = Number(file)
|
||||
|
||||
/* Now we will get the sequence of files */
|
||||
|
||||
for (let i = 0; i <= maxSequence; i++) {
|
||||
fileCloud.getFile(mine, bot, session, product, dataset, exchange, market, undefined, undefined, i, undefined, undefined, onFileReceived)
|
||||
|
||||
function onFileReceived (err, file) {
|
||||
try {
|
||||
if (finalized === true) { return }
|
||||
switch (err.result) {
|
||||
case GLOBAL.DEFAULT_OK_RESPONSE.result: {
|
||||
break
|
||||
}
|
||||
|
||||
case GLOBAL.DEFAULT_FAIL_RESPONSE.result: {
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
return
|
||||
}
|
||||
|
||||
case GLOBAL.CUSTOM_FAIL_RESPONSE.result: {
|
||||
if (ERROR_LOG === true) { logger.write('[INFO] initialize -> onSequenceFileReceived -> onFileReceived -> err.message = ' + err.message) }
|
||||
callBackFunction(err)
|
||||
return
|
||||
}
|
||||
|
||||
default: {
|
||||
callBackFunction(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
files.set(i, file)
|
||||
|
||||
filesLoaded++
|
||||
|
||||
callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE, thisObject) // Note that the callback is called for every file loaded.
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> onSequenceFileReceived -> onFileReceived -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> onSequenceFileReceived -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
|
||||
function updateFiles () {
|
||||
try {
|
||||
if (finalized === true || initialized === false) { return }
|
||||
|
||||
/*
|
||||
|
||||
To keep this data structure up-to-date we need to:
|
||||
|
||||
1. Re-read the sequence file.
|
||||
2. Re-load the current last file.
|
||||
3. Re-load all new files after our last one.
|
||||
|
||||
*/
|
||||
|
||||
let currentMaxSequence = maxSequence
|
||||
|
||||
fileCloud.getFile(mine, bot, session, product, dataset, exchange, market, undefined, undefined, 'Sequence', undefined, undefined, onSequenceFileReceived)
|
||||
|
||||
function onSequenceFileReceived (err, sequenceFile) {
|
||||
try {
|
||||
if (finalized === true) { return }
|
||||
switch (err.result) {
|
||||
case GLOBAL.DEFAULT_OK_RESPONSE.result: {
|
||||
break
|
||||
}
|
||||
|
||||
case GLOBAL.DEFAULT_FAIL_RESPONSE.result: {
|
||||
return
|
||||
}
|
||||
|
||||
case GLOBAL.CUSTOM_OK_RESPONSE.result: {
|
||||
if (ERROR_LOG === true) { logger.write('[WARN] updateFiles -> onSequenceFileReceived -> err.message = ' + err.message) }
|
||||
return
|
||||
}
|
||||
|
||||
case GLOBAL.CUSTOM_FAIL_RESPONSE.result: {
|
||||
if (ERROR_LOG === true) { logger.write('[WARN] updateFiles -> onSequenceFileReceived -> err.message = ' + err.message) }
|
||||
return
|
||||
}
|
||||
|
||||
default: {
|
||||
if (ERROR_LOG === true) { logger.write('[WARN] updateFiles -> onSequenceFileReceived -> Received Unexpected Response.') }
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
maxSequence = Number(sequenceFile)
|
||||
filesLoaded = 0
|
||||
|
||||
/* Now we will get the sequence of files, but in this case only from the currentMaxSequence and above. */
|
||||
|
||||
for (let i = currentMaxSequence; i <= maxSequence; i++) {
|
||||
fileCloud.getFile(mine, bot, session, product, dataset, exchange, market, undefined, undefined, i, undefined, undefined, onFileReceived)
|
||||
|
||||
function onFileReceived (err, file) {
|
||||
try {
|
||||
if (finalized === true) { return }
|
||||
switch (err.result) {
|
||||
case GLOBAL.DEFAULT_OK_RESPONSE.result: {
|
||||
break
|
||||
}
|
||||
case GLOBAL.DEFAULT_FAIL_RESPONSE.result: {
|
||||
return
|
||||
}
|
||||
case GLOBAL.CUSTOM_FAIL_RESPONSE.result: {
|
||||
if (ERROR_LOG === true) { logger.write('[WARN] updateFiles -> onSequenceFileReceived -> onFileReceived -> err.message = ' + err.message) }
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
files.set(i, file)
|
||||
|
||||
filesLoaded++
|
||||
|
||||
if (filesLoaded === maxSequence + 1) {
|
||||
thisObject.eventHandler.raiseEvent('Files Updated', undefined)
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] updateFiles -> onSequenceFileReceived -> onFileReceived -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] updateFiles -> onSequenceFileReceived -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] updateFiles -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
|
||||
function getFile (pSequence) {
|
||||
return files.get(pSequence)
|
||||
}
|
||||
|
||||
function getExpectedFiles () {
|
||||
return maxSequence + 1
|
||||
}
|
||||
|
||||
function getFilesLoaded () {
|
||||
return filesLoaded
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
function newFileStorage(host, port) {
|
||||
const MODULE_NAME = 'File Storage'
|
||||
const INFO_LOG = false
|
||||
const logger = newWebDebugLog()
|
||||
logger.fileName = MODULE_NAME
|
||||
|
||||
const MAX_RETRY = 2
|
||||
let currentRetry = 0
|
||||
|
||||
const recoverableErrors = [
|
||||
'SOCKETTIMEDOUT',
|
||||
'TIMEDOUT',
|
||||
'CONNRESET',
|
||||
'CONNREFUSED',
|
||||
'NOTFOUND',
|
||||
'ENOTFOUND',
|
||||
'ECONNREFUSED',
|
||||
'CONNREFUSED',
|
||||
'NOTFOUND',
|
||||
'ESOCKETTIMEDOUT',
|
||||
'ECONNRESET',
|
||||
'ETIMEDOUT',
|
||||
'EAI_AGAIN'
|
||||
]
|
||||
|
||||
let thisObject = {
|
||||
getFileFromHost: getFileFromHost
|
||||
}
|
||||
|
||||
return thisObject
|
||||
|
||||
async function getFileFromHost(filePath, callBackFunction, pathComplete) {
|
||||
try {
|
||||
if (INFO_LOG === true) { logger.write('[INFO] getFileFromHost -> Entering function.') }
|
||||
|
||||
let folder = ''
|
||||
if (pathComplete === false || pathComplete === undefined) {
|
||||
folder = 'Storage/'
|
||||
}
|
||||
let url = folder + filePath
|
||||
if (host !== undefined && port !== undefined) {
|
||||
url = 'http://' + host + ':' + port + '/' + folder + filePath
|
||||
} else {
|
||||
url = folder + filePath
|
||||
}
|
||||
|
||||
callWebServer(undefined, url, (response, fileContent) => {
|
||||
if (response.result === GLOBAL.DEFAULT_OK_RESPONSE.result) {
|
||||
callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE, fileContent)
|
||||
} else {
|
||||
callBackFunction(response)
|
||||
}
|
||||
})
|
||||
} catch (err) {
|
||||
if (verifyRetry(err.code) && currentRetry < MAX_RETRY) {
|
||||
currentRetry++
|
||||
if (INFO_LOG === true) { console.log('[INFO] getTextFile -> Retrying connection to the server because received error: ' + err.code + '. Retry #: ' + currentRetry) }
|
||||
getFileFromHost(filePath, callBackFunction)
|
||||
} else if (err.message === 'Request aborted') {
|
||||
let err = { code: 'The specified key does not exist.' }
|
||||
callBackFunction(err)
|
||||
} else {
|
||||
console.log('Error geting the file from the serever:', err)
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function verifyRetry(errorCode) {
|
||||
for (let i = 0; i < recoverableErrors.length; i++) {
|
||||
const error = recoverableErrors[i]
|
||||
if (error === errorCode) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
|
||||
function newMarketFiles() {
|
||||
const MODULE_NAME = 'Market Files'
|
||||
const ERROR_LOG = true
|
||||
const logger = newWebDebugLog()
|
||||
logger.fileName = MODULE_NAME
|
||||
|
||||
let thisObject = {
|
||||
eventHandler: undefined,
|
||||
getFile: getFile,
|
||||
getExpectedFiles: getExpectedFiles,
|
||||
getFilesLoaded: getFilesLoaded,
|
||||
getFilesNotLoaded: getFilesNotLoaded,
|
||||
initialize: initialize,
|
||||
finalize: finalize
|
||||
}
|
||||
|
||||
let filesLoaded = 0
|
||||
let filesNotLoaded = 0
|
||||
let filesExpected = 0
|
||||
|
||||
let fileCloud
|
||||
|
||||
let files = new Map()
|
||||
|
||||
let market
|
||||
let exchange
|
||||
let mine
|
||||
let bot
|
||||
let session
|
||||
let dataset
|
||||
let product
|
||||
let periodName
|
||||
|
||||
thisObject.eventHandler = newEventHandler()
|
||||
|
||||
let finalized = false
|
||||
|
||||
let eventSubscriptionIdDatasetUpdated = []
|
||||
let eventsServerClient
|
||||
|
||||
return thisObject
|
||||
|
||||
function finalize() {
|
||||
try {
|
||||
for (let i = 0; i < eventSubscriptionIdDatasetUpdated.length; i++) {
|
||||
eventsServerClient.stopListening('Dataset Updated', eventSubscriptionIdDatasetUpdated[i])
|
||||
}
|
||||
|
||||
thisObject.eventHandler.finalize()
|
||||
thisObject.eventHandler = undefined
|
||||
|
||||
filesLoaded = undefined
|
||||
files = undefined
|
||||
|
||||
market = undefined
|
||||
exchange = undefined
|
||||
mine = undefined
|
||||
bot = undefined
|
||||
session = undefined
|
||||
dataset = undefined
|
||||
product = undefined
|
||||
periodName = undefined
|
||||
eventsServerClient = undefined
|
||||
|
||||
finalized = true
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] finalize -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
|
||||
function initialize(
|
||||
pMine,
|
||||
pBot,
|
||||
pSession,
|
||||
pProduct,
|
||||
pDataset,
|
||||
pExchange,
|
||||
pMarket,
|
||||
pHost,
|
||||
pPort,
|
||||
pEventsServerClient,
|
||||
callBackFunction) {
|
||||
try {
|
||||
let timeFrames
|
||||
|
||||
exchange = pExchange
|
||||
market = pMarket
|
||||
mine = pMine
|
||||
bot = pBot
|
||||
session = pSession
|
||||
dataset = pDataset
|
||||
product = pProduct
|
||||
eventsServerClient = pEventsServerClient
|
||||
|
||||
fileCloud = newFileCloud()
|
||||
fileCloud.initialize(pBot, pHost, pPort)
|
||||
|
||||
/* Some Validations */
|
||||
if (dataset.config.validTimeFrames === undefined) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> err = Can not initialize Market Files for bot ' + pBot.name) }
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> err = You need to define validTimeFrames at the Dataset config. ') }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
return
|
||||
}
|
||||
|
||||
getTimeFramesFile()
|
||||
|
||||
function getTimeFramesFile() {
|
||||
/* First we will get the Data Range */
|
||||
fileCloud.getFile(pMine, pBot, pSession, pProduct, pDataset, exchange, pMarket, undefined, undefined, undefined, undefined, true, onTimeFramesReceived)
|
||||
|
||||
function onTimeFramesReceived(err, pFile) {
|
||||
switch (err.result) {
|
||||
case GLOBAL.DEFAULT_OK_RESPONSE.result: {
|
||||
timeFrames = pFile
|
||||
break
|
||||
}
|
||||
case GLOBAL.DEFAULT_FAIL_RESPONSE.result: {
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
return
|
||||
}
|
||||
case GLOBAL.CUSTOM_FAIL_RESPONSE.result: {
|
||||
if (err.message === 'File does not exist.') {
|
||||
/* It is ok, we will deal with this later */
|
||||
break
|
||||
}
|
||||
if (err.message === 'Configured to not Support This.') {
|
||||
/* It is ok, we will deal with this later */
|
||||
break
|
||||
}
|
||||
if (err.message === 'Missing Configuration.') {
|
||||
if (ERROR_LOG === true) { logger.write('[WARN] initialize -> onFileReceived -> The needed configuration to locate the timeFrames is missing at the dataSet.') }
|
||||
if (ERROR_LOG === true) { logger.write('[WARN] initialize -> onFileReceived -> Time Frames Filters will be ignored -> Product = ' + pProduct.config.codeName) }
|
||||
break
|
||||
}
|
||||
callBackFunction(err)
|
||||
return
|
||||
}
|
||||
default: {
|
||||
callBackFunction(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
getMarketFiles()
|
||||
}
|
||||
}
|
||||
|
||||
function getMarketFiles() {
|
||||
/* Now we will get the market files */
|
||||
for (let i = 0; i < marketFilesPeriods.length; i++) {
|
||||
let periodTime = marketFilesPeriods[i][0]
|
||||
let periodName = marketFilesPeriods[i][1]
|
||||
|
||||
if (dataset.config.validTimeFrames.includes(periodName) === false) {
|
||||
filesNotLoaded++
|
||||
continue
|
||||
}
|
||||
if (timeFrames !== undefined) {
|
||||
if (timeFrames.includes(periodName) === false) {
|
||||
filesNotLoaded++
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
filesExpected++
|
||||
fileCloud.getFile(mine, bot, session, product, dataset, exchange, market, periodName, undefined, undefined, undefined, undefined, onFileReceived)
|
||||
|
||||
function onFileReceived(err, file) {
|
||||
try {
|
||||
if (finalized === true) { return }
|
||||
if (err.result === GLOBAL.DEFAULT_OK_RESPONSE.result) {
|
||||
files.set(periodTime, file)
|
||||
filesLoaded++
|
||||
} else {
|
||||
filesNotLoaded++
|
||||
}
|
||||
|
||||
if (filesLoaded + filesNotLoaded === marketFilesPeriods.length) {
|
||||
let key = mine.config.codeName + '-' + bot.config.codeName + '-' + product.config.codeName + '-' + dataset.config.codeName + '-' + exchange.name + '-' + market.baseAsset + '/' + market.quotedAsset
|
||||
let callerId = key + '-' + periodName + newUniqueId()
|
||||
eventsServerClient.listenToEvent(key, 'Dataset Updated', undefined, callerId, onResponse, updateFiles)
|
||||
|
||||
callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE, thisObject)
|
||||
|
||||
function onResponse(message) {
|
||||
eventSubscriptionIdDatasetUpdated.push(message.eventSubscriptionId)
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> onFileReceived -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE, thisObject)
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
|
||||
function updateFiles(message) {
|
||||
try {
|
||||
if (finalized === true) { return }
|
||||
let updatedFiles = 0
|
||||
|
||||
/* Now we will get the market files */
|
||||
|
||||
for (let i = 0; i < marketFilesPeriods.length; i++) {
|
||||
let periodTime = marketFilesPeriods[i][0]
|
||||
let periodName = marketFilesPeriods[i][1]
|
||||
|
||||
if (dataset.config.validTimeFrames.includes(periodName) !== true) { continue }
|
||||
if (message.event !== undefined) {
|
||||
if (message.event.timeFrames !== undefined) {
|
||||
if (message.event.timeFrames.includes(periodName) !== true) { continue }
|
||||
}
|
||||
}
|
||||
fileCloud.getFile(mine, bot, session, product, dataset, exchange, market, periodName, undefined, undefined, undefined, undefined, onFileReceived)
|
||||
|
||||
function onFileReceived(err, file) {
|
||||
try {
|
||||
if (finalized === true) { return }
|
||||
files.set(periodTime, file)
|
||||
updatedFiles++
|
||||
|
||||
if (updatedFiles === filesExpected) {
|
||||
thisObject.eventHandler.raiseEvent('Files Updated')
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] updateFiles -> onFileReceived -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] updateFiles -> err = ' + err.stack) }
|
||||
}
|
||||
}
|
||||
|
||||
function getFile(pPeriod) {
|
||||
return files.get(pPeriod)
|
||||
}
|
||||
|
||||
function getExpectedFiles() {
|
||||
return marketFilesPeriods.length
|
||||
}
|
||||
|
||||
function getFilesLoaded() {
|
||||
return filesLoaded
|
||||
}
|
||||
|
||||
function getFilesNotLoaded() {
|
||||
return filesNotLoaded
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
|
||||
function newSingleFile () {
|
||||
const MODULE_NAME = 'Single File'
|
||||
const INFO_LOG = false
|
||||
const ERROR_LOG = true
|
||||
const logger = newWebDebugLog()
|
||||
logger.fileName = MODULE_NAME
|
||||
|
||||
let thisObject = {
|
||||
getFile: getFile,
|
||||
initialize: initialize
|
||||
}
|
||||
|
||||
let fileCloud
|
||||
let file
|
||||
|
||||
return thisObject
|
||||
|
||||
function initialize (
|
||||
pMine,
|
||||
pBot,
|
||||
pSession,
|
||||
pProduct,
|
||||
pDataset,
|
||||
pExchange,
|
||||
pMarket,
|
||||
pHost,
|
||||
pPort,
|
||||
pEventsServerClient,
|
||||
callBackFunction) {
|
||||
try {
|
||||
if (INFO_LOG === true) { logger.write('[INFO] initialize -> Entering function.') }
|
||||
if (INFO_LOG === true) { logger.write('[INFO] initialize -> key = ' + pMine.config.codeName + '-' + pBot.config.codeName + '-' + pProduct.config.codeName) }
|
||||
|
||||
let exchange = pExchange
|
||||
|
||||
fileCloud = newFileCloud()
|
||||
fileCloud.initialize(pBot, pHost, pPort)
|
||||
|
||||
/* Now we will get the file */
|
||||
|
||||
fileCloud.getFile(pMine, pBot, pSession, pProduct, pDataset, exchange, pMarket, undefined, undefined, undefined, undefined, undefined, onFileReceived)
|
||||
|
||||
function onFileReceived (err, pFile) {
|
||||
try {
|
||||
if (INFO_LOG === true) { logger.write('[INFO] initialize -> onFileReceived -> Entering function.') }
|
||||
|
||||
switch (err.result) {
|
||||
case GLOBAL.DEFAULT_OK_RESPONSE.result: {
|
||||
if (INFO_LOG === true) { logger.write('[INFO] initialize -> onFileReceived -> Received OK Response.') }
|
||||
break
|
||||
}
|
||||
|
||||
case GLOBAL.DEFAULT_FAIL_RESPONSE.result: {
|
||||
if (INFO_LOG === true) { logger.write('[INFO] initialize -> onFileReceived -> Received FAIL Response.') }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
return
|
||||
}
|
||||
|
||||
case GLOBAL.CUSTOM_FAIL_RESPONSE.result: {
|
||||
if (INFO_LOG === true) { logger.write('[INFO] initialize -> onFileReceived -> Received CUSTOM FAIL Response.') }
|
||||
if (INFO_LOG === true) { logger.write('[INFO] initialize -> onFileReceived -> err.message = ' + err.message) }
|
||||
|
||||
callBackFunction(err)
|
||||
return
|
||||
}
|
||||
|
||||
default: {
|
||||
if (INFO_LOG === true) { logger.write('[INFO] initialize -> onFileReceived -> Received Unexpected Response.') }
|
||||
callBackFunction(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
file = pFile
|
||||
|
||||
callBackFunction(GLOBAL.DEFAULT_OK_RESPONSE)
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> onFileReceived -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (ERROR_LOG === true) { logger.write('[ERROR] initialize -> err = ' + err.stack) }
|
||||
callBackFunction(GLOBAL.DEFAULT_FAIL_RESPONSE)
|
||||
}
|
||||
}
|
||||
|
||||
function getFile () {
|
||||
if (INFO_LOG === true) { logger.write('[INFO] getFile -> Entering function.') }
|
||||
|
||||
return file
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user