|
| 1 | +package com.sqlrec.frontend.common; |
| 2 | + |
| 3 | +import com.sqlrec.common.model.ModelConfig; |
| 4 | +import com.sqlrec.common.model.ModelController; |
| 5 | +import com.sqlrec.common.schema.FieldSchema; |
| 6 | +import com.sqlrec.entity.Checkpoint; |
| 7 | +import com.sqlrec.entity.Model; |
| 8 | +import com.sqlrec.entity.Service; |
| 9 | +import com.sqlrec.model.ModelControllerFactory; |
| 10 | +import com.sqlrec.model.ModelEntityConverter; |
| 11 | + |
| 12 | +import java.text.SimpleDateFormat; |
| 13 | +import java.util.Date; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +public class CommonUtils { |
| 18 | + |
| 19 | + public static void addModelInfo(List<List<String>> rows, Model model) throws Exception { |
| 20 | + ModelConfig modelConfig = ModelEntityConverter.convertToModel(model.getDdl()); |
| 21 | + addModelInfo(rows, modelConfig, model); |
| 22 | + } |
| 23 | + |
| 24 | + public static void addModelInfo(List<List<String>> rows, String modelDdl, Model model) throws Exception { |
| 25 | + ModelConfig modelConfig = ModelEntityConverter.convertToModel(modelDdl); |
| 26 | + addModelInfo(rows, modelConfig, model); |
| 27 | + } |
| 28 | + |
| 29 | + public static void addModelInfo(List<List<String>> rows, ModelConfig modelConfig, Model model) throws Exception { |
| 30 | + ModelController modelController = ModelControllerFactory.getModelController(modelConfig); |
| 31 | + if (modelController == null) { |
| 32 | + throw new IllegalArgumentException("model controller not found for model: " + model.getName()); |
| 33 | + } |
| 34 | + |
| 35 | + List<FieldSchema> inputFields = modelConfig.getInputFields(); |
| 36 | + List<FieldSchema> outputFields = modelController.getOutputFields(modelConfig); |
| 37 | + Map<String, String> params = modelConfig.getParams(); |
| 38 | + |
| 39 | + rows.add(java.util.Arrays.asList("# Model Information", "")); |
| 40 | + rows.add(java.util.Arrays.asList("Model Name:", model.getName())); |
| 41 | + rows.add(java.util.Arrays.asList("Created At:", formatTimestamp(model.getCreatedAt()))); |
| 42 | + rows.add(java.util.Arrays.asList("Updated At:", formatTimestamp(model.getUpdatedAt()))); |
| 43 | + rows.add(java.util.Arrays.asList("", "")); |
| 44 | + |
| 45 | + addFieldSection(rows, "# Input Fields", inputFields); |
| 46 | + addFieldSection(rows, "# Output Fields", outputFields); |
| 47 | + addParametersSection(rows, "# Model Parameters", params); |
| 48 | + } |
| 49 | + |
| 50 | + public static void addCheckpointInfo(List<List<String>> rows, Checkpoint checkpoint) { |
| 51 | + rows.add(java.util.Arrays.asList("", "")); |
| 52 | + rows.add(java.util.Arrays.asList("# Checkpoint Information", "")); |
| 53 | + rows.add(java.util.Arrays.asList("Checkpoint Name:", checkpoint.getCheckpointName())); |
| 54 | + rows.add(java.util.Arrays.asList("Checkpoint Type:", |
| 55 | + checkpoint.getCheckpointType() != null ? checkpoint.getCheckpointType() : "N/A")); |
| 56 | + rows.add(java.util.Arrays.asList("Status:", |
| 57 | + checkpoint.getStatus() != null ? checkpoint.getStatus() : "N/A")); |
| 58 | + rows.add(java.util.Arrays.asList("Created At:", formatTimestamp(checkpoint.getCreatedAt()))); |
| 59 | + rows.add(java.util.Arrays.asList("Updated At:", formatTimestamp(checkpoint.getUpdatedAt()))); |
| 60 | + } |
| 61 | + |
| 62 | + public static void addServiceInfo(List<List<String>> rows, Service service) throws Exception { |
| 63 | + rows.add(java.util.Arrays.asList("# Service Information", "")); |
| 64 | + rows.add(java.util.Arrays.asList("Service Name:", service.getName())); |
| 65 | + rows.add(java.util.Arrays.asList("Model Name:", service.getModelName())); |
| 66 | + if (service.getCheckpointName() != null) { |
| 67 | + rows.add(java.util.Arrays.asList("Checkpoint Name:", service.getCheckpointName())); |
| 68 | + } |
| 69 | + if (service.getUrl() != null) { |
| 70 | + rows.add(java.util.Arrays.asList("URL:", service.getUrl())); |
| 71 | + } |
| 72 | + rows.add(java.util.Arrays.asList("Created At:", formatTimestamp(service.getCreatedAt()))); |
| 73 | + rows.add(java.util.Arrays.asList("Updated At:", formatTimestamp(service.getUpdatedAt()))); |
| 74 | + rows.add(java.util.Arrays.asList("", "")); |
| 75 | + |
| 76 | + if (service.getModelDdl() != null) { |
| 77 | + ModelConfig modelConfig = ModelEntityConverter.convertToModel(service.getModelDdl()); |
| 78 | + ModelController modelController = ModelControllerFactory.getModelController(modelConfig); |
| 79 | + if (modelController == null) { |
| 80 | + throw new IllegalArgumentException("model controller not found for model: " + service.getModelName()); |
| 81 | + } |
| 82 | + |
| 83 | + List<FieldSchema> inputFields = modelConfig.getInputFields(); |
| 84 | + List<FieldSchema> outputFields = modelController.getOutputFields(modelConfig); |
| 85 | + Map<String, String> params = modelConfig.getParams(); |
| 86 | + |
| 87 | + addFieldSection(rows, "# Input Fields", inputFields); |
| 88 | + addFieldSection(rows, "# Output Fields", outputFields); |
| 89 | + addParametersSection(rows, "# Model Parameters", params); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static void addFieldSection(List<List<String>> rows, String sectionTitle, List<FieldSchema> fields) { |
| 94 | + rows.add(java.util.Arrays.asList(sectionTitle, "")); |
| 95 | + rows.add(java.util.Arrays.asList("Name", "Type")); |
| 96 | + if (fields != null && !fields.isEmpty()) { |
| 97 | + for (FieldSchema field : fields) { |
| 98 | + rows.add(java.util.Arrays.asList(field.getName(), field.getType())); |
| 99 | + } |
| 100 | + } else { |
| 101 | + rows.add(java.util.Arrays.asList("(none)", "")); |
| 102 | + } |
| 103 | + rows.add(java.util.Arrays.asList("", "")); |
| 104 | + } |
| 105 | + |
| 106 | + private static void addParametersSection(List<List<String>> rows, String sectionTitle, Map<String, String> params) { |
| 107 | + rows.add(java.util.Arrays.asList(sectionTitle, "")); |
| 108 | + rows.add(java.util.Arrays.asList("Key", "Value")); |
| 109 | + if (params != null && !params.isEmpty()) { |
| 110 | + for (Map.Entry<String, String> entry : params.entrySet()) { |
| 111 | + rows.add(java.util.Arrays.asList(entry.getKey(), entry.getValue())); |
| 112 | + } |
| 113 | + } else { |
| 114 | + rows.add(java.util.Arrays.asList("(none)", "")); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private static String formatTimestamp(long timestamp) { |
| 119 | + return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(timestamp)); |
| 120 | + } |
| 121 | +} |
0 commit comments