Milimetric has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/141077

Change subject: [WIP] Add script to bulk insert cohorts or reports
......................................................................

[WIP] Add script to bulk insert cohorts or reports

The argparse format is fairly complete and nice but is being hijacked by
the wikimetrics argparse setup.  This may be impossible to solve but I
don't think it's necessary to finish this task.  Without solving this
problem, we'd have no help output for this script.

TODO: (optional) solve clash with wikimetrics argparse setup
TODO: implement database logic to insert cohorts (script logic done)
TODO: implement database logic to insert reports (script logic done)

Bug: 65946
Change-Id: I41aa48f0ae39aac5a8ca9be9bd5c256e1aaaf552
---
A scripts/admin
1 file changed, 91 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/analytics/wikimetrics 
refs/changes/77/141077/1

diff --git a/scripts/admin b/scripts/admin
new file mode 100755
index 0000000..392cc49
--- /dev/null
+++ b/scripts/admin
@@ -0,0 +1,91 @@
+#!/usr/bin/env python
+import argparse
+import sys
+
+# TODO: importing this makes the wikimetrics argparse clash with this file's 
argparse
+from wikimetrics.configurables import db
+from wikimetrics.models import (
+    UserStore, CohortStore, ReportStore, CohortUserStore, CohortUserRole,
+)
+from wikimetrics.metrics import metric_classes
+
+
+def run_cohorts(args):
+    print('... validating list of projects')
+    projects = db.get_project_host_map()
+    if args.projects:
+        invalid_projects = [p for p in args.projects if p not in projects]
+        if invalid_projects:
+            print('These are unknown: {}'.format(invalid_projects))
+            sys.exit(1)
+        valid_projects = args.projects
+    elif args.all_projects:
+        valid_projects = projects
+    print('... adding cohorts: {}'.format(valid_projects))
+
+
+def run_reports(args):
+    print('... validating cohorts')
+    print(args.cohorts)
+
+
+def get_user(args):
+    try:
+        s = db.get_session()
+        print('... looking up user')
+        q = s.query(UserStore)
+        if args.user:
+            u = q.get(args.user)
+        else:
+            u = q.filter(UserStore.username == 'WikimetricsBot').one()
+        print('... found user "{}", id: "{}"'.format(u.username or u.email, 
u.id))
+        args.user = u
+
+    except Exception, e:
+        print(e)
+        sys.exit(1)
+    finally:
+        s.close()
+
+
+parser = argparse.ArgumentParser(description='Manage cohorts or reports in 
bulk')
+parser.add_argument(
+    '-u', '--user',
+    help='Owner of any new records being inserted (default: id of 
WikimetricsBot)',
+    type=int,
+)
+
+subparsers = parser.add_subparsers(help='Different modes of administration')
+
+parse_cohorts = subparsers.add_parser('cohorts', help='Add wiki cohorts to a 
user')
+parse_reports = subparsers.add_parser('reports', help='Add recurrent reports 
for a user')
+
+parse_cohorts.set_defaults(func=run_cohorts)
+parse_reports.set_defaults(func=run_reports)
+
+parse_cohorts_projects = 
parse_cohorts.add_mutually_exclusive_group(required=True)
+parse_cohorts_projects.add_argument(
+    '-p', '--projects',
+    help='List of projects to add (default: no projects)',
+    nargs='+',
+)
+parse_cohorts_projects.add_argument(
+    '-a', '--all-projects',
+    help='Add all known projects',
+    action='store_true',
+)
+
+parse_reports.add_argument(
+    'metric',
+    help='Name of a single metric to add reports for',
+    choices=[k for k, v in metric_classes.items() if v.show_in_ui],
+)
+parse_reports.add_argument(
+    'cohorts',
+    help='List of cohorts to add reports for',
+    nargs='+',
+)
+
+args = parser.parse_args()
+get_user(args)
+args.func(args)

-- 
To view, visit https://gerrit.wikimedia.org/r/141077
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I41aa48f0ae39aac5a8ca9be9bd5c256e1aaaf552
Gerrit-PatchSet: 1
Gerrit-Project: analytics/wikimetrics
Gerrit-Branch: master
Gerrit-Owner: Milimetric <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to