Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Execute Ruby Online

#!/usr/bin/env ruby

require "json"

$output = []
$index = 0;

def buildJsonArray(json)
    if json['buckets'].each_with_index {
        |event, indexEvent| 
        if event['SENT'] and event['SENT']['buckets'] and event['SENT']['buckets'].length > 0
            if event['SENT']['buckets'].each_with_index {
                |sent, indexStatus| 
                if sent['RULE'] and sent['RULE']['buckets'] and sent['RULE']['buckets'].length > 0
                    if sent['RULE']['buckets'].each_with_index {
                        |rule, indexRule|
                        if rule['CHANNEL'] and rule['CHANNEL']['buckets'] and rule['CHANNEL']['buckets'].length > 0
                            if rule['CHANNEL']['buckets'].each_with_index {
                                |channel, indexChannel| 
                                if channel['ERROR'] and channel['ERROR']['buckets'] and channel['ERROR']['buckets'].length > 0
                                    if channel['ERROR']['buckets'].each_with_index {
                                        |error, indexReason|
                                        buildRow(event, sent, rule, channel, error, error)
                                    }
                                    end
                                else
                                    buildRow(event, sent, rule, nil, nil, channel)
                                end
                            }
                            end
                        else
                            buildRow(event, sent, rule, nil, nil, rule)
                        end
                    }
                    end
                else
                    buildRow(event, sent, nil, nil, nil, sent)
                end
            }
            end
        else
            buildRow(event, nil, nil, nil, nil, event)
        end
    }
    end

end


def buildRow(event, sent, rule, channel, error, rowCount)
    $output[$output.length] = {
        'eventType' => event ? event['key'] : '',
        'sent' => sent ? sent['key'] == 1 : '',
        'ruleId' => rule ? rule['key'] : '',
        'channel' => channel ? channel['key'] : '',
        'errorType' => error ? error['key'] : '', 
        'count' => rowCount['doc_count'],
        'campaign' => ''
    }
end


def buildJsonArrayCampaign(json)
    if json['buckets'].each_with_index {
        |campaign, indexCampaign| 
        if campaign['SENT'] and campaign['SENT']['buckets'] and campaign['SENT']['buckets'].length > 0
            if campaign['SENT']['buckets'].each_with_index {
                |sent, indexStatus| 
                    if sent['CHANNEL'] and sent['CHANNEL']['buckets'] and sent['CHANNEL']['buckets'].length > 0
                        if sent['CHANNEL']['buckets'].each_with_index {
                            |channel, indexChannel| 
                            if channel['ERROR'] and channel['ERROR']['buckets'] and channel['ERROR']['buckets'].length > 0
                                if channel['ERROR']['buckets'].each_with_index {
                                    |error, indexReason|
                                    buildRowCampaign(campaign, sent, channel, error, error)
                                }
                                end
                            else
                                buildRowCampaign(campaign, sent, nil, nil, channel)
                            end
                        }
                        end
                    else
                        buildRowCampaign(campaign, nil, nil, nil, sent)
                    end
            }
            end
        else
            buildRow(event, nil, nil, nil, nil, event)
        end
    }
    end

end

def buildRowCampaign(campaign, sent, channel, error, rowCount)
    $output[$output.length] = {
        'campaign' => campaign ? campaign['key'] : '',
        'sent' => sent ? sent['key'] == 1 : '',
        'ruleId' => '',
        'channel' => channel ? channel['key'] : '',
        'errorType' => error ? error['key'] : '', 
        'count' => rowCount['doc_count'],
        'eventType' => ''
    }
end


eventJ = '{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "CAMPAIGN": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
          {
            "key": "capaignId",
            "doc_count": 2,
            "SENT": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": 0,
                  "key_as_string": "true",
                  "doc_count": 2,
                  "CHANNEL": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                      {
                        "key": "APP",
                        "doc_count": 2,
                        "ERROR": {
                          "doc_count_error_upper_bound": 0,
                          "sum_other_doc_count": 0,
                          "buckets": [
                            {
                              "key": "SEND_TOKENNOTFOUND",
                              "doc_count": 2
                            },
                            {
                              "key": "ERROR_1",
                              "doc_count": 8
                            }
                          ]
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
      ]
    },
    "EVENT": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 1,
          "doc_count": 6,
          "SENT": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": 1,
                "key_as_string": "true",
                "doc_count": 4,
                "RULE": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                    {
                      "key": "5be1d3e12b0e2479436fa653",
                      "doc_count": 4,
                      "CHANNEL": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                          {
                            "key": "APP",
                            "doc_count": 4,
                            "ERROR": {
                              "doc_count_error_upper_bound": 0,
                              "sum_other_doc_count": 0,
                              "buckets": []
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "key": 0,
                "key_as_string": "false",
                "doc_count": 2,
                "RULE": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                    {
                      "key": "5be1d3e12b0e2479436fa653",
                      "doc_count": 2,
                      "CHANNEL": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                          {
                            "key": "APP",
                            "doc_count": 2,
                            "ERROR": {
                              "doc_count_error_upper_bound": 0,
                              "sum_other_doc_count": 0,
                              "buckets": [
                                {
                                  "key": "SEND_TOKENNOTFOUND",
                                  "doc_count": 2
                                },
                                {
                                  "key": "ERROR_1",
                                  "doc_count": 8
                                }
                              ]
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}'

json = JSON.parse(eventJ)


buildJsonArray(json['aggregations']['EVENT'])

buildJsonArrayCampaign(json['aggregations']['CAMPAIGN'])

puts($output)

#puts JSON.pretty_generate(json)

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.