-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAnalyticsController.java
143 lines (122 loc) · 5.73 KB
/
AnalyticsController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* LASSO - an Observatorium for the Dynamic Selection, Analysis and Comparison of Software
* Copyright (C) 2024 Marcus Kessel (University of Mannheim) and LASSO contributers
*
* This file is part of LASSO.
*
* LASSO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LASSO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LASSO. If not, see <https://www.gnu.org/licenses/>.
*/
package de.uni_mannheim.swt.lasso.service.controller.analytics;
import de.uni_mannheim.swt.lasso.engine.LassoConfiguration;
import de.uni_mannheim.swt.lasso.service.controller.BaseApi;
import de.uni_mannheim.swt.lasso.srm.olap.Warehouse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* LASSO's Analytics API.
*
* @author Marcus Kessel
*
*/
@RestController
@RequestMapping(value = "/publicapi/v1/lasso/analytics")
@Tag(name = "Analytics API")
public class AnalyticsController extends BaseApi {
private static final Logger LOG = LoggerFactory
.getLogger(AnalyticsController.class);
@Autowired
private Environment env;
@Autowired
private LassoConfiguration lassoConfiguration;
@Autowired
private ApplicationContext applicationContext;
@Operation(summary = "Get SRM as Parquet", description = "Get SRM as Parquet")
@RequestMapping(value = "/srm/{type}/{executionId}.parquet", method = RequestMethod.GET, produces = "application/vnd.apache.parquet")
@ResponseBody
public ResponseEntity<Resource> getSrmParquet(
@PathVariable("type") String type,
@PathVariable("executionId") String executionId,
/*@ApiIgnore*/ /*@AuthenticationPrincipal UserDetails userDetails,*/
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
// get user details
//UserInfo userInfo = getUserInfo(httpServletRequest, userDetails);
try {
Resource resource = Warehouse.writeSrmResource(executionId, type);
//
String fileName = "srm_" + executionId + "_" + type + ".parquet";
LOG.info(fileName);
String contentType = "application/vnd.apache.parquet";
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
.body(resource);
} catch (Throwable e) {
if (LOG.isWarnEnabled()) {
LOG.warn(String.format("Could not get SRM for '%s'", executionId), e);
}
// bad request
throw new RuntimeException(String.format("Could not get SRM for '%s'", executionId), e);
}
}
@Operation(summary = "Generate Jupyter notebook", description = "Generate Jupyter notebook")
@RequestMapping(value = "/srm/{type}/{executionId}.ipynb", method = RequestMethod.GET, produces = "application/x-ipynb+json")
@ResponseBody
public ResponseEntity<Resource> getJupyterParquet(
@PathVariable("type") String type,
@PathVariable("executionId") String executionId,
/*@ApiIgnore*/ /*@AuthenticationPrincipal UserDetails userDetails,*/
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
// get user details
//UserInfo userInfo = getUserInfo(httpServletRequest, userDetails);
try {
String parquetFile = executionId + ".parquet";
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/publicapi/v1/lasso/analytics/srm/" + type + "/")
.path(parquetFile)
.toUriString();
String notebook = JupyterUtils.createSrmNotebook(fileDownloadUri);
Resource resource = new ByteArrayResource(notebook.getBytes());
//
String fileName = "srm_" + executionId + "_" + type + ".ipynb";
LOG.info(fileName);
String contentType = "application/x-ipynb+json";
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
.body(resource);
} catch (Throwable e) {
if (LOG.isWarnEnabled()) {
LOG.warn(String.format("Could not get SRM for '%s'", executionId), e);
}
// bad request
throw new RuntimeException(String.format("Could not get SRM for '%s'", executionId), e);
}
}
}