!pip install git+https://github.com/google/starthinker


"""
Explanation: CM360 Log Audit
Downloads Campaign manager logs and allows audits.
License
Copyright 2020 Google LLC,
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
https://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.
Disclaimer
This is not an officially supported Google product. It is a reference implementation. There is absolutely NO WARRANTY provided for using this code. The code is Apache Licensed and CAN BE fully modified, white labeled, and disassembled by your team.
This code generated (see starthinker/scripts for possible source):
  - Command: "python starthinker_ui/manage.py colab"
  - Command: "python starthinker/tools/colab.py [JSON RECIPE]"
1. Install Dependencies
First install the libraries needed to execute recipes, this only needs to be done once, then click play.
End of explanation
"""


from starthinker.util.configuration import Configuration


CONFIG = Configuration(
  project="",
  client={},
  service={},
  user="/content/user.json",
  verbose=True
)



"""
Explanation: 2. Set Configuration
This code is required to initialize the project. Fill in required fields and press play.

If the recipe uses a Google Cloud Project:

Set the configuration project value to the project identifier from these instructions.


If the recipe has auth set to user:

If you have user credentials:
Set the configuration user value to your user credentials JSON.



If you DO NOT have user credentials:

Set the configuration client value to downloaded client credentials.



If the recipe has auth set to service:

Set the configuration service value to downloaded service credentials.
End of explanation
"""


FIELDS = {
  'auth_read':'user',  # Credentials used for reading data.
  'auth_write':'service',  # Credentials used for writing data.
  'accounts':[],  # Comma separated CM account ids.
  'days':7,  # Number of days to backfill the log, works on first run only.
  'recipe_slug':'',  # Google BigQuery dataset to create tables in.
}

print("Parameters Set To: %s" % FIELDS)


"""
Explanation: 3. Enter CM360 Log Audit Recipe Parameters

Wait for BigQuery->->->CM_... to be created.
Wait for BigQuery->->->Barnacle_... to be created, then copy and connect the following data sources.
Join the StarThinker Assets Group to access the following assets
Copy Barnacle Profile Advertiser Map and connect.
Copy Barnacle Profile Campaign Map and connect.
Copy Barnacle Profile Site Map and connect.
Copy Barnacle Profiles Connections and connect.
Copy Barnacle Report Delivery Profiles and connect.
Copy Barnacle Roles Duplicates and connect.
Copy Barnacle Roles Not Used and connect.
Copy Barnacle Site Contacts Profiles and connect.
If reports checked, copy Barnacle Profile Report Map and connect.
Copy Barnacle Report.
When prompted choose the new data sources you just created.
Or give these intructions to the client.
Modify the values below for your use case, can be done multiple times, then click play.
End of explanation
"""


from starthinker.util.configuration import execute
from starthinker.util.recipe import json_set_fields

TASKS = [
  {
    'dataset':{
      'description':'The dataset will hold log table, Create it exists.',
      'hour':[
        1
      ],
      'auth':{'field':{'name':'auth_write','kind':'authentication','order':1,'default':'service','description':'Credentials used for writing data.'}},
      'dataset':{'field':{'name':'recipe_slug','kind':'string','order':4,'default':'','description':'Name of Google BigQuery dataset to create.'}}
    }
  },
  {
    'dcm_log':{
      'description':'Will create tables with format CM_* to hold each endpoint via a call to the API list function. Exclude reports for its own task.',
      'hour':[
        2
      ],
      'auth':{'field':{'name':'auth_read','kind':'authentication','order':0,'default':'user','description':'Credentials used for reading data.'}},
      'accounts':{
        'single_cell':True,
        'values':{'field':{'name':'accounts','kind':'integer_list','order':2,'default':[],'description':'Comma separated CM account ids.'}}
      },
      'days':{'field':{'name':'days','kind':'integer','order':3,'default':7,'description':'Number of days to backfill the log, works on first run only.'}},
      'out':{
        'auth':{'field':{'name':'auth_write','kind':'authentication','order':1,'default':'service','description':'Credentials used for writing data.'}},
        'dataset':{'field':{'name':'recipe_slug','kind':'string','order':5,'default':'','description':'Google BigQuery dataset to create tables in.'}}
      }
    }
  }
]

json_set_fields(TASKS, FIELDS)

execute(CONFIG, TASKS, force=True)


"""
Explanation: 4. Execute CM360 Log Audit
This does NOT need to be modified unless you are changing the recipe, click play.
End of explanation
"""


