Skip to content

Instantly share code, notes, and snippets.

@raecoo
Last active December 20, 2018 09:33
Show Gist options
  • Save raecoo/931474f794990d1bf63a67d16cb5949e to your computer and use it in GitHub Desktop.
Save raecoo/931474f794990d1bf63a67d16cb5949e to your computer and use it in GitHub Desktop.
checkpoint.py
# -*- coding: UTF-8 -*-
import requests
import json
def generate_header(url):
import hashlib
hash_object = hashlib.sha1(url + 'conviva95a')
return {'Authorization': 'Bearer ' + hash_object.hexdigest()}
stages = {
# 'local': 'http://localhost:5000/v%(version)s/customers/%(customer)s',
'dev': 'https://5twvbk64l3.execute-api.us-east-1.amazonaws.com/%(stage)s/v%('
'version)s/customers/%(customer)s',
# 'staging': 'https://7vwpr392j1.execute-api.us-east-1.amazonaws.com/',
}
# segment isn’t in here until the segment feature released
target_fields = ['brand', 'day_part', 'content_type', 'segment_name', 'device', 'dma',
'country', 'day_of_week', 'content']
# target_fields = ['brand']
associate_fields = {
'turner_tbs_tnt': {
'state': {
'associate_key': 'country',
'associate_value': 'united%20states',
},
'custom_tag_value': {
'associate_key': 'custom_tag_name',
'associate_value': 'freeview',
}
},
'hulu': {
'state': {
'associate_key': 'country',
'associate_value': 'united%20states',
},
'custom_tag_value': {
'associate_key': 'custom_tag_name',
'associate_value': 'isLiveSubscriber',
}
},
}
dev_endpoint_base_url = "/%(customer)s/suggestions/%(field)s" % {
'customer': 'hulu', 'field': 'test', 'other': 'ss'}
customer_with_params = {
'turner_tbs_tnt': {'customer': 'turner_tbs_tnt'},
'hulu': {'customer': 'hulu', 'custom_tag_name': ''},
}
api_endpoints = ['/suggestions/']
for stage, base_url in stages.items():
for customer, params in customer_with_params.items():
for field in target_fields:
for api in api_endpoints:
params['stage'] = stage
params['version'] = 1
request_url = (base_url + api + field) % params
headers = generate_header(request_url)
resp = requests.get(url=request_url, headers=headers)
curl_params = {'url': request_url, 'token': headers.get('Authorization')}
print('\n')
print "CMD: curl -X GET \"%(url)s\" -H 'Authorization: %(token)s'" % curl_params
if resp.status_code == 200:
print(json.dumps(resp.json(), sort_keys=True, indent=2))
else:
print resp.status_code, resp.json()
@raecoo
Copy link
Author

raecoo commented Dec 20, 2018

/*
  Segment Bubble
  Replace the segment bubbles by Backend Segment API.

  Author: lcao@conviva.com
  Usage:

  In Lookder Visualization section, create a new visualization named Segment Bubble and set the main file as https://d1aon3wttpl4f7.cloudfront.net/visualization/segment_bubble.js and set the following dependencies:

  https://d1aon3wttpl4f7.cloudfront.net/dependencies/jquery-2.1.1.js
  https://d1aon3wttpl4f7.cloudfront.net/dependencies/utils.js
  https://d1aon3wttpl4f7.cloudfront.net/dependencies/sha1.min.js
  https://d1aon3wttpl4f7.cloudfront.net/dependencies/d3-4.13.0.js
*/

var jsonFormat = function(data) {
    var resultArr = [];
    for (var i = 0; i < data.length; i++ ) {
        var obj = {
            id: data[i].segment_name,
            // name: data[i].segment_name,
            value: data[i].audience_size,
            sampling_factor: data[i].sampling_factor
        };
        resultArr.push(obj);
    }
    console.log(resultArr);
    return resultArr;
};

looker.plugins.visualizations.add({
    id: 'Segment Bubble',
    label: 'Segment Bubble',
    options: {},
    _s: this,

    create: function(element, config) {
        this._svg = d3.select(element).append("svg");
    },
    update: function(data, element, config, queryResponse) {
        console.log(element);
        if (data.length === 0) {
            this.addError({title: 'No Result'});
            return;
        }
        let _widget_this = this;
        let _element = element;

        const dimension_customer_name = queryResponse.fields.dimension_like[0]['name'];
        const customerName = data[0][dimension_customer_name].value.toLowerCase();
        const endpoint = backend_api_host_with_version() + "/customers/" + customerName + "/segments";

        jQuery.ajax({
            beforeSend: function(request) {
                request.setRequestHeader("Accept", 'application/json');
                request.setRequestHeader("Content-Type", 'application/json');
                request.setRequestHeader("Authorization", 'Bearer ' + backend_api_token(endpoint));
            },
            dataType: "json",
            url: endpoint,
            timeout: 3000,
            success: function(result) {
                if (result.code == 200) {
                    var classes = jsonFormat(result.data);
                    // let width = _element.clientWidth;
                    // let height = _element.clientHeight;
                    let width = 600;
                    let height = 200;
                    let svg = _widget_this._svg
                        .attr("width", "100%")
                        .attr("height", "100%")
                        .attr("text-anchor", "middle")
                        .attr("font-family","sans-serif")

                    var format = d3.format(",d");
                    var color = d3.scaleOrdinal(d3.schemeCategory20c);
                    var pack = d3.pack()
                        .size([width, height])
                        .padding(1.5);

                    var root = d3.hierarchy({children: classes});
                        .sum(function(d) { return d.value; })
                        .each(function(d) {
                            if (id = d.data.id) {
                                d.id = d.data.id;
                                d.package = id.slice();
                                d.class = id.slice();
                            }
                        });

                    var node = svg.selectAll(".node")
                    .data(pack(root))
                    .enter().append("g")
                    .attr("class", "node")
                    .attr("transform", function(d) {
                        return "translate(" + d.x + "," + d.y + ")"; });

                    node.append("circle")
                    .attr("id", function(d) { return d.id; })
                    .attr("r", function(d) { return d.r; })
                    .attr("padding", "30")
                    .style("fill", function(d) { return color(d.package); });

                    node.append("clipPath")
                    .attr("id", function(d) { return "clip-" + d.id; })
                    .append("use")
                    .attr("xlink:href", function(d) { return "#" + d.id; });

                    node.append("text")
                    .attr("clip-path", function(d) { return "url(#clip-" + d.id + ")"; })
                    .selectAll("tspan")
                    .data(function(d) { return d.class.replace("."," ").split(/(?=[A-Z][^A-Z])/g); })
                    .enter().append("tspan")
                    .attr("x", 0)
                    .attr("y", (d,i,nodes) => `${i - nodes.length / 2 + 0.8}em`)
                    .text(function(d) {
                          return d;
                    });

                    node.append("title")
                    .text(function(d) { return decodeURI(d.id.replace("."," ")) + "\n" + format(d.value); });
                }else {
                    _widget_this.addError({title: "Cannot get the segment bubble"});
                    return;
                };
            }
        });
    },
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment