"use strict";(self.webpackChunkgrafana=self.webpackChunkgrafana||[]).push([[7537],{36812:(e,a,n)=>{n.r(a),n.d(a,{plugin:()=>I});var t,s,l=n(43215),r=n(68404),i=n(90923),o=n(69783),u=n(78837),h=n(29937),c=n(45916);function d(e){var a;let{value:n,onChange:t}=e;const[s,l]=(0,r.useState)(""),[i,u]=(0,r.useState)(null!==(a=n.query)&&void 0!==a?a:"");(0,r.useEffect)((()=>{l(JSON.stringify(Object.assign({},{query:"*",location:"",ds_uid:"",sort:"",tags:[],kind:[],explain:!1,facet:[{field:"kind"},{field:"tags"}],from:0,limit:20},n),null,2))}),[n]);const h=e=>{i!==n.query&&t(Object.assign({},n,{query:i}))},d=e=>{try{var a;const n=JSON.parse(e);t(n),u(null!==(a=n.query)&&void 0!==a?a:"")}catch(a){console.log("UNABLE TO parse search",e,a)}};return(0,c.jsxs)(c.Fragment,{children:[(0,c.jsx)(o.InlineFieldRow,{children:(0,c.jsx)(o.InlineField,{label:"Query",grow:!0,labelWidth:12,children:(0,c.jsx)(o.Input,{placeholder:"Everything",value:i,onChange:e=>u(e.currentTarget.value),onKeyDown:e=>{"Enter"===e.key&&h()},onBlur:h,spellCheck:!1})})}),(0,c.jsx)(o.CodeEditor,{height:300,language:"json",value:s,onBlur:d,onSave:d,showMiniMap:!1,showLineNumbers:!0})]})}function g(e,a,n){return a in e?Object.defineProperty(e,a,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[a]=n,e}class f extends r.PureComponent{constructor(e){super(e),g(this,"state",{channels:[],channelFields:{}}),g(this,"queryTypes",[{label:"Random Walk",value:h.hR.RandomWalk,description:"Random signal within the selected time range"},{label:"Live Measurements",value:h.hR.LiveMeasurements,description:"Stream real-time measurements from Grafana"},{label:"List public files",value:h.hR.List,description:"Show directory listings for public resources"}]),g(this,"onQueryTypeChange",(e=>{const{onChange:a,query:n,onRunQuery:t}=this.props;a(Object.assign({},n,{queryType:e.value})),t(),this.loadChannelInfo()})),g(this,"onChannelChange",(e=>{const{onChange:a,query:n,onRunQuery:t}=this.props;a(Object.assign({},n,{channel:null==e?void 0:e.value})),t()})),g(this,"onFieldNamesChange",(e=>{var a,n;const{onChange:t,query:s,onRunQuery:l}=this.props;let r=[];if(Array.isArray(e)?r=e.map((e=>e.value)):e.value&&(r=[e.value]),1===r.length&&(null===(a=s.filter)||void 0===a||null===(n=a.fields)||void 0===n||!n.length)&&s.channel){var i;const e=(null!==(i=this.state.channelFields[s.channel])&&void 0!==i?i:[]).find((e=>"time"===e.value||"Time"===e.value));e&&e.value&&e.value!==r[0]&&(r=[e.value,...r])}t(Object.assign({},s,{filter:Object.assign({},s.filter,{fields:r})})),l()})),g(this,"checkAndUpdateValue",((e,a)=>{const{onChange:n,query:t,onRunQuery:s}=this.props;if("buffer"===e){let e;if(a)try{e=1e3*l.rangeUtil.intervalToSeconds(a)}catch(e){console.warn("ERROR",e)}n(Object.assign({},t,{buffer:e}))}else n(Object.assign({},t,{[e]:a}));s()})),g(this,"handleEnterKey",(e=>{"Enter"===e.key&&this.checkAndUpdateValue("buffer",e.target.value)})),g(this,"handleBlur",(e=>{this.checkAndUpdateValue("buffer",e.target.value)})),g(this,"onFolderChanged",(e=>{const{onChange:a,query:n,onRunQuery:t}=this.props;a(Object.assign({},n,{path:null==e?void 0:e.value})),t()})),g(this,"onSearchChange",(e=>{const{query:a,onChange:n,onRunQuery:t}=this.props;n(Object.assign({},a,{search:e})),t()})),i.config.featureToggles.panelTitleSearch&&u.Oh&&this.queryTypes.push({label:"Search",value:h.hR.Search,description:"Search for grafana resources"})}loadChannelInfo(){(0,i.getBackendSrv)().fetch({url:"api/live/list"}).subscribe({next:e=>{var a;const n=null===(a=e.data)||void 0===a?void 0:a.channels;if(null!=n&&n.length){const e={},a=n.map((a=>{if(a.data){const n=new Set,t=(0,l.dataFrameFromJSON)(a.data);for(const e of t.fields)n.add(e.name);e[a.channel]=Array.from(n).map((e=>({value:e,label:e})))}return{value:a.channel,label:a.channel+" ["+a.minute_rate+" msg/min]"}}));this.setState({channelFields:e,channels:a})}}})}loadFolderInfo(){const e={targets:[{queryType:h.hR.List,refId:"A"}]};(0,i.getDataSourceSrv)().get("-- Grafana --").then((a=>{a.query(e).subscribe({next:e=>{if(e.data.length){const a=e.data[0].fields[0].values.toArray().map((e=>({value:e,label:e})));this.setState({folders:a})}}})}))}componentDidMount(){this.loadChannelInfo()}renderMeasurementsQuery(){var e;let{channel:a,filter:n,buffer:s}=this.props.query,{channels:r,channelFields:i}=this.state,u=r.find((e=>e.value===a));a&&!u&&(u={value:a,label:a,description:`Connected to ${a}`},r=[u,...r]);const h=new Set,d=a&&null!==(e=i[a])&&void 0!==e?e:[];if(null!=n&&n.fields)for(const e of n.fields)h.has(e)||(d.push({value:e,label:`${e} (not loaded)`,description:"Configured, but not found in the query results"}),h.add(e));let g="";return s&&(g=l.rangeUtil.secondsToHms(s/1e3)),(0,c.jsxs)(c.Fragment,{children:[(0,c.jsx)("div",{className:"gf-form",children:(0,c.jsx)(o.InlineField,{label:"Channel",grow:!0,labelWidth:12,children:(0,c.jsx)(o.Select,{options:r,value:u||"",onChange:this.onChannelChange,allowCustomValue:!0,backspaceRemovesValue:!0,placeholder:"Select measurements channel",isClearable:!0,noOptionsMessage:"Enter channel name",formatCreateLabel:e=>`Connect to: ${e}`})})}),a&&(0,c.jsxs)("div",{className:"gf-form",children:[(0,c.jsx)(o.InlineField,{label:"Fields",grow:!0,labelWidth:12,children:(0,c.jsx)(o.Select,{options:d,value:(null==n?void 0:n.fields)||[],onChange:this.onFieldNamesChange,allowCustomValue:!0,backspaceRemovesValue:!0,placeholder:"All fields",isClearable:!0,noOptionsMessage:"Unable to list all fields",formatCreateLabel:e=>`Field: ${e}`,isSearchable:!0,isMulti:!0})}),(0,c.jsx)(o.InlineField,{label:"Buffer",children:(0,c.jsx)(o.Input,{placeholder:"Auto",width:12,defaultValue:g,onKeyDown:this.handleEnterKey,onBlur:this.handleBlur,spellCheck:!1})})]}),t||(t=(0,c.jsx)(o.Alert,{title:"Grafana Live - Measurements",severity:"info",children:"This supports real-time event streams in Grafana core. This feature is under heavy development. Expect the interfaces and structures to change as this becomes more production ready."}))]})}renderListPublicFiles(){let{path:e}=this.props.query,{folders:a}=this.state;a||(a=[],this.loadFolderInfo());const n=a.find((a=>a.value===e));return e&&!n&&(a=[...a,{value:e,label:e}]),(0,c.jsx)(o.InlineFieldRow,{children:(0,c.jsx)(o.InlineField,{label:"Path",grow:!0,labelWidth:12,children:(0,c.jsx)(o.Select,{options:a,value:n||"",onChange:this.onFolderChanged,allowCustomValue:!0,backspaceRemovesValue:!0,placeholder:"Select folder",isClearable:!0,formatCreateLabel:e=>`Folder: ${e}`})})})}render(){var e;const a=Object.assign({},h.wi,this.props.query),{queryType:n}=a;return(0,c.jsxs)(c.Fragment,{children:[n===h.hR.Search&&(s||(s=(0,c.jsx)(o.Alert,{title:"Grafana Search",severity:"info",children:"Using this datasource to call the new search system is experimental, and subject to change at any time without notice."}))),(0,c.jsx)(o.InlineFieldRow,{children:(0,c.jsx)(o.InlineField,{label:"Query type",grow:!0,labelWidth:12,children:(0,c.jsx)(o.Select,{options:this.queryTypes,value:this.queryTypes.find((e=>e.value===n))||this.queryTypes[0],onChange:this.onQueryTypeChange})})}),n===h.hR.LiveMeasurements&&this.renderMeasurementsQuery(),n===h.hR.List&&this.renderListPublicFiles(),n===h.hR.Search&&(0,c.jsx)(d,{value:null!==(e=a.search)&&void 0!==e?e:{},onChange:this.onSearchChange})]})}}var p=n(82897),v=n(80890),y=n(73296),m=n(2937),b=n(10010),j=n(13011),C=n(22305),x=n(36636),S=n(67907),w=n(54361);const F=(0,c.jsx)("div",{children:"Specify a list of tags to match. To specify a key and value tag use `key:value` syntax."}),q=[{label:"Dashboard",value:h._$.Dashboard,description:"Query for events created on this dashboard and show them in the panels where they where created"},{label:"Tags",value:h._$.Tags,description:"This will fetch any annotation events that match the tags filter"}],R=[10,50,100,200,300,500,1e3,2e3].map((e=>({label:String(e),value:e})));function T(e){let{query:a,onChange:n}=e;const t=a,{limit:s,matchAny:l,tags:r,type:i}=t,u=O();return(0,c.jsxs)(o.FieldSet,{className:u.container,children:[(0,c.jsx)(o.Field,{label:"Filter by",children:(0,c.jsx)(o.Select,{inputId:"grafana-annotations__filter-by",options:q,value:i,onChange:e=>n(Object.assign({},t,{type:e.value}))})}),(0,c.jsx)(o.Field,{label:"Max limit",children:(0,c.jsx)(o.Select,{inputId:"grafana-annotations__limit",width:16,options:R,value:s,onChange:e=>n(Object.assign({},t,{limit:e.value}))})}),i===h._$.Tags&&(0,c.jsxs)(c.Fragment,{children:[(0,c.jsx)(o.Field,{label:"Match any",description:"Enabling this returns annotations that match any of the tags specified below",children:(0,c.jsx)(o.Switch,{id:"grafana-annotations__match-any",value:l,onChange:e=>n(Object.assign({},t,{matchAny:e.target.checked}))})}),(0,c.jsx)(o.Field,{label:"Tags",description:F,children:(0,c.jsx)(S.D,{allowCustomValue:!0,inputId:"grafana-annotations__tags",onChange:e=>n(Object.assign({},t,{tags:e})),tagOptions:w.lK,tags:null!=r?r:[]})})]})]})}const O=()=>({container:x.css` max-width: 600px; `});let A=100;class k extends i.DataSourceWithBackend{constructor(e){super(e),this.annotations={QueryEditor:T,prepareAnnotation(e){var a,n,t,s,l;return e.target=null!==(a=e.target)&&void 0!==a?a:{type:null!==(n=e.type)&&void 0!==n?n:h._$.Dashboard,limit:null!==(t=e.limit)&&void 0!==t?t:100,tags:null!==(s=e.tags)&&void 0!==s?s:[],matchAny:null!==(l=e.matchAny)&&void 0!==l&&l},e},prepareQuery(e){let a;if((0,p.isString)(e.datasource)){const n=(0,j.p)(e.datasource,{returnDefaultAsNull:!1});n&&(a=n)}else a=e.datasource;return Object.assign({},e,{refId:e.name,queryType:h.hR.Annotations,datasource:a})}}}query(e){const a=[],n=[],t=(0,i.getTemplateSrv)();for(const o of e.targets){if(o.queryType===h.hR.Annotations)return(0,v.D)(this.getAnnotations({range:e.range,rangeRaw:e.range.raw,annotation:o,dashboard:(0,C.h4)().getCurrent()}));if(!o.hide)if(o.queryType===h.hR.LiveMeasurements){var s,r;let n=t.replace(o.channel,e.scopedVars);const{filter:u}=o;n&&n.startsWith("telegraf/")&&(n="stream/"+n,o.channel=n);const h=(0,l.parseLiveChannelAddress)(n);if(!(0,l.isValidLiveChannelAddress)(h))continue;const c={maxLength:null!==(s=e.maxDataPoints)&&void 0!==s?s:500};o.buffer?(c.maxDelta=o.buffer,c.maxLength=2*c.maxLength):"now"===(null===(r=e.rangeRaw)||void 0===r?void 0:r.to)&&(c.maxDelta=e.range.to.valueOf()-e.range.from.valueOf()),a.push((0,i.getGrafanaLiveSrv)().getDataStream({key:`${e.requestId}.${A++}`,addr:h,filter:u,buffer:c}))}else o.queryType||(o.queryType=h.hR.RandomWalk),n.push(o)}return n.length&&a.push(super.query(Object.assign({},e,{targets:n}))),a.length?1===a.length?a[0]:(0,y.T)(...a):(0,m.of)()}listFiles(e){return this.query({targets:[{refId:"A",queryType:h.hR.List,path:e}]}).pipe((0,b.U)((e=>{var a;const n=null!==(a=e.data[0])&&void 0!==a?a:new l.MutableDataFrame;return new l.DataFrameView(n)})))}metricFindQuery(e){return Promise.resolve([])}async getAnnotations(e){var a;const n=(0,i.getTemplateSrv)(),t=e.annotation,s=t.target,r={from:e.range.from.valueOf(),to:e.range.to.valueOf(),limit:s.limit,tags:s.tags,matchAny:s.matchAny};if(s.type===h._$.Dashboard){if(!e.dashboard.id)return Promise.resolve({data:[]});r.dashboardId=e.dashboard.id,delete r.tags}else{if(!Array.isArray(s.tags)||0===s.tags.length)return Promise.resolve({data:[]});const e="__delimiter__",a=[];for(const t of r.tags){const s=n.replace(t,{},(a=>"string"==typeof a?a:a.join(e)));for(const n of s.split(e))a.push(n)}r.tags=a}const o=await(0,i.getBackendSrv)().get("/api/annotations",r,`grafana-data-source-annotations-${t.name}-${null===(a=e.dashboard)||void 0===a?void 0:a.id}`);return{data:[(0,l.toDataFrame)(o)]}}testDatasource(){return Promise.resolve()}}const I=new l.DataSourcePlugin(k).setQueryEditor(f)}}]); //# sourceMappingURL=grafanaPlugin.8c35c5624175da57dce3.js.map