File size: 3,361 Bytes
27867f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
MIT License

Copyright (C) 2023 ROCKY4546
https://github.com/rocky4546

This file is part of Cabernet

Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
"""

import json
import datetime

from lib.db.db import DB
from lib.common.decorators import Backup
from lib.common.decorators import Restore

DB_TEMP_TABLE = 'temp'
DB_CONFIG_NAME = 'db_files-temp_db'

sqlcmds = {
    'ct': [
        """
        CREATE TABLE IF NOT EXISTS temp (
            namespace VARCHAR(255) NOT NULL,
            instance  VARCHAR(255) NOT NULL,
            value     VARCHAR(255) NOT NULL,
            last_update TIMESTAMP,
            json      TEXT NOT NULL,
            UNIQUE(namespace, instance, value)
            )
        """
    ],
    'dt': [
        """
        DROP TABLE IF EXISTS temp
        """,
    ],

    'temp_add':
        """
        INSERT OR REPLACE INTO temp (
            namespace, instance, value, last_update, json
            ) VALUES ( ?, ?, ?, ?, ? )
        """,
    'temp_by_day_del':
        """
        DELETE FROM temp WHERE namespace LIKE ? AND instance LIKE ? AND last_update < DATETIME('NOW',?)
        """,

    'temp_del':
        """
        DELETE FROM temp WHERE namespace=? AND instance LIKE ?
        """,
    'temp_get':
        """
        SELECT * FROM temp WHERE
            namespace=? AND instance=? AND value=?
        """
}


class DBTemp(DB):

    def __init__(self, _config):
        super().__init__(_config, _config['datamgmt'][DB_CONFIG_NAME], sqlcmds)

    def save_json(self, _namespace, _instance, _value, _json):
        """
        saves the json blob under a value item for the namespace/instance
        """
        self.add(DB_TEMP_TABLE, (
            _namespace,
            _instance,
            _value,
            datetime.datetime.utcnow(),
            json.dumps(_json),))

    def cleanup_temp(self, _namespace, _instance, _hours='-6 hours'):
        """
        Removes all records for this namespace/instance that are over 6 hour old
        """
        if not _namespace:
            _namespace = '%'
        if not _instance:
            _instance = '%'
        deleted = self.delete(DB_TEMP_TABLE + '_by_day', (_namespace, _instance, _hours,))
        self.sql_exec('VACUUM')

    def del_instance(self, _namespace, _instance):
        """
        Removes all records for this namespace/instance
        """
        if not _instance:
            _instance = '%'
        return self.delete(DB_TEMP_TABLE, (_namespace, _instance,))

    def get_record(self, _namespace, _instance, _value):
        return self.get_dict(DB_TEMP_TABLE, (_namespace, _instance, _value))

    @Backup(DB_CONFIG_NAME)
    def backup(self, backup_folder):
        self.export_sql(backup_folder)

    @Restore(DB_CONFIG_NAME)
    def restore(self, backup_folder):
        return self.import_sql(backup_folder)