Add link to scheduled pipeline (#7584)

* Add link to scheduled pipeline

* Split utils into separate file

* Fix unit test

* Fix separator recursion
This commit is contained in:
Beto Dealmeida 2019-05-23 11:22:15 -07:00 committed by GitHub
parent f68f979052
commit 265e117a4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 164 additions and 14 deletions

View File

@ -911,6 +911,12 @@ To allow scheduled queries, add the following to your `config.py`:
'container': 'end_date',
},
],
# link to the scheduler; this example links to an Airflow pipeline
# that uses the query id and the output table as its name
'linkback': (
'https://airflow.example.com/admin/airflow/tree?'
'dag_id=query_${id}_${extra_json.schedule_info.output_table}'
),
},
}

View File

@ -0,0 +1,65 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* http://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.
*/
import { getNestedValue, interpolate } from '../../../src/showSavedQuery/utils';
describe('getNestedValue', () => {
it('is a function', () => {
expect(typeof getNestedValue).toBe('function');
});
it('works with simple ids', () => {
const obj = { a: '1' };
const id = 'a';
expect(getNestedValue(obj, id)).toEqual('1');
});
it('works with complex ids', () => {
const obj = { a: { b: '1' } };
const id = 'a.b';
expect(getNestedValue(obj, id)).toEqual('1');
});
it('works with other separators', () => {
const obj = { a: { b: { c: '1' } } };
const id = 'a__b__c';
const separator = '__';
expect(getNestedValue(obj, id, separator)).toEqual('1');
});
});
describe('interpolate', () => {
it('is a function', () => {
expect(typeof interpolate).toBe('function');
});
it('works with simple ids', () => {
const obj = { a: '1' };
// eslint-disable-next-line no-template-curly-in-string
const str = 'value: ${a}';
expect(interpolate(str, obj)).toEqual('value: 1');
});
it('works with complex ids', () => {
const obj = { a: { b: '1' } };
// eslint-disable-next-line no-template-curly-in-string
const str = 'value: ${a.b}';
expect(interpolate(str, obj)).toEqual('value: 1');
});
});

View File

@ -0,0 +1,20 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* http://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.
*/
.btn-add { display: none; }
.linkback { padding: 0 10px 20px 2px; }

View File

@ -19,25 +19,39 @@
import React from 'react';
import ReactDom from 'react-dom';
import Form from 'react-jsonschema-form';
import { interpolate } from 'src/showSavedQuery/utils';
import './index.css';
const scheduleInfoContainer = document.getElementById('schedule-info');
const bootstrapData = JSON.parse(scheduleInfoContainer.getAttribute('data-bootstrap'));
const schemas = bootstrapData.common.feature_flags.SCHEDULED_QUERIES;
const scheduleInfo = bootstrapData.common.extra_json.schedule_info;
const config = bootstrapData.common.feature_flags.SCHEDULED_QUERIES;
const query = bootstrapData.common.query;
const scheduleInfo = query.extra_json.schedule_info;
const linkback = config.linkback
? interpolate(config.linkback, query)
: null;
if (scheduleInfo && schemas) {
if (scheduleInfo && config) {
// hide instructions when showing schedule info
schemas.JSONSCHEMA.description = '';
config.JSONSCHEMA.description = '';
ReactDom.render(
<Form
schema={schemas.JSONSCHEMA}
uiSchema={schemas.UISCHEMA}
formData={scheduleInfo}
disabled
>
<br />
</Form>,
<div>
<Form
schema={config.JSONSCHEMA}
uiSchema={config.UISCHEMA}
formData={scheduleInfo}
disabled
>
<br />
</Form>
{linkback && <div className="linkback">
<a href={linkback}>
<i className="fa fa-link" />&nbsp;
Pipeline status
</a>
</div>}
</div>,
scheduleInfoContainer,
);
}

View File

@ -0,0 +1,44 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* http://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.
*/
export function getNestedValue(obj, id, separator = '.') {
/*
* Given a nested object and an id, return the nested value.
*
* > getNestedValue({a:{b:1}}, 'a.b')
* < 1
*/
const index = id.indexOf(separator);
if (index === -1) {
return obj[id];
}
const name = id.slice(0, index);
const rest = id.slice(index + separator.length);
return getNestedValue(obj[name], rest, separator);
}
export function interpolate(str, obj) {
/*
* Programmatic template string for interpolation.
*
* > interpolate('foo ${a.b}', {a:{b:1}})
* < "foo 1"
*/
return str.replace(/\$\{(.+?)\}/g, (match, id) => getNestedValue(obj, id));
}

View File

@ -122,11 +122,12 @@ class SavedQueryView(SupersetModelView, DeleteMixin):
def show(self, pk):
pk = self._deserialize_pk_if_composite(pk)
widgets = self._show(pk)
extra_json = self.datamodel.get(pk).extra_json
query = self.datamodel.get(pk).to_json()
query['extra_json'] = json.loads(query['extra_json'])
payload = {
'common': {
'feature_flags': get_feature_flags(),
'extra_json': json.loads(extra_json),
'query': query,
},
}