Operations Module moved to old repos

This commit is contained in:
Luis Fernando Molina committed 2020-11-20 19:46:12 +01:00
1 parent 765659d4a2
commit 9e00cd2468
88 files changed
-84

No files matched your search

@@ -0,0 +1,43 @@
import express from 'express'
import 'dotenv/config'
import graphqlHTTP from 'express-graphql'
import schema from './schema'
import mongoose from 'mongoose'
import cors from 'cors'
import logger from './config/logger'
import wrongPreshared from './errors/notAllowed.json'
const app = express()
app.post('/graphql', (req, res, next) => {
if (req.headers.preshared === process.env.OPERATIONS_API_PRESHARED) {
next()
} else {
res.send(wrongPreshared)
}
})
// Allow cross origing request
app.use(cors())
// Database Connection
mongoose.connect(process.env.MONGO_URL, { useNewUrlParser: true })
mongoose.connection.once('open', () => {
logger.info('Connected to the DB.')
})
app.use('/graphql', graphqlHTTP(req => {
return {
schema: schema,
context: {
userId: req.headers.userid,
authorization: req.headers.authorization
},
graphiql: true
}
}))
app.listen(process.env.PORT, () => {
logger.info(`Listening on port ${process.env.PORT}.`)
})
@@ -0,0 +1,11 @@
export const Trading = 'Trading'
export const Indicator = 'Indicator'
export const Sensor = 'Sensor'
export const BotTypesEnum = [
Trading,
Indicator,
Sensor
]
export default BotTypesEnum
@@ -0,0 +1,19 @@
export const LIVE = 'live'
export const BACKTEST = 'backtest'
export const COMPETITION = 'competition'
export const ALL_MONTHS = 'allMonths'
export const ONE_MONTH = 'oneMonth'
export const NO_TIME = 'noTime'
export const FIXED_INTERVAL = 'fixedInterval'
export const CloneModeEnum = [
LIVE,
BACKTEST,
COMPETITION,
ALL_MONTHS,
ONE_MONTH,
NO_TIME,
FIXED_INTERVAL
]
export default CloneModeEnum
@@ -0,0 +1,19 @@
export const UNPUBLISHED = 'UNPUBLISHED';
export const PUBLISHED = 'PUBLISHED';
export const CANCELLED = 'CANCELLED';
export const RUNNING = 'RUNNING';
export const INTERRUPTED = 'INTERRUPTED';
export const FINISHED = 'FINISHED';
export const ARCHIVED = 'ARCHIVED';
export const CloneStateEnum = [
UNPUBLISHED,
PUBLISHED,
CANCELLED,
RUNNING,
INTERRUPTED,
FINISHED,
ARCHIVED,
];
export default CloneStateEnum;
@@ -0,0 +1,47 @@
export class AuthenticationError extends Error {
constructor(message) {
super(message)
this.code = 401
this.message = 'Autentification not found, you have to be authentificated to perform this action.'
}
}
export class AuthorizationError extends Error {
constructor(message) {
super(message)
this.code = 401
this.message = 'You are not authorized to perform this action.'
}
}
export class WrongArgumentsError extends Error {
constructor(message) {
super(message)
this.code = 400
this.message = `Wrong arguments : ${this.message}`
}
}
export class ServiceUnavailableError extends Error {
constructor(message) {
super(message)
this.code = 503
this.message = `At least one service is unresponding ${this.message}`
}
}
export class OperationsError extends Error {
constructor(message) {
super(message)
this.code = 500
this.message = `OperationsModule Internal Error. ${this.message}`
}
}
export class KubernateError extends Error {
constructor(message) {
super(message)
this.code = 500
this.message = `OperationsModule Kubernate Error. ${this.message}`
}
}
@@ -0,0 +1,8 @@
{
"errors": [
{
"code": 401,
"message": "Wait, you're not supposed to be here, go through the gate as everyone else"
}
]
}
@@ -0,0 +1,35 @@
import logger from '../config/logger'
import axios from 'axios'
const addBotMutation = async (authorization, bot) => {
logger.debug('addBotMutation -> Entering function.')
let response = await axios({
url: process.env.GATEWAY_ENDPOINT,
method: 'post',
data: {
query: `
mutation web_AddBot($bot: web_BotInput){
web_AddBot(bot: $bot){
codeName
cloneId
}
}
`,
variables: {
bot
}
},
headers: {
authorization: authorization
}
})
if (response.data.errors) {
throw response.data.errors
}
return response.data.data.web_AddBot
}
export default addBotMutation
@@ -0,0 +1,28 @@
import logger from '../config/logger'
import axios from 'axios'
const authorizeKey = (authorization, keyId, cloneId, releaseClon) => {
logger.debug('authorizeKey -> Entering function.')
return axios({
url: process.env.GATEWAY_ENDPOINT,
method: 'post',
data: {
query: `
mutation keyVault_AuthorizeClone($keyId: String!, $cloneId: String!, $releaseClon: Boolean!){
keyVault_AuthorizeClone(keyId: $keyId, cloneId: $cloneId, releaseClon:$releaseClon)
}
`,
variables: {
keyId: keyId,
cloneId: cloneId,
releaseClon: releaseClon
}
},
headers: {
authorization: authorization
}
})
}
export default authorizeKey
@@ -0,0 +1,40 @@
import logger from '../config/logger'
import axios from 'axios'
const ecosystemQuery = async (authorization) => {
logger.debug('ecosystemQuery -> Entering function')
let response = await axios({
url: process.env.GATEWAY_ENDPOINT,
method: 'post',
data: {
query: `
query {
web_GetEcosystem {
devTeams {
codeName
displayName
host {
url
storage
container
accessKey
}
}
}
}
`
},
headers: {
authorization: authorization
}
})
if (response.data.errors) {
throw response.data.errors
}
return response.data.data.web_GetEcosystem
}
export default ecosystemQuery
@@ -0,0 +1,35 @@
import logger from '../config/logger'
import axios from 'axios'
const removeBotMutation = async (authorization, bot) => {
logger.debug('removeBotMutation -> Entering function.')
let response = await axios({
url: process.env.GATEWAY_ENDPOINT,
method: 'post',
data: {
query: `
mutation web_RemoveBot($bot: web_BotInput){
web_RemoveBot(bot: $bot){
codeName
cloneId
}
}
`,
variables: {
bot
}
},
headers: {
authorization: authorization
}
})
if (response.data.errors) {
throw response.data.errors
}
return response.data.data.web_RemoveBot
}
export default removeBotMutation
@@ -0,0 +1,49 @@
import logger from '../config/logger'
import axios from 'axios'
const teamQuery = (authorization, botId) => {
logger.debug('teamQuery -> Entering function')
return axios({
url: process.env.GATEWAY_ENDPOINT,
method: 'post',
data: {
query: `
query teams_FbByOwner($fbId: String){
teams_FbByOwner(fbId: $fbId) {
edges{
node{
id
kind
name
slug
avatar
team{
id
name
slug
profile{
avatar
}
members{
member{
alias
}
}
}
}
}
}
}
`,
variables: {
fbId: botId
}
},
headers: {
authorization: authorization
}
})
}
export default teamQuery
@@ -0,0 +1,2 @@
require = require('esm')(module)
module.exports = require('./app.js')
@@ -0,0 +1,214 @@
import logger from '../config/logger'
import { toPlatformDatetime, isDefined } from '../config/utils'
import { KubernateError } from '../errors'
import { Client } from 'kubernetes-client'
import deploymentManifest from '../config/clone-deployment.json'
import { BACKTEST, COMPETITION, LIVE, NO_TIME } from '../enums/CloneMode'
import { Trading, Indicator, Sensor } from '../enums/BotTypes'
import kubeconfig from './kubeConfig'
const createClone = async (clone) => {
try {
logger.debug('createClone %s', clone.id)
const Request = require('kubernetes-client/backends/request')
const backend = new Request(Request.config.fromKubeconfig(kubeconfig()))
const client = new Client({ backend, version: '1.9' })
deploymentManifest.metadata.name = clone.id
deploymentManifest.spec.template.spec.containers[0].image = process.env.CLONE_EXECUTOR_IMAGE
logger.debug('createClone Environment and Storage Configuration.')
let env = []
env.push({
'name': 'GATEWAY_ENDPOINT',
'value': process.env.GATEWAY_ENDPOINT
})
env.push({
'name': 'GATEWAY_ENDPOINT_K8S',
'value': process.env.GATEWAY_ENDPOINT_K8S
})
env.push({
'name': 'HOST_STORAGE',
'value': 'localStorage'
})
if (clone.authorization) {
env.push({
'name': 'AUTHORIZATION',
'value': clone.authorization
})
}
if (JSON.parse(process.env.FULL_LOG)) {
env.push({
'name': 'FULL_LOG',
'value': process.env.FULL_LOG
})
}
logger.debug('createClone General Financial Being Configuration.')
env.push({
'name': 'DEV_TEAM',
'value': clone.teamSlug
})
env.push({
'name': 'BOT',
'value': clone.botSlug
})
env.push({
'name': 'TYPE',
'value': clone.botType
})
env.push({
'name': 'CLONE_ID',
'value': clone.id.toString()
})
env.push({
'name': 'START_MODE',
'value': clone.mode
})
env.push({
'name': 'PROCESS',
'value': clone.processName
})
env.push({
'name': 'EXCHANGE_NAME',
'value': clone.exchangeName
})
if (isDefined(clone.timePeriod)) {
env.push({
'name': 'TIME_PERIOD',
'value': clone.timePeriod.toString()
})
}
logger.debug('createClone Trading Configuration.')
if (clone.botType === Trading) {
env.push({
'name': 'RESUME_EXECUTION',
'value': clone.resumeExecution.toString()
})
env.push({
'name': 'DATA_SET',
'value': datasetNames().get(clone.timePeriod)
})
env.push({
'name': 'BASE_ASSET',
'value': clone.baseAsset
})
env.push({
'name': 'INITIAL_BALANCE_ASSET_A',
'value': clone.balanceAssetA.toString()
})
env.push({
'name': 'INITIAL_BALANCE_ASSET_B',
'value': clone.balanceAssetB.toString()
})
if (clone.mode === BACKTEST) {
env.push({
'name': 'BEGIN_DATE_TIME',
'value': toPlatformDatetime(clone.beginDatetime)
})
env.push({
'name': 'END_DATE_TIME',
'value': toPlatformDatetime(clone.endDatetime)
})
env.push({
'name': 'WAIT_TIME',
'value': clone.waitTime.toString()
})
} else if (clone.mode === COMPETITION) {
env.push({
'name': 'BEGIN_DATE_TIME',
'value': toPlatformDatetime(clone.beginDatetime)
})
env.push({
'name': 'END_DATE_TIME',
'value': toPlatformDatetime(clone.endDatetime)
})
env.push({
'name': 'KEY_ID',
'value': clone.keyId
})
env.push({
'name': 'ACCESS_TOKEN',
'value': clone.accessTokenKey
})
} else if (clone.mode === LIVE) {
env.push({
'name': 'KEY_ID',
'value': clone.keyId
})
env.push({
'name': 'ACCESS_TOKEN',
'value': clone.accessTokenKey
})
}
} else if (clone.botType === Indicator || clone.botType === Sensor) {
if (clone.mode === NO_TIME) {
if (!clone.resumeExecution) {
env.push({
'name': 'BEGIN_DATE_TIME',
'value': toPlatformDatetime(clone.beginDatetime)
})
}
} else {
env.push({
'name': 'MIN_YEAR',
'value': isDefined(clone.startYear) ? clone.startYear.toString() : ''
})
env.push({
'name': 'MAX_YEAR',
'value': isDefined(clone.endYear) ? clone.endYear.toString() : ''
})
env.push({
'name': 'MONTH',
'value': isDefined(clone.month) ? clone.month.toString() : ''
})
env.push({
'name': 'INTERVAL',
'value': isDefined(clone.interval) ? clone.interval.toString() : ''
})
}
}
deploymentManifest.spec.template.spec.containers[0].env = env
await client.apis.batch.v1.namespaces('default').jobs.post(
{ body: deploymentManifest })
logger.debug('createClone %s on Kubernates success.', clone.id)
} catch (err) {
logger.error('Error creating clone: %s', err)
throw new KubernateError(err)
}
}
function datasetNames() {
let dataSetMap = new Map()
dataSetMap.set("24-hs", "Multi-Period-Market")
dataSetMap.set("12-hs", "Multi-Period-Market")
dataSetMap.set("08-hs", "Multi-Period-Market")
dataSetMap.set("06-hs", "Multi-Period-Market")
dataSetMap.set("04-hs", "Multi-Period-Market")
dataSetMap.set("03-hs", "Multi-Period-Market")
dataSetMap.set("02-hs", "Multi-Period-Market")
dataSetMap.set("01-hs", "Multi-Period-Market")
dataSetMap.set("45-min", "Multi-Period-Daily")
dataSetMap.set("40-min", "Multi-Period-Daily")
dataSetMap.set("30-min", "Multi-Period-Daily")
dataSetMap.set("20-min", "Multi-Period-Daily")
dataSetMap.set("15-min", "Multi-Period-Daily")
dataSetMap.set("10-min", "Multi-Period-Daily")
dataSetMap.set("05-min", "Multi-Period-Daily")
dataSetMap.set("04-min", "Multi-Period-Daily")
dataSetMap.set("03-min", "Multi-Period-Daily")
dataSetMap.set("02-min", "Multi-Period-Daily")
dataSetMap.set("01-min", "Multi-Period-Daily")
return dataSetMap
}
export default createClone
@@ -0,0 +1,43 @@
import logger from '../config/logger'
import { Client } from 'kubernetes-client'
import kubeconfig from './kubeConfig'
const getClonePodLogs = async (cloneName) => {
try {
logger.debug('getClonePodLogs on kubernates started.')
const Request = require('kubernetes-client/backends/request')
const backend = new Request(Request.config.fromKubeconfig(kubeconfig()))
const client = new Client({ backend, version: '1.9' })
let query = {
'qs': {
'labelSelector': 'job-name=' + cloneName
}
}
const pod = await client.api.v1.namespaces('default').pods.get(query)
let queryLogs = {
'qs': {
'tailLines': '300'
}
}
if(pod.body.items[0] !== undefined){
const logs = await client.api.v1.namespaces('default')
.pods(pod.body.items[0].metadata.name).log.get(queryLogs)
let logsArray = logs.body.split('\n')
logger.debug('getClonePodLogs on kubernates successful.')
return JSON.stringify(logsArray, null, 2)
} else {
logger.warn('getClonePodStatus pod not found %s: ', cloneName)
return "Logs not available."
}
} catch (err) {
logger.error('There was an error getting the pod logs %s: ', err)
return "Logs not available."
}
}
export default getClonePodLogs
@@ -0,0 +1,40 @@
import logger from '../config/logger'
import { Client } from 'kubernetes-client'
import kubeconfig from './kubeConfig'
const getClonePodStatus = async (cloneName) => {
try {
logger.debug('getClonePodStatus on kubernates started.')
const Request = require('kubernetes-client/backends/request')
const backend = new Request(Request.config.fromKubeconfig(kubeconfig()))
const client = new Client({ backend, version: '1.9' })
let query = {
'qs': {
'labelSelector': 'job-name=' + cloneName
}
}
const pod = await client.api.v1.namespaces('default').pods.get(query)
if (pod.body.items[0] !== undefined) {
let clonePodStatus = pod.body.items[0].status.containerStatuses[0].state
if (clonePodStatus.hasOwnProperty('terminated')
&& clonePodStatus.terminated.hasOwnProperty('containerID')) {
delete clonePodStatus.terminated.containerID
}
logger.debug('getClonePodStatus %s on kubernates successful', cloneName)
return JSON.stringify(clonePodStatus, null, 2)
} else {
logger.warn('getClonePodStatus pod not found: %s', cloneName)
return " Clone not found on the server. "
}
} catch (err) {
logger.error('There was an error getting the pod status %s: ', err)
return "Status not available."
}
}
export default getClonePodStatus
@@ -0,0 +1,49 @@
import logger from '../config/logger'
const kubeConfig = () => {
let clusterConfig = {}
clusterConfig.name = process.env.CLUSTER_NAME
let cluster = {}
cluster.server = process.env.CLUSTER_SERVER
if (process.env.CERTIFICATE_AUTHORITY_DATA) {
cluster['certificate-authority-data'] = process.env.CERTIFICATE_AUTHORITY_DATA
}
if (process.env.INSECURE_SKIP_TLS_VERIFY) {
cluster['insecure-skip-tls-verify'] = JSON.parse(process.env.INSECURE_SKIP_TLS_VERIFY)
}
clusterConfig.cluster = cluster
let contextConfig = {}
contextConfig.name = process.env.CONTEXT_NAME
let context = {}
context.cluster = process.env.CLUSTER_NAME
context.user = process.env.USER_NAME
contextConfig.context = context
let userConfig = {}
userConfig.name = process.env.USER_NAME
let user = {}
if (process.env.USER_PASSWORD) {
user.password = process.env.USER_PASSWORD
user.username = process.env.USER_NAME
}
if (process.env.CLIENT_CERTIFICATE_DATA) {
user['client-certificate-data'] = process.env.CLIENT_CERTIFICATE_DATA
user['client-key-data'] = process.env.CLIENT_KEY_DATA
}
userConfig.user = user
const config = {
apiVersion: 'v1',
clusters: [clusterConfig],
contexts: [contextConfig],
'current-context': process.env.CONTEXT_NAME,
kind: 'Config',
users: [userConfig]
}
return config
}
export default kubeConfig
@@ -0,0 +1,29 @@
import logger from '../config/logger'
import { Client } from 'kubernetes-client'
import kubeconfig from './kubeConfig'
const removeClone = async (cloneId) => {
try {
logger.debug('removeClone %s', cloneId)
const Request = require('kubernetes-client/backends/request')
const backend = new Request(Request.config.fromKubeconfig(kubeconfig()))
const client = new Client({ backend, version: '1.9' })
let query = {
'qs': {
'labelSelector': 'job-name=' + cloneId
}
}
await client.apis.batch.v1.namespaces('default').jobs(cloneId).delete()
const pod = await client.api.v1.namespaces('default').pods.get(query)
await client.api.v1.namespaces('default').pods(pod.body.items[0].metadata.name).delete()
logger.debug('removeClone %s on kubernates succesful', cloneId)
} catch (err) {
logger.warn('removeClone %s on kubernates failed, the job was not found on the server.', cloneId)
}
}
export default removeClone
@@ -0,0 +1,65 @@
import mongoose, { Schema } from 'mongoose'
import {
UNPUBLISHED,
CloneStateEnum
} from '../enums/CloneState'
import {
BACKTEST,
CloneModeEnum
} from '../enums/CloneMode'
const cloneSchema = new Schema({
authId: {
type: String,
required: true
},
teamId: {
type: String,
required: true
},
botId: {
type: String,
required: true
},
keyId: String,
mode: {
type: String,
enum: CloneModeEnum,
default: BACKTEST
},
resumeExecution: Boolean,
beginDatetime: Number,
endDatetime: Number,
waitTime: Number,
state: {
type: String,
enum: CloneStateEnum,
default: UNPUBLISHED
},
stateDatetime: Number,
createDatetime: Number,
active: Boolean,
summaryDate: Number,
buyAverage: Number,
sellAverage: Number,
marketRate: Number,
combinedProfitsA: Number,
combinedProfitsB: Number,
assetA: String,
assetB: String,
processName: {
type: String,
required: true
},
exchangeName: String,
timePeriod: String,
balanceAssetA: Number,
balanceAssetB: Number
})
const Clone = mongoose.model('Clone', cloneSchema)
export default Clone
@@ -0,0 +1,5 @@
import Clone from './clone'
export {
Clone
}
@@ -0,0 +1,17 @@
const cloneDetails = (selectedBot, clone) => {
if (selectedBot !== undefined) {
clone.botId = selectedBot.id
clone.kind = selectedBot.kind
clone.botName = selectedBot.name
clone.botSlug = selectedBot.slug
clone.botAvatar = selectedBot.avatar
clone.teamId = selectedBot.team.id
clone.teamName = selectedBot.team.name
clone.teamSlug = selectedBot.team.slug
clone.teamAvatar = selectedBot.team.profile.avatar
clone.botType = selectedBot.kind
}
return clone
}
export default cloneDetails
@@ -0,0 +1,10 @@
import { GraphQLSchema } from 'graphql'
import Mutation from './mutations'
import RootQuery from './queries'
const Schema = new GraphQLSchema({
query: RootQuery,
mutation: Mutation
})
export default Schema
@@ -0,0 +1,150 @@
import { CloneInputType } from '../types/input'
import { CloneType } from '../types'
import { Clone } from '../../models'
import { BotTypesEnum } from '../../enums/BotTypes'
import teamQuery from '../../graphQLCalls/teamQuery'
import authorizeKey from '../../graphQLCalls/authorizeKey'
import addBotMutation from '../../graphQLCalls/addBotMutation'
import cloneDetails from '../cloneDetails'
import {
AuthenticationError,
WrongArgumentsError,
OperationsError,
AuthorizationError
} from '../../errors'
import logger from '../../config/logger'
import createKubernetesClone from '../../kubernetes/createClone'
import { isDefined } from '../../config/utils'
import { CloneModeEnum, LIVE, COMPETITION, BACKTEST } from '../../enums/CloneMode'
import { Trading } from '../../enums/BotTypes'
const args = {
clone: { type: CloneInputType }
}
const resolve = async (parent, { clone }, context) => {
logger.debug('addClone -> Entering Fuction.')
if (!context.userId) {
throw new AuthenticationError()
} else {
clone.authorization = context.authorization
}
if (!Object.values(CloneModeEnum).includes(clone.mode)) {
throw new WrongArgumentsError('The mode selected is not valid.')
}
if (!Object.values(BotTypesEnum).includes(clone.botType)) {
throw new WrongArgumentsError('The bot type selected is not valid.')
}
if ((clone.mode === LIVE || clone.mode === COMPETITION) && !isDefined(clone.keyId)) {
throw new WrongArgumentsError('The key was not provided to run the clone.')
}
let allUserBotsResponse = await teamQuery(context.authorization, clone.botId)
let bot = allUserBotsResponse.data.data.teams_FbByOwner.edges[0].node
clone = cloneDetails(bot, clone)
clone.createDatetime = new Date().valueOf() / 1000 | 0
clone.active = true
// TODO Temporary Limitations on bot creation
if (clone.accessCode !== undefined && clone.accessCode.length > 0) {
if (clone.accessCode === process.env.ACCESS_CODE) {
if (clone.baseAsset === 'assetB') {
clone.balanceAssetA = 0
clone.balanceAssetB = clone.balanceBaseAsset
} else if (clone.baseAsset === 'assetA') {
clone.balanceAssetA = clone.balanceBaseAsset
clone.balanceAssetB = 0
} else {
throw new Error('Invalid base asset.')
}
} else {
throw new Error('Invalid access code.')
}
} else {
if (clone.baseAsset === 'assetB') {
clone.balanceAssetA = 0
clone.balanceAssetB = Number(process.env.DEFAULT_BALANCE_ASSET_B)
} else if (clone.baseAsset === 'assetA') {
clone.balanceAssetA = Number(process.env.DEFAULT_BALANCE_ASSET_A)
clone.balanceAssetB = 0
} else {
throw new Error('Invalid base asset.')
}
}
if (!(clone.teamSlug === "AAMasters" || clone.teamSlug === "AAVikings")) {
let clones = await Clone.find({
authId: context.userId,
active: true
})
if (clones !== undefined && clones.length >= 5) {
throw new Error('Currently maximum number of clones per user is 5. You can delete an existing clone and try again.')
}
}
// END temporary limitations
try {
let newClone = new Clone(clone)
newClone.authId = context.userId
clone.id = newClone._id
logger.debug('addClone -> Creating the clone on the Database.')
let savedClone = await newClone.save()
if (clone.mode === LIVE || clone.mode === COMPETITION) {
logger.debug('addClone -> Authorizing clone to use the key.')
let keyResponse = await authorizeKey(context.authorization, clone.keyId, clone.id, false)
if (isDefined(keyResponse.data.data.keyVault_AuthorizeClone)) {
clone.accessTokenKey = keyResponse.data.data.keyVault_AuthorizeClone
} else {
logger.error('addClone -> Authorizing clone to use the key fail.')
throw new AuthorizationError()
}
}
await createKubernetesClone(clone)
if (clone.botType === Trading) {
let productCodeName
if (clone.mode === LIVE) {
productCodeName = 'Live Trading History'
}
if (clone.mode === COMPETITION) {
productCodeName = 'Competition Trading History'
}
if (clone.mode === BACKTEST) {
productCodeName = 'Backtest Trading History'
}
let newEcosystemBot = {
devTeam: clone.teamSlug,
codeName: clone.botSlug,
cloneId: clone.id,
productCodeName
}
await addBotMutation(context.authorization, newEcosystemBot)
}
logger.debug('addClone -> Clone created sucessfully.')
return savedClone
} catch (err) {
logger.error('addClone -> Error: %s', err.stack)
throw new OperationsError('There has been an error creating the clone.')
}
}
const AddCloneMutation = {
addClone: {
type: CloneType,
args,
resolve
}
}
export default AddCloneMutation
@@ -0,0 +1,77 @@
import { GraphQLNonNull, GraphQLID, GraphQLString } from 'graphql'
import { AuthenticationError, OperationsError } from '../../errors'
import { Clone } from '../../models'
import logger from '../../config/logger'
import removeKuberneteClone from '../../kubernetes/removeClone'
import teamQuery from '../../graphQLCalls/teamQuery'
import { isDefined } from '../../config/utils'
import cloneDetails from '../cloneDetails'
import authorizeKey from '../../graphQLCalls/authorizeKey'
import { LIVE, COMPETITION, BACKTEST } from '../../enums/CloneMode'
import { Trading } from '../../enums/BotTypes'
import removeBotMutation from '../../graphQLCalls/removeBotMutation'
const args = { id: { type: new GraphQLNonNull(GraphQLID) } }
const resolve = async (parent, { id }, context) => {
logger.debug('removeClone -> Entering Fuction.')
if (!context.userId) {
throw new AuthenticationError()
}
try {
let clone = await Clone.findOne({
_id: id,
authId: context.userId,
active: true
})
if (!isDefined(clone)) {
throw new OperationsError('You are not authorized to remove this clone.')
}
let allUserBotsResponse = await teamQuery(context.authorization, clone.botId)
let bot = allUserBotsResponse.data.data.teams_FbByOwner.edges[0].node
clone = cloneDetails(bot, clone)
if ((clone.mode === LIVE || clone.mode === COMPETITION) && isDefined(clone.keyId)) {
logger.debug('removeClone -> Release the clone key.')
await authorizeKey(context.authorization, clone.keyId, clone.id, true)
}
logger.debug('removeClone -> Removing Clone from Kubernates.')
await removeKuberneteClone(clone.id)
const query = {
_id: id,
authId: context.userId
}
const options = { new: true }
const update = { active: false }
return new Promise((res, rej) => {
Clone.findOneAndUpdate(query, update, options, (err, doc) => {
if (err) {
logger.error('removeClone -> Error removing clone from the DB. %s', err.stack)
rej(err)
return
}
logger.debug('removeClone -> Clone Removed from the DB.')
res('Clone Removed.')
})
})
} catch (error) {
logger.error('Error removing the clone. %s', error.stack)
throw new OperationsError(error.message)
}
}
const RemoveCloneMutation = {
removeClone: {
type: GraphQLString,
args,
resolve
}
}
export default RemoveCloneMutation
@@ -0,0 +1,140 @@
import { GraphQLString } from 'graphql'
import { SimulationInputType } from '../types/input'
import removeKuberneteClone from '../../kubernetes/removeClone'
import { Clone } from '../../models'
import { Indicator } from '../../enums/BotTypes'
import { DateTime } from 'luxon'
import teamQuery from '../../graphQLCalls/teamQuery'
import cloneDetails from '../cloneDetails'
import {
AuthenticationError,
OperationsError
} from '../../errors'
import logger from '../../config/logger'
import createKubernetesClone from '../../kubernetes/createClone'
const args = {
simulation: { type: SimulationInputType }
}
const resolve = async (parent, { simulation }, context) => {
logger.debug('runSimulation -> Entering Fuction.')
if (!context.userId) {
throw new AuthenticationError()
}
try {
// Find all running clones
let clones = await Clone.find({
authId: context.userId,
active: true
}).sort({ createDatetime: -1 })
// Find all user bots
let allUserBotsResponse = await teamQuery(context.authorization, null)
let allUserBots = allUserBotsResponse.data.data.teams_FbByOwner.edges
let simulatorBot
// Identify clones to stop, if any
for (var j = 0; j < allUserBots.length; j++) {
let bot = allUserBots[j].node
if (bot.kind === Indicator && bot.slug.substring(0, 10) === "simulator-") {
simulatorBot = bot // TODO One simulator per user
for (var i = 0; i < clones.length; i++) {
if (clones[i].botId === bot.id) {
if ((simulation.timePeriodDailyArray !== undefined && clones[i].processName === "Multi-Period-Daily") ||
(simulation.timePeriodMarketArray !== undefined && clones[i].processName === "Multi-Period-Market") ||
(simulation.timePeriodDailyArray === undefined && simulation.timePeriodMarketArray === undefined)) {
logger.debug('runSimulation -> Stop running clones.')
await removeKuberneteClone(clones[i]._id)
logger.debug('runSimulation -> Clone Removed from Kubernetes: %s.', clones[i]._id)
let clone = await Clone.findOneAndUpdate({ _id: clones[i]._id, authId: context.userId }, { active: false }, { new: true })
if (!clone) throw new Error('Clone Not Found.')
logger.debug('runSimulation -> Clone Removed from the DB.')
}
}
}
}
}
if (simulation.beginDatetime === undefined) {
simulation.beginDatetime = DateTime.utc().minus({ days: 7 }).startOf('day')
}
if (simulation.resumeExecution === undefined) {
simulation.resumeExecution = false
}
if (simulatorBot !== undefined) {
if (simulation.timePeriodDailyArray !== undefined && simulation.timePeriodDailyArray.length > 0) {
simulation.timePeriod = simulation.timePeriodDailyArray
await createSimulatorClone(simulation, "Multi-Period-Daily", context.userId, simulatorBot, context.authorization)
}
if (simulation.timePeriodMarketArray !== undefined && simulation.timePeriodMarketArray.length > 0) {
simulation.timePeriod = simulation.timePeriodMarketArray
await createSimulatorClone(simulation, "Multi-Period-Market", context.userId, simulatorBot, context.authorization)
}
if (simulation.timePeriodDailyArray === undefined && simulation.timePeriodMarketArray === undefined) {
simulation.timePeriod = ["15-min", "10-min", "05-min"]
await createSimulatorClone(simulation, "Multi-Period-Daily", context.userId, simulatorBot, context.authorization)
simulation.timePeriod = ["24-hs", "12-hs", "08-hs", "06-hs", "04-hs", "03-hs", "02-hs", "01-hs"]
await createSimulatorClone(simulation, "Multi-Period-Market", context.userId, simulatorBot, context.authorization)
}
return "Simulation Clones Recreated."
} else {
return "The user doesn't have any simulator bot."
}
} catch (err) {
logger.error('runSimulation -> Error: %s', err.stack)
throw new OperationsError('There has been an error creating the clones.')
}
}
async function createSimulatorClone(simulation, processName, userId, simulatorBot, authorization) {
let clone = {
teamId: simulatorBot.team.id,
botId: simulatorBot.id,
mode: "noTime",
kind: "Indicator",
botSlug: simulatorBot.slug,
resumeExecution: simulation.resumeExecution,
exchangeName: "Poloniex", // TODO pending enable multiple exchange
beginDatetime: simulation.beginDatetime,
processName: processName,
createDatetime: new Date().valueOf() / 1000 | 0,
active: true,
authId: userId,
timePeriod: simulation.timePeriod.toString(),
authorization: authorization
}
let newClone = new Clone(clone)
clone.id = newClone._id
logger.debug('runSimulation -> Creating %s clone on the Database.', processName)
await newClone.save()
clone = cloneDetails(simulatorBot, clone)
await createKubernetesClone(clone)
logger.debug('runSimulation -> %s clone created.', processName)
}
const RunSimulationMutation = {
runSimulation: {
type: GraphQLString,
args,
resolve
}
}
export default RunSimulationMutation
@@ -0,0 +1,91 @@
import {
GraphQLNonNull,
GraphQLID,
GraphQLString,
GraphQLInt,
GraphQLFloat
} from 'graphql'
import { OperationsError, AuthorizationError, AuthenticationError } from '../../errors'
import { Clone } from '../../models'
import logger from '../../config/logger'
import { isDefined } from '../../config/utils'
const args = {
id: { type: new GraphQLNonNull(GraphQLID) },
summaryDate: { type: GraphQLInt },
buyAverage: { type: GraphQLFloat },
sellAverage: { type: GraphQLFloat },
marketRate: { type: GraphQLFloat },
combinedProfitsA: { type: GraphQLFloat },
combinedProfitsB: { type: GraphQLFloat },
assetA: { type: GraphQLString },
assetB: { type: GraphQLString }
}
const resolve = async (parent,
{ id, summaryDate, buyAverage, sellAverage, marketRate, combinedProfitsA,
combinedProfitsB, assetA, assetB
}, context) => {
logger.debug('UpdateExecutionSummary -> Entering Function.')
if (!context.userId) {
throw new AuthenticationError()
}
if (!(context.userId === process.env.AACLOUD_ID)) {
logger.debug('User %s is not authorized to manage team %s.', authId, team.slug)
throw new AuthorizationError()
}
try {
let clone = await Clone.findOne({
_id: id,
active: true
})
if (!isDefined(clone)) {
return 'Clone not found.'
}
const query = { _id: id }
const options = { new: true }
const update = {
summaryDate: summaryDate,
buyAverage: buyAverage,
sellAverage: sellAverage,
marketRate: marketRate,
combinedProfitsA: combinedProfitsA,
combinedProfitsB: combinedProfitsB,
assetA: assetA,
assetB: assetB
}
logger.debug('UpdateExecutionSummary -> Updating clone on database.')
return new Promise((res, rej) => {
Clone.update(query, update, options, (err, clone) => {
if (err) {
logger.error('Error updating execution summary on the database. %s', err.stack)
rej(err)
return
}
logger.debug('Execution summary updated.')
res('Execution summary updated.')
})
})
} catch (error) {
logger.error('Error updating the execution summary. %s', error.stack)
throw new OperationsError(error.message)
}
}
const UpdateExecutionSummary = {
updateExecutionSummary: {
type: GraphQLString,
args,
resolve
}
}
export default UpdateExecutionSummary
@@ -0,0 +1,17 @@
import { GraphQLObjectType } from 'graphql'
import AddCloneMutation from './AddCloneMutation'
import RemoveCloneMutation from './RemoveCloneMutation'
import UpdateExecutionSummary from './UpdateExecutionSummary'
import RunSimulation from './RunSimulation'
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: Object.assign(
AddCloneMutation,
RemoveCloneMutation,
UpdateExecutionSummary,
RunSimulation
)
})
export default Mutation
@@ -0,0 +1,49 @@
import { GraphQLNonNull, GraphQLID } from 'graphql'
import { OperationsError } from '../../errors'
import { GetClonesType } from '../types'
import { Clone } from '../../models'
import logger from '../../config/logger'
import mongoose from 'mongoose'
import { isDefined } from '../../config/utils'
const args = {
cloneId: { type: new GraphQLNonNull(GraphQLID) }
}
const resolve = async(parent, { cloneId }, context) => {
logger.debug('Get Clone -> Entering Fuction.')
try {
if (mongoose.Types.ObjectId.isValid(cloneId)) {
let existingClone = await Clone.findOne({
_id: cloneId
})
if (isDefined(existingClone)) {
return {
id: cloneId,
teamId: existingClone.teamId,
botId: existingClone.botId
}
} else {
throw new Error('Clone not found.')
}
} else {
throw new Error('Invalid Clone ID.')
}
return clones
} catch (err) {
logger.error('Get Clone error: %s', err.stack)
throw new OperationsError('There has been an error getting the clone. ' + err.message)
}
}
const GetClones = {
getClone: {
type: GetClonesType,
args,
resolve
}
}
export default GetClones
@@ -0,0 +1,56 @@
import { GraphQLList, GraphQLID } from 'graphql'
import { OperationsError } from '../../errors'
import { GetClonesType } from '../types'
import { Clone } from '../../models'
import logger from '../../config/logger'
import mongoose from 'mongoose'
import { isDefined } from '../../config/utils'
const args = {
cloneIdList: { type: new GraphQLList(GraphQLID) }
}
const resolve = async(parent, { cloneIdList }, context) => {
logger.debug('Get Clones -> Entering Fuction.')
try {
let clones = []
for (var i = 0; i < cloneIdList.length; i++) {
if (mongoose.Types.ObjectId.isValid(cloneIdList[i])) {
let existingClone = await Clone.findOne({
_id: cloneIdList[i]
})
if (isDefined(existingClone)) {
clones.push({
id: cloneIdList[i],
teamId: existingClone.teamId,
botId: existingClone.botId
})
} else {
clones.push({
id: cloneIdList[i]
})
}
} else {
clones.push({
id: cloneIdList[i]
})
}
}
return clones
} catch (err) {
logger.error('Get Clones error: %s', err.stack)
throw new OperationsError('There has been an error getting the clones.')
}
}
const GetClones = {
getClones: {
type: new GraphQLList(GetClonesType),
args,
resolve
}
}
export default GetClones
@@ -0,0 +1,60 @@
import { GraphQLList, GraphQLString } from 'graphql'
import { AuthenticationError, OperationsError } from '../../errors'
import { CloneType } from '../types'
import { Clone } from '../../models'
import logger from '../../config/logger'
import cloneDetails from '../cloneDetails'
import teamQuery from '../../graphQLCalls/teamQuery'
const args = {
botType: { type: GraphQLString }
}
const resolve = async(parent, { botType }, context) => {
logger.debug('Retrieving History Clones.')
try {
if (!context.userId) {
throw new AuthenticationError()
}
let clones = await Clone.find({
authId: context.userId,
active: false
}).sort({createDatetime: -1})
let cloneResponse = []
let allUserBotsResponse = await teamQuery(context.authorization, null)
let allUserBots = allUserBotsResponse.data.data.teams_FbByOwner.edges
for (var i = 0; i < clones.length; i++) {
for (var j = 0; j < allUserBots.length; j++) {
let bot = allUserBots[j].node
if(clones[i].botId === bot.id){
let clone = cloneDetails(bot, clones[i])
cloneResponse.push(clone)
break
}
}
}
if (botType) {
var filtered = cloneResponse.filter((value) => value.botType === botType)
return filtered
} else {
return cloneResponse
}
} catch (err) {
logger.error('Retrieving History Clones error: %s', err.stack)
throw new OperationsError('There has been an error retrieving history clones.')
}
}
const HistoryClones = {
historyClones: {
type: new GraphQLList(CloneType),
args,
resolve
}
}
export default HistoryClones
@@ -0,0 +1,72 @@
import { GraphQLList, GraphQLString, GraphQLBoolean } from 'graphql'
import { AuthenticationError, OperationsError } from '../../errors'
import { CloneType } from '../types'
import { Clone } from '../../models'
import logger from '../../config/logger'
import getCloneStatus from '../../kubernetes/getClonePodStatus'
import getCloneLogs from '../../kubernetes/getClonePodLogs'
import cloneDetails from '../cloneDetails'
import teamQuery from '../../graphQLCalls/teamQuery'
const args = {
botType: { type: GraphQLString },
queryLogs: { type: GraphQLBoolean }
}
const resolve = async (parent, { botType, queryLogs }, context) => {
logger.debug('List Clones -> Entering Fuction.')
try {
if (!context.userId) {
throw new AuthenticationError()
}
let clones = await Clone.find({
authId: context.userId,
active: true
}).sort({ createDatetime: -1 })
let cloneResponse = []
let allUserBotsResponse = await teamQuery(context.authorization, null)
let allUserBots = allUserBotsResponse.data.data.teams_FbByOwner.edges
for (var i = 0; i < clones.length; i++) {
for (var j = 0; j < allUserBots.length; j++) {
let bot = allUserBots[j].node
if(clones[i].botId === bot.id){
let clone = cloneDetails(bot, clones[i])
cloneResponse.push(clone)
break
}
}
}
if (queryLogs) {
for (var i = 0; i < cloneResponse.length; i++) {
let state = await getCloneStatus(cloneResponse[i].id)
cloneResponse[i].state = state.substring(1, state.length - 1)
let lastLogs = await getCloneLogs(cloneResponse[i].id)
cloneResponse[i].lastLogs = lastLogs.substring(1, lastLogs.length - 1)
}
}
if (botType) {
var filtered = cloneResponse.filter((value) => value.botType === botType)
return filtered
} else {
return cloneResponse
}
} catch (err) {
logger.error('List Clones error: %s', err.stack)
throw new OperationsError('There has been an error listing the clones.')
}
}
const ListClones = {
clones: {
type: new GraphQLList(CloneType),
args,
resolve
}
}
export default ListClones
@@ -0,0 +1,17 @@
import { GraphQLObjectType } from 'graphql'
import ListClones from './ListClones'
import HistoryClones from './HistoryClones'
import GetClones from './GetClones'
import GetClone from './GetClone'
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: Object.assign(
ListClones,
HistoryClones,
GetClones,
GetClone
)
})
export default RootQuery
@@ -0,0 +1,48 @@
import {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLBoolean,
GraphQLID,
GraphQLFloat
} from 'graphql'
const CloneType = new GraphQLObjectType({
name: 'Clone',
fields: () => ({
id: {type: GraphQLID},
authId: {type: GraphQLID},
teamId: {type: GraphQLString},
botId: {type: GraphQLString},
mode: {type: GraphQLString},
resumeExecution: {type: GraphQLBoolean},
beginDatetime: {type: GraphQLInt},
endDatetime: {type: GraphQLInt},
waitTime: {type: GraphQLInt},
state: {type: GraphQLString},
stateDatetime: {type: GraphQLInt},
createDatetime: {type: GraphQLInt},
lastLogs: {type: GraphQLString},
processName: { type: GraphQLString },
summaryDate: {type: GraphQLInt},
buyAverage: {type: GraphQLFloat},
sellAverage: {type: GraphQLFloat},
marketRate: {type: GraphQLFloat},
combinedProfitsA: {type: GraphQLFloat},
combinedProfitsB: {type: GraphQLFloat},
assetA: {type: GraphQLString},
assetB: {type: GraphQLString},
botType: {type: GraphQLString},
teamName: {type: GraphQLString},
botName: {type: GraphQLString},
botAvatar: {type: GraphQLString},
teamAvatar: {type: GraphQLString},
keyId: {type: GraphQLString},
botSlug: {type: GraphQLString},
exchangeName:{type: GraphQLString},
timePeriod:{type: GraphQLString},
balanceAssetA:{type: GraphQLFloat},
balanceAssetB:{type: GraphQLFloat},
})
})
export default CloneType
@@ -0,0 +1,11 @@
import { GraphQLObjectType, GraphQLID } from 'graphql'
const GetClonesType = new GraphQLObjectType({
name: 'GetClones',
fields: () => ({
id: {type: GraphQLID},
teamId: {type: GraphQLID},
botId: {type: GraphQLID}
})
})
export default GetClonesType
@@ -0,0 +1,7 @@
import CloneType from './CloneType'
import GetClonesType from './GetClonesType'
export {
CloneType,
GetClonesType
}
@@ -0,0 +1,37 @@
import {
GraphQLString,
GraphQLInt,
GraphQLInputObjectType,
GraphQLBoolean,
GraphQLNonNull,
GraphQLFloat
} from 'graphql'
const Type = new GraphQLInputObjectType({
name: 'CloneInput',
description: 'Payload for clone input',
fields: () => ({
teamId: { type: new GraphQLNonNull(GraphQLString) },
botId: { type: new GraphQLNonNull(GraphQLString) },
mode: { type: GraphQLString },
resumeExecution: { type: GraphQLBoolean },
beginDatetime: { type: GraphQLInt },
endDatetime: { type: GraphQLInt },
waitTime: { type: GraphQLInt },
startYear: { type: GraphQLInt },
endYear: { type: GraphQLInt },
month: { type: GraphQLInt },
interval: { type: GraphQLInt },
processName: { type: new GraphQLNonNull(GraphQLString) },
botType: { type: new GraphQLNonNull(GraphQLString) },
keyId: { type: GraphQLString },
timePeriod: { type: GraphQLString },
exchangeName: { type: GraphQLString },
balanceBaseAsset: { type: GraphQLFloat },
market: { type: GraphQLString },
baseAsset: { type: GraphQLString },
accessCode: { type: GraphQLString }
})
})
export default Type
@@ -0,0 +1,20 @@
import {
GraphQLString,
GraphQLInt,
GraphQLInputObjectType,
GraphQLNonNull,
GraphQLList,
GraphQLBoolean
} from 'graphql'
const Type = new GraphQLInputObjectType({
name: 'SimulationInput',
fields: () => ({
beginDatetime: { type: new GraphQLNonNull(GraphQLInt) },
timePeriodDailyArray: { type: new GraphQLList(new GraphQLNonNull(GraphQLString)) },
timePeriodMarketArray: { type: new GraphQLList(new GraphQLNonNull(GraphQLString)) },
resumeExecution: { type: GraphQLBoolean }
})
})
export default Type
@@ -0,0 +1,7 @@
import CloneInputType from './Clone'
import SimulationInputType from './Simulation'
export {
CloneInputType,
SimulationInputType
}