Skip to content

Instantly share code, notes, and snippets.

@ahmadrosid
Last active November 12, 2019 12:58
Show Gist options
  • Save ahmadrosid/bf081a220822ac3ed0340ddae104bb5a to your computer and use it in GitHub Desktop.
Save ahmadrosid/bf081a220822ac3ed0340ddae104bb5a to your computer and use it in GitHub Desktop.

contact_pipeline_histories_2019_09

pipeline_id object_id object_type phase_id contact_id status created_at
1 1 recruits 1 1001 1 2019-03-15T05:00:00
1 1 recruits 1 1001 2 2019-03-15T05:10:00
1 1 recruits 2 1001 1 2019-03-15T05:10:00
1 2 recruits 4 1002 1 2019-03-15T05:00:00
1 3 recruits 4 1003 1 2019-03-15T05:00:00
1 3 recruits 4 1003 1 2019-03-15T05:10:00
1 3 recruits 5 1003 1 2019-03-15T05:10:00
1 3 recruits 7 1003 1 2019-03-15T05:10:00

contact_pipeline_histories_2019_10

pipeline_id object_id object_type phase_id contact_id status created_at
1 1 recruits 1 1001 1 2019-03-15T05:00:00
1 1 recruits 1 1001 2 2019-03-15T05:10:00
1 1 recruits 2 1001 1 2019-03-15T05:10:00
1 2 recruits 4 1002 1 2019-03-15T05:00:00
1 3 recruits 4 1003 1 2019-03-15T05:00:00
1 3 recruits 4 1003 1 2019-03-15T05:10:00
1 3 recruits 5 1003 1 2019-03-15T05:10:00
1 3 recruits 7 1003 1 2019-03-15T05:10:00

contact_pipeline_histories_2019_11

pipeline_id object_id object_type phase_id contact_id status created_at
1 1 recruits 1 1001 1 2019-03-15T05:00:00
1 1 recruits 1 1001 2 2019-03-15T05:10:00
1 1 recruits 2 1001 1 2019-03-15T05:10:00
1 2 recruits 4 1002 1 2019-03-15T05:00:00
1 3 recruits 4 1003 1 2019-03-15T05:00:00
1 3 recruits 4 1003 1 2019-03-15T05:10:00
1 3 recruits 5 1003 1 2019-03-15T05:10:00
1 3 recruits 7 1003 1 2019-03-15T05:10:00

PHP Script for generate the data.

<?php

$mappings = '{
    "mappings": {
      "_doc": {
        "properties": {
            "id": {"type": "integer"},
            "pipeline_id": {"type": "integer"},
            "object_type": {"type": "text"},
            "object_id": {"type": "integer"},
            "phase_id": { "type": "integer" },
            "contact_id": { "type": "integer" },
            "org_id": { "type": "integer" },
            "experience": { "type": "integer" },
            "status": {"type": "integer"},
            "created_at": {"type": "date"},
            "updated_at": { "type": "date" },
            "deleted_at": { "type": "date", "null_value": "NULL" }
        }
      }
    }
}';

$indexs = ["contact_pipeline_histories_2019_09","contact_pipeline_histories_2019_10","contact_pipeline_histories_2019_11"];

foreach ($indexs as $index) {
  //request($index,"DELETE", []);
  request($index,"PUT", $mappings);
  generate($index);
}

function generate($index)
{
  $datas = [
'{"pipeline_id": 1,"object_id": 1,"object_type": "recruits","phase_id": 1,"contact_id": 1001,"status": 1,"created_at": "2019-03-15T05:00:00"}'
,
'{"pipeline_id": 1,"object_id": 1,"object_type": "recruits","phase_id": 1,"contact_id": 1001,"status": 2,"created_at": "2019-03-15T05:10:00"}'
,
'{"pipeline_id": 1,"object_id": 1,"object_type": "recruits","phase_id": 2,"contact_id": 1001,"status": 1,"created_at": "2019-03-15T05:10:00"}'
,
'{"pipeline_id": 1,"object_id": 2,"object_type": "recruits","phase_id": 4,"contact_id": 1002,"status": 1,"created_at": "2019-03-15T05:00:00"}'
,
'{"pipeline_id": 1,"object_id": 3,"object_type": "recruits","phase_id": 4,"contact_id": 1003,"status": 1,"created_at": "2019-03-15T05:00:00"}'
,
'{"pipeline_id": 1,"object_id": 3,"object_type": "recruits","phase_id": 4,"contact_id": 1003,"status": 1,"created_at": "2019-03-15T05:10:00"}'
,
'{"pipeline_id": 1,"object_id": 3,"object_type": "recruits","phase_id": 5,"contact_id": 1003,"status": 1,"created_at": "2019-03-15T05:10:00"}',
'{"pipeline_id": 1,"object_id": 3,"object_type": "recruits","phase_id": 7,"contact_id": 1003,"status": 1,"created_at": "2019-03-15T05:10:00"}'
];
  $i = 0;
  foreach ($datas as $data) {
      $data = json_decode($data, true);
      $data['id'] = $i++; 
      $data['org_id'] = 5663; 
      $data['experience'] = rand(1,2);
      request("$index/_doc", "POST", $data);
  }
}

function randomDate($sStartDate, $sEndDate, $sFormat = 'c') {
    $fMin = strtotime($sStartDate);
    $fMax = strtotime($sEndDate);
    $fVal = mt_rand($fMin, $fMax);
    return date($sFormat, $fVal);
}

function request($path, $method, $data)
{
    $curl = curl_init();

    curl_setopt_array($curl, array(
      CURLOPT_PORT => "9200",
      CURLOPT_URL => "http://localhost:9200/" . $path,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => $method,
      CURLOPT_POSTFIELDS => json_encode($data),
      CURLOPT_HTTPHEADER => array(
        "Content-Type: application/json",
      ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response . "\n";
    }
}

APPOINTMENT & GROS THIS MONTH

curl -X POST \
  http://localhost:9200/contact_pipeline_histories_2019_10/_search \
  -H 'Content-Type: application/json' \
  -d '{
  "size": 0,
  "query": {
    "bool": {
      "must_not": {
        "exists": {
          "field": "deleted_at"
        }
      },
      "filter": {
        "term": {
          "org_id": 5663
        }
      },
      "must": {
        "terms": {
          "phase_id": [
            4,
            5,
            7
          ]
        }
      }
    }
  },
  "aggs": {
    "contact_id": {
      "terms": {
        "field": "contact_id"
      },
      "aggs": {
        "phase_id": {
          "terms": {
            "field": "phase_id"
          },
          "aggs": {
            "phases": {
              "top_hits": {
                "sort": [
                  {
                    "_id": {
                      "order": "desc"
                    }
                  }
                ],
                "_source": {
                  "includes": [
                    "id",
                    "phase_id",
                    "contact_id",
                    "status",
                    "org_id"
                  ]
                },
                "size": 1
              }
            }
          }
        }
      }
    }
  }
}'

Response

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 0.0,
    "hits": []
  },
  "aggregations": {
    "contact_id": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 1003,
          "doc_count": 4,
          "phase_id": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": 4,
                "doc_count": 2,
                "phases": {
                  "hits": {
                    "total": 2,
                    "max_score": null,
                    "hits": [
                      {
                        "_index": "contact_pipeline_histories_2019_10",
                        "_type": "_doc",
                        "_id": "_Iv7RG4BnXRrgaZWJwAG",
                        "_score": null,
                        "_source": {
                          "org_id": 5663,
                          "phase_id": 4,
                          "id": 5,
                          "contact_id": 1003,
                          "status": 1
                        },
                        "sort": [
                          "_Iv7RG4BnXRrgaZWJwAG"
                        ]
                      }
                    ]
                  }
                }
              },
              {
                "key": 5,
                "doc_count": 1,
                "phases": {
                  "hits": {
                    "total": 1,
                    "max_score": null,
                    "hits": [
                      {
                        "_index": "contact_pipeline_histories_2019_10",
                        "_type": "_doc",
                        "_id": "_Yv7RG4BnXRrgaZWJwAX",
                        "_score": null,
                        "_source": {
                          "org_id": 5663,
                          "phase_id": 5,
                          "id": 6,
                          "contact_id": 1003,
                          "status": 1
                        },
                        "sort": [
                          "_Yv7RG4BnXRrgaZWJwAX"
                        ]
                      }
                    ]
                  }
                }
              },
              {
                "key": 7,
                "doc_count": 1,
                "phases": {
                  "hits": {
                    "total": 1,
                    "max_score": null,
                    "hits": [
                      {
                        "_index": "contact_pipeline_histories_2019_10",
                        "_type": "_doc",
                        "_id": "_ov7RG4BnXRrgaZWJwAl",
                        "_score": null,
                        "_source": {
                          "org_id": 5663,
                          "phase_id": 7,
                          "id": 7,
                          "contact_id": 1003,
                          "status": 1
                        },
                        "sort": [
                          "_ov7RG4BnXRrgaZWJwAl"
                        ]
                      }
                    ]
                  }
                }
              }
            ]
          }
        },
        {
          "key": 1002,
          "doc_count": 1,
          "phase_id": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": 4,
                "doc_count": 1,
                "phases": {
                  "hits": {
                    "total": 1,
                    "max_score": null,
                    "hits": [
                      {
                        "_index": "contact_pipeline_histories_2019_10",
                        "_type": "_doc",
                        "_id": "-ov7RG4BnXRrgaZWJgDf",
                        "_score": null,
                        "_source": {
                          "org_id": 5663,
                          "phase_id": 4,
                          "id": 3,
                          "contact_id": 1002,
                          "status": 1
                        },
                        "sort": [
                          "-ov7RG4BnXRrgaZWJgDf"
                        ]
                      }
                    ]
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

APPOINTMENT & GROS THIS MONTH

curl -X POST \
  http://localhost:9200/contact_pipeline_histories_2019_11/_search \
  -H 'Content-Type: application/json' \
  -d '{
  "size": 0,
  "query": {
    "bool": {
      "must_not": {
        "exists": {
          "field": "deleted_at"
        }
      },
      "filter": {
        "term": {
          "org_id": 5663
        }
      },
      "must": {
        "terms": {
          "phase_id": [
            4,
            5,
            7
          ]
        }
      }
    }
  },
  "aggs": {
    "contact_id": {
      "terms": {
        "field": "contact_id"
      },
      "aggs": {
        "phase_id": {
          "terms": {
            "field": "phase_id"
          },
          "aggs": {
            "phases": {
              "top_hits": {
                "sort": [
                  {
                    "_id": {
                      "order": "desc"
                    }
                  }
                ],
                "_source": {
                  "includes": [
                    "id",
                    "phase_id",
                    "contact_id",
                    "status",
                    "org_id"
                  ]
                },
                "size": 1
              }
            }
          }
        }
      }
    }
  }
}'

Response

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 0.0,
    "hits": []
  },
  "aggregations": {
    "contact_id": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 1003,
          "doc_count": 4,
          "phase_id": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": 4,
                "doc_count": 2,
                "phases": {
                  "hits": {
                    "total": 2,
                    "max_score": null,
                    "hits": [
                      {
                        "_index": "contact_pipeline_histories_2019_11",
                        "_type": "_doc",
                        "_id": "BIv7RG4BnXRrgaZWKgE1",
                        "_score": null,
                        "_source": {
                          "org_id": 5663,
                          "phase_id": 4,
                          "id": 5,
                          "contact_id": 1003,
                          "status": 1
                        },
                        "sort": [
                          "BIv7RG4BnXRrgaZWKgE1"
                        ]
                      }
                    ]
                  }
                }
              },
              {
                "key": 5,
                "doc_count": 1,
                "phases": {
                  "hits": {
                    "total": 1,
                    "max_score": null,
                    "hits": [
                      {
                        "_index": "contact_pipeline_histories_2019_11",
                        "_type": "_doc",
                        "_id": "BYv7RG4BnXRrgaZWKgFP",
                        "_score": null,
                        "_source": {
                          "org_id": 5663,
                          "phase_id": 5,
                          "id": 6,
                          "contact_id": 1003,
                          "status": 1
                        },
                        "sort": [
                          "BYv7RG4BnXRrgaZWKgFP"
                        ]
                      }
                    ]
                  }
                }
              },
              {
                "key": 7,
                "doc_count": 1,
                "phases": {
                  "hits": {
                    "total": 1,
                    "max_score": null,
                    "hits": [
                      {
                        "_index": "contact_pipeline_histories_2019_11",
                        "_type": "_doc",
                        "_id": "Bov7RG4BnXRrgaZWKgFe",
                        "_score": null,
                        "_source": {
                          "org_id": 5663,
                          "phase_id": 7,
                          "id": 7,
                          "contact_id": 1003,
                          "status": 1
                        },
                        "sort": [
                          "Bov7RG4BnXRrgaZWKgFe"
                        ]
                      }
                    ]
                  }
                }
              }
            ]
          }
        },
        {
          "key": 1002,
          "doc_count": 1,
          "phase_id": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": 4,
                "doc_count": 1,
                "phases": {
                  "hits": {
                    "total": 1,
                    "max_score": null,
                    "hits": [
                      {
                        "_index": "contact_pipeline_histories_2019_11",
                        "_type": "_doc",
                        "_id": "Aov7RG4BnXRrgaZWKgEO",
                        "_score": null,
                        "_source": {
                          "org_id": 5663,
                          "phase_id": 4,
                          "id": 3,
                          "contact_id": 1002,
                          "status": 1
                        },
                        "sort": [
                          "Aov7RG4BnXRrgaZWKgEO"
                        ]
                      }
                    ]
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

APPOINTMENT & GROSS YTD

curl -X POST \
  http://localhost:9200/contact_pipeline_histories_2019_09,contact_pipeline_histories_2019_10,contact_pipeline_histories_2019_11/_search \
  -H 'Content-Type: application/json' \
  -d '{
  "size": 0,
  "query": {
    "bool": {
      "must_not": {
        "exists": {
          "field": "deleted_at"
        }
      },
      "filter": {
        "term": {
          "org_id": 5663
        }
      },
      "must": {
        "terms": {
          "phase_id": [
            4,
            5,
            7
          ]
        }
      }
    }
  },
  "aggs": {
    "contact_id": {
      "terms": {
        "field": "contact_id"
      },
      "aggs": {
        "phase_id": {
          "terms": {
            "field": "phase_id"
          },
          "aggs": {
            "phases": {
              "top_hits": {
                "sort": [
                  {
                    "_id": {
                      "order": "desc"
                    }
                  }
                ],
                "_source": {
                  "includes": [
                    "id",
                    "phase_id",
                    "contact_id",
                    "status",
                    "org_id"
                  ]
                },
                "size": 1
              }
            }
          }
        }
      }
    }
  }
}'

Response

  "took": 22,
  "timed_out": false,
  "_shards": {
    "total": 15,
    "successful": 15,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 15,
    "max_score": 0.0,
    "hits": []
  },
  "aggregations": {
    "contact_id": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 1003,
          "doc_count": 12,
          "phase_id": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": 4,
                "doc_count": 6,
                "phases": {
                  "hits": {
                    "total": 6,
                    "max_score": null,
                    "hits": [
                      {
                        "_index": "contact_pipeline_histories_2019_10",
                        "_type": "_doc",
                        "_id": "_Iv7RG4BnXRrgaZWJwAG",
                        "_score": null,
                        "_source": {
                          "org_id": 5663,
                          "phase_id": 4,
                          "id": 5,
                          "contact_id": 1003,
                          "status": 1
                        },
                        "sort": [
                          "_Iv7RG4BnXRrgaZWJwAG"
                        ]
                      }
                    ]
                  }
                }
              },
              {
                "key": 5,
                "doc_count": 3,
                "phases": {
                  "hits": {
                    "total": 3,
                    "max_score": null,
                    "hits": [
                      {
                        "_index": "contact_pipeline_histories_2019_10",
                        "_type": "_doc",
                        "_id": "_Yv7RG4BnXRrgaZWJwAX",
                        "_score": null,
                        "_source": {
                          "org_id": 5663,
                          "phase_id": 5,
                          "id": 6,
                          "contact_id": 1003,
                          "status": 1
                        },
                        "sort": [
                          "_Yv7RG4BnXRrgaZWJwAX"
                        ]
                      }
                    ]
                  }
                }
              },
              {
                "key": 7,
                "doc_count": 3,
                "phases": {
                  "hits": {
                    "total": 3,
                    "max_score": null,
                    "hits": [
                      {
                        "_index": "contact_pipeline_histories_2019_10",
                        "_type": "_doc",
                        "_id": "_ov7RG4BnXRrgaZWJwAl",
                        "_score": null,
                        "_source": {
                          "org_id": 5663,
                          "phase_id": 7,
                          "id": 7,
                          "contact_id": 1003,
                          "status": 1
                        },
                        "sort": [
                          "_ov7RG4BnXRrgaZWJwAl"
                        ]
                      }
                    ]
                  }
                }
              }
            ]
          }
        },
        {
          "key": 1002,
          "doc_count": 3,
          "phase_id": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": 4,
                "doc_count": 3,
                "phases": {
                  "hits": {
                    "total": 3,
                    "max_score": null,
                    "hits": [
                      {
                        "_index": "contact_pipeline_histories_2019_11",
                        "_type": "_doc",
                        "_id": "Aov7RG4BnXRrgaZWKgEO",
                        "_score": null,
                        "_source": {
                          "org_id": 5663,
                          "phase_id": 4,
                          "id": 3,
                          "contact_id": 1002,
                          "status": 1
                        },
                        "sort": [
                          "Aov7RG4BnXRrgaZWKgEO"
                        ]
                      }
                    ]
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment