ModelTrain.js 3.5 KB

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