Example #1
0
 def test_refinement(self):
     exp = lookml.Explore('exp')
     lookml.mkdir_force('.tmp/scratch')
     with open('.tmp/scratch/refinement_test.model.lkml', 'w') as f:
         f.write(
             '''explore: refine_ex {}'''
         )
     myFile = lookml.File('.tmp/scratch/refinement_test.model.lkml')
     refine_ex = myFile.exps.refine_ex
     print(type(refine_ex))
     # refine_ex.addProperty('aggregate_table',{'materialization':{'datagroup_trigger':'orders_datagroup'}})
     print(refine_ex)
     # refine_ex.addProperty('aggregate_table','foo')
     print(myFile)
Example #2
0
    def test_adding_property(self):
        v = lookml.View('test')
        v + '''
                derived_table: {
                    explore_source: order_items {
                    column: order_id {field: order_items.order_id_no_actions }
                    column: items_in_order { field: order_items.count }
                    column: order_amount { field: order_items.total_sale_price }
                    column: order_cost { field: inventory_items.total_cost }
                    column: user_id {field: order_items.user_id }
                    column: created_at {field: order_items.created_raw}
                    column: order_gross_margin {field: order_items.total_gross_margin}
                    derived_column: order_sequence_number {
                        sql: RANK() OVER (PARTITION BY user_id ORDER BY created_at) ;;
                    }
                    }
                    datagroup_trigger: ecommerce_etl
                }
        '''
        v + 'dimension: id {}'
        v.id + 'sql: ${TABLE}.id ;;'
        for item in ('a', 'b', 'c'):
            v + f'''
                dimension: {item}_id {{ 
                        sql: {v.id.__refs__} + {item} ;;
                    }}'''

            v + f'''measure: sum_of_{item} {{
                type: sum
                sql: ${{{item}_id}};;
            }}
            '''
        for f in v.measures():
            if f.type.value == 'sum':
                f.addTag('my function is to add') 
        
        ex = lookml.Explore(v.name)
        ex + '''join: test_2 {
                    from: test
                    type: left_outer
                    relationship: one_to_many
                    sql_on: ${testid} = ${test_2.id};;
                 }
        '''
        ex.test_2 + 'sql_on: foo ;;'
        F = lookml.File(ex)
        F + v
        print(F)
Example #3
0
    def test_join_back_an_ndt(self):
        v = lookml.View('order_items')
        v + '''
            sql_table_name: public.order_items ;;
            dimension: id {
                primary_key: yes
            }
            dimension: state {}
            dimension: sale_price {}
            parameter: {dynamic_dim_selector} {
                type: unquoted
            #     suggestions: ["Brand","Category","Department"]
                allowed_value: {
                label: "Category"
                value: "Category"
                }
                allowed_value: {
                label: "Brand"
                value: "Brand"
                }
                allowed_value: {
                label: "Department"
                value: "Department"
                }
                allowed_value: {
                label: "State"
                value: "State"
                }
            }
            dimension: user_id {}
            dimension: inventory_item_id { 
                sql: ${TABLE}.inventory_item_id ;; 
            }
            dimension: new_dimension {
                type: string
                sql:
                    {% if order_items.dynamic_dim_selector._parameter_value == 'Brand' %} ${products.brand}
                    {% elsif order_items.dynamic_dim_selector._parameter_value == 'Category' %}  ${products.category}
                    {% elsif order_items.dynamic_dim_selector._parameter_value == 'Department' %} ${products.department}
                    {% elsif order_items.dynamic_dim_selector._parameter_value == 'State' %} ${users.state}
                    {% else %} 'N/A'
                    {% endif %}
                ;;
            }
            measure: total_sale_price {
                type: sum
                sql: ${sale_price} ;;
            }
        '''
        ex = lookml.Explore(v.name)
        agg = lookml.View('agg')
        agg + '''
                derived_table: {
                    explore_source: order_items {
                    column: new_dimension {field: order_items.new_dimension}
                    column: total_sale_price {field: order_items.total_sale_price}
                    derived_column: rank {
                        sql: ROW_NUMBER() OVER (ORDER BY total_sale_price DESC) ;;
                    }
                    # bind_all_filters: yes
                    bind_filters: {
                        from_field: order_items.{dynamic_dim_selector}
                        to_field: order_items.{dynamic_dim_selector}
                    }
                    # bind_filters: {
                    #     from_field: order_items.created_date
                    #     to_field: order_items.created_date
                    # }
                    }
                }
                dimension: new_dimension {
                    sql: ${TABLE}.new_dimension ;;
                }
                dimension: rank {
                    type: number
                    hidden: yes
                }

                filter: tail_threshold {
                    type: number
                    hidden: yes
                }

                dimension: stacked_rank {
                    type: string
                    sql:
                            CASE
                            WHEN ${rank} < 10 then '0' || ${rank} || ') '|| ${new_dimension}
                            ELSE ${rank} || ') ' || ${new_dimension}
                            END
                    ;;
                }

                dimension: ranked_brand_with_tail {
                    type: string
                    sql:
                        CASE WHEN {% condition tail_threshold %} ${rank} {% endcondition %} THEN ${stacked_rank}
                        ELSE 'x) Other'
                        END

                    ;;
                }

                dimension: total_sale_price {
                    value_format: "$#,##0.00"
                    type: number
                }
        '''

        ex + '''
            join: inventory_items {
                type: left_outer
                relationship: one_to_many
                sql_on: ${order_items.inventory_item_id} = ${inventory_items.id} ;;
            }
            join: products {
                type: left_outer
                sql_on: ${inventory_items.product_id} = ${products.id} ;;
                relationship: many_to_one
            }
              join: users {
                type: left_outer
                sql_on: ${order_items.user_id} = ${users.id} ;;
                relationship: many_to_one
                      }
              join: agg {
                type: left_outer
                relationship: many_to_one
                sql_on: ${order_items.new_dimension}  = ${agg.new_dimension};;
            }
        '''
        myModel = lookml.File(ex)
        myModel + v
        myModel + agg
        myModel.properties.addProperty('connection', 'snowlooker')
        myModel.properties.addProperty('include', 'views/*.lkml')
        myModel.name = 'core2.model.lkml'
        proj = lookml.Project(
                 repo= 'russlooker/oi'
                ,access_token=config['github']['access_token']
                ,looker_host="https://profservices.dev.looker.com/"
                ,looker_project_name="test_pylookml"
        )

        proj.put(myModel)
        proj.deploy()
Example #4
0
 def create_explore(file_name):
     ex = lookml.Explore(file_name)
     ex.setProperty('always_filter','{filters: {\n field: event_date \n value: "'+ config.get('start_date') + ' to ' + config.get('end_date')+'"}}')
     print(ex)
     return(ex)
Example #5
0
        tmp_view + dim

    else:
        dim = l.Dimension(data['column_name'])
        dim.set_Field_Level_Permission(f'eid_{entity_id}')
        dim.setType('string')
        # dim.hide()
        tmp_view + dim
###
# Adding a series of measures of type sum with field level permissioning
    measure = l.Measure('total_' + data['column_name']).setProperty(
        'sql', '${' + data['column_name'] + '}').setType('sum')
    measure.set_Field_Level_Permission(f'eid_{entity_id}')
    tmp_view + measure

tmp_view.write()

eidAccessGrants = list(set([data['entity_id'] for data in view_data]))
tmp_model = l.Model('access_grant_model').setFolder(OUTPUT_DIR)
tmp_model.setConnection('thelook_events_redshift')

explore = l.Explore('test')

for grant in eidAccessGrants:
    field = l.Field_Level_Permissions(f'eid_{grant}')
    field.set_User_Attribute('entity_id')
    field.set_Allowed_Value(f'eid_{grant}')
    tmp_model.addAccessGrant(field)

tmp_model.write()
print(tmp_model)