monitorForm.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import {Component, Fragment} from 'react';
  2. import {Observer} from 'mobx-react';
  3. import Access from 'sentry/components/acl/access';
  4. import Field from 'sentry/components/forms/field';
  5. import Form from 'sentry/components/forms/form';
  6. import FormModel from 'sentry/components/forms/model';
  7. import NumberField from 'sentry/components/forms/numberField';
  8. import SelectField from 'sentry/components/forms/selectField';
  9. import TextCopyInput from 'sentry/components/forms/textCopyInput';
  10. import TextField from 'sentry/components/forms/textField';
  11. import {Panel, PanelBody, PanelHeader} from 'sentry/components/panels';
  12. import {t, tct} from 'sentry/locale';
  13. import {PageFilters, Project, SelectValue} from 'sentry/types';
  14. import withPageFilters from 'sentry/utils/withPageFilters';
  15. import withProjects from 'sentry/utils/withProjects';
  16. import {Monitor, MonitorConfig, MonitorTypes, ScheduleType} from './types';
  17. const SCHEDULE_TYPES: SelectValue<ScheduleType>[] = [
  18. {value: 'crontab', label: 'Crontab'},
  19. {value: 'interval', label: 'Interval'},
  20. ];
  21. const MONITOR_TYPES: SelectValue<MonitorTypes>[] = [
  22. {value: 'cron_job', label: 'Cron Job'},
  23. ];
  24. const INTERVALS: SelectValue<string>[] = [
  25. {value: 'minute', label: 'minute(s)'},
  26. {value: 'hour', label: 'hour(s)'},
  27. {value: 'day', label: 'day(s)'},
  28. {value: 'week', label: 'week(s)'},
  29. {value: 'month', label: 'month(s)'},
  30. {value: 'year', label: 'year(s)'},
  31. ];
  32. type Props = {
  33. apiEndpoint: string;
  34. apiMethod: Form['props']['apiMethod'];
  35. onSubmitSuccess: Form['props']['onSubmitSuccess'];
  36. projects: Project[];
  37. selection: PageFilters;
  38. monitor?: Monitor;
  39. };
  40. type TransformedData = {
  41. config?: Partial<MonitorConfig>;
  42. };
  43. function transformData(_data: Record<string, any>, model: FormModel) {
  44. return model.fields.toJSON().reduce<TransformedData>((data, [k, v]) => {
  45. if (k.indexOf('config.') !== 0) {
  46. data[k] = v;
  47. return data;
  48. }
  49. if (!data.config) {
  50. data.config = {};
  51. }
  52. if (k === 'config.schedule.frequency' || k === 'config.schedule.interval') {
  53. if (!Array.isArray(data.config.schedule)) {
  54. data.config.schedule = [null, null];
  55. }
  56. }
  57. if (k === 'config.schedule.frequency') {
  58. data.config!.schedule![0] = parseInt(v as string, 10);
  59. } else if (k === 'config.schedule.interval') {
  60. data.config!.schedule![1] = v;
  61. } else {
  62. data.config[k.substr(7)] = v;
  63. }
  64. return data;
  65. }, {});
  66. }
  67. class MonitorForm extends Component<Props> {
  68. form = new FormModel({transformData});
  69. formDataFromConfig(type: MonitorTypes, config: MonitorConfig) {
  70. const rv = {};
  71. switch (type) {
  72. case 'cron_job':
  73. rv['config.schedule_type'] = config.schedule_type;
  74. rv['config.checkin_margin'] = config.checkin_margin;
  75. rv['config.max_runtime'] = config.max_runtime;
  76. switch (config.schedule_type) {
  77. case 'interval':
  78. rv['config.schedule.frequency'] = config.schedule[0];
  79. rv['config.schedule.interval'] = config.schedule[1];
  80. break;
  81. case 'crontab':
  82. default:
  83. rv['config.schedule'] = config.schedule;
  84. }
  85. break;
  86. default:
  87. }
  88. return rv;
  89. }
  90. render() {
  91. const {monitor} = this.props;
  92. const selectedProjectId = this.props.selection.projects[0];
  93. const selectedProject = selectedProjectId
  94. ? this.props.projects.find(p => p.id === selectedProjectId + '')
  95. : null;
  96. return (
  97. <Access access={['project:write']}>
  98. {({hasAccess}) => (
  99. <Form
  100. allowUndo
  101. requireChanges
  102. apiEndpoint={this.props.apiEndpoint}
  103. apiMethod={this.props.apiMethod}
  104. model={this.form}
  105. initialData={
  106. monitor
  107. ? {
  108. name: monitor.name,
  109. type: monitor.type,
  110. project: monitor.project.slug,
  111. ...this.formDataFromConfig(monitor.type, monitor.config),
  112. }
  113. : {
  114. project: selectedProject ? selectedProject.slug : null,
  115. }
  116. }
  117. onSubmitSuccess={this.props.onSubmitSuccess}
  118. >
  119. <Panel>
  120. <PanelHeader>{t('Details')}</PanelHeader>
  121. <PanelBody>
  122. {monitor && (
  123. <Field label={t('ID')}>
  124. <div className="controls">
  125. <TextCopyInput>{monitor.id}</TextCopyInput>
  126. </div>
  127. </Field>
  128. )}
  129. <SelectField
  130. name="project"
  131. label={t('Project')}
  132. disabled={!hasAccess}
  133. options={this.props.projects
  134. .filter(p => p.isMember)
  135. .map(p => ({value: p.slug, label: p.slug}))}
  136. required
  137. />
  138. <TextField
  139. name="name"
  140. placeholder={t('My Cron Job')}
  141. label={t('Name')}
  142. disabled={!hasAccess}
  143. required
  144. />
  145. </PanelBody>
  146. </Panel>
  147. <Panel>
  148. <PanelHeader>{t('Config')}</PanelHeader>
  149. <PanelBody>
  150. <SelectField
  151. name="type"
  152. label={t('Type')}
  153. disabled={!hasAccess}
  154. options={MONITOR_TYPES}
  155. required
  156. />
  157. <Observer>
  158. {() => {
  159. switch (this.form.getValue('type')) {
  160. case 'cron_job':
  161. return (
  162. <Fragment>
  163. <NumberField
  164. name="config.max_runtime"
  165. label={t('Max Runtime')}
  166. disabled={!hasAccess}
  167. help={t(
  168. "The maximum runtime (in minutes) a check-in is allowed before it's marked as a failure."
  169. )}
  170. placeholder="e.g. 30"
  171. />
  172. <SelectField
  173. name="config.schedule_type"
  174. label={t('Schedule Type')}
  175. disabled={!hasAccess}
  176. options={SCHEDULE_TYPES}
  177. required
  178. />
  179. </Fragment>
  180. );
  181. default:
  182. return null;
  183. }
  184. }}
  185. </Observer>
  186. <Observer>
  187. {() => {
  188. switch (this.form.getValue('config.schedule_type')) {
  189. case 'crontab':
  190. return (
  191. <Fragment>
  192. <TextField
  193. name="config.schedule"
  194. label={t('Schedule')}
  195. disabled={!hasAccess}
  196. placeholder="*/5 * * * *"
  197. required
  198. help={tct(
  199. 'Changes to the schedule will apply on the next check-in. See [link:Wikipedia] for crontab syntax.',
  200. {
  201. link: <a href="https://en.wikipedia.org/wiki/Cron" />,
  202. }
  203. )}
  204. />
  205. <NumberField
  206. name="config.checkin_margin"
  207. label={t('Check-in Margin')}
  208. disabled={!hasAccess}
  209. help={t(
  210. "The margin (in minutes) a check-in is allowed to exceed it's scheduled window before being treated as missed."
  211. )}
  212. placeholder="e.g. 30"
  213. />
  214. </Fragment>
  215. );
  216. case 'interval':
  217. return (
  218. <Fragment>
  219. <NumberField
  220. name="config.schedule.frequency"
  221. label={t('Frequency')}
  222. disabled={!hasAccess}
  223. placeholder="e.g. 1"
  224. required
  225. />
  226. <SelectField
  227. name="config.schedule.interval"
  228. label={t('Interval')}
  229. disabled={!hasAccess}
  230. options={INTERVALS}
  231. required
  232. />
  233. <NumberField
  234. name="config.checkin_margin"
  235. label={t('Check-in Margin')}
  236. disabled={!hasAccess}
  237. help={t(
  238. "The margin (in minutes) a check-in is allowed to exceed it's scheduled window before being treated as missed."
  239. )}
  240. placeholder="e.g. 30"
  241. />
  242. </Fragment>
  243. );
  244. default:
  245. return null;
  246. }
  247. }}
  248. </Observer>
  249. </PanelBody>
  250. </Panel>
  251. </Form>
  252. )}
  253. </Access>
  254. );
  255. }
  256. }
  257. export default withPageFilters(withProjects(MonitorForm));