monitor first charge of patron

pull/8491/head
Thibault Duplessis 2021-03-24 14:32:17 +01:00
parent fe615b8d3a
commit 6c1c62b9de
2 changed files with 32 additions and 22 deletions

View File

@ -405,11 +405,12 @@ object mon {
val json = future("swiss.json")
}
object plan {
val paypal = histogram("plan.amount").withTag("service", "paypal")
val stripe = histogram("plan.amount").withTag("service", "stripe")
val goal = gauge("plan.goal").withoutTags()
val current = gauge("plan.current").withoutTags()
val percent = gauge("plan.percent").withoutTags()
val paypal = histogram("plan.amount").withTag("service", "paypal")
val stripe = histogram("plan.amount").withTag("service", "stripe")
val goal = gauge("plan.goal").withoutTags()
val current = gauge("plan.current").withoutTags()
val percent = gauge("plan.percent").withoutTags()
def firstCharge(service: String) = counter("plan.charge.first").withTag("service", service)
}
object forum {
object post {

View File

@ -347,23 +347,32 @@ final class PlanApi(
}
private def addCharge(charge: Charge): Funit =
chargeColl.insert.one(charge).void >>- {
recentChargeUserIdsCache.invalidateUnit()
monthlyGoalApi.get foreach { m =>
Bus.publish(
lila.hub.actorApi.plan.ChargeEvent(
username = charge.userId.flatMap(lightUserApi.sync).fold("Anonymous")(_.name),
amount = charge.cents.value,
percent = m.percent,
DateTime.now
),
"plan"
)
lila.mon.plan.goal.update(m.goal.value)
lila.mon.plan.current.update(m.current.value)
lila.mon.plan.percent.update(m.percent)
if (charge.isPayPal) lila.mon.plan.paypal.record(charge.cents.value)
else if (charge.isStripe) lila.mon.plan.stripe.record(charge.cents.value)
monitorFirstCharge(charge) >>
chargeColl.insert.one(charge).void >>- {
recentChargeUserIdsCache.invalidateUnit()
monthlyGoalApi.get foreach { m =>
Bus.publish(
lila.hub.actorApi.plan.ChargeEvent(
username = charge.userId.flatMap(lightUserApi.sync).fold("Anonymous")(_.name),
amount = charge.cents.value,
percent = m.percent,
DateTime.now
),
"plan"
)
lila.mon.plan.goal.update(m.goal.value)
lila.mon.plan.current.update(m.current.value)
lila.mon.plan.percent.update(m.percent)
if (charge.isPayPal) lila.mon.plan.paypal.record(charge.cents.value)
else if (charge.isStripe) lila.mon.plan.stripe.record(charge.cents.value)
}
}
private def monitorFirstCharge(charge: Charge): Funit =
charge.userId ?? { userId =>
chargeColl.exists($doc("userId" -> userId)) map {
case false => lila.mon.plan.newPatron(charge.serviceName).increment().unit
case _ =>
}
}