ModelTrain.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. Page({
  2. data: {
  3. rows: [], // 所有表格数据
  4. currentRow: null, // 当前选中的表格行
  5. filteredRows: [], // 过滤后的表格数据,默认为空
  6. tableHeaders: [
  7. "数据集名称","数据条数","更新时间"
  8. ],
  9. types: [{ name: 'reduce', display: '降酸模型' }, { name: 'reflux', display: '反酸模型' }], // 数据种类
  10. currentType: '', // 当前选中的数据种类,默认为空字符串
  11. },
  12. // 页面加载时获取表格数据
  13. onLoad: function() {
  14. this.LoadData();
  15. },
  16. LoadData: function() {
  17. wx.request({
  18. url: 'https://soilgd.com:5000/table',
  19. method: 'POST',
  20. header: {
  21. 'Content-Type': 'application/json'
  22. },
  23. data: {
  24. table: 'Datasets'
  25. },
  26. success: (res) => {
  27. console.log('后端返回数据:', res.data.rows); // 打印返回数据,确认格式
  28. if (res.data && Array.isArray(res.data.rows)) {
  29. const rows = res.data.rows.map(row => {
  30. return {
  31. 'id': row.Dataset_ID,
  32. 'name': row.Dataset_name,
  33. 'description': row.Dataset_description,
  34. 'type': row.Dataset_type,
  35. 'count': row.Row_count,
  36. 'status': row.Status,
  37. 'uploadTime': row.Uploaded_at,
  38. };
  39. });
  40. console.log(rows);
  41. this.setData({
  42. rows: rows,
  43. // 初始时不显示任何数据
  44. });
  45. } else {
  46. wx.showToast({
  47. title: '获取数据失败',
  48. icon: 'none'
  49. });
  50. }
  51. },
  52. fail: (err) => {
  53. wx.showToast({
  54. title: '请求失败,请重试',
  55. icon: 'none'
  56. });
  57. console.error('请求失败:', err);
  58. }
  59. });
  60. },
  61. // 处理行点击事件
  62. onRowClick: function(e) {
  63. const index = e.currentTarget.dataset.index;
  64. const selectedRow = this.data.filteredRows[index];
  65. this.setData({
  66. currentRow: this.data.filteredRows[index] ? {...this.data.filteredRows[index], index} : null
  67. });
  68. console.log('选中的行信息:', selectedRow); // 打印当前选中行的信息
  69. },
  70. // 处理数据种类改变事件
  71. onTypeChange: function(e) {
  72. const type = this.data.types[e.detail.value].name;
  73. this.setData({
  74. currentType: type,
  75. filteredRows: this.data.rows.filter(row => row.type === type)
  76. });
  77. },
  78. // 处理训练模型按钮点击事件
  79. trainModel: function() {
  80. const { currentRow } = this.data;
  81. if (!currentRow) {
  82. wx.showToast({
  83. title: '请先选择一行数据',
  84. icon: 'none'
  85. });
  86. return;
  87. }
  88. const { id, type } = currentRow;
  89. const trainData = {
  90. model_type: "RandomForest",
  91. model_name: "ForestModel1",
  92. model_description: "A random forest model trained on current data.",
  93. data_type: type,
  94. dataset_id: id
  95. };
  96. wx.request({
  97. url: 'https://soilgd.com:5000/train-and-save-model',
  98. method: 'POST',
  99. header: {
  100. 'Content-Type': 'application/json'
  101. },
  102. data: trainData,
  103. success: (res) => {
  104. console.log('模型训练成功:', res);
  105. wx.showToast({
  106. title: '模型训练完成',
  107. icon: 'success'
  108. });
  109. },
  110. fail: (err) => {
  111. console.error('模型训练失败:', err);
  112. wx.showToast({
  113. title: '模型训练失败',
  114. icon: 'none'
  115. });
  116. }
  117. });
  118. }
  119. })