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

Moved query of log file content to GraphQL #536

Merged
merged 11 commits into from
Oct 23, 2023
Merged
2 changes: 0 additions & 2 deletions .github/workflows/translations-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
schedule:
- cron: '00 3 * * 1'



jobs:
crowdin-translations-to-pr:
name: Create a PR with the latest translations from Crowdin
Expand Down
19 changes: 10 additions & 9 deletions crowdin.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
project_id_env: CROWDIN_PROJECT_ID
jurgelenas marked this conversation as resolved.
Show resolved Hide resolved
api_token_env: CROWDIN_PERSONAL_TOKEN
"base_path": "."
"base_url": "https://api.crowdin.com"
'base_path': '.'
'base_url': 'https://api.crowdin.com'

"preserve_hierarchy": true
'preserve_hierarchy': true

files: [
{
"source": "/src/i18n/locales/en/messages.json",
"translation": "/src/i18n/locales/%two_letters_code%/%original_file_name%",
}
]
files:
[
{
'source': '/src/i18n/locales/en/messages.json',
'translation': '/src/i18n/locales/%two_letters_code%/%original_file_name%',
},
]
56 changes: 56 additions & 0 deletions graphql.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,29 @@
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "LogFile",
"description": null,
"fields": [
{
"name": "content",
"description": null,
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [],
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "LuaScript",
Expand Down Expand Up @@ -2071,6 +2094,39 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "logFile",
"description": null,
"args": [
{
"name": "numberOfLines",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "Float",
"ofType": null
}
},
"defaultValue": "20",
"isDeprecated": false,
"deprecationReason": null
}
],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "LogFile",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "luaScript",
"description": null,
Expand Down
4 changes: 4 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import MulticastDnsService from './src/services/MulticastDns';
import MulticastDnsMonitorResolver from './src/graphql/resolvers/MulticastDnsMonitor.resolver';
import LuaService from './src/services/Lua';
import LuaResolver from './src/graphql/resolvers/Lua.resolver';
import LogFileService from './src/services/LogFile';
import LogFileResolver from './src/graphql/resolvers/LogFile.resolver';
import MulticastDnsSimulatorService from './src/services/MulticastDns/MulticastDnsSimulator';
import MulticastDnsNotificationsService from './src/services/MulticastDnsNotificationsService';
import TargetsLoader from './src/services/TargetsLoader';
Expand Down Expand Up @@ -170,6 +172,7 @@ export default class ApiServer {
);

Container.set(LuaService, new LuaService(logger));
Container.set(LogFileService, new LogFileService(logger));
}

async start(
Expand Down Expand Up @@ -202,6 +205,7 @@ export default class ApiServer {
SerialMonitorResolver,
MulticastDnsMonitorResolver,
LuaResolver,
LogFileResolver,
],
container: Container,
pubSub: Container.get<PubSub>(PubSubToken),
Expand Down
11 changes: 11 additions & 0 deletions src/api/src/graphql/args/LogFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ArgsType, Field } from 'type-graphql';

@ArgsType()
export default class LogFileArgs {
@Field()
numberOfLines: number;

constructor() {
this.numberOfLines = 20;
}
}
17 changes: 17 additions & 0 deletions src/api/src/graphql/resolvers/LogFile.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Args, Query, Resolver } from 'type-graphql';
import { Service } from 'typedi';
import LogFileService from '../../services/LogFile';
import LogFile from '../../models/LogFile';
import LogFileArgs from '../args/LogFile';

@Service()
@Resolver()
export default class LogFileResolver {
constructor(private logFileService: LogFileService) {}

@Query(() => LogFile)
async logFile(@Args() args: LogFileArgs): Promise<LogFile> {
const content = await this.logFileService.loadLogFile(args);
return { content };
}
}
9 changes: 8 additions & 1 deletion src/api/src/logger/WinstonLogger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-param-reassign */
/* eslint-disable react/static-property-placement */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Logger } from 'winston';
import { Logger, QueryOptions } from 'winston';
import { Service } from 'typedi';
import { LoggerService } from './index';

Expand Down Expand Up @@ -93,4 +93,11 @@ export default class WinstonLoggerService implements LoggerService {

return this.logger.verbose(message, { context });
}

public query(
options?: QueryOptions | undefined,
vbotnaru marked this conversation as resolved.
Show resolved Hide resolved
callback?: { (err: Error, result: any): void }
vbotnaru marked this conversation as resolved.
Show resolved Hide resolved
): any {
return this.logger.query(options, callback);
}
}
8 changes: 7 additions & 1 deletion src/api/src/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { QueryOptions } from 'winston';
vbotnaru marked this conversation as resolved.
Show resolved Hide resolved

/* eslint-disable @typescript-eslint/no-explicit-any */
export interface LoggerService {
log(message: any, context?: Record<string, unknown>): void;

Expand All @@ -10,4 +11,9 @@ export interface LoggerService {
debug?(message: any, context?: Record<string, unknown>): void;

verbose?(message: any, context?: Record<string, unknown>): void;

query(
options?: QueryOptions,
callback?: { (err: Error, result: any): void }
jurgelenas marked this conversation as resolved.
Show resolved Hide resolved
): void;
}
11 changes: 11 additions & 0 deletions src/api/src/models/LogFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Field, ObjectType } from 'type-graphql';

@ObjectType('LogFile')
export default class LogFile {
@Field({ nullable: true })
content: string | null;

constructor(content: string | null) {
this.content = content;
}
}
37 changes: 37 additions & 0 deletions src/api/src/services/LogFile/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Service } from 'typedi';
import { LoggerService } from '../../logger';

export interface ILogFileArgs {
numberOfLines: number;
}

export interface ILogFile {
loadLogFile(args: ILogFileArgs): Promise<string>;
}

@Service()
export default class LogFileService implements ILogFile {
constructor(private logger: LoggerService) {}

async loadLogFile(args: ILogFileArgs): Promise<string> {
const contentPromise = new Promise<string>((resolve) => {
this.logger.log('Requested log content');
this.logger.query(
jurgelenas marked this conversation as resolved.
Show resolved Hide resolved
{
start: 0,
limit: args.numberOfLines,
order: 'desc',
fields: ['timestamp', 'level', 'message', 'context'],
},
(err, result) => {
if (err) {
resolve(JSON.stringify({ error: err }));
} else {
resolve(JSON.stringify(result.file));
vbotnaru marked this conversation as resolved.
Show resolved Hide resolved
}
}
);
});
return contentPromise;
}
}
7 changes: 4 additions & 3 deletions src/main.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const winstonLogger = winston.createLogger({
transports: [
new winston.transports.Console({
format: winston.format.combine(
winston.format.prettyPrint(),
winston.format.timestamp()
winston.format.timestamp(),
winston.format.prettyPrint()
),
}),
new winston.transports.File({
Expand All @@ -49,8 +49,9 @@ const winstonLogger = winston.createLogger({
maxFiles: 10,
maxsize: 5_000_000, // in bytes
format: winston.format.combine(
winston.format.timestamp(),
winston.format.prettyPrint(),
winston.format.timestamp()
winston.format.json()
),
}),
],
Expand Down
70 changes: 70 additions & 0 deletions src/ui/gql/generated/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ export type GitRepositoryInput = {
readonly url: Scalars['String'];
};

export type LogFile = {
readonly __typename?: 'LogFile';
readonly content?: Maybe<Scalars['String']>;
};

export type LuaScript = {
readonly __typename?: 'LuaScript';
readonly fileLocation?: Maybe<Scalars['String']>;
Expand Down Expand Up @@ -239,6 +244,7 @@ export type Query = {
readonly checkForUpdates: UpdatesAvailability;
readonly gitBranches: ReadonlyArray<Scalars['String']>;
readonly gitTags: ReadonlyArray<Scalars['String']>;
readonly logFile: LogFile;
readonly luaScript: LuaScript;
readonly pullRequests: ReadonlyArray<PullRequestType>;
readonly releases: ReadonlyArray<Release>;
Expand Down Expand Up @@ -269,6 +275,10 @@ export type QueryGitTagsArgs = {
repository: Scalars['String'];
};

export type QueryLogFileArgs = {
numberOfLines?: Scalars['Float'];
};

export type QueryLuaScriptArgs = {
gitBranch?: Scalars['String'];
gitCommit?: Scalars['String'];
Expand Down Expand Up @@ -692,6 +702,18 @@ export type GetReleasesQuery = {
}>;
};

export type LogFileQueryVariables = Exact<{
numberOfLines: Scalars['Float'];
}>;

export type LogFileQuery = {
readonly __typename?: 'Query';
readonly logFile: {
readonly __typename?: 'LogFile';
readonly content?: string | null;
};
};

export type LuaScriptQueryVariables = Exact<{
source: FirmwareSource;
gitTag: Scalars['String'];
Expand Down Expand Up @@ -1718,6 +1740,54 @@ export type GetReleasesQueryResult = Apollo.QueryResult<
GetReleasesQuery,
GetReleasesQueryVariables
>;
export const LogFileDocument = gql`
query logFile($numberOfLines: Float!) {
logFile(numberOfLines: $numberOfLines) {
content
}
}
`;

/**
* __useLogFileQuery__
*
* To run a query within a React component, call `useLogFileQuery` and pass it any options that fit your needs.
* When your component renders, `useLogFileQuery` returns an object from Apollo Client that contains loading, error, and data properties
* you can use to render your UI.
*
* @param baseOptions options that will be passed into the query, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options;
*
* @example
* const { data, loading, error } = useLogFileQuery({
* variables: {
* numberOfLines: // value for 'numberOfLines'
* },
* });
*/
export function useLogFileQuery(
baseOptions: Apollo.QueryHookOptions<LogFileQuery, LogFileQueryVariables>
) {
const options = { ...defaultOptions, ...baseOptions };
return Apollo.useQuery<LogFileQuery, LogFileQueryVariables>(
LogFileDocument,
options
);
}
export function useLogFileLazyQuery(
baseOptions?: Apollo.LazyQueryHookOptions<LogFileQuery, LogFileQueryVariables>
) {
const options = { ...defaultOptions, ...baseOptions };
return Apollo.useLazyQuery<LogFileQuery, LogFileQueryVariables>(
LogFileDocument,
options
);
}
export type LogFileQueryHookResult = ReturnType<typeof useLogFileQuery>;
export type LogFileLazyQueryHookResult = ReturnType<typeof useLogFileLazyQuery>;
export type LogFileQueryResult = Apollo.QueryResult<
LogFileQuery,
LogFileQueryVariables
>;
export const LuaScriptDocument = gql`
query luaScript(
$source: FirmwareSource!
Expand Down
5 changes: 5 additions & 0 deletions src/ui/gql/queries/logFile.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query logFile($numberOfLines: Float!) {
vbotnaru marked this conversation as resolved.
Show resolved Hide resolved
logFile(numberOfLines: $numberOfLines) {
content
}
}
Loading