Adding base version for Operations Module

This commit is contained in:
mati108@gmail.com committed 2018-12-26 19:41:13 -03:00
1 parent ae08794750
commit d397c87d68
64 files changed
+3524 -1

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
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,125 @@
import { CloneInputType } from '../types/input';
import { CloneType } from '../types';
import { Clone } from '../../models';
import { CloneModeEnum , BACKTEST} from '../../enums/CloneMode';
import {
AuthentificationError,
WrongArgumentsError,
OperationsError,
CustomError
} from '../../errors'
import axios from 'axios';
import logger from '../../config/logger'
import createKubernetesClone from '../../kubernetes/createClone'
import { isDefined } from '../../config/utils'
const args = {
clone: {type: CloneInputType }
}
const resolve = async(parent, { clone }, context) => {
logger.debug('addClone -> Entering Fuction.')
if (!context.userId) {
throw new AuthentificationError()
}
if(!Object.values(CloneModeEnum).includes(clone.mode)){
throw new WrongArgumentsError('The mode selected is not valid.')
}
// Disable execution dates if mode is not backtest
if((clone.hasOwnProperty('beginDatetime')
|| clone.hasOwnProperty('endDatetime'))
&& clone.mode !== BACKTEST){
throw new WrongArgumentsError('Ony Backtest mode allows begin and end date selection.')
}
let existingClone = await Clone.find(
{
authId: context.userId,
mode: clone.mode
})
logger.debug('addClone -> Checking existing clone %j', existingClone)
if(isDefined(existingClone) && existingClone.length > 0){
throw new CustomError('You can only create one clone per mode. Remove the'
+ ' existing clone of type: ' + clone.mode + ' and try again.')
}
logger.debug('addClone -> Creating a new clone. %j', clone)
try{
const userTeam = await axios({
url: process.env.GATEWAY_ENDPOINT,
method: 'post',
data: {
query: `
query Teams_FbByTeamMember {
teams_FbByTeamMember {
id
name
slug
fb {
id
name
slug
}
members {
member {
alias
}
}
}
}
`,
},
headers: {
authorization: context.authorization,
preshared: process.env.OPERATIONS_API_PRESHARED
}
})
clone.teamId = userTeam.data.data.teams_FbByTeamMember.slug
clone.botId = userTeam.data.data.teams_FbByTeamMember.fb[0].slug
clone.userLoggedIn = userTeam.data.data.teams_FbByTeamMember.members[0].member.alias
clone.createDatetime = new Date().valueOf() / 1000|0
let newClone = new Clone(clone)
await createKubernetesClone(clone, newClone._id)
newClone.id = newClone._id
newClone.authId = context.userId
return new Promise((resolve, reject) => {
newClone.save((err) => {
if (err){
logger.debug('addClone -> Error: %j', err)
reject('There has been an error storing the clone.')
}
else {
//TODO transaction on database
// saveAuditLog(newClone.id, 'addKey', context)
logger.debug('addClone -> Save clone sucessful.')
resolve(newClone)
}
})
})
} catch(error) {
logger.debug('addClone -> Error: %j', error)
throw new OperationsError('There has been an error creating the clone.')
}
}
const mutation = {
addClone: {
type: CloneType,
args,
resolve
}
}
export default mutation
@@ -0,0 +1,54 @@
import {
GraphQLNonNull,
GraphQLID,
GraphQLString
} from 'graphql'
import {
AuthentificationError,
OperationsError
} from '../../errors'
import { Clone } from '../../models'
import logger from '../../config/logger'
import removeKuberneteClone from '../../kubernetes/removeClone'
//import saveAuditLog from './AddAuditLog'
const args = {
id: {type: new GraphQLNonNull(GraphQLID)}
}
const resolve = async (parent, { id }, context) => {
logger.debug('removeClone -> Entering Fuction.')
if (!context.userId) {
throw new AuthentificationError()
}
await removeKuberneteClone(id)
var query = {
_id: id,
authId: context.userId
}
//saveAuditLog(id, 'removeClone', context)
logger.debug('removeClone -> Removing Clone from DB.')
Clone.deleteOne(query, function (err) {
if (err){
throw new OperationsError(err)
}else{
return 'Clone Removed'
}
})
}
const mutation = {
removeClone: {
type: GraphQLString,
args,
resolve
}
}
export default mutation
+16
View File
@@ -0,0 +1,16 @@
import {
GraphQLObjectType
} from 'graphql'
import AddCloneMutation from './AddCloneMutation'
import RemoveCloneMutation from './RemoveCloneMutation'
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: Object.assign(
AddCloneMutation,
RemoveCloneMutation
)
})
export default Mutation
@@ -0,0 +1,40 @@
import {
GraphQLList,
GraphQLString,
GraphQLNonNull
} from 'graphql'
import {
AuthentificationError
} from '../../errors'
import { AuditLogType } from '../types'
import { AuditLog } from '../../models'
import logger from '../../config/logger'
const args = {
key: { type: new GraphQLNonNull(GraphQLString) }
}
const resolve = (parent, { key }, context) => {
logger.debug('auditLogs -> Entering Fuction.')
if (!context.userId) {
throw new AuthentificationError()
}
return AuditLog.find({
authId: context.userId,
keyId: key
}).sort({date: -1}).limit(20)
}
const query = {
auditLogs: {
type: new GraphQLList(AuditLogType),
args,
resolve
}
}
export default query
@@ -0,0 +1,23 @@
import {
GraphQLList
} from 'graphql'
import { ExchangeType } from '../types'
import { Exchange } from '../../models'
import logger from '../../config/logger'
const args = {}
const resolve = (parent, args, context) => {
logger.debug('exchanges -> Entering Fuction.')
return Exchange
}
const query = {
exchanges: {
type: new GraphQLList(ExchangeType),
args,
resolve
}
}
export default query
+41
View File
@@ -0,0 +1,41 @@
import {
GraphQLList
} from 'graphql'
import {
AuthentificationError
} from '../../errors'
import { CloneType } from '../types'
import { Clone } from '../../models'
import logger from '../../config/logger'
import getKuberneteClonePodStatus from '../../kubernetes/getClonePodStatus'
import getKuberneteClonePodLogs from '../../kubernetes/getClonePodLogs'
const args = {}
const resolve = async(parent, args, context) => {
logger.debug('List Clones -> Entering Fuction.')
if (!context.userId) {
throw new AuthentificationError()
}
let clones = await Clone.find({authId: context.userId})
for (var i = 0; i < clones.length; i++) {
clones[i].state = await getKuberneteClonePodStatus(clones[i]._id)
clones[i].lastLogs = await getKuberneteClonePodLogs(clones[i]._id)
}
return clones
}
const query = {
clones: {
type: new GraphQLList(CloneType),
args,
resolve
}
}
export default query
+14
View File
@@ -0,0 +1,14 @@
import {
GraphQLObjectType
} from 'graphql'
import ListClones from './ListClones'
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: Object.assign(
ListClones
)
})
export default RootQuery
+29
View File
@@ -0,0 +1,29 @@
import {
GraphQLObjectType,
GraphQLID,
GraphQLString,
GraphQLInt
} from 'graphql'
import {
KeyType
} from './index'
const AuditLogType = new GraphQLObjectType({
name: 'AuditLog',
fields: () => ({
id: {type: GraphQLID},
authId: {type: GraphQLID},
action: {type: GraphQLString},
details: {type: GraphQLString},
date: {type: GraphQLInt},
key: {
type: KeyType,
resolve (parent, args) {
return parent.key
}
}
})
})
export default AuditLogType
+28
View File
@@ -0,0 +1,28 @@
import {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLBoolean,
GraphQLID
} 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},
runAsTeam: {type: GraphQLBoolean}
})
})
export default CloneType
+16
View File
@@ -0,0 +1,16 @@
import {
GraphQLObjectType,
GraphQLID,
GraphQLString
} from 'graphql'
const ExchangeType = new GraphQLObjectType({
name: 'Exchange',
fields: () => ({
id: {type: GraphQLID},
name: {type: GraphQLString},
description: {type: GraphQLString}
})
})
export default ExchangeType
@@ -0,0 +1,15 @@
import {
GraphQLObjectType,
GraphQLString
} from 'graphql'
const SignedTransactionType = new GraphQLObjectType({
name: 'SignedTransaction',
fields: () => ({
key: {type: GraphQLString},
signature: {type: GraphQLString},
date: {type: GraphQLString}
})
})
export default SignedTransactionType
+5
View File
@@ -0,0 +1,5 @@
import CloneType from './CloneType'
export {
CloneType
}
+24
View File
@@ -0,0 +1,24 @@
import {
GraphQLString,
GraphQLInt,
GraphQLInputObjectType,
GraphQLBoolean,
GraphQLNonNull
} 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 },
runAsTeam: { type: GraphQLBoolean }
})
});
export default Type;
+5
View File
@@ -0,0 +1,5 @@
import CloneInputType from './Clone';
export {
CloneInputType
};