Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit aca68a2

Browse files
author
leo
authored
insights: add proactive mode pings back (#42554)
1 parent 288371b commit aca68a2

File tree

8 files changed

+150
-3
lines changed

8 files changed

+150
-3
lines changed

client/web/src/search/results/components/aggregation/SearchAggregationResult.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export const SearchAggregationResult: FC<SearchAggregationResultProps> = props =
5353
caseSensitive,
5454
proactive: true,
5555
extendedTimeout,
56+
telemetryService,
5657
})
5758

5859
const handleCollapseClick = (): void => {

client/web/src/search/results/components/aggregation/hooks.ts

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import { useCallback, useLayoutEffect, useMemo, useState } from 'react'
33
import { gql, useQuery } from '@apollo/client'
44
import { useHistory, useLocation } from 'react-router'
55

6-
import { SearchAggregationMode } from '@sourcegraph/shared/src/graphql-operations'
6+
import { NotAvailableReasonType, SearchAggregationMode } from '@sourcegraph/shared/src/graphql-operations'
77
import { SearchPatternType } from '@sourcegraph/shared/src/schema'
8+
import { TelemetryService } from '@sourcegraph/shared/src/telemetry/telemetryService'
89

910
import { GetSearchAggregationResult, GetSearchAggregationVariables } from '../../../../graphql-operations'
1011

1112
import { AGGREGATION_MODE_URL_KEY, AGGREGATION_UI_MODE_URL_KEY } from './constants'
13+
import { GroupResultsPing } from './pings'
1214
import { AggregationUIMode } from './types'
1315

1416
interface URLStateOptions<State, SerializedState> {
@@ -210,6 +212,7 @@ interface SearchAggregationDataInput {
210212
caseSensitive: boolean
211213
extendedTimeout: boolean
212214
proactive?: boolean
215+
telemetryService: TelemetryService
213216
}
214217

215218
interface AggregationState {
@@ -225,7 +228,15 @@ type SearchAggregationResults =
225228
| { data: GetSearchAggregationResult; loading: false; error: undefined }
226229

227230
export const useSearchAggregationData = (input: SearchAggregationDataInput): SearchAggregationResults => {
228-
const { query, patternType, aggregationMode, proactive, caseSensitive, extendedTimeout } = input
231+
const {
232+
query,
233+
patternType,
234+
aggregationMode,
235+
proactive = false,
236+
caseSensitive,
237+
extendedTimeout,
238+
telemetryService,
239+
} = input
229240

230241
const [, setAggregationMode] = useAggregationSearchMode()
231242
const [state, setState] = useState<AggregationState>(INITIAL_STATE)
@@ -276,6 +287,8 @@ export const useSearchAggregationData = (input: SearchAggregationDataInput): Sea
276287
// skip: true resets data field in the useQuery hook, in order to use previously
277288
// saved data we use useState to store data outside useQuery hook
278289
setState({ data, calculatedMode: calculatedAggregationMode })
290+
291+
sendAggregationPing({ data, aggregationMode, extendedTimeout, proactive, telemetryService })
279292
},
280293
}
281294
)
@@ -318,3 +331,59 @@ export const isNonExhaustiveAggregationResults = (response?: GetSearchAggregatio
318331

319332
return response.searchQueryAggregate?.aggregations?.__typename === 'NonExhaustiveSearchAggregationResult'
320333
}
334+
335+
interface UseAggregationPingsArgs {
336+
data: GetSearchAggregationResult | undefined
337+
proactive: boolean
338+
extendedTimeout: boolean
339+
telemetryService: TelemetryService
340+
aggregationMode: SearchAggregationMode | null
341+
}
342+
343+
function sendAggregationPing({
344+
data,
345+
proactive,
346+
extendedTimeout,
347+
telemetryService,
348+
aggregationMode,
349+
}: UseAggregationPingsArgs): void {
350+
if (!data?.searchQueryAggregate.aggregations) {
351+
return
352+
}
353+
354+
const aggregation = data.searchQueryAggregate.aggregations
355+
const aggregationType = aggregation.__typename
356+
const aggregationAvailable =
357+
aggregationType === 'ExhaustiveSearchAggregationResult' ||
358+
aggregationType === 'NonExhaustiveSearchAggregationResult'
359+
const aggregationUnavailable = aggregationType === 'SearchAggregationNotAvailable'
360+
361+
let pingType
362+
363+
if (aggregationUnavailable) {
364+
const extensionAvailable = aggregation.reasonType === NotAvailableReasonType.TIMEOUT_EXTENSION_AVAILABLE
365+
const noExtensionAvailable = aggregation.reasonType === NotAvailableReasonType.TIMEOUT_NO_EXTENSION_AVAILABLE
366+
367+
if (proactive && extensionAvailable) {
368+
pingType = GroupResultsPing.ProactiveLimitHit
369+
}
370+
371+
if (noExtensionAvailable) {
372+
pingType = GroupResultsPing.ExplicitLimitHit
373+
}
374+
}
375+
376+
if (aggregationAvailable) {
377+
if (proactive) {
378+
pingType = GroupResultsPing.ProactiveLimitSuccess
379+
}
380+
381+
if (extendedTimeout) {
382+
pingType = GroupResultsPing.ExplicitLimitSuccess
383+
}
384+
}
385+
386+
if (pingType) {
387+
telemetryService.log(pingType, { aggregationMode }, { aggregationMode })
388+
}
389+
}

client/web/src/search/results/components/aggregation/pings.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ export enum GroupResultsPing {
1313
ExpandFullViewPanel = 'GroupResultsExpandedViewOpen',
1414
CollapseFullViewPanel = 'GroupResultsExpandedViewCollapse',
1515
InfoIconHover = 'GroupResultsInfoIconHover',
16+
17+
// Proactive
18+
ProactiveLimitHit = 'ProactiveLimitHit',
19+
ProactiveLimitSuccess = 'ProactiveLimitSuccess',
20+
ExplicitLimitHit = 'ExplicitLimitHit',
21+
ExplicitLimitSuccess = 'ExplicitLimitSuccess',
1622
}

client/web/src/search/results/sidebar/SearchAggregations.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export const SearchAggregations: FC<SearchAggregationsProps> = memo(props => {
5858
proactive,
5959
caseSensitive,
6060
extendedTimeout,
61+
telemetryService,
6162
})
6263

6364
// When query is updated reset extendedTimeout as per business rules
@@ -156,3 +157,5 @@ export const SearchAggregations: FC<SearchAggregationsProps> = memo(props => {
156157
</article>
157158
)
158159
})
160+
161+
SearchAggregations.displayName = 'SearchAggregations'

doc/dev/background-information/insights/code_insights_pings.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,15 @@ https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegrap
423423
- [WeeklyGroupResultsChartBarClick](https://sourcegraph.com/search?q=context:%40sourcegraph/all+GroupResultsChartBarClick&patternType=regexp)
424424
- [WeeklyGroupResultsChartBarHover](https://sourcegraph.com/search?q=context:%40sourcegraph/all+GroupResultsChartBarHover&patternType=regexp)
425425
- **Version added:** 4.0 ([#40977](https://github.com/sourcegraph/sourcegraph/pull/40977))
426+
427+
### Search mode (proactive/extended) success rate
428+
429+
**Type:** FE event
430+
431+
**Intended purpose:** To track the number of aggregation searches that succeed or hit limit in either a proactive or extended search.
432+
433+
**Functional implementation:** These pings fire a telemetry event when an aggregation search completes or times out.
434+
435+
- Aggregation: weekly
436+
- Event Code: [WeeklyGroupResultsSearches](https://sourcegraph.com/search?q=context:%40sourcegraph/all+WeeklyGroupResultsSearches&patternType=lucky)
437+
- **Version added:** 4.1 ([#42554](https://github.com/sourcegraph/sourcegraph/pull/42554))

internal/types/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,7 @@ type CodeInsightsUsageStatistics struct {
14491449
WeeklyGroupResultsChartBarClick []GroupResultPing
14501450
WeeklyGroupResultsAggregationModeClicked []GroupResultPing
14511451
WeeklyGroupResultsAggregationModeDisabledHover []GroupResultPing
1452+
WeeklyGroupResultsSearches []GroupResultSearchPing
14521453
WeeklySeriesBackfillTime []InsightsBackfillTimePing
14531454
}
14541455

@@ -1464,6 +1465,12 @@ type GroupResultExpandedViewPing struct {
14641465
Count *int32
14651466
}
14661467

1468+
type GroupResultSearchPing struct {
1469+
Name PingName
1470+
AggregationMode *string
1471+
Count *int32
1472+
}
1473+
14671474
type CodeInsightsCriticalTelemetry struct {
14681475
TotalInsights int32
14691476
}

internal/usagestats/code_insights.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,21 @@ func GetCodeInsightsUsageStatistics(ctx context.Context, db database.DB) (*types
227227
}
228228
stats.WeeklyGroupResultsExpandedViewCollapse = weeklyGroupResultsExpandedViewCollapse
229229

230+
weeklyGroupResultsSearches, err := GetGroupResultsSearchesPings(
231+
ctx,
232+
db,
233+
[]types.PingName{
234+
"ProactiveLimitHit",
235+
"ProactiveLimitSuccess",
236+
"ExplicitLimitHit",
237+
"ExplicitLimitSuccess",
238+
},
239+
)
240+
if err != nil {
241+
return nil, errors.Wrap(err, "WeeklyGroupResultsSearches")
242+
}
243+
stats.WeeklyGroupResultsSearches = weeklyGroupResultsSearches
244+
230245
backfillTime, err := GetBackfillTimePing(ctx, db)
231246
if err != nil {
232247
return nil, errors.Wrap(err, "GetBackfillTimePing")
@@ -442,6 +457,40 @@ func GetGroupResultsExpandedViewPing(ctx context.Context, db database.DB, pingNa
442457
return groupResultsExpandedViewPings, nil
443458
}
444459

460+
func GetGroupResultsSearchesPings(ctx context.Context, db database.DB, pingNames []types.PingName) ([]types.GroupResultSearchPing, error) {
461+
pings := []types.GroupResultSearchPing{}
462+
463+
for _, name := range pingNames {
464+
rows, err := db.QueryContext(ctx, getGroupResultsSql, string(name), timeNow())
465+
if err != nil {
466+
return nil, err
467+
}
468+
err = func() error {
469+
defer rows.Close()
470+
var noop *string
471+
for rows.Next() {
472+
ping := types.GroupResultSearchPing{
473+
Name: name,
474+
}
475+
if err := rows.Scan(
476+
&ping.Count,
477+
&ping.AggregationMode,
478+
&noop,
479+
&noop,
480+
); err != nil {
481+
return err
482+
}
483+
pings = append(pings, ping)
484+
}
485+
return nil
486+
}()
487+
if err != nil {
488+
return nil, err
489+
}
490+
}
491+
return pings, nil
492+
}
493+
445494
func GetBackfillTimePing(ctx context.Context, db database.DB) ([]types.InsightsBackfillTimePing, error) {
446495
store := db.EventLogs()
447496
name := InsightsBackfillTimePingName

internal/usagestats/code_insights_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"time"
1111

1212
"github.com/google/go-cmp/cmp"
13-
1413
"github.com/sourcegraph/log/logtest"
1514

1615
"github.com/sourcegraph/sourcegraph/internal/database"
@@ -126,6 +125,7 @@ func TestCodeInsightsUsageStatistics(t *testing.T) {
126125
want.WeeklyGroupResultsChartBarClick = []types.GroupResultPing{}
127126
want.WeeklyGroupResultsAggregationModeClicked = []types.GroupResultPing{}
128127
want.WeeklyGroupResultsAggregationModeDisabledHover = []types.GroupResultPing{}
128+
want.WeeklyGroupResultsSearches = []types.GroupResultSearchPing{}
129129
want.WeeklySeriesBackfillTime = []types.InsightsBackfillTimePing{}
130130

131131
if diff := cmp.Diff(want, have); diff != "" {

0 commit comments

Comments
 (0)