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
use std::{fs::File, io::Write, path::PathBuf, process::Stdio, str::FromStr};

use eyre::eyre;
use fs_extra::dir::{get_dir_content, CopyOptions};
use futures::StreamExt;
use regex::Regex;
use serde::{Deserialize, Serialize};
use tokio::process::Command;

use super::PARTITION_FILE_NAME;

/// rclone command wrapper
pub struct RCloneWrapper {
    config_name: String,
}

impl RCloneWrapper {
    // ensures rclone is installed properly
    pub async fn new(config_name: String) -> eyre::Result<Self> {
        if !Command::new("rclone")
            .arg("--version")
            .spawn()?
            .wait()
            .await?
            .success()
        {
            eyre::bail!("rclone is not installed on this computer, please fix")
        }

        Ok(Self { config_name })
    }

    pub async fn get_most_recent_partition_block(&self) -> eyre::Result<u64> {
        self.get_all_tarballs()
            .await?
            .into_iter()
            .filter_map(|files| u64::from_str(files.split('-').last()?.split('.').next()?).ok())
            .max()
            .ok_or_else(|| eyre!("no files found on r2"))
    }

    pub async fn get_blockrange_list(&self) -> eyre::Result<Vec<BlockRangeList>> {
        Ok(self
            .get_all_tarballs()
            .await?
            .into_iter()
            .filter_map(|mut file_names| {
                if file_names.ends_with("brontes-db-partition-full-range-tables.tar.gz")
                    || file_names.ends_with("brontes-complete-range.tar.gz")
                {
                    return None
                }

                tracing::info!(?file_names);

                let block_range_and_ext = file_names.split_off(PARTITION_FILE_NAME.len() + 1);
                let mut r = block_range_and_ext.split('.').next().unwrap().split('-');
                let start_block = u64::from_str(r.next().unwrap()).unwrap();
                let end_block = u64::from_str(r.next().unwrap()).unwrap();
                Some(BlockRangeList { end_block, start_block })
            })
            .collect::<Vec<_>>())
    }

    async fn get_all_tarballs(&self) -> eyre::Result<Vec<String>> {
        let result = Command::new("rclone")
            .arg("tree")
            .arg(format!("{}:brontes-db", self.config_name))
            .stdout(Stdio::piped())
            .output()
            .await?;

        let string_result = String::from_utf8(result.stdout)?;
        let pattern = Regex::new(r"[\w-]+\.tar\.gz").unwrap();

        // Find the matches
        Ok(pattern
            .find_iter(&string_result)
            .map(|file| file.as_str().to_string())
            .collect::<Vec<_>>())
    }

    async fn upload_tarball(&self, directory_name: &str) {
        if !Command::new("rclone")
            .arg("copy")
            .arg(format!("/tmp/{directory_name}.tar.gz"))
            .arg(format!("{}:brontes-db/", self.config_name))
            .arg("--s3-upload-cutoff=100M")
            .arg("--s3-chunk-size=100M")
            .spawn()
            .unwrap()
            .wait()
            .await
            .unwrap()
            .success()
        {
            panic!("failed to upload tarball");
        }

        if !Command::new("rclone")
            .arg("copy")
            .arg(format!("/tmp/{directory_name}-byte-count.txt"))
            .arg(format!("{}:brontes-db/", self.config_name))
            .spawn()
            .unwrap()
            .wait()
            .await
            .unwrap()
            .success()
        {
            panic!("failed to upload tarball");
        }
    }

    async fn upload_full_range_tables(&self, partition_folder: &PathBuf) -> eyre::Result<()> {
        let directory = PathBuf::from(
            get_dir_content(partition_folder)?
                .directories
                .iter()
                .find(|path| path.ends_with("brontes-db-partition-full-range-tables"))
                .expect("no full range table found"),
        );

        self.tar_ball_dir(&directory, None).await?;

        Ok(())
    }

    pub async fn tar_ball_dir(
        &self,
        directory: &PathBuf,
        new_name: Option<&str>,
    ) -> eyre::Result<()> {
        let mut directory_name = directory
            .components()
            .last()
            .unwrap()
            .as_os_str()
            .to_str()
            .unwrap();

        tracing::info!(?directory, ?directory_name);

        // move to the tmp dir for zipping and zip
        let copy = CopyOptions::new().overwrite(true);

        let tmp = format!("/tmp/{directory_name}");
        fs_extra::dir::create_all(&tmp, true).expect("failed to create tmp folder");
        tracing::info!(from=?directory, to=?tmp, "copying to tmp location");

        // copy the data to tmp
        fs_extra::dir::copy(directory, "/tmp/", &copy)?;

        // if we have a name change request,
        if let Some(new_directory_name) = new_name {
            std::fs::rename(tmp, format!("/tmp/{new_directory_name}"))?;
            directory_name = new_directory_name;
        }

        if !Command::new("tar")
            .arg("-I")
            .arg("pigz")
            .arg("-cf")
            .arg(format!("/tmp/{directory_name}.tar.gz"))
            .arg("-C")
            .arg("/tmp/")
            .arg(directory_name)
            .spawn()?
            .wait()
            .await?
            .success()
        {
            panic!("failed to create tarball");
        }

        // get the tarball file size and write that
        tracing::info!("tarball finished");
        let file_size = filesize::file_real_size(format!("/tmp/{directory_name}.tar.gz"))?;

        let mut file = File::create(format!("/tmp/{directory_name}-byte-count.txt"))?;
        write!(&mut file, "{}", file_size).unwrap();

        tracing::info!("uploading tarball");
        // upload to the r2 bucket using rclone
        self.upload_tarball(directory_name).await;
        Ok(())
    }

    async fn update_block_range_file(&self) -> eyre::Result<()> {
        let ranges = self.get_blockrange_list().await?;
        let mut file = File::create("/tmp/brontes-available-ranges.json")?;
        let str = serde_json::to_string(&ranges)?;
        write!(&mut file, "{str}")?;

        if !Command::new("rclone")
            .arg("copy")
            .arg("/tmp/brontes-available-ranges.json")
            .arg(format!("{}:brontes-db/", self.config_name))
            .spawn()
            .unwrap()
            .wait()
            .await
            .unwrap()
            .success()
        {
            panic!("failed to upload available ranges");
        }

        Ok(())
    }

    pub async fn tar_ball_and_upload_files(
        &self,
        partition_folder: PathBuf,
        start_block: u64,
    ) -> eyre::Result<()> {
        tracing::info!(?partition_folder);
        self.upload_full_range_tables(&partition_folder).await?;

        futures::stream::iter(
            get_dir_content(&partition_folder)?
                .directories
                .iter()
                .filter(|path| {
                    *path != partition_folder.to_str().unwrap()
                        && !path.ends_with("brontes-db-partition-full-range-tables")
                })
                // ensure partition is in range
                .filter_map(|directory| {
                    let pathed = PathBuf::from(directory);

                    let directory = pathed
                        .components()
                        .last()
                        .unwrap()
                        .as_os_str()
                        .to_str()?
                        .to_string();

                    tracing::info!("tar balling directory {}", directory);
                    let end_portion = directory.clone().split_off(PARTITION_FILE_NAME.len() + 1);
                    tracing::info!(?end_portion);

                    let file_start_block = u64::from_str(end_portion.split('-').next()?).unwrap();
                    tracing::info!(%file_start_block);
                    (file_start_block >= start_block).then(|| {
                        let mut path = partition_folder.clone();
                        path.push(directory);
                        path
                    })
                }),
        )
        .map(|directory| async move {
            self.tar_ball_dir(&directory, None)
                .await
                .expect("failed to tarball dir")
        })
        .buffer_unordered(5)
        .collect::<Vec<_>>()
        .await;

        // upload ranges for downloader
        tracing::info!("update block range list");
        self.update_block_range_file().await?;

        Ok(())
    }
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct BlockRangeList {
    pub start_block: u64,
    pub end_block:   u64,
}