import arrow
import emission.core.get_database as edb
import emission.core.wrapper.user as ecwu
import emission.storage.timeseries.timequery as estt

for ue in edb.get_uuid_db().find():
    trip_count = edb.get_analysis_timeseries_db().count_documents({"user_id": ue["uuid"], "metadata.key": "analysis/confirmed_trip"})
    location_count = edb.get_timeseries_db().count_documents({"user_id": ue["uuid"], "metadata.key": "background/location"})
    first_trip = list(edb.get_analysis_timeseries_db().find({"user_id": ue["uuid"], "metadata.key": "analysis/confirmed_trip"}).sort("data.end_ts", 1).limit(1))
    first_trip_time = first_trip[0]["data"]["end_fmt_time"] if len(first_trip) > 0 else None
    last_trip = list(edb.get_analysis_timeseries_db().find({"user_id": ue["uuid"], "metadata.key": "analysis/confirmed_trip"}).sort("data.end_ts", -1).limit(1))
    last_trip_time = last_trip[0]["data"]["end_fmt_time"] if len(last_trip) > 0 else None
    now = arrow.now()
    month_ago = now.shift(months=-1)
    last_month_tq = estt.TimeQuery("data.start_ts", month_ago.timestamp, now.timestamp)
    confirmed_pct, valid_replacement_pct, score = ecwu.User.computeConfirmed(ue["uuid"], last_month_tq)
    print(f"For {ue['user_email']}: Trip count = {trip_count}, location count = {location_count}, first trip = {first_trip_time}, last trip = {last_trip_time}, confirmed_pct ({month_ago} -> {now}) = {confirmed_pct:.2f}")

@@ -1090,16 +1090,66 @@
 estamp)%0A
+    if 'computeConfirmed' in vars(ecwu.User):%0A    
     conf
@@ -1244,16 +1244,20 @@
 nth_tq)%0A
+    
     prin
@@ -1446,16 +1446,479 @@
 %7Bnow%7D) =
+ exactly %7Bconfirmed_pct:.2f%7D%22)%0A    else:%0A        confirmed_count = edb.get_analysis_timeseries_db().count_documents(%7B%22user_id%22: ue%5B%22uuid%22%5D, %22metadata.key%22: %22analysis/confirmed_trip%22, %22data.user_input%22: %7B%22$ne%22: %7B%7D%7D%7D)%0A        confirmed_pct = confirmed_count / trip_count%0A        print(f%22For %7Bue%5B'user_email'%5D%7D: Trip count = %7Btrip_count%7D, location count = %7Blocation_count%7D, first trip = %7Bfirst_trip_time%7D, last trip = %7Blast_trip_time%7D, confirmed_pct  = approximately
  %7Bconfir

Support more and less sophisticated approaches to read the confirmed trip count