rubric 0.16.0

A crate to help grade labs and assignments
Documentation
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! A bundle of data that rubrics are graded against, and is submitted for review

// std uses
use std::collections::HashMap;

// external uses
use chrono::{DateTime, Local};
use serde::{Deserialize, Serialize};
use reqwest::blocking::Response;

// internal uses
use crate::dropbox::results_file::AsCsv;
use crate::rubric::Rubric;
use crate::helpers::web;
use crate::dropbox::fingerprint::Fingerprint;
use crate::TIMESTAMP_FORMAT;

/// A type alias to `HashMap<String, String>`
///
/// This is the data type that all criteria accept,
/// and how data is stored in a submission
pub type TestData = HashMap<String, String>;


// This is only a function so serde can use it
// TODO: #34 Move this to dropbox::mod
fn default_timestamp_format() -> String {
    String::from(TIMESTAMP_FORMAT)
}


/// A submission is a bundle of data that represents
/// one student's submission. They will do some sort of work
/// for a lab, then run a rust script that builds some criteria,
/// runs those criteria with some data from the student, and submits
/// a Submission to a central webserver where the instructor can
/// collect the graded submissions.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct Submission {
    /// A local timestamp when the submission was created
    pub time: DateTime<Local>,
    /// Numerical grade for the submission.
    /// Each criterion will add to this grade if it passes.
    pub grade: isize,
    /// Extra data attached to the submission.
    /// Leave it empty if you don't need it
    pub data: TestData,
    /// If the submission is late or not
    pub late: bool,
    /// The criteria (name) that this submission passed
    pub passed: Vec<String>,
    /// The citeria (name) that this submission failed
    pub failed: Vec<String>,
    /// How to format the timestamp.
    /// This uses TIMESTAMP_FORMAT from the crate root.
    #[serde(default = "default_timestamp_format")]
    timestamp_format: String,
    fingerprint: Option<Fingerprint>
}

impl Submission {
    /// Creates a new submission.
    ///
    /// ## Example
    /// ```rust
    /// use rubric::Submission;
    ///
    /// // You probably want it to be mutable so
    /// // you can attach data and change the grade
    /// let mut sub = Submission::new();
    ///
    /// assert_eq!(sub.grade, 0);
    /// assert_eq!(sub.data.len(), 0);
    /// ```
    pub fn new() -> Submission {
        Submission {
            time: Local::now(),
            grade: 0,
            data: TestData::new(),
            passed: Vec::new(),
            failed: Vec::new(),
            timestamp_format: default_timestamp_format(),
            late: false,
            fingerprint: None
        }
    }

    /// Attaches data to a submission
    ///
    /// The data must be a [`TestData`](crate::submission::TestData).
    /// You may want to use the [`data!`](../macro.data.html) macro to make it
    /// easier to establish your data.
    ///
    /// You may be interested in [`Submission::from_data`](crate::submission::Submission::from_data).
    ///
    /// ## Example
    /// ```rust
    /// # use rubric::data;
    /// # use rubric::Submission;
    /// #
    /// let data = data! {
    ///     "key" => "value",
    ///     "key2" => "value2"
    /// };
    ///
    /// let mut sub = Submission::new();
    /// sub.use_data(data);
    ///
    /// assert_eq!(sub.data["key"], "value");
    /// assert_eq!(sub.data["key2"], "value2");
    /// ```
    pub fn use_data(&mut self, data: TestData) {
        self.data = data;
    }

    /// Creates a new submission and attaches data to it in one step
    ///
    /// ## Example
    /// ```rust
    /// # use rubric::{Submission, data};
    ///
    /// let sub = Submission::from_data(data! {
    ///     "name" => "luke i guess",
    ///     "id" => "1234"
    /// });
    ///
    /// assert_eq!(sub.data["id"], "1234");
    /// ```
    pub fn from_data(data: TestData) -> Self {
        let mut sub = Submission::new();
        sub.use_data(data);
        sub
    }


    /// Creates a fingerprint based on the provided secret key.
    ///
    /// The fingerprint will contain the secret key and some automatically
    /// collected platform information.
    ///
    /// ```no_compile
    /// let mut sub = Submission::new();
    /// sub.set_fingerprint("My secret key");
    /// ```
    pub fn set_fingerprint(&mut self, secret: &str) {
        self.fingerprint = Some(Fingerprint::from_secret(secret));
    }

    /// Returns the submissions fingerprint. It may not be set.
    pub fn fingerprint(&self) -> &Option<Fingerprint> {
        &self.fingerprint
    }

    /// Adds to the grade, with a message why
    fn addition(&mut self, to_add: isize, message: &str) {
        self.grade += to_add;
        self.passed.push(format!("{} (+{})", message, to_add));
    }

    /// Subtracts from the grade, with a message why
    fn penalty(&mut self, to_penalize: isize, message: &str) {
        self.grade -= to_penalize;
        self.failed.push(format!("{} (-{})", message, to_penalize));
    }

    /// Tests a submission against a list of criterion
    pub fn grade_against(&mut self, rubric: &mut Rubric) {
        // Penalties
        if rubric.past_final_deadline() {
            eprintln!("Final deadline ({}) has passed.", rubric.final_deadline.unwrap());
            eprintln!("Your instructor has chosen to not allow late submission");
            eprintln!("This submission will be recorded, but with a grade of 0");
            self.penalty(self.grade, "Past final deadline");
            return;
        }

        if rubric.past_due() {
            // Submission is late, mark it as such
            self.late = true;

            // And subtract the late penalty
            self.penalty(rubric.late_penalty, "Late submission");
            // Related, subtract the late penalty per day
            // This returns the amount of whole days since the deadline + 1.
            // One second after the deadline counts as 1 day,
            // exactly 24 hours + 1 second after the deadline is 2 days.
            let how_late = rubric.deadline
                .unwrap()
                .signed_duration_since(Local::now())
                .num_days()
                .abs() + 1;
            let daily_penalty = rubric.daily_penalty * how_late as isize;
            self.penalty(daily_penalty, &format!("{} days late", how_late));

            // If they disallow late submission
            if !rubric.allow_late {
                // Inform the student and return early without grading
                eprintln!("Deadline ({}) has passed.", rubric.deadline.unwrap());
                eprintln!("Your instructor has chosen to not allow late submission");
                eprintln!("This submission will be recorded, but with a grade of 0");
                // Penalize 100% of the points and return
                self.penalty(self.grade, "Past deadline");
                return;
            }


        }

        // Additions
        for crit in &mut rubric.sorted().into_iter() {
            if crit.test_with_data(&self.data) {
                self.addition(crit.worth, &crit.name);
            } else {
                // Failing a criteria just means +0 points
                self.penalty(0, &crit.name);
            }
        }
    }

    /// Posts the submission to the URL in JSON format. Meant to be sent
    /// to a dropbox. Really just calls [`helpers::web::post_json`](rubric::helpers::web::post_json).
    pub fn submit(&self, url: &str) -> Result<Response, reqwest::Error> {
        web::post_json(url, self)
    }

    /// Overrides the default timestamp format.
    /// The default is `%F %a %T %:z` which gives
    /// ```text
    /// 2001-08-04 Thu 00:34:60 +09:30
    /// ```
    /// This timestamp format is human readable and also sortable in a spreadsheet.
    pub fn set_timestamp_format(&mut self, new_format: &str) {
        self.timestamp_format = String::from(new_format);
    }
}

impl AsCsv for TestData {
    /// Returns the test data, serialized to a csv string. It will be
    /// sorted alphabetically by key.
    fn as_csv(&self) -> String {
        let mut v: Vec<_> = self.into_iter().collect();
        v.sort_by(|x,y| x.0.cmp(&y.0));
        v.iter().map(|v| v.1.replace(",", ";") ).collect::<Vec<_>>().join(",")
    }

    /// Returns the filename that the [`ResultsFile`](crate::results_file::ResultsFile)
    /// uses as its output
    ///
    /// This probably shouldn't get used for test data, as it will be written as part
    /// of a submission, not on it's own.
    fn filename(&self) -> String {
        String::from("submission_data.csv")
    }

    /// Returns a header to write to a csv file. This should match the fields in `as_csv` above.
    fn header(&self) -> String {
        // let keys: Vec<&String> = self.keys().collect();
        // let mut owned_keys: Vec<String> = keys.iter().map(|&k| k.to_owned() ).collect();
        // owned_keys.sort_by(|a,b| a.cmp(&b) );
        // return format!("{}", owned_keys.join(","));
        let mut v: Vec<_> = self.into_iter().collect();
        v.sort_by(|x,y| x.0.cmp(&y.0));
        v.iter().map(|v| v.0.to_owned() ).collect::<Vec<_>>().join(",")
    }
}

impl AsCsv for Submission {
    /// Returns the submission's values in csv format. The `TestData` atttached will be
    /// sorted alphabetically by key.
    fn as_csv(&self) -> String {
        let mut csv = format!(
            "{},{},{},{},{},{}",
            self.time.format(&self.timestamp_format),
            self.late,
            self.grade,
            self.passed.join(";"),
            self.failed.join(";"),
            self.data.as_csv()
        );

        if let Some(fp) = &self.fingerprint {
            csv = format!("{},{}", csv, fp.as_csv());
        }

        csv
    }

    /// Returns the filename to use when writing submissions to disk
    fn filename(&self) -> String {
        String::from("submissions.csv")
    }

    /// Returns a header of all the fields, matching the data in `as_csv`
    fn header(&self) -> String {
        let mut header = format!("time,late,grade,passed,failed,{}", self.data.header());
        if let Some(fp) = &self.fingerprint {
            header = format!("{},{}", header, fp.header());
        }
        header
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{data, yaml, attach};


    #[test]
    fn test_new_submission() {
        let sub = Submission::new();
        assert!(sub.data.len() == 0);
    }

    #[test]
    fn test_submission_use_data() {
        let data = data! {
            "key" => "value"
        };
        let mut sub = Submission::new();
        sub.use_data(data);
        assert!(sub.data.len() == 1);
        assert_eq!(sub.data["key"], "value");

        let sub2 = Submission::from_data(data! {
            "key" => "value"
        });
        assert_eq!(sub2.data["key"], "value");
    }

    #[test]
    fn test_submission_as_csv() {
        let sub = Submission::from_data(data! { "a" => "v", "b" => "v2" });

        // TestData keys are sorted alphabetically when converting to csv
        assert!((&sub).as_csv().contains("v,v2"));

        // Submission with no data, passes, or failures
        let sub2 = Submission::new();
        let expected = "0,,,";
        assert!((&sub2).as_csv().contains(expected));
    }

    #[test]
    fn test_serialize_deserialize_json() {
        let mut sub = Submission::from_data(data! { "k2" => "v2", "k" => "v" });
        sub.passed.push(String::from("something"));
        sub.failed.push(String::from("something"));

        assert!(serde_json::to_string(&sub).unwrap().contains(r#""k2":"v2""#));

        let data = r#"{"time":"2020-05-01T22:23:21.180875-05:00","late":false,"grade":0,"passed":["something"],"failed":["something"],"data":{"k2":"v2","k":"v"}}"#;
        let built_sub: Submission = serde_json::from_str(data).unwrap();
        assert_eq!(built_sub.grade, sub.grade);
    }

    #[test]
    fn test_grade_against_rubric() {
        let yaml = yaml!("../../test_data/test_rubric.yml").unwrap();
        let mut rubric = Rubric::from_yaml(yaml).unwrap();
        let test = |_: &TestData| true;
        attach! {
            rubric,
            "first_crit" => test
        };

        let mut sub = Submission::new();

        sub.grade_against(&mut rubric);
        assert_eq!(sub.grade, 50);
    }

    #[test]
    fn test_test_data_as_csv() {
        let d = data! {
            "b2" => "value2",
            "a1" => "value1"
        };

        let expected_header = "a1,b2";
        let expected_values = "value1,value2";
        let expected_filename = "submission_data.csv";

        assert_eq!(d.header(), expected_header);
        assert_eq!(d.as_csv(), expected_values);
        assert_eq!(d.filename(), expected_filename);
    }

    #[test]
    fn test_as_csv_replaces_commas() {
        let sub = Submission::from_data(data! {
            "key" => "value with, comma"
        });

        assert!(sub.as_csv().contains("value with; comma"));
    }

    #[test]
    fn test_test_data_gets_sorted() {
        let data = data! {
            "a" => "something",
            "b" => "else"
        };

        let csv = data.as_csv();
        assert!(csv.contains("something,else"));
    }

    #[test]
    fn test_custom_timestamp_format() {
        let mut sub = Submission::new();
        assert_eq!(sub.timestamp_format, TIMESTAMP_FORMAT);
        assert!(format!("{}", sub.time.format(&sub.timestamp_format)).len() > 0);

        sub.set_timestamp_format("some other format");
        assert_eq!(sub.timestamp_format, "some other format");
    }

    #[test]
    fn test_grading_past_due() {
        let yaml = yaml!("../../test_data/past_due_rubric.yml").unwrap();
        let mut past_due_rubric = Rubric::from_yaml(yaml).unwrap();

        let mut sub = Submission::new();

        // This rubric will allow late submission, with a 5 point penalty
        assert_eq!(sub.grade, 0);
        sub.grade_against(&mut past_due_rubric);
        assert_eq!(sub.grade, -5);
    }

    #[test]
    fn test_add_fingerprint() {
        let mut sub = Submission::new();
        assert!(sub.fingerprint.is_none());
        sub.set_fingerprint("secret key");
        assert!(sub.fingerprint.is_some());
    }

    #[test]
    fn test_submission_as_csv_with_fingerprint() {
        let mut sub = Submission::new();
        sub.set_fingerprint("secret key");
        assert!(sub.header().contains("secret,platform"));
    }
}