Merge Complete
This commit is contained in:
1 parent
482ef7a81a
commit
dabe23d505
6483 files changed
No files matched your search
@@ -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
|
||||
+91
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user