""" This script compares the data for social timeseries endpoints backed by datasources: 1. neo4j (old way) 2. snowflake (new way) By default it uses graphql on QA. In order to make this thing work you need to set ENV variables in .env ORCHARD_IDENTITY_ID = (uuid) which is your account id in the insights ORCHARD_PROFILE_ID = (int) can be found in dev console in chrome: application -> local storage -> user traits """ import os import dotenv import pytest dotenv.load_dotenv() @pytest.fixture def graphql_endpoint(): headers = { "orchard-identity-id": os.environ['ORCHARD_IDENTITY_ID'], "orchard-profile-id": os.environ['ORCHARD_PROFILE_ID'], "orchard-profile-type": "InsightsProfile", "apollographql-client-name": "test-frontend-insights", "apollographql-client-version": "1.0.0", "cache-control": "no-cache" } from sgqlc.endpoint.http import HTTPEndpoint url = os.environ.get("GRAPHQL_URL", "https://qa-graphql-gateway.theorchard.io/graphql") yield HTTPEndpoint(url, headers) @pytest.mark.parametrize('participant_id, date_lte, date_gte', [ ("37d93121-6b6b-44f9-a383-e70ebdcb0c22", "2021-03-20", "2021-03-15"), ("37d93121-6b6b-44f9-a383-e70ebdcb0c22", "2020-03-01", "2020-01-01"), ("37d93121-6b6b-44f9-a383-e70ebdcb0c22", "2020-05-01", "2020-03-01"), ("37d93121-6b6b-44f9-a383-e70ebdcb0c22", "2020-07-01", "2020-05-01"), ("37d93121-6b6b-44f9-a383-e70ebdcb0c22", "2020-09-01", "2020-07-01"), ("37d93121-6b6b-44f9-a383-e70ebdcb0c22", "2020-11-01", "2020-09-01"), ("37d93121-6b6b-44f9-a383-e70ebdcb0c22", "2021-01-01", "2020-11-01"), ("37d93121-6b6b-44f9-a383-e70ebdcb0c22", "2021-03-01", "2021-01-01"), ("bc5d3083-ec53-4ab5-9756-50511d39e33c", "2021-03-20", "2021-03-15"), ("bc5d3083-ec53-4ab5-9756-50511d39e33c", "2020-03-01", "2020-01-01"), ("bc5d3083-ec53-4ab5-9756-50511d39e33c", "2020-05-01", "2020-03-01"), ("bc5d3083-ec53-4ab5-9756-50511d39e33c", "2020-07-01", "2020-05-01"), ("bc5d3083-ec53-4ab5-9756-50511d39e33c", "2020-09-01", "2020-07-01"), ("bc5d3083-ec53-4ab5-9756-50511d39e33c", "2020-11-01", "2020-09-01"), ("bc5d3083-ec53-4ab5-9756-50511d39e33c", "2021-01-01", "2020-11-01"), ("bc5d3083-ec53-4ab5-9756-50511d39e33c", "2021-03-01", "2021-01-01"), ("7745c66a-e044-4330-9452-0b44d7362424", "2020-03-01", "2020-01-01"), ("7745c66a-e044-4330-9452-0b44d7362424", "2020-05-01", "2020-03-01"), ("7745c66a-e044-4330-9452-0b44d7362424", "2020-07-01", "2020-05-01"), ("7745c66a-e044-4330-9452-0b44d7362424", "2020-09-01", "2020-07-01"), ("7745c66a-e044-4330-9452-0b44d7362424", "2020-11-01", "2020-09-01"), ("7745c66a-e044-4330-9452-0b44d7362424", "2021-01-01", "2020-11-01"), ("7745c66a-e044-4330-9452-0b44d7362424", "2021-03-01", "2021-01-01"), ]) def test_social_accounts_timeseries(participant_id, date_lte, date_gte, graphql_endpoint): old_query = """ query GlobalParticipantSocials($id: ID!, $startDate: String, $endDate: String){ globalParticipantByGpId (id: $id) { id participant { id socialStats { monthlyListeners popularity followers } socialAccounts { id platform url timeseries(filter: { timestamp_gte: { formatted: $startDate }, timestamp_lte: { formatted: $endDate } } ) { followers monthlyListeners popularity views timestamp { formatted } } } } } } """ new_query = """ query GlobalParticipantSocialsV2($id: ID!, $startDate: String, $endDate: String){ globalParticipantByGpId (id: $id) { id participant { id socialStats { monthlyListeners popularity followers } socialAccounts { id platform url timeseriesV2(startDate: $startDate, endDate: $endDate) { followers monthlyListeners popularity views timestamp } } } } } """ variables = { "id": participant_id, "startDate": date_gte, "endDate": date_lte, "filter": { "timestamp_gte": { "formatted": date_gte }, "timestamp_lte": { "formatted": date_lte } } } response = graphql_endpoint(old_query, variables) assert not 'errors' in response, response['errors'] old_data = response['data']['globalParticipantByGpId'][0]['participant']['socialAccounts'] response = graphql_endpoint(new_query, variables) assert not 'errors' in response, response['errors'] new_data = response['data']['globalParticipantByGpId'][0]['participant']['socialAccounts'] # convert new data to the old data structure for social_account in new_data: social_account['timeseries'] = social_account['timeseriesV2'] for ts in social_account['timeseries']: ts['timestamp'] = {'formatted': ts['timestamp']} del social_account['timeseriesV2'] new_social_account_by_url = {a['url']:a for a in new_data} old_social_account_by_url = {a['url']:a for a in old_data} # let's compare the data counter = 0 for url in old_social_account_by_url: old_social_account = old_social_account_by_url[url] new_social_account = new_social_account_by_url[url] counter += 1 print(f"{counter} OLD: {old_social_account['url']} NEW: {new_social_account['url']}") # extract timeseries old_timeseries_list = old_social_account['timeseries'] new_timeseries_list = new_social_account['timeseries'] # create dicts for timeseries old_timeseries = {v['timestamp']['formatted']:v for v in old_timeseries_list} new_timeseries = {v['timestamp']['formatted']:v for v in new_timeseries_list} for timestamp, old_value in old_timeseries.items(): if timestamp.startswith('2020-09-2') \ or timestamp.startswith('2020-09-3') \ or timestamp.startswith('2020-10-0') \ or timestamp.startswith('2020-10-20'): # DS-3330 - missing data in chartmetrics continue assert new_timeseries[timestamp] == old_value # compare all of the data excpet timeseries del old_social_account['timeseries'] del new_social_account['timeseries'] assert old_social_account == new_social_account @pytest.mark.parametrize('participant_id, date_lte, date_gte', [ ("37d93121-6b6b-44f9-a383-e70ebdcb0c22", "2021-03-20", "2021-03-15"), ("37d93121-6b6b-44f9-a383-e70ebdcb0c22", "2020-03-01", "2020-01-01"), ("37d93121-6b6b-44f9-a383-e70ebdcb0c22", "2020-05-01", "2020-03-01"), ("37d93121-6b6b-44f9-a383-e70ebdcb0c22", "2020-07-01", "2020-05-01"), ("37d93121-6b6b-44f9-a383-e70ebdcb0c22", "2020-09-01", "2020-07-01"), ("37d93121-6b6b-44f9-a383-e70ebdcb0c22", "2020-11-01", "2020-09-01"), ("37d93121-6b6b-44f9-a383-e70ebdcb0c22", "2021-01-01", "2020-11-01"), ("37d93121-6b6b-44f9-a383-e70ebdcb0c22", "2021-03-01", "2021-01-01"), ("bc5d3083-ec53-4ab5-9756-50511d39e33c", "2021-03-20", "2021-03-15"), ("bc5d3083-ec53-4ab5-9756-50511d39e33c", "2020-03-01", "2020-01-01"), ("bc5d3083-ec53-4ab5-9756-50511d39e33c", "2020-05-01", "2020-03-01"), ("bc5d3083-ec53-4ab5-9756-50511d39e33c", "2020-07-01", "2020-05-01"), ("bc5d3083-ec53-4ab5-9756-50511d39e33c", "2020-09-01", "2020-07-01"), ("bc5d3083-ec53-4ab5-9756-50511d39e33c", "2020-11-01", "2020-09-01"), ("bc5d3083-ec53-4ab5-9756-50511d39e33c", "2021-01-01", "2020-11-01"), ("bc5d3083-ec53-4ab5-9756-50511d39e33c", "2021-03-01", "2021-01-01"), ("7745c66a-e044-4330-9452-0b44d7362424", "2020-03-01", "2020-01-01"), ("7745c66a-e044-4330-9452-0b44d7362424", "2020-05-01", "2020-03-01"), ("7745c66a-e044-4330-9452-0b44d7362424", "2020-07-01", "2020-05-01"), ("7745c66a-e044-4330-9452-0b44d7362424", "2020-09-01", "2020-07-01"), ("7745c66a-e044-4330-9452-0b44d7362424", "2020-11-01", "2020-09-01"), ("7745c66a-e044-4330-9452-0b44d7362424", "2021-01-01", "2020-11-01"), ("7745c66a-e044-4330-9452-0b44d7362424", "2021-03-01", "2021-01-01"), ]) def test_social_accounts_v2_timeseries(participant_id, date_lte, date_gte, graphql_endpoint): """ Test improved socialAccountsV2, the improved version of socialAccounts """ old_query = """ query GlobalParticipantSocials($id: ID!, $startDate: String, $endDate: String){ globalParticipantByGpId (id: $id) { id participant { id socialStats { monthlyListeners popularity followers } socialAccounts { platform url timeseries(filter: { timestamp_gte: { formatted: $startDate }, timestamp_lte: { formatted: $endDate } } ) { followers monthlyListeners popularity views timestamp { formatted } } } } } } """ new_query = """ query GlobalParticipantSocialsV2($id: ID!, $startDate: String, $endDate: String){ globalParticipantByGpId (id: $id) { id participant { id socialStats { monthlyListeners popularity followers } socialAccountsV2(startDate: $startDate, endDate: $endDate) { platform url timeseriesV2 { followers monthlyListeners popularity views timestamp } } } } } """ variables = { "id": participant_id, "startDate": date_gte, "endDate": date_lte, "filter": { "timestamp_gte": { "formatted": date_gte }, "timestamp_lte": { "formatted": date_lte } } } response = graphql_endpoint(old_query, variables) assert not 'errors' in response, response['errors'] old_data = response['data']['globalParticipantByGpId'][0]['participant']['socialAccounts'] response = graphql_endpoint(new_query, variables) assert not 'errors' in response, response['errors'] new_data = response['data']['globalParticipantByGpId'][0]['participant']['socialAccountsV2'] # convert new data to the old data structure for social_account in new_data: social_account['timeseries'] = social_account['timeseriesV2'] for ts in social_account['timeseries']: ts['timestamp'] = {'formatted': ts['timestamp']} del social_account['timeseriesV2'] new_social_account_by_url = {a['url']:a for a in new_data} old_social_account_by_url = {a['url']:a for a in old_data} # let's compare the data counter = 0 for url in old_social_account_by_url: old_social_account = old_social_account_by_url[url] if not old_social_account.get('timeseries'): # skip if there is no data in old result print(f'SKIP no data for {url}') continue new_social_account = new_social_account_by_url[url] counter += 1 print(f"{counter} OLD: {old_social_account['url']} NEW: {new_social_account['url']}") # extract timeseries old_timeseries_list = old_social_account['timeseries'] new_timeseries_list = new_social_account['timeseries'] # create dicts for timeseries old_timeseries = {v['timestamp']['formatted']:v for v in old_timeseries_list} new_timeseries = {v['timestamp']['formatted']:v for v in new_timeseries_list} for timestamp, old_value in old_timeseries.items(): if timestamp.startswith('2020-09-2') \ or timestamp.startswith('2020-09-3') \ or timestamp.startswith('2020-10-0') \ or timestamp.startswith('2020-10-20'): # DS-3330 - missing data in chartmetrics continue assert new_timeseries[timestamp] == old_value # compare all of the data excpet timeseries del old_social_account['timeseries'] del new_social_account['timeseries'] assert old_social_account == new_social_account