mirror of
https://github.com/lubuntu-team/metrics.git
synced 2025-02-11 06:27:02 +00:00
84 lines
2.6 KiB
Python
Executable File
84 lines
2.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
# Copyright (C) 2020 Simon Quigley <tsimonq2@lubuntu.me>
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
import argparse
|
|
import logging as log
|
|
import sqlite3
|
|
from modules.jenkins import JenkinsModule
|
|
|
|
ENABLED_MODULES = [JenkinsModule]
|
|
|
|
|
|
def sqlite_run(command, db, return_output=False):
|
|
"""Run the given SQLite command on our db
|
|
|
|
command must be a command that SQLite can run
|
|
db must be a valid path to a db, or it's done in memory
|
|
"""
|
|
conn = sqlite3.connect(db)
|
|
c = conn.cursor()
|
|
for cmd in command:
|
|
c.execute(cmd)
|
|
conn.commit()
|
|
|
|
# Make sure we return an output if requested
|
|
try:
|
|
if return_output:
|
|
rows = c.fetchall()
|
|
return rows
|
|
except Exception as e:
|
|
print(e)
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
def main(module):
|
|
"""Given a specific module, set it up and insert recent values"""
|
|
module = module()
|
|
run = []
|
|
run.append(module.sqlite_setup())
|
|
run.append(module.sqlite_add())
|
|
run.append(module.sqlite_time_range(days=10))
|
|
log.debug("Commands to be ran: " + str(run))
|
|
|
|
# Use --db-location to pass to sqlite
|
|
log.info("Running SQLite commands")
|
|
log.debug(sqlite_run(run, db=args.db_location, return_output=True))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Parse CLI arguments
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--db-location", type=str, default=":memory:",
|
|
help="Specify the location for the SQLite database")
|
|
parser.add_argument("--log", type=str, default="WARNING",
|
|
help="Default logging level")
|
|
args = parser.parse_args()
|
|
|
|
# Ensure the logging level is set properly
|
|
num_level = getattr(log, args.log.upper(), None)
|
|
if not isinstance(num_level, int):
|
|
raise ValueError("Invalid log level: %s" % loglevel)
|
|
|
|
# Fully configure the logger
|
|
log.basicConfig(format="%(asctime)s\t%(levelname)s\t%(message)s",
|
|
level=num_level)
|
|
|
|
for module in ENABLED_MODULES:
|
|
log.info("Working on %s..." % module.__name__)
|
|
main(module)
|