WITH _base(uuid,user_uuid,kind,records,state) AS (
SELECT uuid,user_uuid,kind,records,state FROM stats
),
_challenge_plank_per_user(uuid, user_uuid, total) AS (
SELECT uuid, json_extract(value,'$.user_uuid'), SUM(json_extract(value,'$.timerNow')) FROM _base, json_each(records) WHERE kind='challenge.plank' GROUP BY uuid, json_extract(value,'$.user_uuid')
),
_challenge_plank_per_user_sum(uuid, total) AS (
SELECT uuid, SUM(total) FROM _challenge_plank_per_user GROUP BY uuid
),
_challenge_plank_totals(user_uuid, kind, total, suffix) AS (
SELECT
pu.user_uuid,
'challange.' || pu.uuid,
s.total AS total,
'plank.total'
FROM
_challenge_plank_per_user AS pu
INNER JOIN _challenge_plank_per_user_sum AS s ON (pu.uuid = s.uuid)
GROUP BY pu.uuid, pu.user_uuid
)
SELECT * FROM _challenge_plank_totals;
Part of switch
//case challenge.EventChallengeCreated:
// h.onEventChallenge(entry)
//case challenge.EventChallengeNewRecord:
// h.onEventChallenge(entry)
//case challenge.EventChallengeDeleted:
// h.onEventChallenge(entry)
Handler in go
func (h *eventHandler) onEventChallenge(entry event.Eventlog) {
// TODO how to link user to a challenge, this is a great reason to stop
// TODO do I event have this data?
// TODO why do I duplicate the challenge data challenge.newrecord vs challenge.done
switch entry.Kind {
//case challenge.EventChallengeCreated:
//fmt.Println("TODO")
//var momentKV event.EventKV
//b, _ := json.Marshal(entry.Data)
//json.Unmarshal(b, &momentKV)
//b, _ = json.Marshal(momentKV.Data)
//var moment challenge.ChallengeInfo
//json.Unmarshal(b, &moment)
//
//userUUID := moment.Cr
//h.save(
// userUUID,
// challengeUUID,
// "challenge.plank",
// event.ActionUpdated,
// b,
//)
case challenge.EventChallengeNewRecord:
var moment challenge.EventChallengeDoneEntry
b, _ := json.Marshal(entry.Data)
json.Unmarshal(b, &moment)
if moment.Kind != challenge.EventKindPlank {
return
}
//entryWhen := time.Unix(entry.Timestamp, 0).UTC()
challengeUUID := moment.UUID
userUUID := moment.UserUUID
b, _ = json.Marshal(moment.Data)
var plankData openapiv6.Plank
_ = json.Unmarshal(b, &plankData)
entryWhen := time.Unix(plankData.BeginningTime, 0)
data := map[string]interface{}{
"when": entryWhen.Format(time.RFC3339Nano),
"action": event.ActionUpdated,
}
data["uuid"] = challengeUUID
data["user_uuid"] = userUUID
data["beginningTime"] = plankData.BeginningTime
data["timerNow"] = plankData.TimerNow
b, _ = json.Marshal(data)
h.save(
userUUID,
challengeUUID,
"challenge.plank",
event.ActionUpdated,
entryWhen,
b,
)
//fmt.Println("TODO")
//b, _ = json.Marshal(moment.Data)
//var record ChallengePlankRecord
//json.Unmarshal(b, &record)
case challenge.EventChallengeDeleted:
fmt.Println("TODO")
}
}