1212 */
1313
1414import { Observable , RemoteData } from '/js/src/index.js' ;
15- import { createCSVExport , createJSONExport } from '../../utilities/export.js' ;
16- import { TagFilterModel } from '../../components/Filters/common/TagFilterModel.js' ;
17- import { debounce } from '../../utilities/debounce.js' ;
18- import { PaginationModel } from '../../components/Pagination/PaginationModel.js' ;
19- import { getRemoteDataSlice } from '../../utilities/fetch/getRemoteDataSlice.js' ;
20- import { DetectorsFilterModel } from '../../components/Filters/RunsFilter/DetectorsFilterModel.js' ;
21- import { RunDetailsModel } from './Details/RunDetailsModel .js' ;
22- import { RunTypesSelectionDropdownModel } from '../../components/runTypes/RunTypesSelectionDropdownModel .js' ;
23- import { EorReasonFilterModel } from '../../components/Filters/RunsFilter/EorReasonsFilterModel .js' ;
15+ import { createCSVExport , createJSONExport } from '../../../ utilities/export.js' ;
16+ import { TagFilterModel } from '../../../ components/Filters/common/TagFilterModel.js' ;
17+ import { debounce } from '../../../ utilities/debounce.js' ;
18+ import { PaginationModel } from '../../../ components/Pagination/PaginationModel.js' ;
19+ import { getRemoteDataSlice } from '../../../ utilities/fetch/getRemoteDataSlice.js' ;
20+ import { DetectorsFilterModel } from '../../../ components/Filters/RunsFilter/DetectorsFilterModel.js' ;
21+ import { RunTypesSelectionDropdownModel } from '../../../components/runTypes/RunTypesSelectionDropdownModel .js' ;
22+ import { EorReasonFilterModel } from '../../../ components/Filters/RunsFilter/EorReasonsFilterModel .js' ;
23+ import pick from '../../../utilities/pick .js' ;
2424
2525/**
2626 * Model representing handlers for runs page
2727 *
2828 * @implements {OverviewModel}
2929 */
30- export default class RunModel extends Observable {
30+ export class RunsOverviewModel extends Observable {
3131 /**
3232 * The constructor of the Overview model object
3333 * @param {Model } model Pass the model to access the defined functions
3434 * @return {Object } Constructs the Overview model
3535 */
3636 constructor ( model ) {
3737 super ( ) ;
38- this . model = model ;
39-
40- /**
41- * @type {RunDetailsModel }
42- * @private
43- */
44- this . _detailsModel = null ;
4538
4639 this . _listingTagsFilterModel = new TagFilterModel ( ) ;
4740 this . _listingTagsFilterModel . observe ( ( ) => this . _applyFilters ( true ) ) ;
@@ -59,7 +52,7 @@ export default class RunModel extends Observable {
5952 this . _eorReasonsFilterModel . observe ( ( ) => this . _applyFilters ( ) ) ;
6053
6154 this . _pagination = new PaginationModel ( ) ;
62- this . _pagination . observe ( ( ) => this . fetchAllRuns ( ) ) ;
55+ this . _pagination . observe ( ( ) => this . fetchRuns ( ) ) ;
6356 this . _pagination . itemsPerPageSelector$ . observe ( ( ) => this . notify ( ) ) ;
6457
6558 // Content
@@ -69,41 +62,17 @@ export default class RunModel extends Observable {
6962 this . reset ( false ) ;
7063
7164 // eslint-disable-next-line no-return-assign,require-jsdoc
72- const updateDebounceTime = ( ) => this . _debouncedFetchAllRuns = debounce ( this . fetchAllRuns . bind ( this ) , model . inputDebounceTime ) ;
65+ const updateDebounceTime = ( ) => this . _debouncedFetchAllRuns = debounce ( this . fetchRuns . bind ( this ) , model . inputDebounceTime ) ;
7366 model . appConfiguration$ . observe ( ( ) => updateDebounceTime ( ) ) ;
7467 updateDebounceTime ( ) ;
7568 }
7669
77- /**
78- * Load the details page for the given run
79- * @param {string } [params.runNumber = null] the runNumber of the run to display
80- * @param {string } [params.id = null] Usage of this paramter is deprecated, use runNumber if feasible,
81- * @param {string } [params.panel = null] the key of the panel to display
82- * @return {void }
83- */
84- loadDetails ( { runNumber = null , id : runId = null , panel : panelKey = null } ) {
85- runId = runId ? Number ( runId ) : runId ;
86- runNumber = runNumber ? Number ( runNumber ) : runNumber ;
87-
88- if ( ! this . _detailsModel ) {
89- this . _detailsModel = new RunDetailsModel ( ) ;
90- this . _detailsModel . bubbleTo ( this ) ;
91- }
92- this . _detailsModel . clearAndLoad ( { runId, runNumber, panelKey } ) ;
93- }
94-
9570 /**
9671 * Retrieve every relevant run from the API
97- *
98- * @param {boolean } clear if true, any previous data will be discarded, even in infinite mode
99- *
100- * @return {undefined } Injects the data object with the response data
72+ * @return {void } Injects the data object with the response data
10173 */
102- async fetchAllRuns ( clear = false ) {
103- /**
104- * @type {Run[] }
105- */
106- const concatenateWith = ! clear && this . _pagination . currentPage !== 1 && this . _pagination . isInfiniteScrollEnabled
74+ async fetchRuns ( ) {
75+ const concatenateWith = this . _pagination . currentPage !== 1 && this . _pagination . isInfiniteScrollEnabled
10776 ? this . _currentPageRuns . payload || [ ]
10877 : [ ] ;
10978
@@ -112,8 +81,6 @@ export default class RunModel extends Observable {
11281 this . notify ( ) ;
11382 }
11483
115- this . _allRuns = RemoteData . notAsked ( ) ;
116-
11784 const params = {
11885 ...this . _getFilterQueryParams ( ) ,
11986 'page[offset]' : this . _pagination . firstItemOffset ,
@@ -134,15 +101,25 @@ export default class RunModel extends Observable {
134101
135102 /**
136103 * Create the export with the variables set in the model, handling errors appropriately
137- * @param {Object } content The source content.
104+ * @param {object[] } runs The source content.
138105 * @param {string } fileName The name of the file including the output format.
106+ * @param {Object<string, Function<*, string>> } exportFormats defines how particual fields of data units will be formated
139107 * @return {void }
140108 */
141- async createRunsExport ( content , fileName ) {
142- if ( content . length > 0 ) {
109+ async createRunsExport ( runs , fileName , exportFormats ) {
110+ if ( runs . length > 0 ) {
111+ const selectedRunsFields = this . getSelectedRunsFields ( ) || [ ] ;
112+ runs = runs . map ( ( selectedRun ) => {
113+ const entries = Object . entries ( pick ( selectedRun , selectedRunsFields ) ) ;
114+ const formattedEntries = entries . map ( ( [ key , value ] ) => {
115+ const formatExport = exportFormats [ key ] . exportFormat || ( ( identity ) => identity ) ;
116+ return [ key , formatExport ( value , selectedRun ) ] ;
117+ } ) ;
118+ return Object . fromEntries ( formattedEntries ) ;
119+ } ) ,
143120 this . getSelectedExportType ( ) === 'CSV'
144- ? createCSVExport ( content , `${ fileName } .csv` , 'text/csv;charset=utf-8;' )
145- : createJSONExport ( content , `${ fileName } .json` , 'application/json' ) ;
121+ ? createCSVExport ( runs , `${ fileName } .csv` , 'text/csv;charset=utf-8;' )
122+ : createJSONExport ( runs , `${ fileName } .json` , 'application/json' ) ;
146123 } else {
147124 this . _currentPageRuns = RemoteData . failure ( [
148125 {
@@ -154,14 +131,6 @@ export default class RunModel extends Observable {
154131 }
155132 }
156133
157- /**
158- * Getter for all the run data
159- * @return {RemoteData } Returns all of the filtered runs
160- */
161- getRuns ( ) {
162- return this . runs ;
163- }
164-
165134 /**
166135 * Get the field values that will be exported
167136 * @return {Array } the field objects of the current export being created
@@ -845,7 +814,7 @@ export default class RunModel extends Observable {
845814 */
846815 get allRuns ( ) {
847816 if ( this . _allRuns . isNotAsked ( ) ) {
848- this . _fetchAllRunsWithoutPaging ( ) ;
817+ this . _fetchAllRunsWithoutPagination ( ) ;
849818 }
850819
851820 return this . _allRuns ;
@@ -900,15 +869,6 @@ export default class RunModel extends Observable {
900869 return this . _pagination ;
901870 }
902871
903- /**
904- * Returns the run details sub models
905- *
906- * @return {RunDetailsModel|null } the sub-model
907- */
908- get detailsModel ( ) {
909- return this . _detailsModel ;
910- }
911-
912872 /**
913873 * Returns the list of URL params corresponding to the currently applied filter
914874 *
@@ -1005,7 +965,7 @@ export default class RunModel extends Observable {
1005965 * @return {Promise<void> } void
1006966 * @private
1007967 */
1008- async _fetchAllRunsWithoutPaging ( ) {
968+ async _fetchAllRunsWithoutPagination ( ) {
1009969 if ( this . runs . isSuccess ( ) && this . runs . payload . length === this . _pagination . itemsCount ) {
1010970 this . _allRuns = RemoteData . success ( [ ...this . runs . payload ] ) ;
1011971 this . notify ( ) ;
@@ -1037,6 +997,6 @@ export default class RunModel extends Observable {
1037997 */
1038998 _applyFilters ( now = false ) {
1039999 this . _pagination . currentPage = 1 ;
1040- now ? this . fetchAllRuns ( ) : this . _debouncedFetchAllRuns ( true ) ;
1000+ now ? this . fetchRuns ( ) : this . _debouncedFetchAllRuns ( true ) ;
10411001 }
10421002}
0 commit comments