reinfer-cli 0.38.5

Command line interface for Re:infer, the conversational data intelligence platform
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
use anyhow::{Context, Result};
use download::DownloadPackageArgs;
use itertools::Itertools;
use reinfer_client::{
    resources::{bucket::Id as BucketId, email::Email},
    AnnotatedComment, Bucket, Client, CommentId, Dataset, DatasetId, NewAnnotatedComment, NewEmail,
    Source, SourceId,
};
use scoped_threadpool::Pool;
use serde::{de::DeserializeOwned, Serialize};
use std::{
    fs::{File, OpenOptions},
    io::{Read, Write},
    path::{Path, PathBuf},
};
use structopt::StructOpt;
use upload::UploadPackageArgs;
use zip::{write::SimpleFileOptions, ZipArchive, ZipWriter};

mod download;
mod upload;

#[derive(Debug, StructOpt)]
pub enum PackageArgs {
    #[structopt(name = "download")]
    /// Download a package
    Download(DownloadPackageArgs),

    #[structopt(name = "upload")]
    /// Upload a package
    Upload(UploadPackageArgs),
}

pub struct EmailBatchKey(usize);

pub struct CommentBatchKey(usize);
#[derive(Clone)]
pub struct AttachmentKey(usize);

pub fn run(args: &PackageArgs, client: Client, pool: &mut Pool) -> Result<()> {
    match args {
        PackageArgs::Download(args) => download::run(args, &client, pool),
        PackageArgs::Upload(args) => upload::run(args, &client, pool),
    }
}

pub enum PackageContentId<'a> {
    Dataset {
        dataset_id: &'a DatasetId,
    },
    Source {
        source_id: &'a SourceId,
    },
    Bucket {
        bucket_id: &'a BucketId,
    },
    CommentBatch {
        key: CommentBatchKey,
        source_id: &'a SourceId,
    },
    EmailBatch {
        key: EmailBatchKey,
        bucket_id: &'a BucketId,
    },
    Document {
        source_id: &'a SourceId,
        comment_id: &'a CommentId,
        key: &'a AttachmentKey,
        extension: Option<String>,
    },
}

static DATASET_POSTFIX_AND_EXTENSION: &str = "dataset.json";
static SOURCE_POSTFIX_AND_EXTENSION: &str = "source.json";
static COMMENTS_POSTFIX_AND_EXTENSION: &str = "comments.json";
static EMAILS_POSTFIX_AND_EXTENSION: &str = "emails.json";
static BUCKET_POSTFIX_AND_EXTENSION: &str = "buckets.json";
static DATASETS_FOLDER_NAME: &str = "datasets";
static SOURCES_FOLDER_NAME: &str = "sources";
static BUCKETS_FOLDER_NAME: &str = "buckets";
static EMAILS_FOLDER_NAME: &str = "emails";
static COMMENTS_FOLDER_NAME: &str = "comments";
static DOCUMENTS_FOLDER_NAME: &str = "documents";

impl PackageContentId<'_> {
    fn filename(&self) -> String {
        match self {
            PackageContentId::Bucket { bucket_id } => {
                format!(
                    "{BUCKETS_FOLDER_NAME}/{0}.{BUCKET_POSTFIX_AND_EXTENSION}",
                    bucket_id.0
                )
            }
            PackageContentId::Dataset { dataset_id } => {
                format!(
                    "{DATASETS_FOLDER_NAME}/{0}.{DATASET_POSTFIX_AND_EXTENSION}",
                    dataset_id.0
                )
            }
            PackageContentId::Source { source_id } => {
                format!(
                    "{SOURCES_FOLDER_NAME}/{0}.{SOURCE_POSTFIX_AND_EXTENSION}",
                    source_id.0
                )
            }
            PackageContentId::CommentBatch { key, source_id } => {
                format!(
                    "{COMMENTS_FOLDER_NAME}/{0}.{1}.{COMMENTS_POSTFIX_AND_EXTENSION}",
                    source_id.0, key.0
                )
            }
            PackageContentId::EmailBatch { key, bucket_id } => {
                format!(
                    "{EMAILS_FOLDER_NAME}/{0}.{1}.{EMAILS_POSTFIX_AND_EXTENSION}",
                    bucket_id.0, key.0
                )
            }
            PackageContentId::Document {
                source_id,
                comment_id,
                extension,
                key,
            } => {
                if let Some(extension) = extension {
                    format!(
                        "{DOCUMENTS_FOLDER_NAME}/{0}.{1}.{2}.document.{3}",
                        source_id.0, comment_id.0, key.0, extension
                    )
                } else {
                    format!("{0}.{1}.{2}.document", source_id.0, comment_id.0, key.0)
                }
            }
        }
    }

    fn friendly_name(&self) -> String {
        match self {
            PackageContentId::Dataset { dataset_id } => format!("dataset {}", dataset_id.0),
            PackageContentId::Source { source_id } => format!("source {}", source_id.0),
            PackageContentId::Bucket { bucket_id } => format!("bucket {}", bucket_id.0),
            PackageContentId::CommentBatch { key, source_id } => {
                format!("comment batch {0} for source {1}", key.0, source_id.0)
            }
            PackageContentId::EmailBatch { key, bucket_id } => {
                format!("email batch {0} for bucket {1}", key.0, bucket_id.0)
            }
            PackageContentId::Document {
                source_id,
                comment_id,
                key,
                extension,
            } => {
                let extension_part = if let Some(extension) = extension {
                    format!("{extension} ")
                } else {
                    String::new()
                };

                format!(
                    "{0}attachment for comment {1}, in source {2} with key {3}",
                    extension_part, comment_id.0, source_id.0, key.0
                )
            }
        }
    }
}

pub struct PackageWriter {
    writer: ZipWriter<File>,
}

pub struct Package {
    archive: ZipArchive<File>,
}

impl Package {
    fn new(path: &PathBuf) -> Result<Self> {
        let file = File::open(path)?;
        let archive = ZipArchive::new(file)?;

        Ok(Self { archive })
    }

    pub fn read_document(
        &mut self,
        source_id: &SourceId,
        comment_id: &CommentId,
        key: &AttachmentKey,
        extension: Option<String>,
    ) -> Result<Vec<u8>> {
        let content_id = PackageContentId::Document {
            source_id,
            comment_id,
            key,
            extension,
        };

        self.read_bytes(content_id)
    }

    pub fn read_bytes(&mut self, content_id: PackageContentId) -> Result<Vec<u8>> {
        let mut contents = Vec::new();
        let mut file = self.archive.by_name(&content_id.filename())?;

        file.read_to_end(&mut contents)?;
        Ok(contents)
    }

    fn get_filenames_with_postfix_and_extension(&self, postfix: &str) -> Vec<String> {
        self.archive
            .file_names()
            .filter(|name| name.ends_with(postfix))
            .map(str::to_string)
            .collect()
    }

    pub fn get_source_by_id(&mut self, source_id: &SourceId) -> Result<Source> {
        self.read_json_content_by_id(PackageContentId::Source { source_id })
    }

    pub fn get_bucket_by_id(&mut self, bucket_id: &BucketId) -> Result<Bucket> {
        self.read_json_content_by_id(PackageContentId::Bucket { bucket_id })
    }
    pub fn get_email_batch(
        &mut self,
        bucket_id: &BucketId,
        key: EmailBatchKey,
    ) -> Result<Vec<NewEmail>> {
        let content_id = PackageContentId::EmailBatch { key, bucket_id };

        self.read_jsonl_content_by_id(content_id)
    }

    pub fn get_email_batch_count_for_bucket(&mut self, bucket_id: &BucketId) -> usize {
        self.get_filenames_with_postfix_and_extension(EMAILS_POSTFIX_AND_EXTENSION)
            .iter()
            .filter(|filename| {
                let path = Path::new(filename);
                path.file_name()
                    .is_some_and(|name| name.to_string_lossy().starts_with(&bucket_id.0))
            })
            .count()
    }
    pub fn get_comment_batch(
        &mut self,
        source_id: &SourceId,
        key: CommentBatchKey,
    ) -> Result<Vec<NewAnnotatedComment>> {
        let content_id = PackageContentId::CommentBatch { key, source_id };

        self.read_jsonl_content_by_id(content_id)
    }

    pub fn get_comment_batch_count_for_source(&mut self, source_id: &SourceId) -> usize {
        self.get_filenames_with_postfix_and_extension(COMMENTS_POSTFIX_AND_EXTENSION)
            .iter()
            .filter(|filename| {
                let path = Path::new(filename);
                path.file_name()
                    .is_some_and(|name| name.to_string_lossy().starts_with(&source_id.0))
            })
            .count()
    }

    pub fn get_document_count(&mut self) -> usize {
        self.archive
            .file_names()
            .filter(|name| {
                let path = Path::new(name);
                path.parent()
                    .is_some_and(|folder| folder.to_string_lossy() == DOCUMENTS_FOLDER_NAME)
            })
            .count()
    }

    pub fn get_comment_batch_count(&mut self) -> usize {
        self.archive
            .file_names()
            .filter(|name| {
                let path = Path::new(name);
                path.parent()
                    .is_some_and(|folder| folder.to_string_lossy() == COMMENTS_FOLDER_NAME)
            })
            .count()
    }

    pub fn get_emails_batch_count(&mut self) -> usize {
        self.archive
            .file_names()
            .filter(|name| {
                let path = Path::new(name);
                path.parent()
                    .is_some_and(|folder| folder.to_string_lossy() == EMAILS_FOLDER_NAME)
            })
            .count()
    }

    pub fn datasets(&mut self) -> Result<Vec<Dataset>> {
        let dataset_filenames =
            self.get_filenames_with_postfix_and_extension(DATASET_POSTFIX_AND_EXTENSION);

        dataset_filenames
            .iter()
            .map(|filename| self.read_json_content_by_name(filename))
            .try_collect()
    }

    fn read_string_content_by_name(&mut self, filename: &str) -> Result<String> {
        let mut file = self.archive.by_name(filename)?;

        let mut buf = String::new();
        file.read_to_string(&mut buf)?;
        Ok(buf)
    }

    fn read_json_content_by_name<T>(&mut self, filename: &str) -> Result<T>
    where
        T: DeserializeOwned,
    {
        let string = self.read_string_content_by_name(filename)?;

        Ok(serde_json::from_str(&string)?)
    }

    fn read_jsonl_content_by_id<T>(&mut self, content: PackageContentId) -> Result<Vec<T>>
    where
        T: DeserializeOwned,
    {
        let string = self
            .read_string_content_by_name(&content.filename())
            .context(format!(
                "Package does not contain a valid jsonl record for {}",
                content.friendly_name()
            ))?;

        string
            .lines()
            .map(|line| -> Result<T> {
                serde_json::from_str::<T>(line).map_err(anyhow::Error::msg)
            })
            .try_collect()
    }

    fn read_json_content_by_id<T>(&mut self, content: PackageContentId) -> Result<T>
    where
        T: DeserializeOwned,
    {
        self.read_json_content_by_name(&content.filename())
            .context(format!(
                "Package does not contain a valid json record for {}",
                content.friendly_name()
            ))
    }
}

impl PackageWriter {
    pub fn new(path: PathBuf) -> Result<Self> {
        let file = OpenOptions::new().write(true).create_new(true).open(path)?;
        let writer = ZipWriter::new(file);
        Ok(Self { writer })
    }

    pub fn write_dataset(&mut self, dataset: &Dataset) -> Result<()> {
        let dataset_id = &dataset.id;

        self.write_json(PackageContentId::Dataset { dataset_id }, dataset)
    }

    pub fn write_source(&mut self, source: &Source) -> Result<()> {
        let source_id = &source.id;
        self.write_json(PackageContentId::Source { source_id }, source)
    }

    pub fn write_bucket(&mut self, bucket: &Bucket) -> Result<()> {
        let bucket_id = &bucket.id;
        self.write_json(PackageContentId::Bucket { bucket_id }, bucket)
    }

    pub fn write_email_batch(
        &mut self,
        bucket_id: &BucketId,
        key: EmailBatchKey,
        emails: &[Email],
    ) -> Result<()> {
        self.write_jsonl(PackageContentId::EmailBatch { key, bucket_id }, emails)
    }

    pub fn write_comment_batch(
        &mut self,
        source_id: &SourceId,
        key: CommentBatchKey,
        comments: &[AnnotatedComment],
    ) -> Result<()> {
        self.write_jsonl(PackageContentId::CommentBatch { key, source_id }, comments)
    }

    pub fn write_bytes(&mut self, content_id: PackageContentId, content: &[u8]) -> Result<()> {
        self.writer
            .start_file_from_path(content_id.filename(), SimpleFileOptions::default())?;
        self.writer.write_all(content).map_err(anyhow::Error::msg)
    }

    fn write_jsonl<T>(&mut self, content_id: PackageContentId, content: &[T]) -> Result<()>
    where
        T: Serialize,
    {
        self.writer
            .start_file_from_path(content_id.filename(), SimpleFileOptions::default())?;

        content.iter().try_for_each(|item| -> Result<()> {
            let json = serde_json::to_string(item)?;

            self.writer
                .write_all(format!("{json}\n").as_bytes())
                .map_err(anyhow::Error::msg)
        })
    }

    fn write_json<T>(&mut self, content_id: PackageContentId, content: &T) -> Result<()>
    where
        T: Serialize,
    {
        self.writer
            .start_file_from_path(content_id.filename(), SimpleFileOptions::default())?;
        let json_content = serde_json::to_string_pretty(content)?;

        self.writer.write_all(json_content.as_bytes())?;
        Ok(())
    }

    fn finish(self) -> Result<()> {
        self.writer.finish()?;
        Ok(())
    }
}