Improving clone listing
This commit is contained in:
1 parent
c0f64af15e
commit
a913686e04
7 files changed
+91
-89
No files matched your search
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@superalgos/operations-client",
|
||||
"version": "0.0.24",
|
||||
"version": "0.0.25",
|
||||
"description": "Superalgos Operations client-side web app",
|
||||
"main": "src/index.js",
|
||||
"dependencies": {
|
||||
|
||||
@@ -12,7 +12,7 @@ const OPERATIONS_ADD_CLONE = gql`
|
||||
|
||||
const OPERATIONS_LIST_CLONES = gql`
|
||||
query Operations_Clones{
|
||||
operations_Clones {
|
||||
operations_Clones(queryLogs: true) {
|
||||
...clone
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import logger from '../config/logger'
|
||||
import axios from 'axios'
|
||||
|
||||
const teamQuery = (authorization, teamId) => {
|
||||
const teamQuery = (authorization, botId) => {
|
||||
logger.debug('teamQuery -> Entering function')
|
||||
|
||||
return axios({
|
||||
@@ -9,32 +9,35 @@ const teamQuery = (authorization, teamId) => {
|
||||
method: 'post',
|
||||
data: {
|
||||
query: `
|
||||
query Teams_Team($teamId: String!){
|
||||
teams_TeamById(teamId:$teamId) {
|
||||
id
|
||||
name
|
||||
slug
|
||||
fb {
|
||||
id
|
||||
name
|
||||
slug
|
||||
kind
|
||||
avatar
|
||||
}
|
||||
members {
|
||||
member {
|
||||
alias
|
||||
authId
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
profile{
|
||||
avatar
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: {
|
||||
teamId: teamId
|
||||
fbId: botId
|
||||
}
|
||||
},
|
||||
headers: {
|
||||
|
||||
@@ -4,7 +4,7 @@ import { Client, config } from 'kubernetes-client'
|
||||
|
||||
const getClonePodStatus = async (cloneName) => {
|
||||
try {
|
||||
logger.debug('getClonePodStatus on kubernates started. ')
|
||||
logger.debug('getClonePodStatus on kubernates started.')
|
||||
const client = new Client({ config: config.fromKubeconfig(), version: '1.9' })
|
||||
|
||||
let query = {
|
||||
@@ -26,7 +26,7 @@ const getClonePodStatus = async (cloneName) => {
|
||||
|
||||
return JSON.stringify(clonePodStatus, null, 2)
|
||||
} else {
|
||||
logger.warn('getClonePodStatus pod not found %s: ', cloneName)
|
||||
logger.warn('getClonePodStatus pod not found: %s', cloneName)
|
||||
return " Clone not found on the server. "
|
||||
}
|
||||
} catch (err) {
|
||||
|
||||
@@ -1,54 +1,28 @@
|
||||
import logger from '../config/logger'
|
||||
import { isDefined } from '../config/utils'
|
||||
import { AutorizationError } from '../errors'
|
||||
|
||||
const cloneDetails = (authId, team, clone) => {
|
||||
const cloneDetails = (authId, selectedBot, clone) => {
|
||||
logger.debug('cloneDetails -> Entering function.')
|
||||
|
||||
if (!(authId === (process.env.AUTH_CLIENT_ID + '@clients') || isAuthorized(team, authId))) {
|
||||
logger.debug('User %s is not authorized to manage team %s.', authId, team.slug)
|
||||
throw new AutorizationError()
|
||||
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.userLoggedIn = selectedBot.team.members[0].member.alias // TODO change by context info if possible
|
||||
|
||||
clone.botType = selectedBot.kind // Only for listing we show the teams value
|
||||
|
||||
logger.debug('cloneDetails -> Retrieving clone details complete.')
|
||||
}
|
||||
|
||||
let selectedBot = getSelectedBot(team, clone.botId)
|
||||
clone.teamId = team.id
|
||||
clone.botId = selectedBot.id
|
||||
clone.kind = selectedBot.kind
|
||||
clone.teamSlug = team.slug
|
||||
clone.botSlug = selectedBot.slug
|
||||
clone.teamName = team.name
|
||||
clone.botName = selectedBot.name
|
||||
clone.botAvatar = selectedBot.avatar
|
||||
clone.teamAvatar = team.profile.avatar
|
||||
clone.userLoggedIn = team.members[0].member.alias // TODO change by context info if possible
|
||||
|
||||
logger.debug('cloneDetails -> Retrieving clone details complete.')
|
||||
return clone
|
||||
}
|
||||
|
||||
function getSelectedBot (team, selectedBotId) {
|
||||
logger.debug('cloneDetails -> Getting selected bot.')
|
||||
if (isDefined(team)) {
|
||||
for (var i = 0; i < team.fb.length; i++) {
|
||||
if (team.fb[i].id === selectedBotId) {
|
||||
return team.fb[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isAuthorized (team, authId) {
|
||||
logger.debug('cloneDetails -> Checking if the user is authorized by the teams module.')
|
||||
let authorized = false
|
||||
if (isDefined(team)) {
|
||||
for (var i = 0; i < team.members.length; i++) {
|
||||
if (team.members[i].member.authId === authId) {
|
||||
authorized = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return authorized
|
||||
}
|
||||
|
||||
export default cloneDetails
|
||||
@@ -22,16 +22,26 @@ const resolve = async(parent, { botType }, context) => {
|
||||
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++) {
|
||||
let team = await teamQuery(context.authorization, clones[i].teamId)
|
||||
clones[i] = await cloneDetails(context.userId, team.data.data.teams_TeamById, clones[i])
|
||||
clones[i].botType = clones[i].kind // Only for listing we show the teams value
|
||||
for (var j = 0; j < allUserBots.length; j++) {
|
||||
let bot = allUserBots[j].node
|
||||
if(clones[i].botId === bot.id){
|
||||
let clone = cloneDetails(context.userId, bot, clones[i])
|
||||
cloneResponse.push(clone)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (botType) {
|
||||
var filtered = clones.filter((value) => value.botType === botType)
|
||||
var filtered = cloneResponse.filter((value) => value.botType === botType)
|
||||
return filtered
|
||||
} else {
|
||||
return clones
|
||||
return cloneResponse
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error('Retrieving History Clones error: %s', err.stack)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { GraphQLList, GraphQLString } from 'graphql'
|
||||
import { GraphQLList, GraphQLString, GraphQLBoolean } from 'graphql'
|
||||
import { AuthentificationError, OperationsError } from '../../errors'
|
||||
import { CloneType } from '../types'
|
||||
import { Clone } from '../../models'
|
||||
@@ -9,10 +9,11 @@ import cloneDetails from '../cloneDetails'
|
||||
import teamQuery from '../../graphQLCalls/teamQuery'
|
||||
|
||||
const args = {
|
||||
botType: { type: GraphQLString }
|
||||
botType: { type: GraphQLString },
|
||||
queryLogs: { type: GraphQLBoolean }
|
||||
}
|
||||
|
||||
const resolve = async (parent, { botType }, context) => {
|
||||
const resolve = async (parent, { botType, queryLogs }, context) => {
|
||||
logger.debug('List Clones -> Entering Fuction.')
|
||||
try {
|
||||
if (!context.userId) {
|
||||
@@ -22,23 +23,37 @@ const resolve = async (parent, { botType }, context) => {
|
||||
let clones = await Clone.find({
|
||||
authId: context.userId,
|
||||
active: true
|
||||
}).sort({createDatetime: -1})
|
||||
}).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++) {
|
||||
let team = await teamQuery(context.authorization, clones[i].teamId)
|
||||
clones[i] = await cloneDetails(context.userId, team.data.data.teams_TeamById, clones[i])
|
||||
let state = await getCloneStatus(clones[i].id)
|
||||
clones[i].state = state.substring(1, state.length - 1)
|
||||
let lastLogs = await getCloneLogs(clones[i].id)
|
||||
clones[i].lastLogs = lastLogs.substring(1, lastLogs.length - 1)
|
||||
clones[i].botType = clones[i].kind // Only for listing we show the teams value
|
||||
clones[i].botSlug = clones[i].botSlug
|
||||
for (var j = 0; j < allUserBots.length; j++) {
|
||||
let bot = allUserBots[j].node
|
||||
if(clones[i].botId === bot.id){
|
||||
let clone = cloneDetails(context.userId, 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 = clones.filter((value) => value.botType === botType)
|
||||
var filtered = cloneResponse.filter((value) => value.botType === botType)
|
||||
return filtered
|
||||
} else {
|
||||
return clones
|
||||
return cloneResponse
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error('List Clones error: %s', err.stack)
|
||||
|
||||
Reference in new issue
Block a user