Created
February 25, 2016 09:30
-
-
Save kirk91/611c9dc521ce1882522f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
from sqlalchemy import func | |
from moses.models import ( | |
DBSession, | |
Application, | |
Provider, | |
DeviceToken, | |
TaskProcess, | |
) | |
from moses.constants import PLATFORM_ANDROID | |
def main(): | |
# eleme_app = Application.get_by_package_name_and_platform( | |
# 'me.ele', PLATFORM_ANDROID | |
# ) | |
jpush_provider = Provider.get_by_name_and_platform( | |
'jpush', PLATFORM_ANDROID | |
) | |
igetui_provider = Provider.get_by_name_and_platform( | |
'igetui', PLATFORM_ANDROID | |
) | |
session = DBSession() | |
result = session.query(DeviceToken.device_id).filter( | |
# DeviceToken.app_id == eleme_app.id, | |
DeviceToken.provider_id == jpush_provider.id | |
) | |
jpush_device_ids = [device_id for (device_id, ) in result] | |
result = session.query(DeviceToken.device_id).filter( | |
# DeviceToken.app_id == eleme_app.id, | |
DeviceToken.provider_id == igetui_provider.id, | |
DeviceToken.device_id.in_(jpush_device_ids) | |
) | |
common_device_ids = [device_id for (device_id, ) in result] | |
print 'common devices number is: %d' % len(common_device_ids) | |
jpush_arrive_rate, jpush_send_count, jpush_arrive_count = \ | |
calculate_arrive_rate( | |
jpush_provider.id, common_device_ids | |
) | |
igetui_arrive_rate, igetui_send_count, igetui_arrive_count = \ | |
calculate_arrive_rate( | |
igetui_provider.id, common_device_ids | |
) | |
print 'Jpush send: %d arrive: %d, rate is: %.5f' % ( | |
jpush_send_count, jpush_arrive_count, jpush_arrive_rate | |
) | |
print 'Igetui send: %d arrive: %d, rate is %.5f' % ( | |
igetui_send_count, igetui_arrive_count, igetui_arrive_rate | |
) | |
def calculate_arrive_rate(provider_id, device_ids=None, start_at=None, end_at=None): | |
device_ids = device_ids or [] | |
start_at = start_at or '2016-01-29 00:00:00' | |
end_at = end_at or '2016-02-25 00:00:00' | |
session = DBSession() | |
cls = TaskProcess | |
def count(statuses): | |
return session.query( | |
func.count(cls.id) | |
).filter( | |
cls.provider_id == provider_id, | |
cls.device_id.in_(device_ids), | |
cls.status.in_(statuses), | |
cls.created_at >= start_at, | |
cls.created_at <= end_at | |
).scalar() | |
send_count = count((cls.STATUS_SUCCESS, cls.STATUS_ARRIVE, | |
cls.STATUS_READ)) | |
arrive_count = count((cls.STATUS_ARRIVE, cls.STATUS_READ)) | |
rate = float(arrive_count) / send_count if send_count else 0 | |
return rate, send_count, arrive_count | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment