diff --git a/stackalytics/processor/default_data_processor.py b/stackalytics/processor/default_data_processor.py index bdd2a015d..29a260c1f 100644 --- a/stackalytics/processor/default_data_processor.py +++ b/stackalytics/processor/default_data_processor.py @@ -190,11 +190,8 @@ def _store_users(runtime_storage_inst, users): for user in users: stored_user = user_processor.load_user(runtime_storage_inst, user_id=user['user_id']) - if stored_user: - stored_user.update(user) - user = stored_user - user['static'] = True - user_processor.store_user(runtime_storage_inst, user) + updated_user = user_processor.update_user_profile(stored_user, user) + user_processor.store_user(runtime_storage_inst, updated_user) def _store_companies(runtime_storage_inst, companies): diff --git a/stackalytics/processor/user_processor.py b/stackalytics/processor/user_processor.py index 98236be8f..b7c13f4d8 100644 --- a/stackalytics/processor/user_processor.py +++ b/stackalytics/processor/user_processor.py @@ -12,6 +12,7 @@ # implied. # See the License for the specific language governing permissions and # limitations under the License. +import copy from oslo_log import log as logging @@ -88,3 +89,16 @@ def load_user(runtime_storage_inst, seq=None, user_id=None, email=None, def delete_user(runtime_storage_inst, user): LOG.debug('Delete user: %s', user) runtime_storage_inst.delete_by_key('user:%s' % user['seq']) + + +def update_user_profile(stored_user, user): + # update stored_user with user and return it + if stored_user: + updated_user = copy.deepcopy(stored_user) + updated_user.update(user) + updated_user['emails'] = list(set(stored_user.get('emails', [])) | + set(user.get('emails', []))) + else: + updated_user = copy.deepcopy(user) + updated_user['static'] = True + return updated_user diff --git a/tests/unit/test_user_processor.py b/tests/unit/test_user_processor.py new file mode 100644 index 000000000..6d816f5ef --- /dev/null +++ b/tests/unit/test_user_processor.py @@ -0,0 +1,89 @@ +# Copyright (c) 2015 Mirantis Inc. +# +# Licensed 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. + +import testtools + +from stackalytics.processor import user_processor + + +class TestUserProcessor(testtools.TestCase): + def setUp(self): + super(TestUserProcessor, self).setUp() + + def test_update_user(self): + user = { + "launchpad_id": "user", + "companies": [ + { + "company_name": "Rackspace", + "end_date": "2011-Nov-20" + }, + { + "company_name": "IBM", + "end_date": None + } + ], + "user_name": "John Smith", + "emails": ["john@smith.com"] + } + + stored_user = { + "launchpad_id": "user", + "companies": [ + { + "company_name": "Rackspace", + "end_date": "2011-Nov-20" + }, + { + "company_name": "IBM", + "end_date": None + } + ], + "user_name": "Johnny", + "emails": ["john@smith.com", "mapped_email@gmail.com"], + "static": True + } + + updated_user = user_processor.update_user_profile(stored_user, user) + + # merge emails from profile with those discovered by Stackalytics + self.assertEqual(set(stored_user['emails']), + set(updated_user['emails'])) + # name from the profile has higher priority over mined + self.assertEqual(user['user_name'], updated_user['user_name']) + # static flag must present + self.assertTrue(updated_user.get('static')) + + def test_update_user_unknown_user(self): + user = { + "launchpad_id": "user", + "companies": [ + { + "company_name": "Rackspace", + "end_date": "2011-Nov-20" + }, + { + "company_name": "IBM", + "end_date": None + } + ], + "user_name": "John Smith", + "emails": ["john@smith.com"] + } + + stored_user = None + + updated_user = user_processor.update_user_profile(stored_user, user) + self.assertTrue(updated_user.get('static'))