github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/app/models/variable.js (about) 1 // @ts-check 2 import Model from '@ember-data/model'; 3 import { computed } from '@ember/object'; 4 import classic from 'ember-classic-decorator'; 5 // eslint-disable-next-line no-unused-vars 6 import MutableArray from '@ember/array/mutable'; 7 import { trimPath } from '../helpers/trim-path'; 8 import { attr } from '@ember-data/model'; 9 10 /** 11 * @typedef KeyValue 12 * @type {object} 13 * @property {string} key 14 * @property {string} value 15 */ 16 17 /** 18 * @typedef Variable 19 * @type {object} 20 */ 21 22 /** 23 * A Variable has a path, namespace, and an array of key-value pairs within the client. 24 * On the server, these key-value pairs are serialized into object structure. 25 * @class 26 * @extends Model 27 */ 28 @classic 29 export default class VariableModel extends Model { 30 /** 31 * Can be any arbitrary string, but behaves best when used as a slash-delimited file path. 32 * 33 * @type {string} 34 */ 35 @attr('string', { defaultValue: '' }) path; 36 37 /** 38 * @type {MutableArray<KeyValue>} 39 */ 40 @attr({ 41 defaultValue() { 42 return [{ key: '', value: '' }]; 43 }, 44 }) 45 keyValues; 46 47 /** @type {number} */ 48 @attr('number') createIndex; 49 /** @type {number} */ 50 @attr('number') modifyIndex; 51 /** @type {Date} */ 52 @attr('date') createTime; 53 /** @type {Date} */ 54 @attr('date') modifyTime; 55 /** @type {string} */ 56 @attr('string', { defaultValue: 'default' }) namespace; 57 58 @computed('path') 59 get parentFolderPath() { 60 const split = this.path.split('/'); 61 const [, ...folderPath] = [split.pop(), ...split]; 62 return folderPath.join('/'); 63 } 64 65 /** 66 * Removes starting and trailing slashes, pathLinkedEntitiesand sets the ID property 67 */ 68 setAndTrimPath() { 69 this.set('path', trimPath([this.path])); 70 if (!this.get('id')) { 71 this.set('id', `${this.get('path')}@${this.get('namespace')}`); 72 } 73 } 74 75 /** 76 * Translates the key-value pairs into an object structure. 77 */ 78 @computed('keyValues') 79 get items() { 80 return this.keyValues.reduce((acc, { key, value }) => { 81 acc[key] = value; 82 return acc; 83 }, {}); 84 } 85 86 // Gets the path of the variable, and if it starts with jobs/, delimits on / and returns each part separately in an array 87 88 /** 89 * @typedef pathLinkedEntities 90 * @type {Object} 91 * @property {string} job 92 * @property {string} [group] 93 * @property {string} [task] 94 */ 95 96 /** 97 * @type {pathLinkedEntities} 98 */ 99 get pathLinkedEntities() { 100 const entityTypes = ['job', 'group', 'task']; 101 const emptyEntities = { job: '', group: '', task: '' }; 102 if ( 103 this.path?.startsWith('nomad/jobs/') && 104 this.path?.split('/').length <= 5 105 ) { 106 return this.path 107 .split('/') 108 .slice(2, 5) 109 .reduce((acc, pathPart, index) => { 110 acc[entityTypes[index]] = pathPart; 111 return acc; 112 }, emptyEntities); 113 } else { 114 return emptyEntities; 115 } 116 } 117 }