0% found this document useful (0 votes)
12 views

Elasticsearch Script

Uploaded by

bhagawatmishra02
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Elasticsearch Script

Uploaded by

bhagawatmishra02
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

POST /.ds-topspin-testing-demo-multiplayer-gameround-2023.10.

27-000001/_update/
190982
{
"script": {
"source": """
if (ctx._source.containsKey('metrics') &&
ctx._source.metrics.containsKey('playerMetrics')) {
for (int i = 0; i < ctx._source.metrics.playerMetrics.size(); i++) {
def playerMetrics = ctx._source.metrics.playerMetrics[i].playerMetrics;
if (playerMetrics != null) {
playerMetrics['cashoutMultiplier'] = params.cashoutMultiplier;
playerMetrics['conversionRate'] = params.conversionRate;
}
}
}
ctx._source.totalStake = params.totalStake;
ctx._source.totalReturn = params.totalReturn;
""",
"params": {
"totalStake": 984,
"totalReturn": 1,
"cashoutMultiplier": 1,
"conversionRate": 2
}
}
}

POST /.ds-topspin-testing-demo-multiplayer-gameround-2023.10.27-000001/_update/
190982
{
"script": {
"source": """
if (ctx._source.containsKey('metrics') &&
ctx._source.metrics.containsKey('playerMetrics')) {
for (int i = 0; i < ctx._source.metrics.playerMetrics.size(); i++) {
def playerMetrics = ctx._source.metrics.playerMetrics[i].playerMetrics;
if (playerMetrics != null) {
// Fetch the exchange rates from the currency API
def currencyApiUrl = 'https://currency-api.topspingames.net/latest?
baseCurrency=INR'
def currencyApiResponse = ctx.httpClient.get(currencyApiUrl);

// Parse the response to extract exchange rates


def exchangeRates = /<PARSE YOUR RESPONSE HERE>/;

// Check if the currency exists in exchange rates


def currency = playerMetrics['currency'];
if (exchangeRates.containsKey(currency)) {
def conversionFactor = 1 / exchangeRates[currency];
playerMetrics['cashoutMultiplier'] = params.cashoutMultiplier *
conversionFactor;
playerMetrics['conversionRate'] = params.conversionRate *
conversionFactor;
}
}
}
}
ctx._source.totalStake = params.totalStake;
ctx._source.totalReturn = params.totalReturn;
""",
"params": {
"totalStake": 984,
"totalReturn": 1,
"cashoutMultiplier": 1,
"conversionRate": 2
}
}
}

***********************************************************************************
POST /.ds-prod-parimatch-multiplayer-gameround-2023.11.01-000001/_update_by_query
{
"script": {
"source": """
boolean isINR = false; // Initialize a variable to false
def initialConversionRate = null;
if (ctx._source.containsKey('metrics') &&
ctx._source.metrics.containsKey('playerMetrics')) {
for (int i = 0; i < ctx._source.metrics.playerMetrics.size(); i++) {
def playerMetrics = ctx._source.metrics.playerMetrics[i].playerMetrics;
if (playerMetrics != null && playerMetrics['currency'] == 'KZT') {
isINR = true; // Set the variable to true if currency is INR
if (playerMetrics.containsKey('conversionRate')) {
initialConversionRate = playerMetrics['conversionRate'];
playerMetrics['conversionRate'] = (playerMetrics['conversionRate'] /
initialConversionRate)*0.17782; // Divide by 82.45
}
}
}
}
if (isINR && ctx._source.containsKey('totalStake') &&
ctx._source.containsKey('totalReturn')) {
if (initialConversionRate != null) {
ctx._source.totalStake = ctx._source.totalStake / initialConversionRate;
ctx._source.totalReturn = ctx._source.totalReturn /
initialConversionRate;
}
}
""",
"params": {}
},
"query": {
"terms": {
"_id": ["55636856"]
}
}
}

***********************************************************************************
******

POST /.ds-stage-gametech-multiplayer-gameround-2023.10.23-000001/_update_by_query
{
"script": {
"source": """
def totalStake = 0.0;
def totalReturn = 0.0;
if (ctx._source.containsKey('metrics') &&
ctx._source.metrics.containsKey('playerMetrics')) {
for (def playerMetric : ctx._source.metrics.playerMetrics) {
if (playerMetric != null &&
playerMetric.playerMetrics.containsKey('isPlayForFun') &&
playerMetric.playerMetrics['isPlayForFun'] == false) {
if (playerMetric.playerMetrics.containsKey('conversionRate') &&
playerMetric.playerMetrics.containsKey('betAmount')) {
// Log totalStake before updating
ctx._source['log_totalStake_before'] = totalStake;

totalStake += playerMetric.playerMetrics['conversionRate'] *
playerMetric.playerMetrics['betAmount'];

// Hypothetical example: totalReturn is calculated as the sum of


winAmount and bonusAmount
if (playerMetric.playerMetrics.containsKey('winAmount') &&
playerMetric.playerMetrics.containsKey('bonusAmount')) {
totalReturn += playerMetric.playerMetrics['winAmount'] +
playerMetric.playerMetrics['bonusAmount'];
}
}
}
}
}

// Log the final totalStake value


ctx._source['log_final_totalStake'] = totalStake;

ctx._source.totalStake = totalStake;
ctx._source.totalReturn = totalReturn;
""",
"params": {}
},
"query": {
"terms": {
"_id": ["8197863"]
}
}
}

***********************************************************************************
****************************

// Update totalStake and totalReturn


POST /.ds-prod-hub_b2b_as-multiplayer-gameround-2023.11.02-000001/_update_by_query
{
"script": {
"source": """
def totalStake = 0.0;
def totalReturn = 0.0;

if (ctx._source.containsKey('metrics') &&
ctx._source.metrics.containsKey('playerMetrics')) {
for (def playerMetric : ctx._source.metrics.playerMetrics) {
if (playerMetric != null &&
playerMetric.playerMetrics.containsKey('isPlayForFun') &&
playerMetric.playerMetrics['isPlayForFun'] == false) {
if (playerMetric.playerMetrics.containsKey('conversionRate') &&
playerMetric.playerMetrics.containsKey('betAmount')) {
// Log totalStake before updating
// ctx._source['log_totalStake_before'] = totalStake;

totalStake += playerMetric.playerMetrics['conversionRate'] *
playerMetric.playerMetrics['betAmount'];

// Hypothetical example: totalReturn is calculated as the sum of


winAmount and bonusAmount
if (playerMetric.playerMetrics.containsKey('winAmount')) {
totalReturn += playerMetric.playerMetrics['winAmount'] *
playerMetric.playerMetrics['conversionRate'];
}
}
}
}
}

// Log the final totalStake value


// ctx._source['log_final_totalStake'] = totalStake;

ctx._source.totalStake = totalStake;
ctx._source.totalReturn = totalReturn;
""",
"params": {}
},
"query": {
"terms": {
"_id":
["2263977","2263981","2263988","2263991","2263997","2264004","2264007","2264011","2
264017"]
}
}
}

**************Painless script to update winAmount based on multi_player_command it


will include decimal part

POST /.ds-prod-gametech-multiplayer-gameround-2023.10.26-000001/_update_by_query
{
"script": {
"source": """
// Create an object to store the summation for each player
def playerCommandSummation = [:];

// Iterate through multi_player_command array


for (def command : ctx._source.multi_player_command) {
def playerId = command.playerId;
def action = command.action;
def transactionAmount = command.transactionAmount;

// Check if action type is not 'Bet'


if (action != null && action.type != 'Bet') {
// Initialize the summation for the player if not already present
if (!playerCommandSummation.containsKey(playerId)) {
playerCommandSummation[playerId] = 0.0;
}
// Add the transactionAmount to the summation for the player
playerCommandSummation[playerId] += transactionAmount;
}
}

// Output the results


//println("Summation of transactionAmount for each player (where action type
is not 'Bet'):");
//println(playerCommandSummation);

// Create an object to store the summation for each player


def playerMetricsSummation = [:];

// Iterate through playerMetrics array


for (def metrics : ctx._source.metrics.playerMetrics) {
def playerId = metrics.playerId;
def winAmount = metrics.playerMetrics.winAmount;

// Initialize accumulatedWinAmount for the playerId if not already


present
if (!playerMetricsSummation.containsKey(playerId)) {
playerMetricsSummation[playerId] = 0.0;
}

// Add winAmount to accumulatedWinAmount for the playerId


playerMetricsSummation[playerId] += winAmount;
}

// Output the accumulated winAmount for each playerId


//println("Accumulated winAmount for each playerId:");
//println(playerMetricsSummation);

def differenceAmount = [:];

//// Iterate through unique playerIds from both objects


//def allPlayerIds = (playerCommandSummation.keySet() +
playerMetricsSummation.keySet()).unique();

// Convert sets to lists and combine them


def allPlayerIdsList = new ArrayList(playerCommandSummation.keySet());
allPlayerIdsList.addAll(new ArrayList(playerMetricsSummation.keySet()));

// Convert the list to a HashSet to ensure uniqueness


def uniquePlayerIdsSet = new HashSet(allPlayerIdsList);

// Convert the HashSet back to a list if needed


def allPlayerIds = new ArrayList(uniquePlayerIdsSet);

// Now allPlayerIds contains unique playerIds from both sets

// Calculate the difference for each playerId


for (def playerId : allPlayerIds) {
def commandSummationValue =
playerCommandSummation.containsKey(playerId) ? playerCommandSummation[playerId] :
0.0;
def metricsSummationValue =
playerMetricsSummation.containsKey(playerId) ? playerMetricsSummation[playerId] :
0.0;

// Calculate the difference and store in differenceAmount object


differenceAmount[playerId] = commandSummationValue -
metricsSummationValue;
}

// Output the differenceAmount for each playerId


//println("DifferenceAmount for each playerId:");
//println(differenceAmount);

def updatedPlayerIds = [:];

// Iterate through playerMetrics array and update only one object for each
playerId
for (def metrics : ctx._source.metrics.playerMetrics) {
def playerId = metrics.playerId;

if (differenceAmount.containsKey(playerId) && !
updatedPlayerIds.containsKey(playerId)) {
// Update one object for each playerId with the differenceAmount
metrics.playerMetrics.winAmount = (metrics.playerMetrics.winAmount ?:
0.0) + differenceAmount[playerId];
updatedPlayerIds[playerId] = true; // Mark playerId as updated
}
}

// Output the updated playerMetrics array


//println("Updated playerMetrics array:");
//println(params._source.playerMetrics);

""",
"params": {}
},
"query": {
"terms": {
"_id": ["8694590"]
}
}
}

You might also like