@pittica/google-business-intelligence-helpers 1.0.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/contents/storage.js +43 -0
- package/dist/index.js +39 -1
- package/dist/naming/dataset.js +38 -0
- package/dist/naming/date.js +35 -0
- package/dist/naming/file.js +57 -0
- package/dist/naming/split.js +66 -0
- package/dist/scripts/unlistened.js +47 -0
- package/package.json +6 -3
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Copyright 2024-2025 Pittica S.r.l.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
const {
|
|
16
|
+
getSchemaKeys
|
|
17
|
+
} = require("./schema");
|
|
18
|
+
const {
|
|
19
|
+
splitName
|
|
20
|
+
} = require("../naming/split");
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Maps Google Cloud Storage response.
|
|
24
|
+
*
|
|
25
|
+
* @param {Array} response Google Cloud Storage response.
|
|
26
|
+
* @param {string} schemas Schema files folder path.
|
|
27
|
+
* @returns {object} Mapped response.
|
|
28
|
+
*/
|
|
29
|
+
exports.mapStorageResponse = (response, schemas) => {
|
|
30
|
+
const files = {};
|
|
31
|
+
getSchemaKeys(schemas).map(key => files[key] = []);
|
|
32
|
+
response.flat(3).forEach(({
|
|
33
|
+
id
|
|
34
|
+
}) => {
|
|
35
|
+
if (id) {
|
|
36
|
+
const filename = splitName(id);
|
|
37
|
+
if (typeof files[filename.name] !== "undefined") {
|
|
38
|
+
files[filename.name].push(id);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return Object.values(files).flat(1).map(file => splitName(file));
|
|
43
|
+
};
|
package/dist/index.js
CHANGED
|
@@ -21,8 +21,46 @@ const {
|
|
|
21
21
|
const {
|
|
22
22
|
getSqlFilePath
|
|
23
23
|
} = require("./contents/sql");
|
|
24
|
+
const {
|
|
25
|
+
mapStorageResponse
|
|
26
|
+
} = require("./contents/storage");
|
|
27
|
+
const {
|
|
28
|
+
getTemporaryTableName,
|
|
29
|
+
getTemporaryTableSuffix
|
|
30
|
+
} = require("./naming/dataset");
|
|
31
|
+
const {
|
|
32
|
+
partsToDate,
|
|
33
|
+
stringToDate
|
|
34
|
+
} = require("./naming/date");
|
|
35
|
+
const {
|
|
36
|
+
getFilenameVersion,
|
|
37
|
+
getJsonStorageName,
|
|
38
|
+
createFilename,
|
|
39
|
+
mergeFilename
|
|
40
|
+
} = require("./naming/file");
|
|
41
|
+
const {
|
|
42
|
+
splitName,
|
|
43
|
+
splitIdByKeys
|
|
44
|
+
} = require("./naming/split");
|
|
45
|
+
const {
|
|
46
|
+
scriptUnlistened
|
|
47
|
+
} = require("./scripts/unlistened");
|
|
24
48
|
exports.getSchemas = getSchemas;
|
|
25
49
|
exports.getSchema = getSchema;
|
|
26
50
|
exports.getSchemaKeys = getSchemaKeys;
|
|
27
51
|
exports.hasSchema = hasSchema;
|
|
28
|
-
exports.getSqlFilePath = getSqlFilePath;
|
|
52
|
+
exports.getSqlFilePath = getSqlFilePath;
|
|
53
|
+
exports.mapStorageResponse = mapStorageResponse;
|
|
54
|
+
exports.getTemporaryTableName = getTemporaryTableName;
|
|
55
|
+
exports.getTemporaryTableSuffix = getTemporaryTableSuffix;
|
|
56
|
+
exports.partsToDate = partsToDate;
|
|
57
|
+
exports.stringToDate = stringToDate;
|
|
58
|
+
exports.getFilenameVersion = getFilenameVersion;
|
|
59
|
+
exports.getJsonStorageName = getJsonStorageName;
|
|
60
|
+
exports.createFilename = createFilename;
|
|
61
|
+
exports.mergeFilename = mergeFilename;
|
|
62
|
+
exports.splitName = splitName;
|
|
63
|
+
exports.splitIdByKeys = splitIdByKeys;
|
|
64
|
+
exports.scripts = {
|
|
65
|
+
unlistened: scriptUnlistened
|
|
66
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Copyright 2024-2025 Pittica S.r.l.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
const {
|
|
16
|
+
format
|
|
17
|
+
} = require("date-and-time");
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Gets the suffix of a temporary table.
|
|
21
|
+
*
|
|
22
|
+
* @param {Date} date Date.
|
|
23
|
+
* @returns {string} The suffix of a temporary table.
|
|
24
|
+
*/
|
|
25
|
+
exports.getTemporaryTableSuffix = date => format(date, "YYYY-MM-DD");
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Gets the name of a temporary table.
|
|
29
|
+
*
|
|
30
|
+
* @param {object} filedata Filename data.
|
|
31
|
+
* @param {string} prefix The prefix of a temporary table.
|
|
32
|
+
* @returns {string} The name of a temporary table.
|
|
33
|
+
*/
|
|
34
|
+
exports.getTemporaryTableName = ({
|
|
35
|
+
date,
|
|
36
|
+
name,
|
|
37
|
+
version
|
|
38
|
+
}, prefix) => prefix + name + "-" + version + "-" + this.getTemporaryTableSuffix(date);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Copyright 2024-2025 Pittica S.r.l.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Converts the file name parts to a date object.
|
|
17
|
+
*
|
|
18
|
+
* @param {Array} parts String array from regular expression split representing a date from file name.
|
|
19
|
+
* @returns {Date} The date from the given array.
|
|
20
|
+
*/
|
|
21
|
+
exports.partsToDate = parts => new Date(parseInt(parts[1]), parseInt(parts[2]) - 1, parseInt(parts[3]));
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Extracts a date from the given date representation.
|
|
25
|
+
*
|
|
26
|
+
* @param {string} date Date in string format YYYY-MM-DD.
|
|
27
|
+
* @returns {Date} A date from the given date representation.
|
|
28
|
+
*/
|
|
29
|
+
exports.stringToDate = date => {
|
|
30
|
+
const split = date.split(/^(\d{4})\-(\d{2})\-(\d{2})$/gis);
|
|
31
|
+
if (split.length === 5) {
|
|
32
|
+
return this.partsToDate(split);
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Copyright 2024-2025 Pittica S.r.l.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
const {
|
|
16
|
+
format
|
|
17
|
+
} = require("date-and-time");
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Gets the version of a filename.
|
|
21
|
+
*
|
|
22
|
+
* @param {string} name Filename.
|
|
23
|
+
* @returns {number} The version of a filename.
|
|
24
|
+
*/
|
|
25
|
+
exports.getFilenameVersion = name => {
|
|
26
|
+
const split = name.split("_");
|
|
27
|
+
const last = split.slice(-1).pop();
|
|
28
|
+
const version = typeof last !== "undefined" ? parseInt(last) : 0;
|
|
29
|
+
return isNaN(version) ? 0 : version;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Gets the name of a JSON storage file.
|
|
34
|
+
*
|
|
35
|
+
* @param {Date} date Date object.
|
|
36
|
+
* @param {string} file File name.
|
|
37
|
+
* @returns {string} The name of a JSON storage file.
|
|
38
|
+
*/
|
|
39
|
+
exports.getJsonStorageName = (date, file) => `${format(date, "YYYY-MM-DD")}/${format(date, "YYYYMMDDHHmmss")}-${file}.json`;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Generates a filename from the givend data.
|
|
43
|
+
*
|
|
44
|
+
* @param {object} filedata File date.
|
|
45
|
+
* @returns {string} A filename from the givend data.
|
|
46
|
+
*/
|
|
47
|
+
exports.createFilename = filedata => `${format(filedata.date, "YYYY-MM-DD")}-${filedata.fullname}.${filedata.extension}`;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Merges the given filename parts.
|
|
51
|
+
*
|
|
52
|
+
* @param {Date} date File date.
|
|
53
|
+
* @param {string} name File or function name.
|
|
54
|
+
* @param {string} extension File extension.
|
|
55
|
+
* @returns {string} The merged given filename parts.
|
|
56
|
+
*/
|
|
57
|
+
exports.mergeFilename = (date, name, extension) => `${format(date, "YYYY-MM-DD")}-${name}.${extension.toLowerCase()}`;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// Copyright 2024-2025 Pittica S.r.l.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
const {
|
|
16
|
+
getFilenameVersion
|
|
17
|
+
} = require("./file");
|
|
18
|
+
const {
|
|
19
|
+
partsToDate
|
|
20
|
+
} = require("./date");
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Splits the given filename.
|
|
24
|
+
*
|
|
25
|
+
* @param {string} name Filename.
|
|
26
|
+
* @returns {object} An object containing the splitted name.
|
|
27
|
+
*/
|
|
28
|
+
exports.splitName = name => {
|
|
29
|
+
const split = name.split(/^(\d{4})\-(\d{2})\-(\d{2})\-(([\w_-]+)(?:\_[0-9]+)|([\w_-]+))(?:\.(\w+))$/gis);
|
|
30
|
+
if (split.length >= 8) {
|
|
31
|
+
return {
|
|
32
|
+
date: partsToDate(split),
|
|
33
|
+
name: typeof split[5] !== "undefined" ? split[5] : split[6],
|
|
34
|
+
fullname: typeof split[4] !== "undefined" ? split[4] : name,
|
|
35
|
+
extension: split[7],
|
|
36
|
+
match: true,
|
|
37
|
+
version: typeof split[4] !== "undefined" ? getFilenameVersion(split[4]) : 0
|
|
38
|
+
};
|
|
39
|
+
} else {
|
|
40
|
+
return {
|
|
41
|
+
date: new Date(),
|
|
42
|
+
name,
|
|
43
|
+
fullname: name,
|
|
44
|
+
extension: /[.]/.exec(name) ? /[^.]+$/.exec(name) : undefined,
|
|
45
|
+
match: false,
|
|
46
|
+
version: getFilenameVersion(name)
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Splits an ID into an object.
|
|
53
|
+
*
|
|
54
|
+
* @param {string} id ID representation.
|
|
55
|
+
* @param {array} keys An array of keys.
|
|
56
|
+
* @returns {object} Sliced object of the given ID.
|
|
57
|
+
*/
|
|
58
|
+
exports.splitIdByKeys = (id, keys) => {
|
|
59
|
+
const slices = {};
|
|
60
|
+
id.split("_").forEach((value, index) => {
|
|
61
|
+
if (typeof keys[index] !== "undefined") {
|
|
62
|
+
slices[keys[index]] = parseInt(value);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
return slices;
|
|
66
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// Copyright 2024-2025 Pittica S.r.l.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
const {
|
|
16
|
+
getStorage,
|
|
17
|
+
getFiles,
|
|
18
|
+
deleteFile
|
|
19
|
+
} = require("@pittica/google-cloud-storage-helpers");
|
|
20
|
+
const {
|
|
21
|
+
hasSchema
|
|
22
|
+
} = require("../contents/schema");
|
|
23
|
+
const {
|
|
24
|
+
splitName
|
|
25
|
+
} = require("../naming/split");
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Removes all not allowed files in given bucket.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} bucket Bucket name.
|
|
31
|
+
* @param {string} folder Path of the local folder to scan for schemas.
|
|
32
|
+
* @returns {void}
|
|
33
|
+
*/
|
|
34
|
+
exports.scriptUnlistened = async (bucket, folder) => {
|
|
35
|
+
if (bucket) {
|
|
36
|
+
const storage = getStorage();
|
|
37
|
+
const [files] = await getFiles(storage, bucket);
|
|
38
|
+
files.forEach(file => {
|
|
39
|
+
const {
|
|
40
|
+
name
|
|
41
|
+
} = splitName(file.id);
|
|
42
|
+
if (!hasSchema(name, folder)) {
|
|
43
|
+
deleteFile(file);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
};
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pittica/google-business-intelligence-helpers",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "1.0
|
|
4
|
+
"version": "1.2.0",
|
|
5
5
|
"description": "Helpers for business intelligence for Google Cloud.",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "rimraf ./dist && mkdirp ./dist && babel src --out-dir dist --copy-files",
|
|
9
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
10
|
+
"unlistened": "run-func ./dist/scripts/unlistened.js scriptUnlistened"
|
|
10
11
|
},
|
|
11
12
|
"repository": {
|
|
12
13
|
"type": "git",
|
|
@@ -25,11 +26,13 @@
|
|
|
25
26
|
},
|
|
26
27
|
"homepage": "https://github.com/pittica/google-business-intelligence-helpers#README.md",
|
|
27
28
|
"dependencies": {
|
|
29
|
+
"@pittica/google-cloud-storage-helpers": "^1.2.2",
|
|
30
|
+
"date-and-time": "^3.6.0"
|
|
28
31
|
},
|
|
29
32
|
"devDependencies": {
|
|
30
33
|
"@babel/cli": "^7.27.0",
|
|
31
34
|
"mkdirp": "^3.0.1",
|
|
32
35
|
"prettier": "^3.5.3",
|
|
33
|
-
"rimraf": "^6.0.1"
|
|
36
|
+
"rimraf": "^6.0.1"
|
|
34
37
|
}
|
|
35
38
|
}
|