<commit_before>import { ApolloServer, gql } from "apollo-server";

import { findAll, Kernel, launchKernel } from "@nteract/fs-kernels";

import { JupyterMessage, kernelInfoRequest } from "@nteract/messaging";

import GraphQLJSON from "graphql-type-json";

const Types = gql`
  scalar JSON

  type KernelSpec {
    id: ID!
    name: String
  }

  type KernelSession {
    id: ID!
    status: String
  }

  type Message {
    id: ID!
    payload: JSON
  }
`;

const Query = gql`
  type Query {
    listKernelSpecs: [KernelSpec!]!
    running: [KernelSession!]!
    messages: [Message!]!
  }
`;

const Mutation = gql`
  type Mutation {
    startKernel(name: String): KernelSession!
  }
`;

interface StartKernel {
  name: string;
}

const kernels: { [id: string]: Kernel } = {};

const messages: {
  [kernelId: string]: JupyterMessage[];
} = {};

const typeDefs = [Types, Query, Mutation];
const resolvers = {
  JSON: GraphQLJSON,
  Mutation: {
    startKernel: async (parentValue: any, args: StartKernel) => {
      const kernel = await launchKernel(args.name);

      console.log("kernel launched", kernel);

      // NOTE: we should generate IDs
      // We're also setting a session ID within the enchannel-zmq setup, I wonder
      // if we should use that
      const id = kernel.connectionInfo.key;

      messages[id] = [];

      const subscription = kernel.channels.subscribe(
        (message: JupyterMessage) => {
          messages[id].push(message);
          console.log(message);
        }
      );

      const request = kernelInfoRequest();
      messages[id].push(request);
      kernel.channels.next(request);

      // NOTE: We are going to want to both:
      //
      //   subscription.unsubscribe()
      //   AND
      //   kernel.channels.complete()
      //
      //   Within our cleanup code

      kernels[kernel.connectionInfo.key] = kernel;
      return {
        id,
        status: "launched"
      };
    }
  },
  Query: {
    listKernelSpecs: async () => {
      const kernelspecs = await findAll();

      return Object.keys(kernelspecs).map(key => {
        return {
          id: key,
          ...kernelspecs[key]
        };
      });
    },
    messages: () => {
      return ([] as Array<JupyterMessage>)
        .concat(...Object.values(messages))
        .map(message => ({ id: message.header.msg_id, payload: message }));
    },
    running: () => {
      return Object.keys(kernels).map(id => ({ id, status: "pretend" }));
    }
  }
};<commit_msg>provide lightly better logging and organization<commit_after>import { ApolloServer, gql } from "apollo-server";

import { findAll, Kernel, launchKernel } from "@nteract/fs-kernels";

import { JupyterMessage, kernelInfoRequest } from "@nteract/messaging";

import GraphQLJSON from "graphql-type-json";

const Types = gql`
  scalar JSON

  type KernelSpec {
    id: ID!
    name: String
  }

  type KernelSession {
    id: ID!
    status: String
  }

  type Message {
    id: ID!
    payload: JSON
  }
`;

const Query = gql`
  type Query {
    listKernelSpecs: [KernelSpec!]!
    running: [KernelSession!]!
    messages: [Message!]!
  }
`;

const Mutation = gql`
  type Mutation {
    startKernel(name: String): KernelSession!
  }
`;

interface StartKernel {
  name: string;
}

const kernels: { [id: string]: Kernel } = {};

const messages: {
  [kernelId: string]: JupyterMessage[];
} = {};

const typeDefs = [Types, Query, Mutation];
const resolvers = {
  JSON: GraphQLJSON,
  Mutation: {
    startKernel: async (parentValue: any, args: StartKernel) => {
      const kernel = await launchKernel(args.name);

      console.log(`kernel ${args.name}:${kernel.connectionInfo.key} launched`);

      // NOTE: we should generate IDs
      // We're also setting a session ID within the enchannel-zmq setup, I wonder
      // if we should use that
      const id = kernel.connectionInfo.key;

      messages[id] = [];
      kernels[id] = kernel;

      const subscription = kernel.channels.subscribe(
        (message: JupyterMessage) => {
          messages[id].push(message);
        }
      );

      const request = kernelInfoRequest();
      messages[id].push(request);
      kernel.channels.next(request);

      // NOTE: We are going to want to both:
      //
      //   subscription.unsubscribe()
      //   AND
      //   kernel.channels.complete()
      //
      //   Within our cleanup code

      return {
        id,
        status: "launched"
      };
    }
  },
  Query: {
    listKernelSpecs: async () => {
      const kernelspecs = await findAll();

      return Object.keys(kernelspecs).map(key => {
        return {
          id: key,
          ...kernelspecs[key]
        };
      });
    },
    messages: () => {
      return ([] as Array<JupyterMessage>)
        .concat(...Object.values(messages))
        .map(message => ({ id: message.header.msg_id, payload: message }));
    },
    running: () => {
      return Object.keys(kernels).map(id => ({ id, status: "pretend" }));
    }
  }
};
{'fraction_non_alphanumeric': 0.13804848727709879, 'fraction_numerical': 0.0, 'mean_word_length': 2.3936097892590076, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7229508', 'n_tokens_mistral': 1597, 'n_tokens_neox': 1560, 'n_words': 378}