def test_metric_as_dict(db_connection): Metric.create(name='avg_monthly_users', value=1200) Metric.create(name='avg_monthly_pageviews', value=6400) assert Metric.as_dict() == { 'avg_monthly_users': 1200, 'avg_monthly_pageviews': 6400, }
def main(): google_analytics_metrics = fetch_from_google_analytics() mailchimp_metrics = fetch_from_mailchimp() with db: Metric.drop_table() Metric.create_table() google_analytics_metric_names = [ 'avg_monthly_users', 'avg_monthly_pageviews', 'avg_monthly_handbook_users', 'avg_monthly_handbook_pageviews', 'avg_monthly_handbook_logo_clicks', 'avg_monthly_jobs_users', ] for name in google_analytics_metric_names: Metric.create(name=name, value=google_analytics_metrics[name]) Metric.create(name='subscribers', value=mailchimp_metrics['subscribers']) JobMetric.drop_table() JobMetric.create_table() for url, value in google_analytics_metrics['users_per_job'].items(): try: job = Job.get_by_url(url) JobMetric.create(job=job, name='users', value=value) except Job.DoesNotExist: pass for url, value in google_analytics_metrics['pageviews_per_job'].items( ): try: job = Job.get_by_url(url) JobMetric.create(job=job, name='pageviews', value=value) except Job.DoesNotExist: pass for url, value in google_analytics_metrics[ 'applications_per_job'].items(): try: job = Job.get_by_url(url) JobMetric.create(job=job, name='applications', value=value) except Job.DoesNotExist: pass users_per_external_url = merge_metric_dicts( google_analytics_metrics['users_per_external_job'], mailchimp_metrics['users_per_external_url']) for url, value in users_per_external_url.items(): try: job = Job.get_by_link(url) JobMetric.create(job=job, name='users', value=value) except Job.DoesNotExist: pass pageviews_per_external_url = merge_metric_dicts( google_analytics_metrics['pageviews_per_external_job'], mailchimp_metrics['pageviews_per_external_url']) for url, value in pageviews_per_external_url.items(): try: job = Job.get_by_link(url) JobMetric.create(job=job, name='pageviews', value=value) except Job.DoesNotExist: pass LogoMetric.drop_table() LogoMetric.create_table() for url, value in google_analytics_metrics['clicks_per_logo'].items(): try: logo = Logo.get_by_url(url) LogoMetric.create(logo=logo, name='clicks', value=value) except Logo.DoesNotExist: pass for logo in Logo.listing(): values = google_analytics_metrics['handbook_users_per_date'] metric = LogoMetric.from_values_per_date(logo, 'users', values) metric.save() values = google_analytics_metrics['handbook_pageviews_per_date'] metric = LogoMetric.from_values_per_date(logo, 'pageviews', values) metric.save()