Skip to content

Commit

Permalink
feat: Updated src/Snps/Utils.php
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] committed Mar 12, 2024
1 parent 78a634f commit dadbf18
Showing 1 changed file with 77 additions and 4 deletions.
81 changes: 77 additions & 4 deletions src/Snps/Utils.php
Expand Up @@ -47,17 +47,25 @@ class Parallelizer
public function __construct(bool $parallelize = false, ?int $processes = null): void
{
$this->_parallelize = $parallelize;
/**
* Utils class provides utility functions for file manipulation, parallel processing,
* and other common tasks. It includes methods for gzipping files, creating directories,
* fetching current UTC time, saving data as CSV, cleaning strings, and zipping files.
*/
$this->_processes = $processes ?? os_cpu_count();
}

public function __invoke(callable $f, array $tasks): array
{
if ($this->_parallelize) {
// Implement parallel (multi-process) execution using pthreads, parallel or another multi-processing library
// For example, using the parallel extension:
// Parallel execution using the parallel extension. Tasks are distributed across multiple threads.
// Each task is executed in a separate thread, and the results are collected and returned.
$runtime = new \parallel\Runtime();
$promises = array_map(fn($task) => $runtime->run($f, [$task]), $tasks);
return array_map(fn($promise) => $promise->value(), $promises);
$futures = [];
foreach ($tasks as $task) {
$futures[] = $runtime->run($f, [$task]);
}
return array_map(fn($future) => $future->value, $futures);
} else {
return array_map($f, $tasks);
}
Expand Down Expand Up @@ -127,3 +135,68 @@ public static function gzip_file(string $src, string $dest): string
return $dest;
}
}
/**
* Creates a directory if it doesn't exist.
*
* @param string $path Path to the directory to create.
* @return void
*/
public static function create_dir(string $path): void
{
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
}

/**
* Gets the current UTC time.
*
* @return string Current UTC time in 'Y-m-d H:i:s' format.
*/
public static function get_utc_now(): string
{
return gmdate('Y-m-d H:i:s');
}

/**
* Saves data as a CSV file.
*
* @param array $data Data to save.
* @param string $filename Path to the CSV file.
* @return void
*/
public static function save_df_as_csv(array $data, string $filename): void
{
$fp = fopen($filename, 'w');
foreach ($data as $row) {
fputcsv($fp, $row);
}
fclose($fp);
}

/**
* Cleans a string to be used as a variable name.
*
* @param string $str String to clean.
* @return string Cleaned string.
*/
public static function clean_str(string $str): string
{
return preg_replace('/[^A-Za-z0-9_]/', '', $str);
}

/**
* Zips a file.
*
* @param string $src Path to the file to zip.
* @param string $dest Path to the output zip file.
* @return void
*/
public static function zip_file(string $src, string $dest): void
{
$zip = new ZipArchive();
if ($zip->open($dest, ZipArchive::CREATE) === TRUE) {
$zip->addFile($src, basename($src));
$zip->close();
}
}

0 comments on commit dadbf18

Please sign in to comment.