Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix database issues with the Increment Custom Metric solution #64

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 53 additions & 43 deletions custom solutions/Increment Custom Metric/increment-custom-metric.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,65 @@
/**
* Increments a metric in the bot_analytics table
* @title Increment Metric in bot_analytics
* @category Custom
* @author Botpress
* @param metric {string} the name of the metric to increment
* @param n {int} how much to increment it
*/
/**
* Increments a metric in the bot_analytics table
* @title Increment Metric in bot_analytics
* @category Custom
* @author Botpress
* @param metric {string} the name of the metric to increment
* @param n {int} how much to increment it
*/

async function getNewLeads(botId, date, metric) {
var new_leads = await bp.database.raw(
`SELECT * FROM bot_analytics WHERE
botId='${botId}' AND
date = '${date}' AND
channel = '${event.channel}' AND
metric = '${metric}' AND
subMetric = 'n/a'`
)
try {
return new_leads[0]
} catch (error) {
return undefined
}
async function getNewLeads(botId, date, metric) {
var new_leads = await bp.database.raw(
`SELECT *
FROM bot_analytics
WHERE "botId" = '${botId}'
AND
date = '${date}'
AND
channel = '${event.channel}'
AND
metric = '${metric}'
AND
"subMetric" = 'n/a'`
)
try {
return new_leads[0]
} catch (error) {
return undefined
}
}

const myAction = async (metric, n) => {
let date = event.createdOn.toISOString().split('T')[0]
bp.logger.info(date)
const incrementCustomMetric = async (metric, n) => {
let date = event.createdOn.toISOString().split('T')[0]
bp.logger.info(date)

var new_leads = await getNewLeads(event.botId, date, metric)
if (new_leads) {
new_leads = { ...new_leads, value: new_leads.value + n } // Increment the value
await bp.database.raw(
//Update the database
`UPDATE bot_analytics
SET value = ${new_leads.value}
WHERE botId='${event.botId}' AND
date = '${date}' AND
channel = '${event.channel}' AND
metric = '${metric}' AND
subMetric = 'n/a'`
)
} else {
await bp.database.insertAndRetrieve('bot_analytics', {
var new_leads = await getNewLeads(event.botId, date, metric)
if (new_leads) {
new_leads = { ...new_leads, value: new_leads.value + n } // Increment the value
await bp.database.raw(
//Update the database
`UPDATE bot_analytics
SET value = ${new_leads.value}
WHERE "botId" = '${event.botId}'
AND
date = '${date}'
AND
channel = '${event.channel}'
AND
metric = '${metric}'
AND
"subMetric" = 'n/a'`
)
} else {
await bp.database.insertAndRetrieve('bot_analytics', {
botId: event.botId,
date: date,
channel: event.channel,
metric: metric,
subMetric: 'n/a',
value: 1
})
}
},
['botId', 'date', 'channel', 'metric', 'subMetric'])
}
}

return myAction(args.metric, parseInt(args.n))
return incrementCustomMetric(args.metric, parseInt(args.n))