/*** Generated by streamline 0.10.17 (callbacks) - DO NOT EDIT ***/ "use strict"; var __rt=require('streamline/lib/callbacks/runtime').runtime(__filename, false),__func=__rt.__func,__cb=__rt.__cb,__tryCatch=__rt.__tryCatch; var __ = require("underscore");

















var util = require("util");
var utils = require("../../../util/utils");
var insightsUtils = require("./insights.utils");

var $ = utils.getLocaleString;

exports.init = function(cli) {
  var log = cli.output;
  var insightsCommand = cli.category("insights").description($("Commands related to monitoring Insights (events, alert rules, autoscale settings, metrics)"));


  var insightsEventsCommand = insightsCommand.category("logs").description($("Retrieve operation logs for a subscription, a correlationId, a resource group, resource, or resource provider."));


  insightsEventsCommand.command("list").description($("List operation logs for a subscription.")).usage("[options]").option("-c --correlationId <correlationId>", $("The correlation id of the query.")).option("-g --resourceGroup <resourceGroup>", $("The resource group.")).option("-i --resourceId <resourceId>", $("The resource Id.")).option("-p --resourceProvider <resourceProvider>", $("The resource provider.")).option("-b --startTime <startTime>", $("The start time of the query.")).option("-e --endTime <endTime>", $("The end time of the query.")).option("-m --maxEvents <maxEvents>", $("The maximum number of records to be returned by the command.")).option("--status <status>", $("The status.")).option("--caller <caller>", $("Caller to look for when querying.")).option("-d --detailedOutput", $("Shows the details of the events in the log.")).option("-s --subscription <subscription>", $("The subscription identifier.")).execute(function __1(options, _) { var presentSwitches; var __frame = { name: "__1", line: 47 }; return __func(_, this, arguments, __1, 1, __frame, function __$__1() {














      presentSwitches = 0;
      __.each(["correlationId","resourceGroup","resourceId","resourceProvider",], function(switchName) { if (options[switchName]) { presentSwitches++; }; }); return (function __$__1(__then) {
        if ((presentSwitches > 1)) {
          return _(new Error($("The switches correlationId, resourceGroup, resourceId, and resourceProvider are optional and mutually exclusive."))); } else { return (function __$__1(__then) {
            if ((presentSwitches === 0)) {
              return insightsCommand._prepareAndExecuteSimple(options, __cb(_, __frame, 6, 26, __then, true)); } else { return (function __$__1(__then) {
                if (options.correlationId) {
                  return insightsCommand._prepareAndExecute(options.correlationId, "correlationId", "correlationId", options, __cb(_, __frame, 8, 26, __then, true)); } else { return (function __$__1(__then) {
                    if (options.resourceGroup) {
                      return insightsCommand._prepareAndExecute(options.resourceGroup, "resourceGroup", "resourceGroupName", options, __cb(_, __frame, 10, 26, __then, true)); } else { return (function __$__1(__then) {
                        if (options.resourceId) {
                          return insightsCommand._prepareAndExecute(options.resourceId, "resourceId", "resourceId", options, __cb(_, __frame, 12, 26, __then, true)); } else {

                          return insightsCommand._prepareAndExecute(options.resourceProvider, "resourceProvider", "resourceProvider", options, __cb(_, __frame, 14, 26, __then, true)); } ; })(__then); } ; })(__then); } ; })(__then); } ; })(__then); } ; })(_); }); });



  insightsCommand._processGeneralParameters = function(startTime, endTime, status, caller) {
    var queryFilter = insightsUtils.validateDateTimeRangeAndAddDefaultsEvents(startTime, endTime);
    queryFilter = insightsUtils.addConditionIfPresent(queryFilter, "status", status);
    queryFilter = insightsUtils.addConditionIfPresent(queryFilter, "caller", caller);

    return queryFilter; };


  insightsCommand._prepareAndExecuteSimple = function insightsCommand__prepareAndExecuteSimple__2(options, _) { var client, queryFilter, __this = this; var __frame = { name: "insightsCommand__prepareAndExecuteSimple__2", line: 73 }; return __func(_, this, arguments, insightsCommand__prepareAndExecuteSimple__2, 1, __frame, function __$insightsCommand__prepareAndExecuteSimple__2() {
      client = insightsUtils.createInsightsClient(log, options);
      queryFilter = __this._processGeneralParameters(options.startTime, options.endTime, options.status, options.caller);

      return __this._executeEventsCmd(client, queryFilter, (!options.detailedOutput ? insightsUtils.selectedFields : null), insightsUtils.passAllFilter, options, __cb(_, __frame, 4, 9, function __$insightsCommand__prepareAndExecuteSimple__2() { _(); }, true)); }); };


  insightsCommand._prepareAndExecute = function insightsCommand__prepareAndExecute__3(fieldValue, fieldName, fieldLabel, options, _) { var client, queryFilter, __this = this; var __frame = { name: "insightsCommand__prepareAndExecute__3", line: 80 }; return __func(_, this, arguments, insightsCommand__prepareAndExecute__3, 4, __frame, function __$insightsCommand__prepareAndExecute__3() {
      if (!__.isString(fieldValue)) {
        cli.missingArgument(fieldName); } ;


      client = insightsUtils.createInsightsClient(log, options);

      queryFilter = __this._processGeneralParameters(options.startTime, options.endTime, options.status, options.caller);
      queryFilter = insightsUtils.addConditionIfPresent(queryFilter, fieldLabel, fieldValue);

      return __this._executeEventsCmd(client, queryFilter, (!options.detailedOutput ? insightsUtils.selectedFields : null), insightsUtils.passAllFilter, options, __cb(_, __frame, 10, 9, function __$insightsCommand__prepareAndExecute__3() { _(); }, true)); }); };


  insightsCommand._executeEventsCmd = function insightsCommand__executeEventsCmd__4(client, queryFilter, selectedFields, keepTheRecord, options, _) { var defaultMaxEvents, progress, result, maxNumberOfRecords, response, recordFilter, nextLink; var __frame = { name: "insightsCommand__executeEventsCmd__4", line: 93 }; return __func(_, this, arguments, insightsCommand__executeEventsCmd__4, 5, __frame, function __$insightsCommand__executeEventsCmd__4() {
      defaultMaxEvents = 1000;
      progress = cli.interaction.progress(util.format($("Querying \"%s\""), queryFilter));
      result = []; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$insightsCommand__executeEventsCmd__4() {

            log.silly(("options: " + util.inspect(options)));
            log.silly(("parseInt options.maxEvents: " + parseInt(options.maxEvents)));

            maxNumberOfRecords = (__.isString(options.maxEvents) ? parseInt(options.maxEvents) : defaultMaxEvents);
            if ((__.isNaN(maxNumberOfRecords) || (maxNumberOfRecords < 1))) {
              return _(new Error(util.format($("Invalid value for argument %s: %s"), "maxEvents", options.maxEvents))); } ;


            log.silly(("MaxNumberOfRecords: " + maxNumberOfRecords));

            return client.eventOperations.listEvents(queryFilter, selectedFields, __cb(_, __frame, 15, 44, function ___(__0, __1) { response = __1;

              log.silly((__.isObject(response) ? util.inspect(response) : "nothing in response"));
              log.silly(((__.isObject(response) && response.eventDataCollection) ? util.inspect(response.eventDataCollection) : "nothing in eventDataCollection"));

              recordFilter = function(element) { if (keepTheRecord(element)) { result.push(element); } ; };
              __.each(response.eventDataCollection.value, recordFilter);

              log.silly(("result filtered #1: " + result.length));

              nextLink = response.eventDataCollection.nextLink; return (function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$insightsCommand__executeEventsCmd__4() { __more = false;
                  var __4 = (nextLink && (result.length < maxNumberOfRecords)); if (__4) {
                    log.silly("Following nextLink");
                    return client.eventOperations.listEventsNext(nextLink, __cb(_, __frame, 28, 42, function ___(__0, __2) { response = __2;
                      __.each(response.eventDataCollection.value, recordFilter);
                      nextLink = response.eventDataCollection.nextLink; while (__more) { __loop(); }; __more = true; }, true)); } else { __break(); } ; }); do { __loop(); } while (__more); __more = true; })(function __$insightsCommand__executeEventsCmd__4() {


                log.silly(("result filtered #2: " + result.length));

                if ((result.length > maxNumberOfRecords)) {
                  result = result.slice(0, maxNumberOfRecords); } ;


                log.silly(("result filtered #3: " + result.length)); _(null, null, true); }); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$insightsCommand__executeEventsCmd__4() {


              progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$insightsCommand__executeEventsCmd__4() {


          insightsUtils.formatOutputList(cli, log, options, result); _(); }); }); }); };};
