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
// policy is to have an overall deny, and place allow attributes where needed 
#![deny(clippy::all)]
#![deny(warnings)]
use serde::de::Error as SerdeDeError;
use serde::{de, Deserialize, Serialize};
use std::{collections, convert, error, fmt, io, num}; // need to expose trait but don't want to use name

use chrono::TimeZone;
use chrono_tz::Australia::Brisbane;
use log::info;

pub mod daily;
pub mod dispatch_is;
pub mod dispatch_scada;
pub mod predispatch_is;
pub mod predispatch_sensitivities;
pub mod rooftop_actual;
pub mod rooftop_forecast;
pub mod yestbid;

// this is useful to get the date part of nem settlementdate / lastchanged fields
pub fn to_nem_date(ndt: &chrono::NaiveDateTime) -> chrono::Date<chrono_tz::Tz> {
    Brisbane.from_local_datetime(ndt).unwrap().date()
}

#[derive(Debug)]
pub enum Error {
    /// This occurs when we are missing the footer record which lists the number of rows in the file
    MissingFooterRecord,
    MissingHeaderRecord,
    /// This occurs when the desired file key can't be found in the RawAemoFile
    MissingFile(FileKey),
    /// This occurs when an entire row is empty after the first three columns
    EmptyRow,
    UnexpectedRowType(String),
    TooShortRow(usize),
    IncorrectLineCount { got: usize, expected: usize },
    ThreadBroken,
    ParseInt(num::ParseIntError),
    Csv(csv::Error),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::MissingHeaderRecord => write!(f, "aemo file is missing the first `c` record"),
            Self::MissingFooterRecord => write!(f, "aemo file is missing the final `c` record"),
            Self::MissingFile((name, sub_name, version)) => write!(
                f,
                "aemo file was missing {}.{}.v{} section in the file ",
                name, sub_name, version
            ),
            Self::EmptyRow => write!(f, "aemo file row is empty"),
            Self::UnexpectedRowType(t) => write!(f, "unexpeted row type of {}", t),
            Self::TooShortRow(len) => {
                write!(f, "aemo file data row of length {} is too short", len)
            }
            Self::IncorrectLineCount { got, expected } => write!(
                f,
                "aemo file was supposed to be {} lines long but was instead {} lines long",
                expected, got
            ),
            Self::ThreadBroken => write!(f, "Broken Thread"),
            Self::ParseInt(e) => write!(f, "parse int error: {}", e),
            Self::Csv(e) => write!(f, "csv error: {}", e),
        }
    }
}

impl From<num::ParseIntError> for Error {
    fn from(error: num::ParseIntError) -> Self {
        Error::ParseInt(error)
    }
}

impl From<csv::Error> for Error {
    fn from(error: csv::Error) -> Self {
        Error::Csv(error)
    }
}

impl error::Error for Error {}

type Result<T> = std::result::Result<T, Error>;

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct AemoHeader {
    record_type: char,
    data_source: String,
    file_name: String,
    participant_name: String,
    privacy_level: String,
    #[serde(deserialize_with = "au_date_deserialize")]
    effective_date: chrono::NaiveDate,
    #[serde(deserialize_with = "au_time_deserialize")]
    effective_time: chrono::NaiveTime,
    serial_number: u64,
    file_name_2: String,
    serial_number_2: u64,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
struct AemoFooter {
    record_type: char,
    end_of_report: String,
    line_count_inclusive: usize,
}

#[derive(Debug, Clone)]
pub struct RawAemoFile {
    pub header: AemoHeader,
    pub data: collections::HashMap<FileKey, Vec<csv::StringRecord>>,
    //footer: AemoFooter, // don't reall
}

pub type FileKey = (String, String, i32);

// potentially have RawAemoFile<T> where T: forms the key of the hashmap??

impl RawAemoFile {
    pub fn from_bufread(br: impl io::Read) -> Result<Self> {
        let mut reader = csv::ReaderBuilder::new()
            .has_headers(false)
            .flexible(true)
            .from_reader(br);
        let mut records = reader.records();
        let header: AemoHeader = records
            .next()
            .ok_or(Error::MissingHeaderRecord)??
            .deserialize(None)?;

        // placeholder
        let mut footer: Result<AemoFooter> = Err(Error::MissingFooterRecord);
        let mut data: collections::HashMap<FileKey, Vec<csv::StringRecord>> =
            collections::HashMap::new();

        for record in records {
            let record = record?;
            match record.get(0) {
                Some("C") => {
                    footer = record.deserialize(None).map_err(convert::Into::into);
                }
                Some("D") => {
                    let row_len = record.len();
                    if row_len < 5 {
                        return Err(Error::TooShortRow(row_len));
                    }
                    let file: String = record[1].into();
                    let sub_file: String = record[2].into();
                    let sub_file_version: i32 = record[3].parse()?;

                    // remove the unwanted fields from the stringrecord
                    let rest_record =
                        record
                            .into_iter()
                            .skip(4)
                            .fold(csv::StringRecord::new(), |mut acc, x| {
                                acc.push_field(x);
                                acc
                            });

                    if let Some((k, mut v)) =
                        data.remove_entry(&(file.clone(), sub_file.clone(), sub_file_version))
                    {
                        v.push(rest_record);
                        data.insert(k, v);
                    } else {
                        data.insert(
                            (file.clone(), sub_file.clone(), sub_file_version),
                            vec![rest_record],
                        );
                    }

                    // would be more ideal but can't use because rest_record is moved into the first closure
                    // data.entry((sub_file, sub_file_version))
                    //     .and_modify(|v| v.push(rest_record))
                    //     .or_insert(vec![rest_record.clone()]);
                }
                Some("I") => continue, //"i" row, or unexpected row
                Some(t) => return Err(Error::UnexpectedRowType(t.into())), //unexpected row, as correct files only have "C", "I" and "D"
                None => return Err(Error::EmptyRow),
            }
        }
        // set footer
        let expected_line_count = footer?.line_count_inclusive;

        let file = Self { header, data };

        let data_rows = file.data.iter().fold(0, |acc, (_, v)| acc + 1 + v.len());

        if data_rows + 2 == expected_line_count {
            Ok(file)
        } else {
            Err(Error::IncorrectLineCount {
                got: data_rows + 2,
                expected: expected_line_count,
            })
        }
    }
}

pub trait FileKeyable {
    fn key() -> FileKey;
}

pub trait GetFromRawAemo {
    type Output: FileKeyable + serde::de::DeserializeOwned;
    fn from_map(
        data: &mut collections::HashMap<FileKey, Vec<csv::StringRecord>>,
    ) -> Result<Vec<Self::Output>> {
        let key = &Self::Output::key();
        info!("Extracting file {:?}", key);
        data.remove_entry(key)
            .ok_or_else(|| Error::MissingFile(Self::Output::key()))?
            .1
            .into_iter()
            .map(|rec| rec.deserialize(None))
            .collect::<std::result::Result<Vec<Self::Output>, csv::Error>>()
            .map_err(convert::Into::into)
    }
}

pub trait AemoFile: Sized + Send {
    fn from_raw(raw: RawAemoFile) -> Result<Self>;
}

fn au_datetime_deserialize<'de, D>(d: D) -> std::result::Result<chrono::NaiveDateTime, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let s = serde::Deserialize::deserialize(d)?;
    chrono::NaiveDateTime::parse_from_str(s, "%Y/%m/%d %H:%M:%S").map_err(de::Error::custom)
}

fn opt_au_datetime_deserialize<'de, D>(
    d: D,
) -> std::result::Result<Option<chrono::NaiveDateTime>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let a_str: &'de str = serde::Deserialize::deserialize(d)?;
    if a_str.len() == 0 {
        Ok(None)
    } else {
        chrono::NaiveDateTime::parse_from_str(a_str, "%Y/%m/%d %H:%M:%S")
            .map_err(D::Error::custom)
            .map(Some)
    }
}

fn au_date_deserialize<'de, D>(d: D) -> std::result::Result<chrono::NaiveDate, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let s = serde::Deserialize::deserialize(d)?;
    chrono::NaiveDate::parse_from_str(s, "%Y/%m/%d").map_err(de::Error::custom)
}

fn au_time_deserialize<'de, D>(d: D) -> std::result::Result<chrono::NaiveTime, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let s = serde::Deserialize::deserialize(d)?;
    chrono::NaiveTime::parse_from_str(s, "%H:%M:%S").map_err(de::Error::custom)
}