Exemplo n.º 1
0
def parse_args():
    import argparse

    storage_methods = get_storage_methods()
    storage_method_names = get_storage_method_names()

    parser = argparse.ArgumentParser(description='Starts an ActivityWatch server')
    parser.add_argument('--testing',
                        action='store_true',
                        help='Run aw-server in testing mode using different ports and database')
    parser.add_argument('--verbose',
                        action='store_true',
                        help='Be chatty.')
    parser.add_argument('--log-json',
                        action='store_true',
                        help='Output the logs in JSON format')
    parser.add_argument('--port',
                        dest='port',
                        type=int,
                        default=None,
                        help='Which port to run the server on')
    parser.add_argument('--storage', dest='storage',
                        choices=storage_method_names,
                        default=storage_method_names[0],
                        help='The method to use for storing data. Some methods (such as MongoDB) require specific Python packages to be available (in the MongoDB case: pymongo)')

    args = parser.parse_args()
    if not args.port:
        args.port = 5600 if not args.testing else 5666
    storage_method = storage_methods[storage_method_names.index(args.storage)]
    return args, storage_method
Exemplo n.º 2
0
def create_app(
    host: str, testing=True, storage_method=None, cors_origins=[], custom_static=dict()
) -> AWFlask:
    app = AWFlask("aw-server", static_folder=static_folder, static_url_path="")

    if storage_method is None:
        storage_method = aw_datastore.get_storage_methods()["memory"]

    # Only pretty-print JSON if in testing mode (because of performance)
    app.config["JSONIFY_PRETTYPRINT_REGULAR"] = testing

    with app.app_context():
        _config_cors(cors_origins, testing)

    app.json_encoder = rest.CustomJSONEncoder

    app.register_blueprint(root)
    app.register_blueprint(rest.blueprint)
    app.register_blueprint(get_custom_static_blueprint(custom_static))

    db = Datastore(storage_method, testing=testing)
    app.api = ServerAPI(db=db, testing=testing)

    # needed for host-header check
    app.config["HOST"] = host

    return app
Exemplo n.º 3
0
def parse_settings():
    import argparse
    """ CLI Arguments """
    parser = argparse.ArgumentParser(
        description="Starts an ActivityWatch server")
    parser.add_argument(
        "--testing",
        action="store_true",
        help="Run aw-server in testing mode using different ports and database",
    )
    parser.add_argument("--verbose", action="store_true", help="Be chatty.")
    parser.add_argument("--log-json",
                        action="store_true",
                        help="Output the logs in JSON format")
    parser.add_argument("--host",
                        dest="host",
                        help="Which host address to bind the server to")
    parser.add_argument("--port",
                        dest="port",
                        type=int,
                        help="Which port to run the server on")
    parser.add_argument(
        "--storage",
        dest="storage",
        help=
        "The method to use for storing data. Some methods (such as MongoDB) require specific Python packages to be available (in the MongoDB case: pymongo)",
    )
    parser.add_argument(
        "--cors-origins",
        dest="cors_origins",
        help="CORS origins to allow (as a comma separated list)",
    )
    args = parser.parse_args()
    """ Parse config file """
    configsection = "server" if not args.testing else "server-testing"
    settings = argparse.Namespace()
    settings.host = config[configsection]["host"]
    settings.port = config[configsection].getint("port")
    settings.storage = config[configsection]["storage"]
    settings.cors_origins = config[configsection]["cors_origins"]
    """ If a argument is not none, override the config value """
    for key, value in vars(args).items():
        if value is not None:
            vars(settings)[key] = value

    settings.cors_origins = [o for o in settings.cors_origins.split(",") if o]

    storage_methods = get_storage_methods()
    storage_method = storage_methods[settings.storage]

    return settings, storage_method
Exemplo n.º 4
0
def parse_settings():
    import argparse

    """ CLI Arguments """
    parser = argparse.ArgumentParser(description='Starts an ActivityWatch server')
    parser.add_argument('--testing',
                        action='store_true',
                        help='Run aw-server in testing mode using different ports and database')
    parser.add_argument('--verbose',
                        action='store_true',
                        help='Be chatty.')
    parser.add_argument('--log-json',
                        action='store_true',
                        help='Output the logs in JSON format')
    parser.add_argument('--host',
                        dest='host',
                        help='Which host address to bind the server to')
    parser.add_argument('--port',
                        dest='port',
                        type=int,
                        help='Which port to run the server on')
    parser.add_argument('--storage', dest='storage',
                        help='The method to use for storing data. Some methods (such as MongoDB) require specific Python packages to be available (in the MongoDB case: pymongo)')
    parser.add_argument('--cors-origins',
                        dest='cors_origins',
                        help='CORS origins to allow (as a comma separated list)')
    args = parser.parse_args()

    """ Parse config file """
    configsection = "server" if not args.testing else "server-testing"
    settings = argparse.Namespace()
    settings.host = config[configsection]["host"]
    settings.port = config[configsection].getint("port")
    settings.storage = config[configsection]["storage"]
    settings.cors_origins = config[configsection]["cors_origins"]

    """ If a argument is not none, override the config value """
    for key, value in vars(args).items():
        if value is not None:
            vars(settings)[key] = value

    settings.cors_origins = [o for o in settings.cors_origins.split(',') if o]

    storage_methods = get_storage_methods()
    storage_method = storage_methods[settings.storage]

    return settings, storage_method
Exemplo n.º 5
0
def server_run() -> None:
    storage_methods = get_storage_methods()
    storage_method = storage_methods['peewee']

    app = create_app(
        storage_method=storage_method,
        testing=False,
        cors_origins=[]
    )
    app.static_folder = '.build/static/'
    app.run(
        debug=False,
        host='localhost',
        port=5600,
        request_handler=FlaskLogHandler,
        use_reloader=False,
        threaded=False
    )
Exemplo n.º 6
0
def parse_settings():
    import argparse

    storage_methods = get_storage_methods()
    """ CLI Arguments """
    parser = argparse.ArgumentParser(
        description='Starts an ActivityWatch server')
    parser.add_argument(
        '--testing',
        action='store_true',
        help='Run aw-server in testing mode using different ports and database'
    )
    parser.add_argument('--verbose', action='store_true', help='Be chatty.')
    parser.add_argument('--log-json',
                        action='store_true',
                        help='Output the logs in JSON format')
    parser.add_argument('--host',
                        dest='host',
                        help='Which host address to bind the server to')
    parser.add_argument('--port',
                        dest='port',
                        type=int,
                        help='Which port to run the server on')
    parser.add_argument(
        '--storage',
        dest='storage',
        help=
        'The method to use for storing data. Some methods (such as MongoDB) require specific Python packages to be available (in the MongoDB case: pymongo)'
    )
    args = parser.parse_args()
    """ Parse config file """
    configsection = "server" if not args.testing else "server-testing"
    settings = argparse.Namespace()
    settings.host = config[configsection]["host"]
    settings.port = config[configsection].getint("port")
    settings.storage = config[configsection]["storage"]
    """ If a argument is not none, override the config value """
    for key, value in vars(args).items():
        if value is not None:
            vars(settings)[key] = value

    storage_method = storage_methods[settings.storage]
    return settings, storage_method
def create_app(testing=True, storage_method=None, cors_origins=[]) -> AWFlask:
    app = AWFlask("aw-server", static_folder=static_folder, static_url_path='')

    if storage_method is None:
        storage_method = aw_datastore.get_storage_methods()['memory']

    # Only pretty-print JSON if in testing mode (because of performance)
    app.config["JSONIFY_PRETTYPRINT_REGULAR"] = testing

    with app.app_context():
        _config_cors(cors_origins, testing)

    app.json_encoder = rest.CustomJSONEncoder

    app.register_blueprint(root)
    app.register_blueprint(rest.blueprint)

    db = Datastore(storage_method, testing=testing)
    app.api = ServerAPI(db=db, testing=testing)

    return app
Exemplo n.º 8
0
def create_app(testing=True, storage_method=None, cors_origins=[]) -> AWFlask:
    app = AWFlask("aw-server", static_folder=static_folder, static_url_path='')

    if storage_method is None:
        storage_method = aw_datastore.get_storage_methods()['memory']

    # Only pretty-print JSON if in testing mode (because of performance)
    app.config["JSONIFY_PRETTYPRINT_REGULAR"] = testing

    with app.app_context():
        _config_cors(cors_origins, testing)

    app.json_encoder = rest.CustomJSONEncoder

    app.register_blueprint(root)
    app.register_blueprint(rest.blueprint)

    db = Datastore(storage_method, testing=testing)
    app.api = ServerAPI(db=db, testing=testing)

    return app
def test_get_storage_methods():
    assert get_storage_methods()
Exemplo n.º 10
0
class TempTestBucket:
    """Context manager for creating a test bucket"""

    def __init__(self, datastore):
        self.ds = datastore
        self.bucket_id = "test-{}".format(random.randint(0, 10**4))

    def __enter__(self):
        self.ds.create_bucket(bucket_id=self.bucket_id, type="testtype", client="testclient", hostname="testhost", name="testname")
        return self.ds[self.bucket_id]

    def __exit__(self, *_):
        self.ds.delete_bucket(bucket_id=self.bucket_id)

    def __repr__(self):
        return "<TempTestBucket using {}>".format(self.ds.storage_strategy.__class__.__name__)


_storage_methods = get_storage_methods()

def param_datastore_objects():
    return [Datastore(storage_strategy=strategy, testing=True)
            for name, strategy in _storage_methods.items()]


def param_testing_buckets_cm():
    datastores = [Datastore(storage_strategy=strategy, testing=True)
                  for name, strategy in _storage_methods.items()]
    return [TempTestBucket(ds) for ds in datastores]
Exemplo n.º 11
0
            client="testclient",
            hostname="testhost",
            name="testname",
        )
        return self.ds[self.bucket_id]

    def __exit__(self, *_):
        self.ds.delete_bucket(bucket_id=self.bucket_id)

    def __repr__(self):
        return "<TempTestBucket using {}>".format(
            self.ds.storage_strategy.__class__.__name__
        )


_storage_methods = get_storage_methods()


def param_datastore_objects():
    return [
        Datastore(storage_strategy=strategy, testing=True)
        for name, strategy in _storage_methods.items()
    ]


def param_testing_buckets_cm():
    datastores = [
        Datastore(storage_strategy=strategy, testing=True)
        for name, strategy in _storage_methods.items()
    ]
    return [TempTestBucket(ds) for ds in datastores]
Exemplo n.º 12
0
            with ttt(f" bulk insert {num_bulk_events} events"):
                bucket.insert(bulk_events)

            with ttt(f" replace last {num_replace_events}"):
                for e in replace_events:
                    bucket.replace_last(e)

            with ttt(" insert 1 event"):
                bucket.insert(events[-1])

            with ttt(" get one"):
                events_tmp = bucket.get(limit=1)

            with ttt(" get all"):
                events_tmp = bucket.get(limit=-1)
                assert len(events_tmp) == num_final_events

            with ttt(" get range"):
                events_tmp = bucket.get(
                    limit=-1,
                    starttime=events[1].timestamp + 0.01 * td1s,
                    endtime=events[-1].timestamp + events[-1].duration,
                )
                assert len(events_tmp) == num_final_events - 1


if __name__ == "__main__":
    for storage in get_storage_methods().values():
        if len(sys.argv) <= 1 or storage.__name__ in sys.argv:
            benchmark(storage)
def test_get_storage_methods():
    assert get_storage_methods()
    print(storage.__name__)

    with temporary_bucket(ds) as bucket:
        with ttt(" sum"):
            with ttt(" single insert {} events".format(num_single_events)):
                for event in single_events:
                    bucket.insert(event)

            with ttt(" bulk insert {} events".format(num_bulk_events)):
                bucket.insert(bulk_events)

            with ttt(" replace last {}".format(num_replace_events)):
                for e in replace_events:
                    bucket.replace_last(e)

            with ttt(" insert 1 event"):
                bucket.insert(events[-1])

            with ttt(" get one"):
                events_tmp = bucket.get(limit=1)

            with ttt(" get all"):
                events_tmp = bucket.get()
                assert len(events_tmp) == num_final_events


if __name__ == "__main__":
    for storage in get_storage_methods().values():
        if len(sys.argv) <= 1 or storage.__name__ in sys.argv:
            benchmark(storage)