瀏覽代碼

feat(core): Add optional metadata to JobInfo

Michael Bromley 6 年之前
父節點
當前提交
7ddec3601b

+ 1 - 0
admin-ui/src/app/common/generated-types.ts

@@ -1215,6 +1215,7 @@ export type JobInfo = {
   name: Scalars['String'],
   state: JobState,
   progress: Scalars['Float'],
+  metadata?: Maybe<Scalars['JSON']>,
   result?: Maybe<Scalars['JSON']>,
   started?: Maybe<Scalars['DateTime']>,
   ended?: Maybe<Scalars['DateTime']>,

+ 22 - 0
packages/common/src/generated-shop-types.ts

@@ -1649,6 +1649,17 @@ export type PriceRange = {
     max: Scalars['Int'];
 };
 
+export type PriceRangeBucket = {
+    __typename?: 'PriceRangeBucket';
+    to: Scalars['Int'];
+    count: Scalars['Int'];
+};
+
+export type PriceRangeInput = {
+    min: Scalars['Int'];
+    max: Scalars['Int'];
+};
+
 export type Product = Node & {
     __typename?: 'Product';
     id: Scalars['ID'];
@@ -1962,6 +1973,8 @@ export type SearchInput = {
     take?: Maybe<Scalars['Int']>;
     skip?: Maybe<Scalars['Int']>;
     sort?: Maybe<SearchResultSortParameter>;
+    priceRange?: Maybe<PriceRangeInput>;
+    priceRangeWithTax?: Maybe<PriceRangeInput>;
 };
 
 export type SearchReindexResponse = {
@@ -1974,6 +1987,15 @@ export type SearchResponse = {
     items: Array<SearchResult>;
     totalItems: Scalars['Int'];
     facetValues: Array<FacetValueResult>;
+    prices: SearchResponsePriceData;
+};
+
+export type SearchResponsePriceData = {
+    __typename?: 'SearchResponsePriceData';
+    range: PriceRange;
+    rangeWithTax: PriceRange;
+    buckets: Array<PriceRangeBucket>;
+    bucketsWithTax: Array<PriceRangeBucket>;
 };
 
 export type SearchResult = {

+ 1 - 0
packages/common/src/generated-types.ts

@@ -1214,6 +1214,7 @@ export type JobInfo = {
   name: Scalars['String'],
   state: JobState,
   progress: Scalars['Float'],
+  metadata?: Maybe<Scalars['JSON']>,
   result?: Maybe<Scalars['JSON']>,
   started?: Maybe<Scalars['DateTime']>,
   ended?: Maybe<Scalars['DateTime']>,

+ 17 - 4
packages/core/e2e/graphql/generated-e2e-admin-types.ts

@@ -1218,6 +1218,7 @@ export type JobInfo = {
     name: Scalars['String'];
     state: JobState;
     progress: Scalars['Float'];
+    metadata?: Maybe<Scalars['JSON']>;
     result?: Maybe<Scalars['JSON']>;
     started?: Maybe<Scalars['DateTime']>;
     ended?: Maybe<Scalars['DateTime']>;
@@ -4277,8 +4278,12 @@ export type GetProductsQuery = { __typename?: 'Query' } & {
     products: { __typename?: 'ProductList' } & Pick<ProductList, 'totalItems'> & {
             items: Array<
                 { __typename?: 'Product' } & Pick<Product, 'id' | 'name' | 'slug' | 'description'> & {
-                        featuredAsset: Maybe<{ __typename?: 'Asset' } & Pick<Asset, 'id' | 'name'>>;
-                        assets: Array<{ __typename?: 'Asset' } & Pick<Asset, 'id' | 'name'>>;
+                        featuredAsset: Maybe<
+                            { __typename?: 'Asset' } & Pick<Asset, 'id' | 'name' | 'preview' | 'source'>
+                        >;
+                        assets: Array<
+                            { __typename?: 'Asset' } & Pick<Asset, 'id' | 'name' | 'preview' | 'source'>
+                        >;
                         optionGroups: Array<
                             { __typename?: 'ProductOptionGroup' } & Pick<
                                 ProductOptionGroup,
@@ -4302,9 +4307,17 @@ export type GetProductsQuery = { __typename?: 'Query' } & {
                                     options: Array<
                                         { __typename?: 'ProductOption' } & Pick<ProductOption, 'id' | 'code'>
                                     >;
-                                    assets: Array<{ __typename?: 'Asset' } & Pick<Asset, 'id' | 'name'>>;
+                                    assets: Array<
+                                        { __typename?: 'Asset' } & Pick<
+                                            Asset,
+                                            'id' | 'name' | 'preview' | 'source'
+                                        >
+                                    >;
                                     featuredAsset: Maybe<
-                                        { __typename?: 'Asset' } & Pick<Asset, 'id' | 'name'>
+                                        { __typename?: 'Asset' } & Pick<
+                                            Asset,
+                                            'id' | 'name' | 'preview' | 'source'
+                                        >
                                     >;
                                     facetValues: Array<
                                         { __typename?: 'FacetValue' } & Pick<

+ 22 - 0
packages/core/e2e/graphql/generated-e2e-shop-types.ts

@@ -1649,6 +1649,17 @@ export type PriceRange = {
     max: Scalars['Int'];
 };
 
+export type PriceRangeBucket = {
+    __typename?: 'PriceRangeBucket';
+    to: Scalars['Int'];
+    count: Scalars['Int'];
+};
+
+export type PriceRangeInput = {
+    min: Scalars['Int'];
+    max: Scalars['Int'];
+};
+
 export type Product = Node & {
     __typename?: 'Product';
     id: Scalars['ID'];
@@ -1962,6 +1973,8 @@ export type SearchInput = {
     take?: Maybe<Scalars['Int']>;
     skip?: Maybe<Scalars['Int']>;
     sort?: Maybe<SearchResultSortParameter>;
+    priceRange?: Maybe<PriceRangeInput>;
+    priceRangeWithTax?: Maybe<PriceRangeInput>;
 };
 
 export type SearchReindexResponse = {
@@ -1974,6 +1987,15 @@ export type SearchResponse = {
     items: Array<SearchResult>;
     totalItems: Scalars['Int'];
     facetValues: Array<FacetValueResult>;
+    prices: SearchResponsePriceData;
+};
+
+export type SearchResponsePriceData = {
+    __typename?: 'SearchResponsePriceData';
+    range: PriceRange;
+    rangeWithTax: PriceRange;
+    buckets: Array<PriceRangeBucket>;
+    bucketsWithTax: Array<PriceRangeBucket>;
 };
 
 export type SearchResult = {

+ 1 - 0
packages/core/src/api/schema/admin-api/job.api.graphql

@@ -20,6 +20,7 @@ type JobInfo {
     name: String!
     state: JobState!
     progress: Float!
+    metadata: JSON
     result: JSON
     started: DateTime
     ended: DateTime

+ 8 - 1
packages/core/src/plugin/default-search-plugin/indexer/search-index.service.ts

@@ -39,6 +39,10 @@ export class SearchIndexService {
     updateProductOrVariant(ctx: RequestContext, updatedEntity: Product | ProductVariant) {
         return this.jobService.createJob({
             name: 'update-index',
+            metadata: {
+                entity: updatedEntity.constructor.name,
+                id: updatedEntity.id,
+            },
             work: reporter => {
                 const data =
                     updatedEntity instanceof Product
@@ -57,7 +61,10 @@ export class SearchIndexService {
 
     updateVariantsById(ctx: RequestContext, ids: ID[]) {
         return this.jobService.createJob({
-            name: 'update-index',
+            name: 'update-variants',
+            metadata: {
+                variantIds: ids,
+            },
             work: reporter => {
                 Logger.verbose(`sending reindex message`);
                 this.workerService

+ 8 - 3
packages/core/src/service/helpers/job-manager/job-manager.ts

@@ -29,8 +29,13 @@ export class JobManager {
      * property of the job. If the function throws, the job will fail and the `result` property
      * will be the error thrown.
      */
-    createJob(name: string, work: () => Promise<any>, reporter: PartialJobReporter): Job {
-        const job = new Job(name, work, reporter);
+    createJob(
+        name: string,
+        work: () => Promise<any>,
+        reporter: PartialJobReporter,
+        metadata?: Record<string, any>,
+    ): Job {
+        const job = new Job(name, work, reporter, metadata);
         this.jobs.set(job.id, job);
         return job;
     }
@@ -84,7 +89,7 @@ export class JobManager {
     }
 
     private toJobInfo(job: Job): JobInfo {
-        const info = pick(job, ['id', 'name', 'state', 'progress', 'result', 'started', 'ended']);
+        const info = pick(job, ['id', 'name', 'state', 'progress', 'result', 'started', 'ended', 'metadata']);
         const duration = job.ended ? +job.ended - +info.started : Date.now() - +info.started;
         return { ...info, duration };
     }

+ 3 - 1
packages/core/src/service/helpers/job-manager/job.ts

@@ -12,7 +12,7 @@ export class Job {
     id: string;
     state: JobState = JobState.PENDING;
     progress = 0;
-    result = null;
+    result: any = null;
     started: Date;
     ended: Date;
 
@@ -20,6 +20,7 @@ export class Job {
         public name: string,
         public work: () => any | Promise<any>,
         private reporter: PartialJobReporter,
+        public metadata: Record<string, any> = {},
     ) {
         this.id = generatePublicId();
         this.started = new Date();
@@ -44,5 +45,6 @@ export class Job {
             this.result = e;
         }
         this.ended = new Date();
+        return this.result;
     }
 }

+ 2 - 1
packages/core/src/service/services/job.service.ts

@@ -19,6 +19,7 @@ export type PartialJobReporter = Omit<JobReporter, 'setProgress'> & Partial<Pick
 
 export interface CreateJobOptions {
     name: string;
+    metadata?: any;
     work: (reporter: JobReporter) => any | Promise<any>;
     /** Limit this job to a single instance at a time */
     singleInstance?: boolean;
@@ -60,7 +61,7 @@ export class JobService implements OnModuleInit, OnModuleDestroy {
                 }
             });
         };
-        return this.manager.createJob(options.name, wrappedWork, reporter);
+        return this.manager.createJob(options.name, wrappedWork, reporter, options.metadata);
     }
 
     getAll(input?: JobListInput): JobInfo[] {

文件差異過大導致無法顯示
+ 0 - 0
schema-admin.json


文件差異過大導致無法顯示
+ 0 - 0
schema-shop.json


部分文件因文件數量過多而無法顯示