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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use futures::future::BoxFuture;
use url::Url;

use crate::{
    exercise_service_info::{
        get_all_exercise_services_by_type, ExerciseServiceInfo, ExerciseServiceInfoApi,
    },
    prelude::*,
};

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct ExerciseService {
    pub id: Uuid,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub deleted_at: Option<DateTime<Utc>>,
    pub name: String,
    pub slug: String,
    pub public_url: String,
    /// This is needed because connecting to services directly inside the cluster with a special url is much for efficient than connecting to the same service with a url that would get routed though the internet. If not defined, use we can reach the service with the public url.
    pub internal_url: Option<String>,
    pub max_reprocessing_submissions_at_once: i32,
}

/// Exercise service definition that the CMS can use to render the editor view.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct ExerciseServiceIframeRenderingInfo {
    pub id: Uuid,
    pub name: String,
    pub slug: String,
    pub public_iframe_url: String,
    // #[serde(skip_serializing_if = "Option::is_none")]
    pub has_custom_view: bool,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct ExerciseServiceNewOrUpdate {
    pub name: String,
    pub slug: String,
    pub public_url: String,
    pub internal_url: Option<String>,
    pub max_reprocessing_submissions_at_once: i32,
}

pub async fn get_exercise_service(
    conn: &mut PgConnection,
    id: Uuid,
) -> ModelResult<ExerciseService> {
    let res = sqlx::query_as!(
        ExerciseService,
        r#"
SELECT *
FROM exercise_services
WHERE id = $1
  "#,
        id
    )
    .fetch_one(conn)
    .await?;
    Ok(res)
}

pub async fn update_exercise_service(
    conn: &mut PgConnection,
    id: Uuid,
    exercise_service_update: &ExerciseServiceNewOrUpdate,
) -> ModelResult<ExerciseService> {
    let res = sqlx::query_as!(
        ExerciseService,
        r#"
UPDATE exercise_services
    SET name=$1, slug=$2, public_url=$3, internal_url=$4, max_reprocessing_submissions_at_once=$5
WHERE id=$6
    RETURNING *
        "#,
        exercise_service_update.name,
        exercise_service_update.slug,
        exercise_service_update.public_url,
        exercise_service_update.internal_url,
        exercise_service_update.max_reprocessing_submissions_at_once,
        id
    )
    .fetch_one(conn)
    .await?;
    Ok(res)
}

pub async fn delete_exercise_service(
    conn: &mut PgConnection,
    id: Uuid,
) -> ModelResult<ExerciseService> {
    let deleted = sqlx::query_as!(
        ExerciseService,
        r#"
UPDATE exercise_services
    SET deleted_at = now()
WHERE id = $1
    RETURNING *
        "#,
        id
    )
    .fetch_one(conn)
    .await?;
    Ok(deleted)
}

pub async fn get_exercise_service_by_exercise_type(
    conn: &mut PgConnection,
    exercise_type: &str,
) -> ModelResult<ExerciseService> {
    let res = sqlx::query_as!(
        ExerciseService,
        r#"
SELECT *
FROM exercise_services
WHERE slug = $1
AND deleted_at IS NULL
  "#,
        exercise_type
    )
    .fetch_one(conn)
    .await?;
    Ok(res)
}

pub async fn get_exercise_service_internally_preferred_baseurl_by_exercise_type(
    conn: &mut PgConnection,
    exercise_type: &str,
) -> ModelResult<Url> {
    let exercise_service = get_exercise_service_by_exercise_type(conn, exercise_type).await?;
    get_exercise_service_internally_preferred_baseurl(&exercise_service)
}

pub fn get_exercise_service_internally_preferred_baseurl(
    exercise_service: &ExerciseService,
) -> ModelResult<Url> {
    let stored_url_str = exercise_service
        .internal_url
        .as_ref()
        .unwrap_or(&exercise_service.public_url);
    let mut url = Url::parse(stored_url_str).map_err(|original_error| {
        ModelError::new(
            ModelErrorType::Generic,
            original_error.to_string(),
            Some(original_error.into()),
        )
    })?;
    // remove the path because all relative urls in service info assume
    // that the base url prefix has no path
    url.set_path("");
    Ok(url)
}

pub fn get_exercise_service_externally_preferred_baseurl(
    exercise_service: &ExerciseService,
) -> ModelResult<Url> {
    let stored_url_str = &exercise_service.public_url;
    let mut url = Url::parse(stored_url_str).map_err(|original_error| {
        ModelError::new(
            ModelErrorType::Generic,
            original_error.to_string(),
            Some(original_error.into()),
        )
    })?;
    // remove the path because all relative urls in service info assume
    // that the base url prefix has no path
    url.set_path("");
    Ok(url)
}

/**
Returns a url that can be used to grade a submission for this exercise service.
*/
pub async fn get_internal_grade_url(
    exercise_service: &ExerciseService,
    exercise_service_info: &ExerciseServiceInfo,
) -> ModelResult<Url> {
    let mut url = get_exercise_service_internally_preferred_baseurl(exercise_service)?;
    url.set_path(&exercise_service_info.grade_endpoint_path);
    Ok(url)
}

/**
Returns a url that can be used to generate a public version of a private spec.
*/
pub fn get_internal_public_spec_url(
    exercise_service: &ExerciseService,
    exercise_service_info: &ExerciseServiceInfo,
) -> ModelResult<Url> {
    let mut url = get_exercise_service_internally_preferred_baseurl(exercise_service)?;
    url.set_path(&exercise_service_info.public_spec_endpoint_path);
    Ok(url)
}

pub fn get_model_solution_url(
    exercise_service: &ExerciseService,
    exercise_service_info: &ExerciseServiceInfo,
) -> ModelResult<Url> {
    let mut url = get_exercise_service_internally_preferred_baseurl(exercise_service)?;
    url.set_path(&exercise_service_info.model_solution_spec_endpoint_path);
    Ok(url)
}

pub async fn get_exercise_services(conn: &mut PgConnection) -> ModelResult<Vec<ExerciseService>> {
    let res = sqlx::query_as!(
        ExerciseService,
        r#"
SELECT *
FROM exercise_services
WHERE deleted_at IS NULL
"#
    )
    .fetch_all(conn)
    .await?;
    Ok(res)
}

pub async fn get_all_exercise_services_iframe_rendering_infos(
    conn: &mut PgConnection,
    fetch_service_info: impl Fn(Url) -> BoxFuture<'static, ModelResult<ExerciseServiceInfoApi>>,
) -> ModelResult<Vec<ExerciseServiceIframeRenderingInfo>> {
    let services = get_exercise_services(conn).await?;
    let service_infos = get_all_exercise_services_by_type(conn, fetch_service_info).await?;
    let res = services
        .into_iter()
        .filter_map(|exercise_service| {
            if let Some((_, service_info)) = service_infos.get(&exercise_service.slug) {
                if let Ok(mut url) =  get_exercise_service_externally_preferred_baseurl(&exercise_service) {
                    url.set_path(&service_info.user_interface_iframe_path);
                    Some(ExerciseServiceIframeRenderingInfo {
                        id: exercise_service.id,
                        name: exercise_service.name,
                        slug: exercise_service.slug,
                        public_iframe_url: url.to_string(),
                        has_custom_view: service_info.has_custom_view,
                    })
                } else {
                    warn!(exercise_service_id = ?exercise_service.id, "Skipping exercise service from the list because it has an invalid base url");
                    None
                }

            } else {
                warn!(exercise_service_id = ?exercise_service.id, "Skipping exercise service from the list because it doesn't have a service info");
                None
            }
        })
        .collect::<Vec<_>>();
    Ok(res)
}

pub async fn insert_exercise_service(
    conn: &mut PgConnection,
    exercise_service_update: &ExerciseServiceNewOrUpdate,
) -> ModelResult<ExerciseService> {
    let res = sqlx::query_as!(
        ExerciseService,
        r#"
INSERT INTO exercise_services (
    name,
    slug,
    public_url,
    internal_url,
    max_reprocessing_submissions_at_once
  )
VALUES ($1, $2, $3, $4, $5)
RETURNING *
  "#,
        exercise_service_update.name,
        exercise_service_update.slug,
        exercise_service_update.public_url,
        exercise_service_update.internal_url,
        exercise_service_update.max_reprocessing_submissions_at_once
    )
    .fetch_one(conn)
    .await?;
    Ok(res)
}