ponymail-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject incubator-ponymail git commit: Better fix for null favorites (#392)
Date Sat, 19 Aug 2017 14:28:24 GMT
Repository: incubator-ponymail
Updated Branches:
  refs/heads/master afb891ee8 -> 3821f702d


Better fix for null favorites (#392)

Project: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/commit/3821f702
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/tree/3821f702
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/diff/3821f702

Branch: refs/heads/master
Commit: 3821f702d83384ae1bd417f74e51ae963c93c351
Parents: afb891e
Author: Sebb <sebb@apache.org>
Authored: Sat Aug 19 15:28:02 2017 +0100
Committer: Sebb <sebb@apache.org>
Committed: Sat Aug 19 15:28:02 2017 +0100

----------------------------------------------------------------------
 tools/elastic.py |  6 ++++
 tools/nullfav.py | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/3821f702/tools/elastic.py
----------------------------------------------------------------------
diff --git a/tools/elastic.py b/tools/elastic.py
index 427273d..2a65101 100755
--- a/tools/elastic.py
+++ b/tools/elastic.py
@@ -77,6 +77,12 @@ class Elastic:
             **kwargs
         )
     
+    def update(self, **kwargs):
+        return self.es.update(
+            index=self.dbname,
+            **kwargs
+        )
+
     def scan(self, doc_type='mbox', scroll='3m', size = 100, **kwargs):
         return self.es.search(
             index=self.dbname,

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/3821f702/tools/nullfav.py
----------------------------------------------------------------------
diff --git a/tools/nullfav.py b/tools/nullfav.py
new file mode 100755
index 0000000..271a5de
--- /dev/null
+++ b/tools/nullfav.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""
+
+Scan accounts to drop null entries from favorites
+
+"""
+
+import argparse
+from elastic import Elastic
+
+parser = argparse.ArgumentParser(description='Command line options.')
+
+parser.add_argument('--apply', dest='apply', action='store_true',
+                   help='Update the account favorites. Default is list ids only')
+
+args = parser.parse_args()
+
+FAVES='favorites'
+TARGET=None
+
+elastic = Elastic()
+scroll = '5m'
+page = elastic.scan(doc_type='account',
+    scroll = scroll,
+    body = {
+        "_source" : [FAVES],
+    }
+)
+sid = page['_scroll_id']
+scroll_size = page['hits']['total']
+print("Found %d accounts" % scroll_size)
+
+updated=0
+failed=0
+while (scroll_size > 0):
+    page = elastic.scroll(scroll_id = sid, scroll = scroll)
+    sid = page['_scroll_id']
+    scroll_size = len(page['hits']['hits'])
+    for hit in page['hits']['hits']:
+        id = hit['_id']
+        source = hit['_source']
+        if FAVES in source:
+            favorites = source[FAVES]
+            if TARGET in favorites:
+                newfav = [x for x in favorites if x != TARGET]
+                if not args.apply:
+                    print("Would update account id %s" % id)
+                    continue
+                print("Updating account id %s" % id)
+                try:
+                    elastic.update(doc_type='account',
+                        id = id,
+                        body = {
+                          'doc': {
+                            FAVES: newfav
+                          }
+                        }
+                    )
+                    updated +=1
+                except Exception as e:
+                    print("Error updating id %s: %s" % (id,e))
+                    failed += 1
+
+if args.apply:
+    print("Updated %d account(s) with %d failures" % (updated, failed))


Mime
View raw message