/**
   * Get Mondrian Stats
   *
   * @summary Get Mondrian stats
   * @return A selection of Mondrian stats.
   */
  @GET
  @Produces({"application/json"})
  @Path("/mondrian")
  public MondrianStats getMondrianStats() {

    MondrianServer mondrianServer = MondrianServer.forId(null);
    if (mondrianServer != null) {
      MondrianVersion mv = mondrianServer.getVersion();

      final Monitor monitor = mondrianServer.getMonitor();
      final ServerInfo server = monitor.getServer();

      int statementCurrentlyOpenCount = 0; // server.statementCurrentlyOpenCount();
      int connectionCurrentlyOpenCount = 0; // server.connectionCurrentlyOpenCount();
      int sqlStatementCurrentlyOpenCount = 0; // server.sqlStatementCurrentlyOpenCount();
      int statementCurrentlyExecutingCount = 0; // server.statementCurrentlyExecutingCount();
      float avgCellDimensionality = ((float) server.cellCoordinateCount / (float) server.cellCount);

      final List<ConnectionInfo> connections = monitor.getConnections();
      final List<StatementInfo> statements = monitor.getStatements();

      return new MondrianStats(
          server,
          mv,
          statementCurrentlyOpenCount,
          connectionCurrentlyOpenCount,
          sqlStatementCurrentlyOpenCount,
          statementCurrentlyExecutingCount,
          avgCellDimensionality,
          connections,
          statements);
    }

    return null;
  }
 /** Tells the cache that a segment is no longer available in an external cache. */
 public void externalSegmentDeleted(SegmentHeader header, MondrianServer server) {
   ACTOR.event(
       handler,
       new ExternalSegmentDeletedEvent(
           System.currentTimeMillis(),
           server.getMonitor(),
           server.getId(),
           0,
           0,
           0,
           this,
           header));
 }
  /**
   * Get Mondrian Server Info
   *
   * @summary Get Mondrian Info
   * @return Server Info
   */
  @GET
  @Produces({"application/json"})
  @Path("/mondrian/server")
  public ServerInfo getMondrianServer() {
    MondrianServer mondrianServer = MondrianServer.forId(null);
    if (mondrianServer != null) {
      MondrianVersion mv = mondrianServer.getVersion();

      final Monitor monitor = mondrianServer.getMonitor();
      final ServerInfo server = monitor.getServer();
      return server;
    }
    return null;
  }
  /**
   * Exercises as many fields of the monitoring stats classes as possible. So that we can check that
   * they are being populated.
   */
  public void testMe() throws SQLException {
    String queryString =
        "WITH MEMBER [Measures].[Foo] AS\n"
            + " [Measures].[Unit Sales]"
            + " + case when [Measures].[Unit Sales] > 0\n"
            + "   then CInt( ([Measures].[Foo], [Time].PrevMember) )\n"
            + "   end\n"
            + "SELECT [Measures].[Foo] on 0\n"
            + "from [Sales]\n"
            + "where [Time].[1997].[Q3].[9]";

    final OlapStatement statement1 = getTestContext().getOlap4jConnection().createStatement();
    CellSet cellSet = statement1.executeOlapQuery(queryString);
    StringWriter stringWriter = new StringWriter();
    new RectangularCellSetFormatter(true).format(cellSet, new PrintWriter(stringWriter));
    statement1.close();
    println(stringWriter);

    final MondrianServer mondrianServer = MondrianServer.forConnection(getConnection());
    final Monitor monitor = mondrianServer.getMonitor();
    final ServerInfo server = monitor.getServer();

    println("# stmts open: " + server.statementCurrentlyOpenCount());

    println("# connections open: " + server.connectionCurrentlyOpenCount());

    println("# rows fetched: " + server.sqlStatementRowFetchCount);

    println("# sql stmts open: " + server.sqlStatementCurrentlyOpenCount());

    // # sql stmts by category (cell query, member query, other)
    //  -- if you want to do this, capture sql statement events

    // cell cache requests
    // cell cache misses
    // cell cache hits
    final List<ConnectionInfo> connections = monitor.getConnections();
    ConnectionInfo lastConnection = connections.get(connections.size() - 1);
    final List<StatementInfo> statements = monitor.getStatements();
    StatementInfo lastStatement = statements.get(statements.size() - 1);
    println(
        "# cell cache requests, misses, hits; "
            + "by server, connection, mdx statement: "
            + server.cellCacheRequestCount
            + ", "
            + server.cellCacheMissCount()
            + ", "
            + server.cellCacheHitCount
            + "; "
            + lastConnection.cellCacheRequestCount
            + ", "
            + (lastConnection.cellCacheRequestCount - lastConnection.cellCacheHitCount)
            + ", "
            + lastConnection.cellCacheHitCount
            + "; "
            + lastStatement.cellCacheRequestCount
            + ", "
            + lastStatement.cellCacheMissCount
            + ", "
            + lastStatement.cellCacheHitCount);

    // cache misses in the last minute
    // cache hits in the last minute
    // -- build a layer on top of monitor that polls say every 15 seconds,
    //    and keeps results for a few minutes

    println("number of mdx statements currently open: " + server.statementCurrentlyOpenCount());
    println(
        "number of mdx statements currently executing: "
            + server.statementCurrentlyExecutingCount());

    println(
        "jvm memory: "
            + server.jvmHeapBytesUsed
            + ", max: "
            + server.jvmHeapBytesMax
            + ", committed: "
            + server.jvmHeapBytesCommitted);

    println(
        "number of segments: "
            + server.segmentCount
            + ", ever created: "
            + server.segmentCreateCount
            + ", number of cells: "
            + server.cellCount
            + ", number of cell coordinates: "
            + server.cellCoordinateCount
            + ", average cell dimensionality: "
            + ((float) server.cellCoordinateCount / (float) server.cellCount));

    println("Statement: " + lastStatement);
    println("Connection: " + lastConnection);
    println("Server: " + server);

    // number of mdx function calls cumulative
    // how many operations have been evaluated in sql?
    // number of members in cache
    // number of cells in segments
    // mdx query time
    // sql query time
    // sql rows
    // olap4j connection pool size
    // sql connection pool size
    // thread count
    // # schemas in schema cache
    // cells fulfilled by sql statements
    // mondrian server count (other stats relate to just one server)
    //
    // Events:
    //
    // SQL statement start
    // SQL statment stop
    // external cache call
    // sort
    // (other expensive operations similar to sort?)
  }