@@ -1185,7 +1185,7 @@ describe('dashboard router', () => {
11851185 . expect ( 200 ) ;
11861186
11871187 const stored = await Dashboard . findById ( created . body . id ) . lean ( ) ;
1188- expect ( stored ?. filters ?. [ 0 ] . minSelections ) . toBe ( 1 ) ;
1188+ expect ( stored ?. filters ?. [ 0 ] ) . toMatchObject ( { minSelections : 1 } ) ;
11891189 } ) ;
11901190
11911191 // Absence is meaningful: the client reads a missing value as not required,
@@ -1794,6 +1794,306 @@ describe('dashboard router', () => {
17941794 } ) ;
17951795 } ) ;
17961796
1797+ describe ( 'ad hoc filters' , ( ) => {
1798+ const createSource = ( kind : SourceKind , teamId = team . _id ) =>
1799+ Source . create ( {
1800+ kind,
1801+ name : `Test ${ kind } source` ,
1802+ team : teamId ,
1803+ connection : new Types . ObjectId ( ) . toString ( ) ,
1804+ from : { databaseName : 'test_db' , tableName : `${ kind } _table` } ,
1805+ timestampValueExpression : 'timestamp' ,
1806+ defaultTableSelectExpression : 'body' ,
1807+ ...( kind === SourceKind . Metric && {
1808+ metricTables : { gauge : 'otel_metrics_gauge' } ,
1809+ resourceAttributesExpression : 'ResourceAttributes' ,
1810+ } ) ,
1811+ } ) ;
1812+
1813+ let logSourceId : string ;
1814+ let traceSourceId : string ;
1815+ let promqlSourceId : string ;
1816+ beforeEach ( async ( ) => {
1817+ logSourceId = ( await createSource ( SourceKind . Log ) ) . _id . toString ( ) ;
1818+ traceSourceId = ( await createSource ( SourceKind . Trace ) ) . _id . toString ( ) ;
1819+ promqlSourceId = ( await createSource ( SourceKind . Promql ) ) . _id . toString ( ) ;
1820+ } ) ;
1821+
1822+ const makeAdhocFilter = ( overrides = { } ) => ( {
1823+ id : new Types . ObjectId ( ) . toString ( ) ,
1824+ type : 'ADHOC' as const ,
1825+ name : 'Ad hoc' ,
1826+ sourceType : 'sql' as const ,
1827+ sources : [ logSourceId ] ,
1828+ isVariableEnabled : true as const ,
1829+ variableName : 'adhoc' ,
1830+ ...overrides ,
1831+ } ) ;
1832+
1833+ const adhocValue = {
1834+ type : 'adhoc' as const ,
1835+ name : 'adhoc' ,
1836+ conditions : [
1837+ { key : 'ServiceName' , operator : '=~' as const , value : 'api.*' } ,
1838+ {
1839+ key : "LogAttributes['env']" ,
1840+ operator : 'NOT LIKE' as const ,
1841+ value : 'dev' ,
1842+ } ,
1843+ ] ,
1844+ } ;
1845+
1846+ it ( 'persists a SQL ad hoc filter and its saved conditions' , async ( ) => {
1847+ const filter = makeAdhocFilter ( {
1848+ sources : [ logSourceId , traceSourceId ] ,
1849+ appliesToSourceIds : [ traceSourceId ] ,
1850+ isBroadcastEnabled : false ,
1851+ } ) ;
1852+
1853+ const created = await agent
1854+ . post ( '/dashboards' )
1855+ . send ( {
1856+ ...MOCK_DASHBOARD ,
1857+ filters : [ filter ] ,
1858+ savedFilterValues : [ adhocValue ] ,
1859+ } )
1860+ . expect ( 200 ) ;
1861+
1862+ expect ( created . body . filters ) . toEqual ( [ filter ] ) ;
1863+ expect ( created . body . savedFilterValues ) . toEqual ( [ adhocValue ] ) ;
1864+
1865+ const stored = await Dashboard . findById ( created . body . id ) . lean ( ) ;
1866+ expect ( stored ?. filters ) . toEqual ( [ filter ] ) ;
1867+ expect ( stored ?. savedFilterValues ) . toEqual ( [ adhocValue ] ) ;
1868+
1869+ const list = await agent . get ( '/dashboards' ) . expect ( 200 ) ;
1870+ const listed = list . body . find (
1871+ ( d : { id : string } ) => d . id === created . body . id ,
1872+ ) ;
1873+ expect ( listed . filters ) . toEqual ( [ filter ] ) ;
1874+ expect ( listed . savedFilterValues ) . toEqual ( [ adhocValue ] ) ;
1875+ } ) ;
1876+
1877+ it ( 'persists a PromQL ad hoc filter' , async ( ) => {
1878+ const filter = makeAdhocFilter ( {
1879+ sourceType : 'promql' ,
1880+ sources : [ promqlSourceId ] ,
1881+ } ) ;
1882+
1883+ const created = await agent
1884+ . post ( '/dashboards' )
1885+ . send ( { ...MOCK_DASHBOARD , filters : [ filter ] } )
1886+ . expect ( 200 ) ;
1887+
1888+ const stored = await Dashboard . findById ( created . body . id ) . lean ( ) ;
1889+ expect ( stored ?. filters ) . toEqual ( [ filter ] ) ;
1890+ } ) ;
1891+
1892+ it ( 'persists saved conditions alongside other filter values on PATCH' , async ( ) => {
1893+ const filter = makeAdhocFilter ( ) ;
1894+ const created = await agent
1895+ . post ( '/dashboards' )
1896+ . send ( { ...MOCK_DASHBOARD , filters : [ filter ] } )
1897+ . expect ( 200 ) ;
1898+
1899+ const savedFilterValues = [
1900+ { type : 'sql' as const , condition : "ServiceName IN ('api')" } ,
1901+ { type : 'variable' as const , name : 'env' , values : [ 'prod' ] } ,
1902+ adhocValue ,
1903+ ] ;
1904+ await agent
1905+ . patch ( `/dashboards/${ created . body . id } ` )
1906+ . send ( { savedFilterValues } )
1907+ . expect ( 200 ) ;
1908+
1909+ const stored = await Dashboard . findById ( created . body . id ) . lean ( ) ;
1910+ expect ( stored ?. savedFilterValues ) . toEqual ( savedFilterValues ) ;
1911+ } ) ;
1912+
1913+ it . each ( [
1914+ [ 'no source type' , { sourceType : undefined } ] ,
1915+ [ 'an unknown source type' , { sourceType : 'lucene' } ] ,
1916+ [ 'no sources' , { sources : undefined } ] ,
1917+ [ 'empty sources' , { sources : [ ] } ] ,
1918+ [ 'an empty source ID' , { sources : [ '' ] } ] ,
1919+ [ 'an invalid variable name' , { variableName : '1adhoc' } ] ,
1920+ [ 'not variable-enabled' , { isVariableEnabled : false } ] ,
1921+ [ 'variables unset' , { isVariableEnabled : undefined } ] ,
1922+ [ 'a minimum selection' , { minSelections : 1 } ] ,
1923+ [ 'a maximum selection' , { maxSelections : 1 } ] ,
1924+ [ 'a global requirement' , { isGlobalRequirement : true } ] ,
1925+ ] ) ( 'rejects an ad hoc filter with %s' , async ( _label , overrides ) => {
1926+ await agent
1927+ . post ( '/dashboards' )
1928+ . send ( { ...MOCK_DASHBOARD , filters : [ makeAdhocFilter ( overrides ) ] } )
1929+ . expect ( 400 ) ;
1930+ } ) ;
1931+
1932+ it . each ( [
1933+ [
1934+ 'an unknown operator' ,
1935+ { conditions : [ { key : 'a' , operator : '>' , value : '1' } ] } ,
1936+ ] ,
1937+ [
1938+ 'an empty key' ,
1939+ { conditions : [ { key : '' , operator : '=' , value : '1' } ] } ,
1940+ ] ,
1941+ [
1942+ 'too many conditions' ,
1943+ {
1944+ conditions : Array . from ( { length : 101 } , ( ) => ( {
1945+ key : 'a' ,
1946+ operator : '=' ,
1947+ value : '1' ,
1948+ } ) ) ,
1949+ } ,
1950+ ] ,
1951+ ] ) ( 'rejects saved conditions with %s' , async ( _label , overrides ) => {
1952+ await agent
1953+ . post ( '/dashboards' )
1954+ . send ( {
1955+ ...MOCK_DASHBOARD ,
1956+ filters : [ makeAdhocFilter ( ) ] ,
1957+ savedFilterValues : [ { ...adhocValue , ...overrides } ] ,
1958+ } )
1959+ . expect ( 400 ) ;
1960+ } ) ;
1961+
1962+ it ( 'rejects a variable name it shares with another filter' , async ( ) => {
1963+ await agent
1964+ . post ( '/dashboards' )
1965+ . send ( {
1966+ ...MOCK_DASHBOARD ,
1967+ filters : [
1968+ makeAdhocFilter ( { variableName : 'service' } ) ,
1969+ {
1970+ id : new Types . ObjectId ( ) . toString ( ) ,
1971+ type : 'QUERY_EXPRESSION' as const ,
1972+ name : 'Service' ,
1973+ expression : 'ServiceName' ,
1974+ source : logSourceId ,
1975+ isVariableEnabled : true ,
1976+ variableName : 'service' ,
1977+ } ,
1978+ ] ,
1979+ } )
1980+ . expect ( 400 ) ;
1981+ } ) ;
1982+
1983+ describe ( 'source validation' , ( ) => {
1984+ it ( 'rejects a source that does not exist' , async ( ) => {
1985+ const source = new Types . ObjectId ( ) . toString ( ) ;
1986+
1987+ const response = await agent
1988+ . post ( '/dashboards' )
1989+ . send ( {
1990+ ...MOCK_DASHBOARD ,
1991+ filters : [ makeAdhocFilter ( { sources : [ logSourceId , source ] } ) ] ,
1992+ } )
1993+ . expect ( 400 ) ;
1994+
1995+ expect ( response . body . message ) . toContain ( source ) ;
1996+ expect ( response . body . message ) . not . toContain ( logSourceId ) ;
1997+ } ) ;
1998+
1999+ it . each ( [
2000+ [ 'a PromQL source in a SQL filter' , 'sql' , SourceKind . Promql ] ,
2001+ [ 'a metric source in a SQL filter' , 'sql' , SourceKind . Metric ] ,
2002+ [ 'a log source in a PromQL filter' , 'promql' , SourceKind . Log ] ,
2003+ ] ) ( 'rejects %s' , async ( _label , sourceType , kind ) => {
2004+ const source = ( await createSource ( kind ) ) . _id . toString ( ) ;
2005+
2006+ const response = await agent
2007+ . post ( '/dashboards' )
2008+ . send ( {
2009+ ...MOCK_DASHBOARD ,
2010+ filters : [ makeAdhocFilter ( { sourceType, sources : [ source ] } ) ] ,
2011+ } )
2012+ . expect ( 400 ) ;
2013+
2014+ expect ( response . body . message ) . toContain ( source ) ;
2015+ } ) ;
2016+
2017+ it ( 'rejects an applies-to source of the wrong type' , async ( ) => {
2018+ const response = await agent
2019+ . post ( '/dashboards' )
2020+ . send ( {
2021+ ...MOCK_DASHBOARD ,
2022+ filters : [
2023+ makeAdhocFilter ( { appliesToSourceIds : [ promqlSourceId ] } ) ,
2024+ ] ,
2025+ } )
2026+ . expect ( 400 ) ;
2027+
2028+ expect ( response . body . message ) . toContain ( promqlSourceId ) ;
2029+ } ) ;
2030+
2031+ it ( "rejects another team's source" , async ( ) => {
2032+ const otherTeamSource = await createSource (
2033+ SourceKind . Log ,
2034+ new Types . ObjectId ( ) ,
2035+ ) ;
2036+
2037+ await agent
2038+ . post ( '/dashboards' )
2039+ . send ( {
2040+ ...MOCK_DASHBOARD ,
2041+ filters : [
2042+ makeAdhocFilter ( { sources : [ otherTeamSource . _id . toString ( ) ] } ) ,
2043+ ] ,
2044+ } )
2045+ . expect ( 400 ) ;
2046+ } ) ;
2047+
2048+ it ( 'rejects a source added by PATCH, leaving the stored filter alone' , async ( ) => {
2049+ const filter = makeAdhocFilter ( ) ;
2050+ const created = await agent
2051+ . post ( '/dashboards' )
2052+ . send ( { ...MOCK_DASHBOARD , filters : [ filter ] } )
2053+ . expect ( 200 ) ;
2054+
2055+ await agent
2056+ . patch ( `/dashboards/${ created . body . id } ` )
2057+ . send ( {
2058+ filters : [ { ...filter , sources : [ logSourceId , promqlSourceId ] } ] ,
2059+ } )
2060+ . expect ( 400 ) ;
2061+
2062+ const stored = await Dashboard . findById ( created . body . id ) . lean ( ) ;
2063+ expect ( stored ?. filters ) . toEqual ( [ filter ] ) ;
2064+ } ) ;
2065+
2066+ it ( "lets unrelated edits through after a filter's source is deleted" , async ( ) => {
2067+ const filter = makeAdhocFilter ( { appliesToSourceIds : [ traceSourceId ] } ) ;
2068+ const created = await agent
2069+ . post ( '/dashboards' )
2070+ . send ( { ...MOCK_DASHBOARD , filters : [ filter ] } )
2071+ . expect ( 200 ) ;
2072+
2073+ await Source . findByIdAndDelete ( logSourceId ) ;
2074+ await Source . findByIdAndDelete ( traceSourceId ) ;
2075+
2076+ await agent
2077+ . patch ( `/dashboards/${ created . body . id } ` )
2078+ . send ( { name : 'Renamed' , filters : [ { ...filter , name : 'Renamed' } ] } )
2079+ . expect ( 200 ) ;
2080+ } ) ;
2081+
2082+ it ( 'rejects a PATCH changing the source type to one its sources do not match' , async ( ) => {
2083+ const filter = makeAdhocFilter ( ) ;
2084+ const created = await agent
2085+ . post ( '/dashboards' )
2086+ . send ( { ...MOCK_DASHBOARD , filters : [ filter ] } )
2087+ . expect ( 200 ) ;
2088+
2089+ await agent
2090+ . patch ( `/dashboards/${ created . body . id } ` )
2091+ . send ( { filters : [ { ...filter , sourceType : 'promql' } ] } )
2092+ . expect ( 400 ) ;
2093+ } ) ;
2094+ } ) ;
2095+ } ) ;
2096+
17972097 describe ( 'preset dashboards' , ( ) => {
17982098 const MOCK_SOURCE : Omit < Extract < TSource , { kind : 'log' } > , 'id' > = {
17992099 kind : SourceKind . Log ,
0 commit comments