Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

query types and constants #394

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/DataSource.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DataQueryResponse, DataSourceInstanceSettings, toDataFrame } from '@grafana/data';
import { GitHubVariableQuery } from 'types';
import { of } from 'rxjs';
import { GitHubDataSource } from 'DataSource';
import type { GitHubVariableQuery } from 'types/query';

describe('DataSource', () => {
describe('metricFindQuery', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import { prepareAnnotation } from 'migrations';
import { Observable } from 'rxjs';
import { trackRequest } from 'tracking';
import type { GitHubQuery, GitHubVariableQuery } from './types';
import type { GitHubQuery, GitHubVariableQuery } from './types/query';
import type { GitHubDataSourceOptions } from './types/config';

export class GitHubDataSource extends DataSourceWithBackend<GitHubQuery, GitHubDataSourceOptions> {
Expand Down Expand Up @@ -66,7 +66,7 @@
interval: request.interval,
} as DataQueryRequest<GitHubQuery>;

const res = await this.query(query).toPromise();

Check warning on line 69 in src/DataSource.ts

View workflow job for this annotation

GitHub Actions / Build, lint and unit tests

'toPromise' is deprecated. Replaced with {@link firstValueFrom } and {@link lastValueFrom }. Will be removed in v8. Details: https://rxjs.dev/deprecations/to-promise

if (!res?.data?.length) {
return [];
Expand Down Expand Up @@ -95,7 +95,7 @@
} as DataQueryRequest;

try {
const res = await this.query(request).toPromise();

Check warning on line 98 in src/DataSource.ts

View workflow job for this annotation

GitHub Actions / Build, lint and unit tests

'toPromise' is deprecated. Replaced with {@link firstValueFrom } and {@link lastValueFrom }. Will be removed in v8. Details: https://rxjs.dev/deprecations/to-promise
const columns = (res?.data[0]?.fields || []).map((f: any) => f.name) || [];
return columns;
} catch (err) {
Expand All @@ -115,7 +115,7 @@
range: options.range,
} as DataQueryRequest;
try {
const res = await this.query(request).toPromise();

Check warning on line 118 in src/DataSource.ts

View workflow job for this annotation

GitHub Actions / Build, lint and unit tests

'toPromise' is deprecated. Replaced with {@link firstValueFrom } and {@link lastValueFrom }. Will be removed in v8. Details: https://rxjs.dev/deprecations/to-promise
if (!res?.data?.length) {
return [];
}
Expand Down
55 changes: 55 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
export enum QueryType {
Commits = 'Commits',
Issues = 'Issues',
Contributors = 'Contributors',
Tags = 'Tags',
Releases = 'Releases',
Pull_Requests = 'Pull_Requests',
Labels = 'Labels',
Repositories = 'Repositories',
Organizations = 'Organizations',
GraphQL = 'GraphQL',
Milestones = 'Milestones',
Packages = 'Packages',
Vulnerabilities = 'Vulnerabilities',
Projects = 'Projects',
ProjectItems = 'ProjectItems',
Stargazers = 'Stargazers',
Workflows = 'Workflows',
Workflow_Usage = 'Workflow_Usage',
}

export const DefaultQueryType = QueryType.Issues;

export enum PackageType {
NPM = 'NPM',
RUBYGEMS = 'RUBYGEMS',
MAVEN = 'MAVEN',
DOCKER = 'DOCKER',
DEBIAN = 'DEBIAN',
NUGET = 'NUGET',
PYPI = 'PYPI',
}

export enum PullRequestTimeField {
ClosedAt,
CreatedAt,
MergedAt,
None,
}

export enum IssueTimeField {
CreatedAt,
ClosedAt,
UpdatedAt,
}

export enum WorkflowsTimeField {
CreatedAt,
UpdatedAt,
}

export enum ProjectQueryType {
ORG = 0,
USER = 1,
}
2 changes: 1 addition & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import ConfigEditor from './views/ConfigEditor';
import QueryEditor from './views/QueryEditor';
import VariableQueryEditor from './views/VariableQueryEditor';
import type { GitHubQuery } from './types';
import type { GitHubQuery } from './types/query';
import type { GitHubDataSourceOptions, GitHubSecureJsonData } from './types/config';

export const plugin = new DataSourcePlugin<
Expand All @@ -13,5 +13,5 @@
GitHubSecureJsonData
>(GitHubDataSource)
.setConfigEditor(ConfigEditor)
.setVariableQueryEditor(VariableQueryEditor)

Check warning on line 16 in src/module.ts

View workflow job for this annotation

GitHub Actions / Build, lint and unit tests

'setVariableQueryEditor' is deprecated. -- prefer using {@link StandardVariableSupport} or {@link CustomVariableSupport} or {@link DataSourceVariableSupport} in data source instead
.setQueryEditor(QueryEditor);
3 changes: 2 additions & 1 deletion src/tracking.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CoreApp, DataQueryRequest } from '@grafana/data';
import { reportInteraction } from '@grafana/runtime';
import { GitHubQuery, IssueTimeField, PullRequestTimeField, WorkflowsTimeField } from 'types';
import { IssueTimeField, PullRequestTimeField, WorkflowsTimeField } from './constants';
import type { GitHubQuery } from 'types/query';

export const trackRequest = (request: DataQueryRequest<GitHubQuery>) => {
if (request.app === CoreApp.Dashboard || request.app === CoreApp.PanelViewer) {
Expand Down
91 changes: 19 additions & 72 deletions src/types.ts → src/types/query.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,40 @@
import { PullRequestTimeField, IssueTimeField, WorkflowsTimeField, PackageType, ProjectQueryType } from '../constants';
import type { DataQuery } from '@grafana/schema';
import type { Filter } from 'components/Filters';

export interface Label {
color: string;
description: string;
name: string;
}

export interface RepositoryOptions {
repository?: string;
owner?: string;
}

export enum QueryType {
Commits = 'Commits',
Issues = 'Issues',
Contributors = 'Contributors',
Tags = 'Tags',
Releases = 'Releases',
Pull_Requests = 'Pull_Requests',
Labels = 'Labels',
Repositories = 'Repositories',
Organizations = 'Organizations',
GraphQL = 'GraphQL',
Milestones = 'Milestones',
Packages = 'Packages',
Vulnerabilities = 'Vulnerabilities',
Projects = 'Projects',
ProjectItems = 'ProjectItems',
Stargazers = 'Stargazers',
Workflows = 'Workflows',
Workflow_Usage = 'Workflow_Usage',
}

export enum PackageType {
NPM = 'NPM',
RUBYGEMS = 'RUBYGEMS',
MAVEN = 'MAVEN',
DOCKER = 'DOCKER',
DEBIAN = 'DEBIAN',
NUGET = 'NUGET',
PYPI = 'PYPI',
}

export enum PullRequestTimeField {
ClosedAt,
CreatedAt,
MergedAt,
None,
}

export enum IssueTimeField {
CreatedAt,
ClosedAt,
UpdatedAt,
export interface GitHubQuery extends Indexable, DataQuery, RepositoryOptions {
options?:
| PullRequestsOptions
| ReleasesOptions
| LabelsOptions
| TagsOptions
| CommitsOptions
| IssuesOptions
| ContributorsOptions
| ProjectsOptions
| WorkflowsOptions
| WorkflowUsageOptions;
}

export enum WorkflowsTimeField {
CreatedAt,
UpdatedAt,
export interface Label {
color: string;
description: string;
name: string;
}

export interface Indexable {
[index: string]: any;
}

export interface ReleasesOptions extends Indexable {}

export interface TagsOptions extends Indexable {}

export interface PullRequestsOptions extends Indexable {
timeField?: PullRequestTimeField;
query?: string;
Expand Down Expand Up @@ -115,25 +83,6 @@ export interface ProjectsOptions extends Indexable {
filters?: Filter[];
}

export enum ProjectQueryType {
ORG = 0,
USER = 1,
}

export interface GitHubQuery extends Indexable, DataQuery, RepositoryOptions {
options?:
| PullRequestsOptions
| ReleasesOptions
| LabelsOptions
| TagsOptions
| CommitsOptions
| IssuesOptions
| ContributorsOptions
| ProjectsOptions
| WorkflowsOptions
| WorkflowUsageOptions;
}

export interface GitHubVariableQuery extends GitHubQuery {
key?: string;
field?: string;
Expand All @@ -142,5 +91,3 @@ export interface GitHubVariableQuery extends GitHubQuery {
export interface GitHubAnnotationQuery extends GitHubVariableQuery {
timeField?: string;
}

export const DefaultQueryType = QueryType.Issues;
3 changes: 2 additions & 1 deletion src/validation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isEmpty } from 'lodash';
import { GitHubQuery, ProjectQueryType, QueryType } from './types';
import { ProjectQueryType, QueryType } from './constants';
import type { GitHubQuery } from './types/query';

export const isValid = (query: GitHubQuery): boolean => {
if (query.queryType === QueryType.Repositories) {
Expand Down
2 changes: 1 addition & 1 deletion src/variables.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { replaceVariables } from './variables';
import { GitHubQuery } from './types';
import type { GitHubQuery } from './types/query';

describe('variables', () => {
it('should not interpolate refId', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/variables.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GitHubQuery } from './types';
import { TemplateSrv } from '@grafana/runtime';
import { ScopedVars } from '@grafana/data';
import type { GitHubQuery } from './types/query';

export const replaceVariable = (
t: TemplateSrv,
Expand Down
3 changes: 2 additions & 1 deletion src/views/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import QueryEditorVulnerabilities from './QueryEditorVulnerabilities';
import QueryEditorProjects from './QueryEditorProjects';
import QueryEditorWorkflows from './QueryEditorWorkflows';
import QueryEditorWorkflowUsage from './QueryEditorWorkflowUsage';
import { type GitHubQuery, QueryType, DefaultQueryType } from '../types';
import { QueryType, DefaultQueryType } from '../constants';
import type { GitHubQuery } from '../types/query';
import type { GitHubDataSourceOptions } from '../types/config';

interface Props extends QueryEditorProps<GitHubDataSource, GitHubQuery, GitHubDataSourceOptions> {
Expand Down
3 changes: 1 addition & 2 deletions src/views/QueryEditorCommits.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import React, { useState } from 'react';
import { Input, InlineField } from '@grafana/ui';

import { CommitsOptions } from '../types';
import { RightColumnWidth, LeftColumnWidth } from './QueryEditor';
import { components } from 'components/selectors';
import type { CommitsOptions } from '../types/query';

interface Props extends CommitsOptions {
onChange: (value: CommitsOptions) => void;
Expand Down
3 changes: 1 addition & 2 deletions src/views/QueryEditorContributors.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React, { useState } from 'react';
import { Input, InlineField } from '@grafana/ui';

import { ContributorsOptions } from '../types';
import { RightColumnWidth, LeftColumnWidth } from './QueryEditor';
import type { ContributorsOptions } from '../types/query';

interface Props extends ContributorsOptions {
onChange: (value: ContributorsOptions) => void;
Expand Down
3 changes: 2 additions & 1 deletion src/views/QueryEditorIssues.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, { useState } from 'react';
import { Input, Select, InlineField } from '@grafana/ui';
import { SelectableValue } from '@grafana/data';
import { IssuesOptions, IssueTimeField } from '../types';
import { RightColumnWidth, LeftColumnWidth } from './QueryEditor';
import { components } from 'components/selectors';
import { IssueTimeField } from '../constants';
import type { IssuesOptions } from '../types/query';

interface Props extends IssuesOptions {
onChange: (value: IssuesOptions) => void;
Expand Down
3 changes: 1 addition & 2 deletions src/views/QueryEditorLabels.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React, { useState } from 'react';
import { Input, InlineField } from '@grafana/ui';

import { LabelsOptions } from '../types';
import { RightColumnWidth, LeftColumnWidth } from './QueryEditor';
import type { LabelsOptions } from '../types/query';

interface Props extends LabelsOptions {
onChange: (value: LabelsOptions) => void;
Expand Down
4 changes: 1 addition & 3 deletions src/views/QueryEditorMilestones.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import React, { useState } from 'react';

import { Input, InlineField } from '@grafana/ui';

import { MilestonesOptions } from '../types';
import { RightColumnWidth, LeftColumnWidth } from './QueryEditor';
import type { MilestonesOptions } from '../types/query';

interface Props extends MilestonesOptions {
onChange: (value: MilestonesOptions) => void;
Expand Down
2 changes: 1 addition & 1 deletion src/views/QueryEditorPackages.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import QueryEditorIssues, { DefaultPackageType } from './QueryEditorPackages';
import { render } from '@testing-library/react';
import { PackageType } from 'types';
import { PackageType } from './../constants';

describe('QueryEditorPackages', () => {
it('should update package type to default one if no package is selected', async () => {
Expand Down
5 changes: 2 additions & 3 deletions src/views/QueryEditorPackages.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React, { useEffect, useMemo, useState } from 'react';

import { Input, Select, InlineField } from '@grafana/ui';
import { SelectableValue } from '@grafana/data';

import { PackagesOptions, PackageType } from '../types';
import { RightColumnWidth, LeftColumnWidth } from './QueryEditor';
import { PackageType } from '../constants';
import type { PackagesOptions } from '../types/query';

interface Props extends PackagesOptions {
onChange: (value: PackagesOptions) => void;
Expand Down
4 changes: 2 additions & 2 deletions src/views/QueryEditorProjects.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useState } from 'react';
import { Input, InlineLabel, RadioButtonGroup, InlineField } from '@grafana/ui';

import { QueryEditorRow } from '../components/Forms';
import { RightColumnWidth, LeftColumnWidth } from './QueryEditor';
import { components } from '../components/selectors';
import { ProjectsOptions, ProjectQueryType } from 'types';
import { SelectableValue } from '@grafana/data';
import { Filter, Filters } from 'components/Filters';
import { ProjectQueryType } from './../constants';
import type { ProjectsOptions } from 'types/query';

interface Props extends ProjectsOptions {
onChange: (value: ProjectsOptions) => void;
Expand Down
6 changes: 2 additions & 4 deletions src/views/QueryEditorPullRequests.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import React, { useState } from 'react';

import { Input, Select, InlineField } from '@grafana/ui';
import { SelectableValue } from '@grafana/data';

import { PullRequestsOptions, PullRequestTimeField } from '../types';

import { RightColumnWidth, LeftColumnWidth } from './QueryEditor';
import { PullRequestTimeField } from '../constants';
import type { PullRequestsOptions } from '../types/query';

interface Props extends PullRequestsOptions {
onChange: (value: PullRequestsOptions) => void;
Expand Down
3 changes: 1 addition & 2 deletions src/views/QueryEditorRepository.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React, { useState, useEffect } from 'react';
import { Input, InlineLabel } from '@grafana/ui';

import { QueryEditorRow } from '../components/Forms';
import { RepositoryOptions } from '../types';
import { RightColumnWidth, LeftColumnWidth } from './QueryEditor';
import { components } from '../components/selectors';
import type { RepositoryOptions } from '../types/query';

interface Props extends RepositoryOptions {
onChange: (value: RepositoryOptions) => void;
Expand Down
2 changes: 1 addition & 1 deletion src/views/QueryEditorWorkflowUsage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState } from 'react';
import { Input, InlineField } from '@grafana/ui';
import { RightColumnWidth, LeftColumnWidth } from './QueryEditor';
import { WorkflowUsageOptions } from 'types';
import type { WorkflowUsageOptions } from 'types/query';

interface Props extends WorkflowUsageOptions {
onChange: (value: WorkflowUsageOptions) => void;
Expand Down
5 changes: 3 additions & 2 deletions src/views/QueryEditorWorkflows.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import { Select, InlineField } from '@grafana/ui';
import { RightColumnWidth, LeftColumnWidth } from './QueryEditor';
import { WorkflowsOptions, WorkflowsTimeField } from 'types';
import { SelectableValue } from '@grafana/data';
import { RightColumnWidth, LeftColumnWidth } from './QueryEditor';
import { WorkflowsTimeField } from './../constants';
import type { WorkflowsOptions } from 'types/query';

interface Props extends WorkflowsOptions {
onChange: (value: WorkflowsOptions) => void;
Expand Down
Loading
Loading