<commit_before>library clean_logging.mongo_logger;

import 'dart:async';
import 'dart:io';
import 'package:http_server/http_server.dart';
import 'package:mongo_dart/mongo_dart.dart';

class MongoLogger {
  HttpServer httpServer;
  Db mongodb;
  static String collectionName = 'logs';

  static _createIndexes(db, List<String> keys) =>
    db.createIndex(collectionName, keys: new Map.fromIterable(keys, value: (v) => 1));

  MongoLogger.config(this.httpServer, this.mongodb);

  _insertLog(Map log) => mongodb.collection(collectionName).insert(log);

  _handleRequest(request) {
    HttpBodyHandler.processRequest(request).then((body) {
      request.response.headers.add("Access-Control-Allow-Origin", "*");
      request.response.headers.add("Access-Control-Allow-Methods", "POST, OPTIONS");
      if (body.request.method == "OPTIONS") {
        // For cross origin request
        request.response.headers.add("Access-Control-Allow-Headers", "Content-Type");
      } else if (body.type != "json") {
        request.response.statusCode = HttpStatus.BAD_REQUEST;
      } else {
        _insertLog(body.body);
        request.response.statusCode = HttpStatus.OK;
        request.response.write({"result":"OK"});
      }
      request.response.close();
    });
  }

  start() =>
    httpServer.listen((request) => _handleRequest(request));

  static Future<MongoLogger> bind(host, port, connectionString) {
    var mongodb = new Db(connectionString);
    return mongodb.open()
      .then((_) => _createIndexes(mongodb, ['event','timestamp','level','name']))
      .then((_) => HttpServer.bind(host,port))
      .then((server) => new MongoLogger.config(server, mongodb)..start());
    }
}<commit_msg>Fix indexes.<commit_after>library clean_logging.mongo_logger;

import 'dart:async';
import 'dart:io';
import 'package:http_server/http_server.dart';
import 'package:mongo_dart/mongo_dart.dart';

class MongoLogger {
  HttpServer httpServer;
  Db mongodb;
  static const String COLLECTION_NAME = 'logs';

  static Future _createIndexes(Db mongodb) {
    var indexes = [
      {'level': 1, 'timestamp': 1},
      {'timestamp': 1},
      {'source': 1, 'timestamp':1},
      {'source': 1, 'level': 1},
      {'event': 1, 'timestamp': 1},
    ];
    return Future.wait(indexes.map((i) =>
        mongodb.ensureIndex(COLLECTION_NAME, keys: i)));
  }

  MongoLogger.config(this.httpServer, this.mongodb);

  _insertLog(Map log) => mongodb.collection(COLLECTION_NAME).insert(log);

  _handleRequest(request) {
    HttpBodyHandler.processRequest(request).then((body) {
      request.response.headers.add("Access-Control-Allow-Origin", "*");
      request.response.headers.add("Access-Control-Allow-Methods", "POST, OPTIONS");
      if (body.request.method == "OPTIONS") {
        // For cross origin request
        request.response.headers.add("Access-Control-Allow-Headers", "Content-Type");
      } else if (body.type != "json") {
        request.response.statusCode = HttpStatus.BAD_REQUEST;
      } else {
        _insertLog(body.body);
        request.response.statusCode = HttpStatus.OK;
        request.response.write({"result":"OK"});
      }
      request.response.close();
    });
  }

  start() =>
    httpServer.listen((request) => _handleRequest(request));

  static Future<MongoLogger> bind(host, port, connectionString) {
    var mongodb = new Db(connectionString);
    return mongodb.open()
      .then((_) => _createIndexes(mongodb))
      .then((_) => HttpServer.bind(host,port))
      .then((server) => new MongoLogger.config(server, mongodb)..start());
    }
}
{'fraction_non_alphanumeric': 0.1588235294117647, 'fraction_numerical': 0.0028011204481792717, 'mean_word_length': 3.78045515394913, 'pattern_counts': {'":': 2, '<': 6, '<?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': '5861720', 'n_tokens_mistral': 1151, 'n_tokens_neox': 1098, 'n_words': 190}