Bez popisu

tasks.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # IRIS Source Code
  2. # Copyright (C) 2021 - Airbus CyberSecurity (SAS)
  3. # ir@cyberactionlab.net
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 3 of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program; if not, write to the Free Software Foundation,
  17. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. # IMPORTS ------------------------------------------------
  19. import os
  20. import urllib.parse
  21. from celery.signals import task_prerun
  22. from flask_login import current_user
  23. from app import app
  24. from app import db
  25. from app.datamgmt.case.case_db import get_case
  26. from app.iris_engine.module_handler.module_handler import pipeline_dispatcher
  27. from app.iris_engine.utils.common import build_upload_path
  28. from app.iris_engine.utils.tracker import track_activity
  29. from iris_interface import IrisInterfaceStatus as IStatus
  30. from iris_interface.IrisModuleInterface import IrisPipelineTypes
  31. app.config['timezone'] = 'Europe/Paris'
  32. # CONTENT ------------------------------------------------
  33. @task_prerun.connect
  34. def on_task_init(*args, **kwargs):
  35. db.engine.dispose()
  36. def task_case_update(module, pipeline, pipeline_args, caseid):
  37. """
  38. Update the current case of the current user with fresh data.
  39. The files should have already been uploaded.
  40. :return: Tuple (success, errors)
  41. """
  42. errors = []
  43. case = get_case(caseid=caseid)
  44. if case:
  45. # We have a case so we can update the current case
  46. # Build the upload path where the files should be
  47. fpath = build_upload_path(case_customer=case.client.name,
  48. case_name=urllib.parse.unquote(case.name),
  49. module=module)
  50. # Check the path is valid and exists
  51. if fpath:
  52. if os.path.isdir(fpath):
  53. # Build task args
  54. task_args = {
  55. "pipeline_args": pipeline_args,
  56. "db_name": '',
  57. "user": current_user.name,
  58. "user_id": current_user.id,
  59. "case_name": case.name,
  60. "case_id": case.case_id,
  61. "path": fpath,
  62. "is_update": True
  63. }
  64. track_activity("started a new analysis import with pipeline {}".format(pipeline))
  65. pipeline_dispatcher.delay(module_name=module,
  66. hook_name=IrisPipelineTypes.pipeline_type_update,
  67. pipeline_type=IrisPipelineTypes.pipeline_type_update,
  68. pipeline_data=task_args,
  69. init_user=current_user.name,
  70. caseid=caseid)
  71. return IStatus.I2Success('Pipeline task queued')
  72. return IStatus.I2FileNotFound("Built path was not found ")
  73. return IStatus.I2UnexpectedResult("Unable to build path")
  74. else:
  75. # The user do not have any context so we cannot update
  76. # Return an error
  77. errors.append('Current user does not have a valid case in context')
  78. return IStatus.I2UnexpectedResult("Invalid context")
  79. def chunks(lst, n):
  80. """Yield successive n-sized chunks from lst."""
  81. for i in range(0, len(lst), n):
  82. yield lst[i:i + n]